Skip to content

Commit 018f1fc

Browse files
committed
feat(nixfmt...): use new options
also check version for indent in nixfmt and nixfmt-rfc-style hooks
1 parent 1d3be3c commit 018f1fc

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

modules/hooks.nix

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,48 +3404,54 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm
34043404
files = "\\.nix$";
34053405
};
34063406
nixfmt =
3407-
let
3408-
inherit (hooks) nixfmt;
3409-
exec = "${nixfmt.package}/bin/nixfmt";
3410-
arg = name:
3411-
let opt = nixfmt.settings.${name}; in
3412-
lib.optionalString (opt != null) "--${name}=${toString opt}";
3413-
in
34143407
{
3415-
name = "nixfmt-deprecated";
3416-
description = "Deprecated Nix code prettifier. Use nixfmt-classic.";
3408+
name = "nixfmt";
3409+
description = "Official Nix code formatter.";
34173410
package = tools.nixfmt;
3418-
entry = "${exec} ${arg "width"}";
3411+
entry =
3412+
let
3413+
nixfmt = hooks.nixfmt.package;
3414+
hasIndent = lib.versionAtLeast nixfmt.version "1.0.0";
3415+
cmdArgs = mkCmdArgs (with hooks.nixfmt.settings; [
3416+
[ (width != null) "--width=${builtins.toString width}" ]
3417+
[ (indent != null) "--indent=${builtins.toString indent}" ]
3418+
]);
3419+
in
3420+
lib.throwIf (hooks.nixfmt.settings.indent != null && !hasIndent) "`indent` option in `nixfmt` hook can only be used with version >= 1.0.0"
3421+
"${nixfmt}/bin/nixfmt ${cmdArgs}";
34193422
files = "\\.nix$";
34203423
};
34213424
nixfmt-classic =
3422-
let
3423-
nixfmt = hooks.nixfmt-classic;
3424-
exec = "${nixfmt.package}/bin/nixfmt";
3425-
arg = name:
3426-
let opt = nixfmt.settings.${name}; in
3427-
lib.optionalString (opt != null) "--${name}=${toString opt}";
3428-
in
34293425
{
34303426
name = "nixfmt-classic";
34313427
description = "Nix code prettifier (classic).";
34323428
package = tools.nixfmt-classic;
3433-
entry = "${exec} ${arg "width"}";
3429+
entry =
3430+
let
3431+
nixfmt-classic = hooks.nixfmt-classic.package;
3432+
cmdArgs = mkCmdArgs (with hooks.nixfmt-classic.settings; [
3433+
[ (width != null) "--width=${builtins.toString width}" ]
3434+
]);
3435+
in
3436+
"${nixfmt-classic}/bin/nixfmt ${cmdArgs}";
34343437
files = "\\.nix$";
34353438
};
34363439
nixfmt-rfc-style =
3437-
let
3438-
nixfmt = hooks.nixfmt-rfc-style;
3439-
exec = "${nixfmt.package}/bin/nixfmt";
3440-
arg = name:
3441-
let opt = nixfmt.settings.${name}; in
3442-
lib.optionalString (opt != null) "--${name}=${toString opt}";
3443-
in
34443440
{
34453441
name = "nixfmt-rfc-style";
34463442
description = "Nix code prettifier (RFC 166 style).";
34473443
package = tools.nixfmt-rfc-style;
3448-
entry = "${exec} ${arg "width"} ${arg "indent"}";
3444+
entry =
3445+
let
3446+
nixfmt-rfc-style = hooks.nixfmt-rfc-style.package;
3447+
hasIndent = lib.versionAtLeast nixfmt-rfc-style.version "1.0.0";
3448+
cmdArgs = mkCmdArgs (with hooks.nixfmt-rfc-style.settings; [
3449+
[ (width != null) "--width=${builtins.toString width}" ]
3450+
[ (indent != null) "--indent=${builtins.toString indent}" ]
3451+
]);
3452+
in
3453+
lib.throwIf (hooks.nixfmt-rfc-style.settings.indent != null && !hasIndent) "`indent` option in `nixfmt-rfc-style` hook can only be used with version >= 1.0.0"
3454+
"${nixfmt-rfc-style}/bin/nixfmt ${cmdArgs}";
34493455
files = "\\.nix$";
34503456
};
34513457
nixpkgs-fmt =

0 commit comments

Comments
 (0)