Skip to content

Commit f6bcb56

Browse files
authored
nix fmt (#203)
* use nixfmt-tree for nix fmt nixfmt-tree allows you to easily format the entire git tree with one call to `nix fmt`. * github workflows: add format checker * gh actions: only check formatting on changed files we dont want to annoy people by failing a test on a pr due to something that is not the fault of the pr note: this still includes files where the formatting issues are not in the changed lines of that file, thus not the fault of the pr
2 parents 0c5e0c6 + 913f2ca commit f6bcb56

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

.github/workflows/format-check.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Check formatting
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
branches:
7+
- main
8+
push:
9+
branches:
10+
- main
11+
12+
jobs:
13+
check-format:
14+
runs-on: ci-ocf-nix-build
15+
steps:
16+
- uses: actions/checkout@v4
17+
# we need history to be able to get a list of changed files. a treeless
18+
# clone allows us to get full history without downloading massive
19+
# amounts of file changes.
20+
with:
21+
fetch-depth: 0
22+
filter: blob:none
23+
- name: Check formatting with nixfmt-tree
24+
run: |
25+
# for pull requests, check against the target branch
26+
# for push, check against the previous commit
27+
if [ -n "${{ github.base_ref }}" ]; then
28+
base="origin/${{ github.base_ref }}"
29+
else
30+
base="HEAD^"
31+
fi
32+
33+
# get a list of changes (add, copy, modify, rename) since branch
34+
# diverged from main
35+
# deleted files are not included to avoid treefmt errors
36+
changes=$(git diff --name-only --diff-filter=ACMR $base...HEAD)
37+
38+
# only check formatting on changed files
39+
# we dont want to annoy people by failing a test on a pr due to
40+
# something that is not the fault of the pr
41+
# note: this still includes files where the formatting issues are not
42+
# in the changed lines of that file, thus not the fault of the pr
43+
if [ -n "$changes" ]; then
44+
nix develop -c treefmt --ci $changes
45+
fi

flake.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
hosts;
210210
in
211211
{
212-
formatter = forAllSystems (pkgs: pkgs.nixpkgs-fmt);
212+
formatter = forAllSystems (pkgs: pkgs.nixfmt-tree);
213213

214214
colmenaHive = colmena.lib.makeHive (colmenaHosts // {
215215
meta = {
@@ -268,6 +268,7 @@
268268
pkgs.agenix-rekey
269269
pkgs.age-plugin-fido2-hmac
270270
pkgs.wol
271+
pkgs.nixfmt-tree
271272
colmena.packages.${pkgs.stdenv.hostPlatform.system}.colmena
272273
];
273274
};
@@ -276,6 +277,7 @@
276277
pkgs.git
277278
pkgs.openssh
278279
pkgs.wol
280+
pkgs.nixfmt-tree
279281
colmena.packages.${pkgs.stdenv.hostPlatform.system}.colmena
280282
];
281283
};

0 commit comments

Comments
 (0)