mirror of
https://github.com/nix-community/home-manager
synced 2024-11-23 03:29:45 +01:00
flake: add formatter (#3620)
* format: improve argument handling For now, fail if the user tries to format a specific file/directory, or runs the formatter from within a subdirectory. Handling these situations is slightly tricky because `find -path` is not very flexible. * flake: add formatter This allows running the formatter with `nix fmt`, added in Nix 2.8. * format: use git ls-files This is cleaner than `find` and allows us to restrict formatting to particular files or subdirectories.
This commit is contained in:
parent
4c08f65ab5
commit
9dd107a1d5
2 changed files with 53 additions and 22 deletions
|
@ -55,7 +55,7 @@
|
||||||
- 'system'
|
- 'system'
|
||||||
|
|
||||||
have been removed. Instead use the arguments 'pkgs' and
|
have been removed. Instead use the arguments 'pkgs' and
|
||||||
'modules'. See the 22.11 release notes for more: https://nix-community.github.io/home-manager/release-notes.html#sec-release-22.11-highlights
|
'modules'. See the 22.11 release notes for more: https://nix-community.github.io/home-manager/release-notes.html#sec-release-22.11-highlights
|
||||||
'';
|
'';
|
||||||
|
|
||||||
throwForRemovedArgs = v:
|
throwForRemovedArgs = v:
|
||||||
|
@ -93,6 +93,13 @@
|
||||||
tests = import ./tests { inherit pkgs; };
|
tests = import ./tests { inherit pkgs; };
|
||||||
in tests.run);
|
in tests.run);
|
||||||
|
|
||||||
|
formatter = forAllSystems (system:
|
||||||
|
let pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
in pkgs.linkFarm "format" [{
|
||||||
|
name = "bin/format";
|
||||||
|
path = ./format;
|
||||||
|
}]);
|
||||||
|
|
||||||
packages = forAllSystems (system:
|
packages = forAllSystems (system:
|
||||||
let
|
let
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
|
66
format
66
format
|
@ -1,27 +1,51 @@
|
||||||
#! /usr/bin/env nix-shell
|
#! /usr/bin/env nix-shell
|
||||||
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/6616de389ed55fba6eeba60377fc04732d5a207c.tar.gz -i bash -p findutils nixfmt
|
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/6616de389ed55fba6eeba60377fc04732d5a207c.tar.gz -i bash -p git gnugrep gnused findutils nixfmt
|
||||||
|
|
||||||
CHECK_ARG=
|
nixfmt_args=()
|
||||||
|
files=()
|
||||||
|
|
||||||
case $1 in
|
for arg do
|
||||||
-h)
|
case $arg in
|
||||||
echo "$0 [-c]"
|
-h)
|
||||||
;;
|
echo "$0 [-c]"
|
||||||
-c)
|
exit
|
||||||
CHECK_ARG=-c
|
;;
|
||||||
;;
|
-c)
|
||||||
esac
|
nixfmt_args+=("$arg")
|
||||||
|
;;
|
||||||
|
-*)
|
||||||
|
echo "unrecognised flag: $arg" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
files+=("$arg")
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
# The excludes are for files touched by open pull requests and we want
|
# The excludes are for files touched by open pull requests and we want
|
||||||
# to avoid merge conflicts.
|
# to avoid merge conflicts.
|
||||||
find . -name '*.nix' \
|
excludes=(
|
||||||
! -path ./modules/default.nix \
|
modules/default.nix
|
||||||
! -path ./modules/files.nix \
|
modules/files.nix
|
||||||
! -path ./modules/home-environment.nix \
|
modules/home-environment.nix
|
||||||
! -path ./modules/lib/default.nix \
|
modules/lib/default.nix
|
||||||
! -path ./modules/lib/file-type.nix \
|
modules/lib/file-type.nix
|
||||||
! -path ./modules/misc/news.nix \
|
modules/misc/news.nix
|
||||||
! -path ./modules/programs/ssh.nix \
|
modules/programs/ssh.nix
|
||||||
! -path ./modules/programs/zsh.nix \
|
modules/programs/zsh.nix
|
||||||
! -path ./tests/default.nix \
|
tests/default.nix
|
||||||
-exec nixfmt $CHECK_ARG {} +
|
)
|
||||||
|
|
||||||
|
exclude_args=()
|
||||||
|
for e in "${excludes[@]}"; do
|
||||||
|
exclude_args+=(-e "$e")
|
||||||
|
done
|
||||||
|
|
||||||
|
git_root=$(git rev-parse --show-toplevel)
|
||||||
|
|
||||||
|
git ls-files -z --cached --others --full-name -- "${files[@]}" |
|
||||||
|
grep -z '\.nix$' |
|
||||||
|
grep -z -v "${exclude_args[@]}" |
|
||||||
|
sed -z "s|^|$git_root/|" |
|
||||||
|
xargs -0 nixfmt "${nixfmt_args[@]}"
|
||||||
|
|
Loading…
Reference in a new issue