Skip to content

Commit e891a93

Browse files
committed
docs: document nix fmt support
1 parent c840984 commit e891a93

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

README.md

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,62 @@ Given the following `flake.nix` example:
5151

5252
```nix
5353
{
54-
description = "An example project.";
54+
description = "An example project";
5555
56-
inputs.pre-commit-hooks.url = "github:cachix/git-hooks.nix";
56+
inputs = {
57+
systems.url = "github:nix-systems/default";
58+
git-hooks.url = "github:cachix/git-hooks.nix";
59+
};
5760
58-
outputs = { self, nixpkgs, ... }@inputs:
61+
outputs =
62+
{
63+
self,
64+
systems,
65+
nixpkgs,
66+
...
67+
}@inputs:
5968
let
60-
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
61-
62-
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
69+
forEachSystem = nixpkgs.lib.genAttrs (import systems);
6370
in
6471
{
65-
checks = forAllSystems (system: {
66-
pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
72+
# Run the hooks with `nix fmt`.
73+
formatter = forEachSystem (
74+
system:
75+
let
76+
pkgs = nixpkgs.legacyPackages.${system};
77+
config = self.checks.${system}.pre-commit-check.config;
78+
inherit (config) package configFile;
79+
script = ''
80+
${package}/bin/pre-commit run --all-files --config ${configFile}
81+
'';
82+
in
83+
pkgs.writeShellScriptBin "pre-commit-run" script
84+
);
85+
86+
# Run the hooks in a sandbox with `nix flake check`.
87+
# Read-only filesystem and no internet access.
88+
checks = forEachSystem (system: {
89+
pre-commit-check = inputs.git-hooks.lib.${system}.run {
6790
src = ./.;
6891
hooks = {
69-
nixpkgs-fmt.enable = true;
92+
nixfmt-rfc-style.enable = true;
7093
};
7194
};
7295
});
7396
74-
devShells = forAllSystems (system: {
75-
default = nixpkgs.legacyPackages.${system}.mkShell {
76-
inherit (self.checks.${system}.pre-commit-check) shellHook;
77-
buildInputs = self.checks.${system}.pre-commit-check.enabledPackages;
78-
};
97+
# Enter a development shell with `nix develop`.
98+
# The hooks will be installed automatically.
99+
# Or run pre-commit manually with `nix develop -c pre-commit run --all-files`
100+
devShells = forEachSystem (system: {
101+
default =
102+
let
103+
pkgs = nixpkgs.legacyPackages.${system};
104+
inherit (self.checks.${system}.pre-commit-check) shellHook enabledPackages;
105+
in
106+
pkgs.mkShell {
107+
inherit shellHook;
108+
buildInputs = enabledPackages;
109+
};
79110
});
80111
};
81112
}
@@ -106,6 +137,12 @@ A better alternative in such cases is to run `pre-commit` through `nix develop`:
106137
nix develop -c pre-commit run -a
107138
```
108139

140+
Or configure a `formatter` like in the example above and use `nix fmt`:
141+
142+
```shell
143+
nix fmt
144+
```
145+
109146
### flake-parts
110147

111148
If your flake uses [flake-parts](https://flake.parts/), we provide a flake-parts module as well. Checkout [`./template/flake.nix`](https://github.com/cachix/git-hooks.nix/blob/master/template/flake.nix) for an example.

0 commit comments

Comments
 (0)