Skip to content

Commit 0c1e27e

Browse files
authored
Merge pull request #627 from shimeoki/feat/nixfmt-indent/main
feat: nixfmt indent
2 parents 8e7576e + 0a9f42d commit 0c1e27e

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

modules/hooks.nix

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -974,14 +974,19 @@ in
974974
};
975975
nixfmt = mkOption {
976976
description = "nixfmt hook";
977-
visible = false;
978977
type = types.submodule {
979978
imports = [ hookModule ];
980979
options.settings = {
981980
width =
982981
mkOption {
983-
type = types.nullOr types.int;
984-
description = "Line width.";
982+
type = with types; nullOr int;
983+
description = "Maximum width in characters.";
984+
default = null;
985+
};
986+
indent =
987+
mkOption {
988+
type = with types; nullOr int;
989+
description = "Number of spaces to use for indentation.";
985990
default = null;
986991
};
987992
};
@@ -1008,8 +1013,14 @@ in
10081013
options.settings = {
10091014
width =
10101015
mkOption {
1011-
type = types.nullOr types.int;
1012-
description = "Line width.";
1016+
type = with types; nullOr int;
1017+
description = "Maximum width in characters.";
1018+
default = null;
1019+
};
1020+
indent =
1021+
mkOption {
1022+
type = with types; nullOr int;
1023+
description = "Number of spaces to use for indentation.";
10131024
default = null;
10141025
};
10151026
};
@@ -3461,23 +3472,50 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm
34613472
name = "nixfmt";
34623473
description = "Official Nix code formatter.";
34633474
package = tools.nixfmt;
3464-
entry = "${hooks.nixfmt.package}/bin/nixfmt ${lib.optionalString (hooks.nixfmt.settings.width != null) "--width=${toString hooks.nixfmt.settings.width}"}";
3475+
entry =
3476+
let
3477+
nixfmt = hooks.nixfmt.package;
3478+
hasIndent = lib.versionAtLeast nixfmt.version "1.0.0";
3479+
cmdArgs = mkCmdArgs (with hooks.nixfmt.settings; [
3480+
[ (width != null) "--width=${builtins.toString width}" ]
3481+
[ (indent != null) "--indent=${builtins.toString indent}" ]
3482+
]);
3483+
in
3484+
lib.throwIf (hooks.nixfmt.settings.indent != null && !hasIndent) "`indent` option in `nixfmt` hook can only be used with version >= 1.0.0"
3485+
"${nixfmt}/bin/nixfmt ${cmdArgs}";
34653486
files = "\\.nix$";
34663487
};
34673488
nixfmt-classic =
34683489
{
34693490
name = "nixfmt-classic";
34703491
description = "Nix code prettifier (classic).";
34713492
package = tools.nixfmt-classic;
3472-
entry = "${hooks.nixfmt-classic.package}/bin/nixfmt ${lib.optionalString (hooks.nixfmt-classic.settings.width != null) "--width=${toString hooks.nixfmt-classic.settings.width}"}";
3493+
entry =
3494+
let
3495+
nixfmt-classic = hooks.nixfmt-classic.package;
3496+
cmdArgs = mkCmdArgs (with hooks.nixfmt-classic.settings; [
3497+
[ (width != null) "--width=${builtins.toString width}" ]
3498+
]);
3499+
in
3500+
"${nixfmt-classic}/bin/nixfmt ${cmdArgs}";
34733501
files = "\\.nix$";
34743502
};
34753503
nixfmt-rfc-style =
34763504
{
34773505
name = "nixfmt-rfc-style";
34783506
description = "Nix code prettifier (RFC 166 style).";
34793507
package = tools.nixfmt-rfc-style;
3480-
entry = "${hooks.nixfmt-rfc-style.package}/bin/nixfmt ${lib.optionalString (hooks.nixfmt-rfc-style.settings.width != null) "--width=${toString hooks.nixfmt-rfc-style.settings.width}"}";
3508+
entry =
3509+
let
3510+
nixfmt-rfc-style = hooks.nixfmt-rfc-style.package;
3511+
hasIndent = lib.versionAtLeast nixfmt-rfc-style.version "1.0.0";
3512+
cmdArgs = mkCmdArgs (with hooks.nixfmt-rfc-style.settings; [
3513+
[ (width != null) "--width=${builtins.toString width}" ]
3514+
[ (indent != null) "--indent=${builtins.toString indent}" ]
3515+
]);
3516+
in
3517+
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"
3518+
"${nixfmt-rfc-style}/bin/nixfmt ${cmdArgs}";
34813519
files = "\\.nix$";
34823520
};
34833521
nixpkgs-fmt =

0 commit comments

Comments
 (0)