2024-03-27 16:48:59 -06:00
|
|
|
# This file was copied mostly from check-maintainers-sorted.yaml.
|
|
|
|
# NOTE: Formatting with the RFC-style nixfmt command is not yet stable. See
|
|
|
|
# https://github.com/NixOS/rfcs/pull/166.
|
|
|
|
# Because of this, this action is not yet enabled for all files -- only for
|
|
|
|
# those who have opted in.
|
|
|
|
name: Check that Nix files are formatted
|
|
|
|
|
|
|
|
on:
|
|
|
|
pull_request_target:
|
2024-09-02 13:33:28 -06:00
|
|
|
# See the comment at the same location in ./nixpkgs-vet.yml
|
2024-07-23 13:03:15 -06:00
|
|
|
types: [opened, synchronize, reopened, edited]
|
2024-03-27 16:48:59 -06:00
|
|
|
permissions:
|
|
|
|
contents: read
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
nixos:
|
2024-07-31 21:28:40 -06:00
|
|
|
name: nixfmt-check
|
2024-03-27 16:48:59 -06:00
|
|
|
runs-on: ubuntu-latest
|
2024-07-27 06:14:12 -06:00
|
|
|
if: "!contains(github.event.pull_request.title, '[skip treewide]')"
|
2024-03-27 16:48:59 -06:00
|
|
|
steps:
|
2024-10-14 05:35:32 -06:00
|
|
|
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
|
2024-03-27 16:48:59 -06:00
|
|
|
with:
|
|
|
|
# pull_request_target checks out the base branch by default
|
|
|
|
ref: refs/pull/${{ github.event.pull_request.number }}/merge
|
2024-07-23 13:03:15 -06:00
|
|
|
# Fetches the merge commit and its parents
|
|
|
|
fetch-depth: 2
|
|
|
|
- name: Checking out base branch
|
|
|
|
run: |
|
|
|
|
base=$(mktemp -d)
|
|
|
|
baseRev=$(git rev-parse HEAD^1)
|
|
|
|
git worktree add "$base" "$baseRev"
|
|
|
|
echo "baseRev=$baseRev" >> "$GITHUB_ENV"
|
|
|
|
echo "base=$base" >> "$GITHUB_ENV"
|
2024-06-25 16:52:13 -06:00
|
|
|
- name: Get Nixpkgs revision for nixfmt
|
|
|
|
run: |
|
|
|
|
# pin to a commit from nixpkgs-unstable to avoid e.g. building nixfmt
|
|
|
|
# from staging
|
|
|
|
# This should not be a URL, because it would allow PRs to run arbitrary code in CI!
|
|
|
|
rev=$(jq -r .rev ci/pinned-nixpkgs.json)
|
|
|
|
echo "url=https://github.com/NixOS/nixpkgs/archive/$rev.tar.gz" >> "$GITHUB_ENV"
|
2024-10-07 05:17:58 -06:00
|
|
|
- uses: cachix/install-nix-action@08dcb3a5e62fa31e2da3d490afc4176ef55ecd72 # v30
|
2024-03-27 16:48:59 -06:00
|
|
|
with:
|
|
|
|
# explicitly enable sandbox
|
|
|
|
extra_nix_config: sandbox = true
|
2024-06-25 16:52:13 -06:00
|
|
|
nix_path: nixpkgs=${{ env.url }}
|
2024-03-27 16:48:59 -06:00
|
|
|
- name: Install nixfmt
|
2024-04-03 11:57:03 -06:00
|
|
|
run: "nix-env -f '<nixpkgs>' -iAP nixfmt-rfc-style"
|
2024-03-27 16:48:59 -06:00
|
|
|
- name: Check that Nix files are formatted according to the RFC style
|
|
|
|
run: |
|
2024-07-23 13:03:15 -06:00
|
|
|
unformattedFiles=()
|
|
|
|
|
|
|
|
# TODO: Make this more parallel
|
|
|
|
|
|
|
|
# Loop through all Nix files touched by the PR
|
|
|
|
while readarray -d '' -n 2 entry && (( ${#entry[@]} != 0 )); do
|
|
|
|
type=${entry[0]}
|
|
|
|
file=${entry[1]}
|
|
|
|
case $type in
|
|
|
|
A*)
|
|
|
|
source=""
|
|
|
|
dest=$file
|
|
|
|
;;
|
|
|
|
M*)
|
|
|
|
source=$file
|
|
|
|
dest=$file
|
|
|
|
;;
|
|
|
|
C*|R*)
|
|
|
|
source=$file
|
|
|
|
read -r -d '' dest
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Ignoring file $file with type $type"
|
|
|
|
continue
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Ignore files that weren't already formatted
|
|
|
|
if [[ -n "$source" ]] && ! nixfmt --check ${{ env.base }}/"$source" 2>/dev/null; then
|
|
|
|
echo "Ignoring file $file because it's not formatted in the base commit"
|
|
|
|
elif ! nixfmt --check "$dest"; then
|
2024-07-27 06:08:22 -06:00
|
|
|
unformattedFiles+=("$dest")
|
2024-03-27 16:48:59 -06:00
|
|
|
fi
|
2024-07-23 13:03:15 -06:00
|
|
|
done < <(git diff -z --name-status ${{ env.baseRev }} -- '*.nix')
|
|
|
|
|
|
|
|
if (( "${#unformattedFiles[@]}" > 0 )); then
|
|
|
|
echo "Some new/changed Nix files are not properly formatted"
|
2024-08-19 08:26:54 -06:00
|
|
|
echo "Please go to the Nixpkgs root directory, run \`nix-shell\`, then:"
|
2024-07-23 13:03:15 -06:00
|
|
|
echo "nixfmt ${unformattedFiles[*]@Q}"
|
2024-08-24 17:12:25 -06:00
|
|
|
echo "If you're having trouble, please ping @NixOS/nix-formatting"
|
2024-06-25 17:08:17 -06:00
|
|
|
exit 1
|
|
|
|
fi
|