From 48a99a99fc2336dc87d8aa091418a38c1d64c818 Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Thu, 21 May 2026 16:01:19 -0400 Subject: [PATCH 1/6] lib.generators.toGitINI: only define helper variables once --- lib/generators.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index 7b53d3912822..0b8393e8ebe2 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -379,7 +379,6 @@ rec { See the [git-config documentation](https://git-scm.com/docs/git-config#_variables) for possible values. */ toGitINI = - attrs: let mkSectionName = name: @@ -427,7 +426,7 @@ rec { toINI_ = toINI { inherit mkKeyValue mkSectionName; }; in - toINI_ (gitFlattenAttrs attrs); + attrs: toINI_ (gitFlattenAttrs attrs); /** `mkKeyValueDefault` wrapper that handles dconf INI quirks. From af62ce48876c09dfb7b32ca0f794ef3bb53d3ddf Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Thu, 21 May 2026 16:02:22 -0400 Subject: [PATCH 2/6] lib.generators.toGitINI: avoid creating several variables on every call --- lib/generators.nix | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index 0b8393e8ebe2..9361404a5a0e 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -381,15 +381,17 @@ rec { toGitINI = let mkSectionName = + let + containsQuote = hasInfix ''"''; + in name: let - containsQuote = hasInfix ''"'' name; sections = splitString "." name; - section = head sections; - subsections = tail sections; - subsection = concatStringsSep "." subsections; in - if containsQuote || subsections == [ ] then name else ''${section} "${subsection}"''; + if containsQuote name || length sections == 1 then + name + else + ''${head sections} "${concatStringsSep "." (tail sections)}"''; mkValueString = v: From 9b4dde7ebaa50349c2c3bd8d0f5dc0cc2a31d0ec Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Thu, 21 May 2026 16:04:12 -0400 Subject: [PATCH 3/6] lib.generators.toGitINI: move escape logic outside of hot loop --- lib/generators.nix | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index 9361404a5a0e..b742797bb627 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -394,11 +394,10 @@ rec { ''${head sections} "${concatStringsSep "." (tail sections)}"''; mkValueString = - v: let - escapedV = ''"${replaceStrings [ "\n" " " ''"'' "\\" ] [ "\\n" "\\t" ''\"'' "\\\\" ] v}"''; + escape = replaceStrings [ "\n" " " ''"'' "\\" ] [ "\\n" "\\t" ''\"'' "\\\\" ]; in - mkValueStringDefault { } (if isString v then escapedV else v); + v: mkValueStringDefault { } (if isString v then ''"${escape v}"'' else v); # generation for multiple ini values mkKeyValue = From 69bb7293960eefd236c2e258fb8ec69d56a28b58 Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Thu, 21 May 2026 16:05:08 -0400 Subject: [PATCH 4/6] lib.generators.toGitINI: avoid concatenating and mapping on non-lists --- lib/generators.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index b742797bb627..4106e9af6581 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -401,11 +401,11 @@ rec { # generation for multiple ini values mkKeyValue = - k: v: let - mkKeyValue = mkKeyValueDefault { inherit mkValueString; } " = " k; + mkKeyValue = mkKeyValueDefault { inherit mkValueString; } " = "; + attrToString = k: v: "\t" + mkKeyValue k v; in - concatStringsSep "\n" (map (kv: "\t" + mkKeyValue kv) (toList v)); + k: v: if isList v then concatStringsSep "\n" (map (attrToString k) v) else attrToString k v; # converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI gitFlattenAttrs = From ad02f2310f1276127956957e0898db62750a1f7c Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Thu, 21 May 2026 16:12:06 -0400 Subject: [PATCH 5/6] lib.generators.toGitINI: use concatMap to avoid a flatten at the end --- lib/generators.nix | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index 4106e9af6581..c9361f2092dc 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -26,6 +26,7 @@ let assertMsg attrNames concatLists + concatMap concatMapStringsSep concatStrings concatStringsSep @@ -413,17 +414,21 @@ rec { recurse = path: value: if isAttrs value && !isDerivation value then - mapAttrsToList (name: value: recurse ([ name ] ++ path) value) value + concatMap (name: recurse ([ name ] ++ path) value.${name}) (attrNames value) else if length path > 1 then - { - ${concatStringsSep "." (reverseList (tail path))}.${head path} = value; - } + [ + { + ${concatStringsSep "." (reverseList (tail path))}.${head path} = value; + } + ] else - { - ${head path} = value; - }; + [ + { + ${head path} = value; + } + ]; in - attrs: foldl recursiveUpdate { } (flatten (recurse [ ] attrs)); + attrs: foldl recursiveUpdate { } (recurse [ ] attrs); toINI_ = toINI { inherit mkKeyValue mkSectionName; }; in From e9c2828c5a48c0f9ea604b59ffb9e54c0793f374 Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Thu, 21 May 2026 18:16:47 -0400 Subject: [PATCH 6/6] lib.generators.toGitINI: only recurse into necessary attributes --- lib/generators.nix | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index c9361f2092dc..0e15a56db312 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -23,8 +23,10 @@ let inherit (lib) addErrorContext + any assertMsg attrNames + attrValues concatLists concatMap concatMapStringsSep @@ -53,6 +55,7 @@ let isString last length + genAttrs mapAttrs mapAttrsToList optionals @@ -411,9 +414,10 @@ rec { # converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI gitFlattenAttrs = let + isNonDrvAttrs = value: isAttrs value && !isDerivation value; recurse = path: value: - if isAttrs value && !isDerivation value then + if isNonDrvAttrs value then concatMap (name: recurse ([ name ] ++ path) value.${name}) (attrNames value) else if length path > 1 then [ @@ -428,7 +432,16 @@ rec { } ]; in - attrs: foldl recursiveUpdate { } (recurse [ ] attrs); + attrs: + let + # Filter the names for any that contain nested attrsets. attrs that + # don't contain nested attrsets can stay the same = + namesToRewrite = filter ( + name: isAttrs attrs.${name} && any isNonDrvAttrs (attrValues attrs.${name}) + ) (attrNames attrs); + attrsToRewrite = genAttrs namesToRewrite (name: attrs.${name}); + in + removeAttrs attrs namesToRewrite // foldl recursiveUpdate { } (recurse [ ] attrsToRewrite); toINI_ = toINI { inherit mkKeyValue mkSectionName; }; in