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

185
direnvrc
View File

@ -53,9 +53,10 @@ _require_cmd_version() {
_require_version "$cmd" "${BASH_REMATCH[1]}" "$required" _require_version "$cmd" "${BASH_REMATCH[1]}" "$required"
} }
_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
@ -81,7 +82,7 @@ _nix_direnv_preflight () {
# Because direnv_layout_dir is user controlled, # Because direnv_layout_dir is user controlled,
# we can't assume to be able to reverse it to get the source dir # we can't assume to be able to reverse it to get the source dir
# So there's little to be done about this. # So there's little to be done about this.
cat > "${layout_dir}/bin/nix-direnv-reload" <<-EOF cat >"${layout_dir}/bin/nix-direnv-reload" <<-EOF
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
if [[ ! -d "$PWD" ]]; then if [[ ! -d "$PWD" ]]; 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,17 +138,17 @@ _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
eval "$(< "$profile_rc")" eval "$(<"$profile_rc")"
# `nix print-dev-env` will create a temporary directory and use it as TMPDIR # `nix print-dev-env` will create a temporary directory and use it as TMPDIR
# We cannot rely on this directory being available at all times, # We cannot rely on this directory being available at all times,
# 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"
@ -186,14 +187,14 @@ _nix_argsum_suffix() {
if [ -n "$1" ]; then if [ -n "$1" ]; then
if has sha1sum; then if has sha1sum; then
out=$(sha1sum <<< "$1") out=$(sha1sum <<<"$1")
elif has shasum; then elif has shasum; then
out=$(shasum <<< "$1") out=$(shasum <<<"$1")
else else
# degrade gracefully both tools are not present # degrade gracefully both tools are not present
return return
fi fi
read -r checksum _ <<< "$out" read -r checksum _ <<<"$out"
echo "-$checksum" echo "-$checksum"
fi fi
} }
@ -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
@ -295,7 +294,7 @@ use_flake() {
_nix_clean_old_gcroots "$layout_dir" _nix_clean_old_gcroots "$layout_dir"
# We need to update our cache # We need to update our cache
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"*
@ -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
@ -335,20 +334,23 @@ use_nix() {
layout_dir=$(direnv_layout_dir) layout_dir=$(direnv_layout_dir)
if path=$(_nix eval --impure --expr "<nixpkgs>" 2>/dev/null); then if path=$(_nix eval --impure --expr "<nixpkgs>" 2>/dev/null); then
if [[ -f "${path}/.version-suffix" ]]; then if [[ -f "${path}/.version-suffix" ]]; then
version=$(< "${path}/.version-suffix") version=$(<"${path}/.version-suffix")
elif [[ -f "${path}/.git/HEAD" ]]; then elif [[ -f "${path}/.git/HEAD" ]]; then
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,48 +371,48 @@ use_nix() {
nixfile="./default.nix" nixfile="./default.nix"
fi fi
while [[ "$#" -gt 0 ]]; do while [[ $# -gt 0 ]]; do
i="$1" i="$1"
shift shift
case $i in case $i in
-p|--packages) -p | --packages)
in_packages=1 in_packages=1
;; ;;
--command|--run|--exclude) --command | --run | --exclude)
# These commands are unsupported # These commands are unsupported
# ignore them # ignore them
shift shift
;; ;;
--pure|-i|--keep) --pure | -i | --keep)
# These commands are unsupported (but take no argument) # These commands are unsupported (but take no argument)
# ignore them # ignore them
;; ;;
--include|-I) --include | -I)
extra_args+=("$i" "$1") extra_args+=("$i" "$1")
shift shift
;; ;;
--attr|-A) --attr | -A)
attribute="$1" attribute="$1"
shift shift
;; ;;
--option|-o|--arg|--argstr) --option | -o | --arg | --argstr)
extra_args+=("$i" "$1" "$2") extra_args+=("$i" "$1" "$2")
shift shift
shift shift
;; ;;
-*) -*)
# Other arguments are assumed to be of a single arg form # Other arguments are assumed to be of a single arg form
# (--foo=bar or -j4) # (--foo=bar or -j4)
extra_args+=("$i") extra_args+=("$i")
;; ;;
*) *)
if [[ $in_packages -eq 1 ]]; then if [[ $in_packages -eq 1 ]]; then
packages+=" $i" packages+=" $i"
else else
nixfile=$i nixfile=$i
fi fi
;; ;;
esac esac
done done
@ -421,50 +423,47 @@ 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" \
--impure \ --impure \
"${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"*
_nix_direnv_info "renewed cache" _nix_direnv_info "renewed cache"
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

@ -2,26 +2,26 @@
set -eu -o pipefail set -eu -o pipefail
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" 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
if [[ "$(git symbolic-ref --short HEAD)" != "master" ]]; then if [[ "$(git symbolic-ref --short HEAD)" != "master" ]]; then
echo "must be on master branch" 2>/dev/null echo "must be on master branch" 2>/dev/null
exit 1 exit 1
fi fi
sed -Ei "s!(NIX_DIRENV_VERSION=).*!\1$version!" direnvrc sed -Ei "s!(NIX_DIRENV_VERSION=).*!\1$version!" direnvrc
sed -i README.md templates/flake/.envrc \ sed -i README.md templates/flake/.envrc \
-e 's!\(nix-direnv/\).*\(/direnvrc\)!\1'"${version}"'\2!' \ -e 's!\(nix-direnv/\).*\(/direnvrc\)!\1'"${version}"'\2!' \
-e 's?\( ! nix_direnv_version \)[0-9.]\+\(; \)?\1'"${version}"'\2?' -e 's?\( ! nix_direnv_version \)[0-9.]\+\(; \)?\1'"${version}"'\2?'
git add README.md direnvrc templates/flake/.envrc git add README.md direnvrc templates/flake/.envrc
git commit -m "bump version ${version}" git commit -m "bump version ${version}"
git tag -e "${version}" git tag -e "${version}"

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
cd "$SCRIPT_DIR/.." cd "$SCRIPT_DIR/.."
tag=$(git describe --tags | cut -d- -f1) tag=$(git describe --tags | cut -d- -f1)

View File

@ -8,40 +8,38 @@
# 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" ];
};
python = {
command = "sh";
options = [
"-eucx"
''
${lib.getExe pkgs.ruff} --fix "$@"
${lib.getExe pkgs.ruff} format "$@"
''
"--" # this argument is ignored by bash
];
includes = [ "*.py" ];
};
}; };
settings.formatter =
let
sh-includes = [ "*.sh" "direnvrc" ];
in
{
python = {
command = "sh";
options = [
"-eucx"
''
${lib.getExe pkgs.ruff} --fix "$@"
${lib.getExe pkgs.ruff} format "$@"
''
"--" # this argument is ignored by bash
];
includes = [ "*.py" ];
};
shellcheck.includes = sh-includes;
shfmt.includes = sh-includes;
};
}; };
}; };
} }