Skip to content

Commit 1f0b359

Browse files
authored
Enforce nixfmt on new files and changed files that were already formatted (#326407)
* workflows/check-nix-format: Enforce nixfmt on new/changed files This makes the Nix format workflow check new/changed files instead of just an allowlist. This enforces that all PRs updated after this is merged are required to have fully standard formatted Nix files! * workflows/check-nix-format: determine changed files via base commit The next commit will use this to have a simpler change * workflows/check-nix-format: Only ensure for already formatted files This prevents situations where contributors need to suddenly format a huge file even if they only changed a small part of it (e.g. all-packages.nix)
1 parent 42cc340 commit 1f0b359

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

.github/workflows/check-nix-format.yml

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,29 @@ name: Check that Nix files are formatted
77

88
on:
99
pull_request_target:
10+
# See the comment at the same location in ./check-by-name.yml
11+
types: [opened, synchronize, reopened, edited]
1012
permissions:
1113
contents: read
1214

1315
jobs:
1416
nixos:
1517
runs-on: ubuntu-latest
16-
if: github.repository_owner == 'NixOS'
18+
if: "github.repository_owner == 'NixOS' && !contains(github.event.pull_request.title, '[skip treewide]')"
1719
steps:
1820
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
1921
with:
2022
# pull_request_target checks out the base branch by default
2123
ref: refs/pull/${{ github.event.pull_request.number }}/merge
24+
# Fetches the merge commit and its parents
25+
fetch-depth: 2
26+
- name: Checking out base branch
27+
run: |
28+
base=$(mktemp -d)
29+
baseRev=$(git rev-parse HEAD^1)
30+
git worktree add "$base" "$baseRev"
31+
echo "baseRev=$baseRev" >> "$GITHUB_ENV"
32+
echo "base=$base" >> "$GITHUB_ENV"
2233
- name: Get Nixpkgs revision for nixfmt
2334
run: |
2435
# pin to a commit from nixpkgs-unstable to avoid e.g. building nixfmt
@@ -34,43 +45,44 @@ jobs:
3445
- name: Install nixfmt
3546
run: "nix-env -f '<nixpkgs>' -iAP nixfmt-rfc-style"
3647
- name: Check that Nix files are formatted according to the RFC style
37-
# Each environment variable beginning with NIX_FMT_PATHS_ is a list of
38-
# paths to check with nixfmt.
39-
env:
40-
NIX_FMT_PATHS_BSD: pkgs/os-specific/bsd
41-
NIX_FMT_PATHS_MPVSCRIPTS: pkgs/applications/video/mpv/scripts
42-
# Format paths related to the Nixpkgs CUDA ecosystem.
43-
NIX_FMT_PATHS_CUDA: |-
44-
pkgs/development/cuda-modules
45-
pkgs/test/cuda
46-
pkgs/top-level/cuda-packages.nix
47-
NIX_FMT_PATHS_MAINTAINERS: |-
48-
maintainers/maintainer-list.nix
49-
maintainers/team-list.nix
50-
NIX_FMT_PATHS_K3S: |-
51-
nixos/modules/services/cluster/k3s
52-
nixos/tests/k3s
53-
pkgs/applications/networking/cluster/k3s
54-
NIX_FMT_PATHS_VSCODE_EXTS: pkgs/applications/editors/vscode/extensions
55-
NIX_FMT_PATHS_PHP_PACKAGES: pkgs/development/php-packages
56-
NIX_FMT_PATHS_BUILD_SUPPORT_PHP: pkgs/build-support/php
57-
# Iterate over all environment variables beginning with NIX_FMT_PATHS_.
5848
run: |
59-
unformattedPaths=()
60-
for env_var in "${!NIX_FMT_PATHS_@}"; do
61-
readarray -t paths <<< "${!env_var}"
62-
if [[ "${paths[*]}" == "" ]]; then
63-
echo "Error: $env_var is empty."
64-
exit 1
65-
fi
66-
echo "Checking paths: ${paths[@]}"
67-
if ! nixfmt --check "${paths[@]}"; then
68-
unformattedPaths+=("${paths[@]}")
49+
unformattedFiles=()
50+
51+
# TODO: Make this more parallel
52+
53+
# Loop through all Nix files touched by the PR
54+
while readarray -d '' -n 2 entry && (( ${#entry[@]} != 0 )); do
55+
type=${entry[0]}
56+
file=${entry[1]}
57+
case $type in
58+
A*)
59+
source=""
60+
dest=$file
61+
;;
62+
M*)
63+
source=$file
64+
dest=$file
65+
;;
66+
C*|R*)
67+
source=$file
68+
read -r -d '' dest
69+
;;
70+
*)
71+
echo "Ignoring file $file with type $type"
72+
continue
73+
esac
74+
75+
# Ignore files that weren't already formatted
76+
if [[ -n "$source" ]] && ! nixfmt --check ${{ env.base }}/"$source" 2>/dev/null; then
77+
echo "Ignoring file $file because it's not formatted in the base commit"
78+
elif ! nixfmt --check "$dest"; then
79+
unformattedFiles+=("$file")
6980
fi
70-
done
71-
if (( "${#unformattedPaths[@]}" > 0 )); then
72-
echo "Some required Nix files are not properly formatted"
81+
done < <(git diff -z --name-status ${{ env.baseRev }} -- '*.nix')
82+
83+
if (( "${#unformattedFiles[@]}" > 0 )); then
84+
echo "Some new/changed Nix files are not properly formatted"
7385
echo "Please run the following in \`nix-shell\`:"
74-
echo "nixfmt ${unformattedPaths[*]@Q}"
86+
echo "nixfmt ${unformattedFiles[*]@Q}"
7587
exit 1
7688
fi

0 commit comments

Comments
 (0)