summary refs log tree commit diff
path: root/lib/types.nix
diff options
context:
space:
mode:
authorRobert Helgesson <robert@rycee.net>2018-03-24 17:38:45 +0100
committerRobert Helgesson <robert@rycee.net>2018-05-07 20:23:52 +0200
commit08e8701673a84b800e0960c50f78793a23e635ba (patch)
tree7e3f3efeda1396775b1bba321ea420d3b38f1f00 /lib/types.nix
parent0b04ed61771d9f4848347d468b73ed581d43f847 (diff)
downloadnixpkgs-08e8701673a84b800e0960c50f78793a23e635ba.tar
nixpkgs-08e8701673a84b800e0960c50f78793a23e635ba.tar.gz
nixpkgs-08e8701673a84b800e0960c50f78793a23e635ba.tar.bz2
nixpkgs-08e8701673a84b800e0960c50f78793a23e635ba.tar.lz
nixpkgs-08e8701673a84b800e0960c50f78793a23e635ba.tar.xz
nixpkgs-08e8701673a84b800e0960c50f78793a23e635ba.tar.zst
nixpkgs-08e8701673a84b800e0960c50f78793a23e635ba.zip
lib.types: fix loaOf behavior for long lists
Assigning a list of 10 or more elements to an option having the type
`loaOf a` produces a configuration value that is not honoring the
order of the original list. This commit fixes this and a related issue
arising when 10 or more lists are merged into this type of option.
Diffstat (limited to 'lib/types.nix')
-rw-r--r--lib/types.nix29
1 files changed, 20 insertions, 9 deletions
diff --git a/lib/types.nix b/lib/types.nix
index 17acb92a944..77271689772 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -280,15 +280,26 @@ rec {
     # List or attribute set of ...
     loaOf = elemType:
       let
-        convertIfList = defIdx: def:
+        convertAllLists = defs:
+          let
+            padWidth = stringLength (toString (length defs));
+            unnamedPrefix = i: "unnamed-" + fixedWidthNumber padWidth i + ".";
+          in
+            imap1 (i: convertIfList (unnamedPrefix i)) defs;
+
+        convertIfList = unnamedPrefix: def:
           if isList def.value then
-            { inherit (def) file;
-              value = listToAttrs (
-                imap1 (elemIdx: elem:
-                  { name = elem.name or "unnamed-${toString defIdx}.${toString elemIdx}";
-                    value = elem;
-                  }) def.value);
-            }
+            let
+              padWidth = stringLength (toString (length def.value));
+              unnamed = i: unnamedPrefix + fixedWidthNumber padWidth i;
+            in
+              { inherit (def) file;
+                value = listToAttrs (
+                  imap1 (elemIdx: elem:
+                    { name = elem.name or (unnamed elemIdx);
+                      value = elem;
+                    }) def.value);
+              }
           else
             def;
         listOnly = listOf elemType;
@@ -297,7 +308,7 @@ rec {
         name = "loaOf";
         description = "list or attribute set of ${elemType.description}s";
         check = x: isList x || isAttrs x;
-        merge = loc: defs: attrOnly.merge loc (imap1 convertIfList defs);
+        merge = loc: defs: attrOnly.merge loc (convertAllLists defs);
         getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name?>"]);
         getSubModules = elemType.getSubModules;
         substSubModules = m: loaOf (elemType.substSubModules m);