From 708f498c4b008d3d6cbe178b5eeebda5f55c4350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Wed, 25 Jan 2023 12:58:38 +0100 Subject: [PATCH 1/3] 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. --- format | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/format b/format index ef5528b76588..6e0d6100cf35 100755 --- a/format +++ b/format @@ -1,16 +1,30 @@ #! /usr/bin/env nix-shell #! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/6616de389ed55fba6eeba60377fc04732d5a207c.tar.gz -i bash -p findutils nixfmt -CHECK_ARG= +nixfmt_args=() -case $1 in - -h) - echo "$0 [-c]" - ;; - -c) - CHECK_ARG=-c - ;; -esac +for arg do + case $arg in + -h) + echo "$0 [-c]" + exit + ;; + -c) + nixfmt_args+=("$arg") + ;; + .) + ;; + *) + echo "unrecognised argument: $arg" >&2 + exit 1 + ;; + esac +done + +if [[ ! -e flake.nix ]]; then + echo "must be run from the root directory" >&2 + exit 1 +fi # The excludes are for files touched by open pull requests and we want # to avoid merge conflicts. @@ -24,4 +38,4 @@ find . -name '*.nix' \ ! -path ./modules/programs/ssh.nix \ ! -path ./modules/programs/zsh.nix \ ! -path ./tests/default.nix \ - -exec nixfmt $CHECK_ARG {} + + -exec nixfmt "${nixfmt_args[@]}" {} + From 8d4fd9bb10de646a477c62f78502954beb459a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Wed, 25 Jan 2023 13:00:41 +0100 Subject: [PATCH 2/3] flake: add formatter This allows running the formatter with `nix fmt`, added in Nix 2.8. --- flake.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 0b7eaa913cf8..5e354f780f28 100644 --- a/flake.nix +++ b/flake.nix @@ -55,7 +55,7 @@ - 'system' 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: @@ -93,6 +93,13 @@ tests = import ./tests { inherit pkgs; }; in tests.run); + formatter = forAllSystems (system: + let pkgs = nixpkgs.legacyPackages.${system}; + in pkgs.linkFarm "format" [{ + name = "bin/format"; + path = ./format; + }]); + packages = forAllSystems (system: let pkgs = nixpkgs.legacyPackages.${system}; From bbec47d88b558b593037e3c72c59e26b08a846cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Sat, 28 Jan 2023 12:17:31 +0100 Subject: [PATCH 3/3] format: use git ls-files This is cleaner than `find` and allows us to restrict formatting to particular files or subdirectories. --- format | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/format b/format index 6e0d6100cf35..ff4b32e4761a 100755 --- a/format +++ b/format @@ -1,7 +1,8 @@ #! /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 nixfmt_args=() +files=() for arg do case $arg in @@ -12,30 +13,39 @@ for arg do -c) nixfmt_args+=("$arg") ;; - .) + -*) + echo "unrecognised flag: $arg" >&2 + exit 1 ;; *) - echo "unrecognised argument: $arg" >&2 - exit 1 + files+=("$arg") ;; esac done -if [[ ! -e flake.nix ]]; then - echo "must be run from the root directory" >&2 - exit 1 -fi - # The excludes are for files touched by open pull requests and we want # to avoid merge conflicts. -find . -name '*.nix' \ - ! -path ./modules/default.nix \ - ! -path ./modules/files.nix \ - ! -path ./modules/home-environment.nix \ - ! -path ./modules/lib/default.nix \ - ! -path ./modules/lib/file-type.nix \ - ! -path ./modules/misc/news.nix \ - ! -path ./modules/programs/ssh.nix \ - ! -path ./modules/programs/zsh.nix \ - ! -path ./tests/default.nix \ - -exec nixfmt "${nixfmt_args[@]}" {} + +excludes=( + modules/default.nix + modules/files.nix + modules/home-environment.nix + modules/lib/default.nix + modules/lib/file-type.nix + modules/misc/news.nix + modules/programs/ssh.nix + modules/programs/zsh.nix + tests/default.nix +) + +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[@]}"