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

View File

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