From 10da3c02ed2dc24b48282436259931412ead2292 Mon Sep 17 00:00:00 2001 From: shimeoki Date: Mon, 15 Sep 2025 23:48:12 +0300 Subject: [PATCH 1/9] feat(nixfmt-rfc-style): add indent option --- modules/hooks.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 6a364a9e..f91dad5e 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -998,6 +998,12 @@ in description = "Line width."; default = null; }; + indent = + mkOption { + type = types.nullOr types.int; + description = "Line indent."; + default = null; + }; }; }; }; @@ -3413,7 +3419,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm name = "nixfmt-rfc-style"; description = "Nix code prettifier (RFC 166 style)."; package = tools.nixfmt-rfc-style; - 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}"}"; + 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}"} ${lib.optionalString (hooks.nixfmt-rfc-style.settings.indent != null) "--indent=${toString hooks.nixfmt-rfc-style.settings.indent}"}"; files = "\\.nix$"; }; nixpkgs-fmt = From e0a5b7734f66e71393189c426f19331b7dfc4073 Mon Sep 17 00:00:00 2001 From: shimeoki Date: Mon, 15 Sep 2025 23:58:13 +0300 Subject: [PATCH 2/9] refactor(nixfmt): extract option logic --- modules/hooks.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index f91dad5e..309e2288 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3399,11 +3399,15 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm files = "\\.nix$"; }; nixfmt = + let + inherit (hooks.nixfmt) settings; + width = lib.optionalString (settings.width != null) "--width=${toString settings.width}"; + in { name = "nixfmt-deprecated"; description = "Deprecated Nix code prettifier. Use nixfmt-classic."; package = tools.nixfmt; - entry = "${hooks.nixfmt.package}/bin/nixfmt ${lib.optionalString (hooks.nixfmt.settings.width != null) "--width=${toString hooks.nixfmt.settings.width}"}"; + entry = "${hooks.nixfmt.package}/bin/nixfmt ${width}"; files = "\\.nix$"; }; nixfmt-classic = From cb4dacc600444cf2a0aafc571e18d489bfc806c5 Mon Sep 17 00:00:00 2001 From: shimeoki Date: Tue, 16 Sep 2025 00:04:54 +0300 Subject: [PATCH 3/9] refactor(nixfmt): generic arg function --- modules/hooks.nix | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 309e2288..fff97294 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3400,14 +3400,15 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm }; nixfmt = let - inherit (hooks.nixfmt) settings; - width = lib.optionalString (settings.width != null) "--width=${toString settings.width}"; + arg = name: + let opt = hooks.nixfmt.settings.${name}; in + lib.optionalString (opt != null) "--${name}=${toString opt}"; in { name = "nixfmt-deprecated"; description = "Deprecated Nix code prettifier. Use nixfmt-classic."; package = tools.nixfmt; - entry = "${hooks.nixfmt.package}/bin/nixfmt ${width}"; + entry = "${hooks.nixfmt.package}/bin/nixfmt ${arg "width"}"; files = "\\.nix$"; }; nixfmt-classic = From 6db7302edeb3cf10c4a71697329aad026f5d85c3 Mon Sep 17 00:00:00 2001 From: shimeoki Date: Tue, 16 Sep 2025 00:08:03 +0300 Subject: [PATCH 4/9] refactor(nixfmt): exec variable --- modules/hooks.nix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index fff97294..299b6ec3 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3400,15 +3400,17 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm }; nixfmt = let + inherit (hooks) nixfmt; + exec = "${nixfmt.package}/bin/nixfmt"; arg = name: - let opt = hooks.nixfmt.settings.${name}; in + let opt = nixfmt.settings.${name}; in lib.optionalString (opt != null) "--${name}=${toString opt}"; in { name = "nixfmt-deprecated"; description = "Deprecated Nix code prettifier. Use nixfmt-classic."; package = tools.nixfmt; - entry = "${hooks.nixfmt.package}/bin/nixfmt ${arg "width"}"; + entry = "${exec} ${arg "width"}"; files = "\\.nix$"; }; nixfmt-classic = From e95a5e89ab8bfe7d93e59d87e93e4ae13895acac Mon Sep 17 00:00:00 2001 From: shimeoki Date: Tue, 16 Sep 2025 00:14:44 +0300 Subject: [PATCH 5/9] refactor(nixfmt...): use helpers --- modules/hooks.nix | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 299b6ec3..43dfd561 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3414,19 +3414,33 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm files = "\\.nix$"; }; nixfmt-classic = + let + nixfmt = hooks.nixfmt-classic; + exec = "${nixfmt.package}/bin/nixfmt"; + arg = name: + let opt = nixfmt.settings.${name}; in + lib.optionalString (opt != null) "--${name}=${toString opt}"; + in { name = "nixfmt-classic"; description = "Nix code prettifier (classic)."; package = tools.nixfmt-classic; - entry = "${hooks.nixfmt-classic.package}/bin/nixfmt ${lib.optionalString (hooks.nixfmt-classic.settings.width != null) "--width=${toString hooks.nixfmt-classic.settings.width}"}"; + entry = "${exec} ${arg "width"}"; files = "\\.nix$"; }; nixfmt-rfc-style = + let + nixfmt = hooks.nixfmt-rfc-style; + exec = "${nixfmt.package}/bin/nixfmt"; + arg = name: + let opt = nixfmt.settings.${name}; in + lib.optionalString (opt != null) "--${name}=${toString opt}"; + in { name = "nixfmt-rfc-style"; description = "Nix code prettifier (RFC 166 style)."; package = tools.nixfmt-rfc-style; - 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}"} ${lib.optionalString (hooks.nixfmt-rfc-style.settings.indent != null) "--indent=${toString hooks.nixfmt-rfc-style.settings.indent}"}"; + entry = "${exec} ${arg "width"} ${arg "indent"}"; files = "\\.nix$"; }; nixpkgs-fmt = From fe4486583b42a9818d2b7eb9eaf5e1d4eec68823 Mon Sep 17 00:00:00 2001 From: shimeoki Date: Wed, 29 Oct 2025 20:48:03 +0300 Subject: [PATCH 6/9] feat(nixfmt): remove 'visible = false' because otherwise there is no way to enable it, as i understand, and adding options to it is pointless --- modules/hooks.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 43dfd561..74a2eea6 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -960,7 +960,6 @@ in }; nixfmt = mkOption { description = "Deprecated nixfmt hook. Use nixfmt-classic or nixfmt-rfc-style instead."; - visible = false; type = types.submodule { imports = [ hookModule ]; options.settings = { From 2b7cf87b21449d7bc0569b209ff2e546cba33bca Mon Sep 17 00:00:00 2001 From: shimeoki Date: Wed, 29 Oct 2025 20:49:52 +0300 Subject: [PATCH 7/9] refactor(nixfmt-rfc-style): options - use original description from the cli - use 'with types' for readability --- modules/hooks.nix | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 74a2eea6..6e699a47 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -993,14 +993,14 @@ in options.settings = { width = mkOption { - type = types.nullOr types.int; - description = "Line width."; + type = with types; nullOr int; + description = "Maximum width in characters."; default = null; }; indent = mkOption { - type = types.nullOr types.int; - description = "Line indent."; + type = with types; nullOr int; + description = "Number of spaces to use for indentation."; default = null; }; }; From 1d3be3c2662a45bb33619693df4e725f367eb573 Mon Sep 17 00:00:00 2001 From: shimeoki Date: Wed, 29 Oct 2025 20:51:04 +0300 Subject: [PATCH 8/9] feat(nixfmt): options add new options for the nixfmt hook as well --- modules/hooks.nix | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 6e699a47..63ddff7e 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -965,8 +965,14 @@ in options.settings = { width = mkOption { - type = types.nullOr types.int; - description = "Line width."; + type = with types; nullOr int; + description = "Maximum width in characters."; + default = null; + }; + indent = + mkOption { + type = with types; nullOr int; + description = "Number of spaces to use for indentation."; default = null; }; }; From 018f1fc29aa8e28e139e7ec41d6b28dc2b3b3c16 Mon Sep 17 00:00:00 2001 From: shimeoki Date: Wed, 29 Oct 2025 21:16:03 +0300 Subject: [PATCH 9/9] feat(nixfmt...): use new options also check version for indent in nixfmt and nixfmt-rfc-style hooks --- modules/hooks.nix | 58 ++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 63ddff7e..d7efe61d 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3404,48 +3404,54 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm files = "\\.nix$"; }; nixfmt = - let - inherit (hooks) nixfmt; - exec = "${nixfmt.package}/bin/nixfmt"; - arg = name: - let opt = nixfmt.settings.${name}; in - lib.optionalString (opt != null) "--${name}=${toString opt}"; - in { - name = "nixfmt-deprecated"; - description = "Deprecated Nix code prettifier. Use nixfmt-classic."; + name = "nixfmt"; + description = "Official Nix code formatter."; package = tools.nixfmt; - entry = "${exec} ${arg "width"}"; + entry = + let + nixfmt = hooks.nixfmt.package; + hasIndent = lib.versionAtLeast nixfmt.version "1.0.0"; + cmdArgs = mkCmdArgs (with hooks.nixfmt.settings; [ + [ (width != null) "--width=${builtins.toString width}" ] + [ (indent != null) "--indent=${builtins.toString indent}" ] + ]); + in + lib.throwIf (hooks.nixfmt.settings.indent != null && !hasIndent) "`indent` option in `nixfmt` hook can only be used with version >= 1.0.0" + "${nixfmt}/bin/nixfmt ${cmdArgs}"; files = "\\.nix$"; }; nixfmt-classic = - let - nixfmt = hooks.nixfmt-classic; - exec = "${nixfmt.package}/bin/nixfmt"; - arg = name: - let opt = nixfmt.settings.${name}; in - lib.optionalString (opt != null) "--${name}=${toString opt}"; - in { name = "nixfmt-classic"; description = "Nix code prettifier (classic)."; package = tools.nixfmt-classic; - entry = "${exec} ${arg "width"}"; + entry = + let + nixfmt-classic = hooks.nixfmt-classic.package; + cmdArgs = mkCmdArgs (with hooks.nixfmt-classic.settings; [ + [ (width != null) "--width=${builtins.toString width}" ] + ]); + in + "${nixfmt-classic}/bin/nixfmt ${cmdArgs}"; files = "\\.nix$"; }; nixfmt-rfc-style = - let - nixfmt = hooks.nixfmt-rfc-style; - exec = "${nixfmt.package}/bin/nixfmt"; - arg = name: - let opt = nixfmt.settings.${name}; in - lib.optionalString (opt != null) "--${name}=${toString opt}"; - in { name = "nixfmt-rfc-style"; description = "Nix code prettifier (RFC 166 style)."; package = tools.nixfmt-rfc-style; - entry = "${exec} ${arg "width"} ${arg "indent"}"; + entry = + let + nixfmt-rfc-style = hooks.nixfmt-rfc-style.package; + hasIndent = lib.versionAtLeast nixfmt-rfc-style.version "1.0.0"; + cmdArgs = mkCmdArgs (with hooks.nixfmt-rfc-style.settings; [ + [ (width != null) "--width=${builtins.toString width}" ] + [ (indent != null) "--indent=${builtins.toString indent}" ] + ]); + in + 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" + "${nixfmt-rfc-style}/bin/nixfmt ${cmdArgs}"; files = "\\.nix$"; }; nixpkgs-fmt =