treefmt: use shfmt and statix

simplifies treefmt.nix and adds `direnvrc` to treefmt includes for
shellcheck
This commit is contained in:
Arthur Noel 2023-12-14 17:55:33 +00:00
parent fc1c760987
commit 4d326252ae
5 changed files with 144 additions and 141 deletions

View File

@ -54,8 +54,9 @@ _require_cmd_version() {
} }
_nix_direnv_preflight() { _nix_direnv_preflight() {
if [[ -z "$direnv" ]]; then if [[ -z $direnv ]]; then
_nix_direnv_fatal "\$direnv environment variable was not defined. Was this script run inside direnv?" # shellcheck disable=2016
_nix_direnv_fatal '$direnv environment variable was not defined. Was this script run inside direnv?'
fi fi
if [[ -n ${NIX_BIN_PREFIX:-} ]]; then if [[ -n ${NIX_BIN_PREFIX:-} ]]; then
@ -119,7 +120,7 @@ nix_direnv_version() {
_nix_export_or_unset() { _nix_export_or_unset() {
local key=$1 value=$2 local key=$1 value=$2
if [[ "$value" == __UNSET__ ]]; then if [[ $value == __UNSET__ ]]; then
unset "$key" unset "$key"
else else
export "$key=$value" export "$key=$value"
@ -137,7 +138,7 @@ _nix_import_env() {
local old_xdg_data_dirs=${XDG_DATA_DIRS:-} local old_xdg_data_dirs=${XDG_DATA_DIRS:-}
# On the first run in manual mode, the profile_rc does not exist. # On the first run in manual mode, the profile_rc does not exist.
if [[ ! -e "$profile_rc" ]]; then if [[ ! -e $profile_rc ]]; then
return return
fi fi
@ -147,7 +148,7 @@ _nix_import_env() {
# as it may be garbage collected. # as it may be garbage collected.
# Instead - just remove it immediately. # Instead - just remove it immediately.
# Use recursive & force as it may not be empty. # Use recursive & force as it may not be empty.
if [[ -n "${NIX_BUILD_TOP+x}" && "$NIX_BUILD_TOP" == */nix-shell.* && -d "$NIX_BUILD_TOP" ]]; then if [[ -n ${NIX_BUILD_TOP+x} && $NIX_BUILD_TOP == */nix-shell.* && -d $NIX_BUILD_TOP ]]; then
rm -rf "$NIX_BUILD_TOP" rm -rf "$NIX_BUILD_TOP"
fi fi
@ -161,7 +162,7 @@ _nix_import_env() {
local IFS=: local IFS=:
for dir in $new_xdg_data_dirs${old_xdg_data_dirs:+:}$old_xdg_data_dirs; do for dir in $new_xdg_data_dirs${old_xdg_data_dirs:+:}$old_xdg_data_dirs; do
dir="${dir%/}" # remove trailing slashes dir="${dir%/}" # remove trailing slashes
if [[ :$XDG_DATA_DIRS: = *:$dir:* ]]; then if [[ :$XDG_DATA_DIRS: == *:$dir:* ]]; then
continue # already present, skip continue # already present, skip
fi fi
XDG_DATA_DIRS="$XDG_DATA_DIRS${XDG_DATA_DIRS:+:}$dir" XDG_DATA_DIRS="$XDG_DATA_DIRS${XDG_DATA_DIRS:+:}$dir"
@ -206,14 +207,14 @@ nix_direnv_watch_file() {
_nix_direnv_watches() { _nix_direnv_watches() {
local -n _watches=$1 local -n _watches=$1
if [[ -z "${DIRENV_WATCHES-}" ]]; then if [[ -z ${DIRENV_WATCHES-} ]]; then
return return
fi fi
while IFS= read -r line; do while IFS= read -r line; do
local regex='"[Pp]ath": "(.+)"$' local regex='"[Pp]ath": "(.+)"$'
if [[ "$line" =~ $regex ]]; then if [[ $line =~ $regex ]]; then
local path="${BASH_REMATCH[1]}" local path="${BASH_REMATCH[1]}"
if [[ "$path" == "${XDG_DATA_HOME:-${HOME:-/var/empty}/.local/share}/direnv/allow/"* ]]; then if [[ $path == "${XDG_DATA_HOME:-${HOME:-/var/empty}/.local/share}/direnv/allow/"* ]]; then
continue continue
fi fi
# expand new lines and other json escapes # expand new lines and other json escapes
@ -230,10 +231,10 @@ nix_direnv_manual_reload() {
} }
_nix_direnv_warn_manual_reload() { _nix_direnv_warn_manual_reload() {
if [[ -e "$1" ]]; then if [[ -e $1 ]]; then
_nix_direnv_warning "cache is out of date. use \"nix-direnv-reload\" to reload" _nix_direnv_warning 'cache is out of date. use "nix-direnv-reload" to reload'
else else
_nix_direnv_warning "cache does not exist. use \"nix-direnv-reload\" to create it" _nix_direnv_warning 'cache does not exist. use "nix-direnv-reload" to create it'
fi fi
} }
@ -244,9 +245,9 @@ use_flake() {
flake_dir="${flake_expr%#*}" flake_dir="${flake_expr%#*}"
flake_dir=${flake_dir#"path:"} flake_dir=${flake_dir#"path:"}
if [[ "$flake_expr" = -* ]]; then if [[ $flake_expr == -* ]]; then
local message="the first argument must be a flake expression" local message="the first argument must be a flake expression"
if [[ -n "$2" ]]; then if [[ -n $2 ]]; then
_nix_direnv_fatal "$message" _nix_direnv_fatal "$message"
else else
_nix_direnv_fatal "$message. did you mean 'use flake . $1'?" _nix_direnv_fatal "$message. did you mean 'use flake . $1'?"
@ -256,7 +257,7 @@ use_flake() {
local files_to_watch local files_to_watch
files_to_watch=("$HOME/.direnvrc" "$HOME/.config/direnv/direnvrc") files_to_watch=("$HOME/.direnvrc" "$HOME/.config/direnv/direnvrc")
if [[ -d "$flake_dir" ]]; then if [[ -d $flake_dir ]]; then
files_to_watch+=("$flake_dir/flake.nix" "$flake_dir/flake.lock" "$flake_dir/devshell.toml") files_to_watch+=("$flake_dir/flake.nix" "$flake_dir/flake.lock" "$flake_dir/devshell.toml")
fi fi
@ -273,19 +274,17 @@ use_flake() {
_nix_direnv_watches watches _nix_direnv_watches watches
local file= local file=
for file in "${watches[@]}"; do for file in "${watches[@]}"; do
if [[ "$file" -nt "$profile_rc" ]]; then if [[ $file -nt $profile_rc ]]; then
need_update=1 need_update=1
break break
fi fi
done done
if [[ ! -e $profile ||
if [[ ! -e "$profile" ! -e $profile_rc ||
|| ! -e "$profile_rc" $need_update -eq 1 ]] \
|| $need_update -eq 1 ; then
]]; if [[ $_nix_direnv_manual_reload -eq 1 && -z ${_nix_direnv_force_reload-} ]]; then
then
if [[ $_nix_direnv_manual_reload -eq 1 && -z "${_nix_direnv_force_reload-}" ]]; then
_nix_direnv_warn_manual_reload "$profile_rc" _nix_direnv_warn_manual_reload "$profile_rc"
else else
@ -306,7 +305,7 @@ use_flake() {
--json --no-write-lock-file \ --json --no-write-lock-file \
"$flake_dir") "$flake_dir")
while [[ "$flake_input_paths" =~ /nix/store/[^\"]+ ]]; do while [[ $flake_input_paths =~ /nix/store/[^\"]+ ]]; do
local store_path="${BASH_REMATCH[0]}" local store_path="${BASH_REMATCH[0]}"
_nix_add_gcroot "${store_path}" "${flake_inputs}/${store_path##*/}" _nix_add_gcroot "${store_path}" "${flake_inputs}/${store_path##*/}"
flake_input_paths="${flake_input_paths/${store_path}/}" flake_input_paths="${flake_input_paths/${store_path}/}"
@ -316,7 +315,7 @@ use_flake() {
fi fi
fi fi
else else
if [[ -e "${profile_rc}" ]]; then if [[ -e ${profile_rc} ]]; then
# Our cache is valid, use that # Our cache is valid, use that
_nix_direnv_info "using cached dev shell" _nix_direnv_info "using cached dev shell"
else else
@ -340,15 +339,18 @@ use_nix() {
local head local head
read -r head <"${path}/.git/HEAD" read -r head <"${path}/.git/HEAD"
local regex="ref: (.*)" local regex="ref: (.*)"
if [[ "$head" =~ $regex ]]; then if [[ $head =~ $regex ]]; then
read -r version <"${path}/.git/${BASH_REMATCH[1]}" read -r version <"${path}/.git/${BASH_REMATCH[1]}"
else else
version="$head" version="$head"
fi fi
elif [[ -f "${path}/.version" && "${path}" == "/nix/store/"* ]]; then elif [[ -f "${path}/.version" && ${path} == "/nix/store/"* ]]; then
# borrow some bits from the store path # borrow some bits from the store path
local version_prefix local version_prefix
read -r version_prefix < <(cat "${path}/.version" ; echo) read -r version_prefix < <(
cat "${path}/.version"
echo
)
version="${version_prefix}-${path:11:16}" version="${version_prefix}-${path:11:16}"
fi fi
fi fi
@ -369,7 +371,7 @@ use_nix() {
nixfile="./default.nix" nixfile="./default.nix"
fi fi
while [[ "$#" -gt 0 ]]; do while [[ $# -gt 0 ]]; do
i="$1" i="$1"
shift shift
@ -421,34 +423,32 @@ use_nix() {
_nix_direnv_watches watches _nix_direnv_watches watches
local file= local file=
for file in "${watches[@]}"; do for file in "${watches[@]}"; do
if [[ "$file" -nt "$profile_rc" ]]; then if [[ $file -nt $profile_rc ]]; then
need_update=1 need_update=1
break break
fi fi
done done
if [[ ! -e "$profile" if [[ ! -e $profile ||
|| ! -e "$profile_rc" ! -e $profile_rc ||
|| $need_update -eq 1 $need_update -eq 1 ]] \
]]; ; then
then if [[ $_nix_direnv_manual_reload -eq 1 && -z ${_nix_direnv_force_reload-} ]]; then
if [[ $_nix_direnv_manual_reload -eq 1 && -z "${_nix_direnv_force_reload-}" ]]; then
_nix_direnv_warn_manual_reload "$profile_rc" _nix_direnv_warn_manual_reload "$profile_rc"
else else
local tmp_profile="${layout_dir}/nix-tmp-profile.$$" local tmp_profile="${layout_dir}/nix-tmp-profile.$$"
local tmp_profile_rc local tmp_profile_rc
if [[ -n "$packages" ]]; then if [[ -n $packages ]]; then
extra_args+=("--expr" "with import <nixpkgs> {}; mkShell { buildInputs = [ $packages ]; }") extra_args+=("--expr" "with import <nixpkgs> {}; mkShell { buildInputs = [ $packages ]; }")
else else
# figure out what attribute we should build # figure out what attribute we should build
if [[ -z "$attribute" ]]; then if [[ -z $attribute ]]; then
extra_args+=("--file" "$nixfile") extra_args+=("--file" "$nixfile")
else else
extra_args+=("--expr" "(import ${nixfile} {}).${attribute}") extra_args+=("--expr" "(import ${nixfile} {}).${attribute}")
fi fi
fi fi
if tmp_profile_rc=$(_nix \ if tmp_profile_rc=$(_nix \
print-dev-env \ print-dev-env \
--profile "$tmp_profile" \ --profile "$tmp_profile" \
@ -456,7 +456,6 @@ use_nix() {
"${extra_args[@]}"); then "${extra_args[@]}"); then
_nix_clean_old_gcroots "$layout_dir" _nix_clean_old_gcroots "$layout_dir"
echo "$tmp_profile_rc" >"$profile_rc" echo "$tmp_profile_rc" >"$profile_rc"
_nix_add_gcroot "$tmp_profile" "$profile" _nix_add_gcroot "$tmp_profile" "$profile"
rm -f "$tmp_profile" "$tmp_profile"* rm -f "$tmp_profile" "$tmp_profile"*
@ -464,7 +463,7 @@ use_nix() {
fi fi
fi fi
else else
if [[ -e "${profile_rc}" ]]; then if [[ -e ${profile_rc} ]]; then
_nix_direnv_info "using cached dev shell" _nix_direnv_info "using cached dev shell"
else else
_nix_direnv_fatal "use_nix failed - Is your nix shell working?" _nix_direnv_fatal "use_nix failed - Is your nix shell working?"

View File

@ -1,10 +1,16 @@
{ {
description = "A faster, persistent implementation of `direnv`'s `use_nix`, to replace the built-in one."; description = "A faster, persistent implementation of `direnv`'s `use_nix`, to replace the built-in one.";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; inputs = {
inputs.flake-parts.url = "github:hercules-ci/flake-parts"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; flake-parts = {
inputs.treefmt-nix.url = "github:numtide/treefmt-nix"; url = "github:hercules-ci/flake-parts";
inputs.treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs-lib.follows = "nixpkgs";
};
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs @ { flake-parts, ... }: outputs = inputs @ { flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } flake-parts.lib.mkFlake { inherit inputs; }

View File

@ -6,7 +6,7 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
cd "$SCRIPT_DIR/.." cd "$SCRIPT_DIR/.."
version=${1:-} version=${1:-}
if [[ -z "$version" ]]; then if [[ -z $version ]]; then
echo "USAGE: $0 version" 2>/dev/null echo "USAGE: $0 version" 2>/dev/null
exit 1 exit 1
fi fi

View File

@ -8,27 +8,21 @@
# Used to find the project root # Used to find the project root
projectRootFile = "flake.lock"; projectRootFile = "flake.lock";
programs.deno.enable = true; programs = {
programs.mypy.enable = true; deadnix.enable = true;
programs.shellcheck.enable = true; deno.enable = true;
mypy.enable = true;
settings.formatter = { nixpkgs-fmt.enable = true;
nix = { shellcheck.enable = true;
command = "sh"; shfmt.enable = true;
options = [ statix.enable = true;
"-eucx"
''
# First deadnix
${lib.getExe pkgs.deadnix} --edit "$@"
# Then nixpkgs-fmt
${lib.getExe pkgs.nixpkgs-fmt} "$@"
''
"--"
];
includes = [ "*.nix" ];
excludes = [ "nix/sources.nix" ];
}; };
settings.formatter =
let
sh-includes = [ "*.sh" "direnvrc" ];
in
{
python = { python = {
command = "sh"; command = "sh";
options = [ options = [
@ -41,6 +35,10 @@
]; ];
includes = [ "*.py" ]; includes = [ "*.py" ];
}; };
shellcheck.includes = sh-includes;
shfmt.includes = sh-includes;
}; };
}; };
}; };