Skip to content

Commit 9306d88

Browse files
authored
Merge pull request #639 from cachix/update-nixfmt
nixfmt: support the new official 1.0 release of nixfmt
2 parents e00bda9 + c39a7a5 commit 9306d88

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,34 @@
2222
2323
{
2424
git-hooks.hooks = {
25-
# lint shell scripts
25+
# Format Nix code
26+
nixfmt.enable = true;
27+
28+
# Format Python code
29+
black.enable = true;
30+
31+
# Lint shell scripts
2632
shellcheck.enable = true;
27-
# execute example shell from Markdown files
33+
34+
# Execute shell examples in Markdown files
2835
mdsh.enable = true;
29-
# format Python code
30-
black.enable = true;
3136
32-
# override a package with a different version
37+
# Override a package with a different version
3338
ormolu.enable = true;
3439
ormolu.package = pkgs.haskellPackages.ormolu;
3540
36-
# some hooks have more than one package, like clippy:
41+
# Some hooks have more than one package, like clippy:
3742
clippy.enable = true;
3843
clippy.packageOverrides.cargo = pkgs.cargo;
3944
clippy.packageOverrides.clippy = pkgs.clippy;
40-
# some hooks provide settings
45+
# Some hooks provide settings
4146
clippy.settings.allFeatures = true;
47+
48+
# Define your own custom hooks
49+
my-custom-hook = {
50+
name = "My own hook";
51+
exec = "on-pre-commit.sh";
52+
};
4253
};
4354
}
4455
```
@@ -89,7 +100,7 @@ Given the following `flake.nix` example:
89100
pre-commit-check = inputs.git-hooks.lib.${system}.run {
90101
src = ./.;
91102
hooks = {
92-
nixfmt-rfc-style.enable = true;
103+
nixfmt.enable = true;
93104
};
94105
};
95106
});
@@ -371,6 +382,7 @@ use nix
371382
- [deadnix](https://github.com/astro/deadnix)
372383
- [flake-checker](https://github.com/DeterminateSystems/flake-checker)
373384
- [nil](https://github.com/oxalica/nil)
385+
- [nixfmt](https://github.com/NixOS/nixfmt/) (supports `nixfmt` >=v1.0)
374386
- [nixfmt-classic](https://github.com/NixOS/nixfmt/tree/v0.6.0)
375387
- [nixfmt-rfc-style](https://github.com/NixOS/nixfmt/)
376388
- [nixpkgs-fmt](https://github.com/nix-community/nixpkgs-fmt)

modules/hooks.nix

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ in
959959
};
960960
};
961961
nixfmt = mkOption {
962-
description = "Deprecated nixfmt hook. Use nixfmt-classic or nixfmt-rfc-style instead.";
962+
description = "nixfmt hook";
963963
visible = false;
964964
type = types.submodule {
965965
imports = [ hookModule ];
@@ -2143,10 +2143,19 @@ in
21432143
lib.optional cfg.hooks.rome.enable ''
21442144
The hook `hooks.rome` has been renamed to `hooks.biome`.
21452145
''
2146-
++ lib.optional cfg.hooks.nixfmt.enable ''
2146+
++ lib.optional (cfg.hooks.nixfmt.enable && lib.versionOlder cfg.hooks.nixfmt.package.version "1.0") ''
21472147
The hook `hooks.nixfmt` has been renamed to `hooks.nixfmt-classic`.
21482148
21492149
The new RFC 166-style nixfmt is available as `hooks.nixfmt-rfc-style`.
2150+
''
2151+
++ lib.optional (cfg.hooks.nixfmt-classic.enable && lib.versionAtLeast cfg.hooks.nixfmt-classic.package.version "1.0") ''
2152+
The hook `hooks.nixfmt-classic` is using an incompatible version of `nixfmt`.
2153+
2154+
Found: ${cfg.hooks.nixfmt-classic.package.version}.
2155+
Expected: < v1.0
2156+
2157+
`hooks.nixfmt-classic` supports versions of `nixfmt` up to `v1.0`.
2158+
For `nixfmt` `v1.0` and newer, switch to `hooks.nixfmt`.
21502159
'';
21512160

21522161
# PLEASE keep this sorted alphabetically.
@@ -3411,8 +3420,8 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm
34113420
};
34123421
nixfmt =
34133422
{
3414-
name = "nixfmt-deprecated";
3415-
description = "Deprecated Nix code prettifier. Use nixfmt-classic.";
3423+
name = "nixfmt";
3424+
description = "Official Nix code formatter.";
34163425
package = tools.nixfmt;
34173426
entry = "${hooks.nixfmt.package}/bin/nixfmt ${lib.optionalString (hooks.nixfmt.settings.width != null) "--width=${toString hooks.nixfmt.settings.width}"}";
34183427
files = "\\.nix$";

nix/tools.nix

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,16 @@ in
233233
# Disable tests as these take way to long on our infra.
234234
julia-bin = julia-bin.overrideAttrs (_: _: { doInstallCheck = false; });
235235

236-
# nixfmt was renamed to nixfmt-classic in 24.05.
237-
# nixfmt may be replaced by nixfmt-rfc-style in the future.
238-
nixfmt = if nixfmt-classic == null then nixfmt else nixfmt-classic;
236+
# nixfmt 1.0 is now the official Nix formatter as of 25.11.
237+
#
238+
# In 24.05, the `nixfmt` package was deprecated and replaced with two separate packages:
239+
# - nixfmt-classic
240+
# - nixfmt-rfc-style
241+
#
242+
# Remove this block in 26.05
243+
nixfmt =
244+
if lib.versionOlder nixfmt.version "1.0" && nixfmt-classic != null
245+
then nixfmt-classic
246+
else nixfmt;
239247
inherit nixfmt-classic nixfmt-rfc-style;
240248
}

0 commit comments

Comments
 (0)