Skip to content

Commit ba6425a

Browse files
committed
dev: Configure nixfmt (rfc style)
1 parent 99dbcca commit ba6425a

File tree

5 files changed

+114
-3
lines changed

5 files changed

+114
-3
lines changed

doc/manual/source/development/building.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ You may run the formatters as a one-off using:
278278
./maintainers/format.sh
279279
```
280280

281+
### Pre-commit hooks
282+
281283
If you'd like to run the formatters before every commit, install the hooks:
282284

283285
```
@@ -292,3 +294,30 @@ If it fails, run `git add --patch` to approve the suggestions _and commit again_
292294
To refresh pre-commit hook's config file, do the following:
293295
1. Exit the development shell and start it again by running `nix develop`.
294296
2. If you also use the pre-commit hook, also run `pre-commit-hooks-install` again.
297+
298+
### VSCode
299+
300+
Insert the following json into your `.vscode/settings.json` file to configure `nixfmt`.
301+
This will be picked up by the _Format Document_ command, `"editor.formatOnSave"`, etc.
302+
303+
```json
304+
{
305+
"nix.formatterPath": "nixfmt",
306+
"nix.serverSettings": {
307+
"nixd": {
308+
"formatting": {
309+
"command": [
310+
"nixfmt"
311+
],
312+
},
313+
},
314+
"nil": {
315+
"formatting": {
316+
"command": [
317+
"nixfmt"
318+
],
319+
},
320+
},
321+
},
322+
}
323+
```

flake.lock

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# work around 7730 and https://github.com/NixOS/nix/issues/7807
1818
inputs.git-hooks-nix.inputs.flake-compat.follows = "";
1919
inputs.git-hooks-nix.inputs.gitignore.follows = "";
20+
inputs.nixfmt.url = "github:NixOS/nixfmt";
2021

2122
outputs = inputs@{ self, nixpkgs, nixpkgs-regression, ... }:
2223

@@ -301,7 +302,7 @@
301302
});
302303

303304
devShells = let
304-
makeShell = import ./packaging/dev-shell.nix { inherit lib devFlake; };
305+
makeShell = import ./packaging/dev-shell.nix { inherit inputs lib devFlake; };
305306
prefixAttrs = prefix: lib.concatMapAttrs (k: v: { "${prefix}-${k}" = v; });
306307
in
307308
forAllSystems (system:

maintainers/flake-module.nix

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,35 @@
3131
touch $out
3232
''}";
3333
};
34+
nixfmt-rfc-style = {
35+
enable = true;
36+
package = inputs.nixfmt.packages.${pkgs.hostPlatform.system}.default;
37+
excludes = [
38+
# Invalid
39+
''^tests/functional/lang/parse-.*\.nix$''
40+
41+
# Formatting-sensitive
42+
''^tests/functional/lang/eval-okay-curpos\.nix$''
43+
''^tests/functional/lang/.*comment.*\.nix$''
44+
''^tests/functional/lang/.*newline.*\.nix$''
45+
''^tests/functional/lang/.*eol.*\.nix$''
46+
47+
# Syntax tests
48+
''^tests/functional/shell.shebang\.nix$''
49+
''^tests/functional/lang/eval-okay-ind-string\.nix$''
50+
51+
# Not supported by nixfmt
52+
''^tests/functional/lang/eval-okay-deprecate-cursed-or\.nix$''
53+
''^tests/functional/lang/eval-okay-attrs5\.nix$''
54+
55+
# More syntax tests
56+
# These tests, or parts of them, should have been parse-* test cases.
57+
''^tests/functional/lang/eval-fail-eol-2\.nix$''
58+
''^tests/functional/lang/eval-fail-path-slash\.nix$''
59+
''^tests/functional/lang/eval-fail-toJSON-non-utf-8\.nix$''
60+
''^tests/functional/lang/eval-fail-set\.nix$''
61+
];
62+
};
3463
clang-format = {
3564
enable = true;
3665
# https://github.com/cachix/git-hooks.nix/pull/532
@@ -660,7 +689,6 @@
660689
''^src/libutil-tests/data/git/check-data\.sh$''
661690
];
662691
};
663-
# TODO: nixfmt, https://github.com/NixOS/nixfmt/issues/153
664692
};
665693
};
666694
};

packaging/dev-shell.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ lib, devFlake }:
1+
{ lib, inputs, devFlake }:
22

33
{ pkgs }:
44

@@ -108,6 +108,7 @@ in {
108108
modular.pre-commit.settings.package
109109
(pkgs.writeScriptBin "pre-commit-hooks-install"
110110
modular.pre-commit.settings.installationScript)
111+
inputs.nixfmt.packages.${pkgs.hostPlatform.system}.default
111112
]
112113
# TODO: Remove the darwin check once
113114
# https://github.com/NixOS/nixpkgs/pull/291814 is available

0 commit comments

Comments
 (0)