lib: introduce imap0, imap1 (#25543)

* lib: introduce imap0, imap1

For historical reasons, imap starts counting at 1 and it's not
consistent with the rest of the lib.

So for now we split imap into imap0 that starts counting at zero and
imap1 that starts counting at 1. And imap is marked as deprecated.

See https://github.com/NixOS/nixpkgs/commit/c71e2d42359f9900ea2c290d141c0d606471da16#commitcomment-21873221

* replace uses of lib.imap

* lib: move imap to deprecated.nix

zimbatm 4d545297 a0fa6178

Changed files
+31 -17
lib
nixos
modules
misc
services
pkgs
games
+8
lib/deprecated.nix
···
else if isInt x then "int"
else "string";
}
···
else if isInt x then "int"
else "string";
+
/* deprecated:
+
+
For historical reasons, imap has an index starting at 1.
+
+
But for consistency with the rest of the library we want an index
+
starting at zero.
+
*/
+
imap = imap1;
}
+10 -4
lib/lists.nix
···
*/
foldl' = builtins.foldl' or foldl;
-
/* Map with index
-
FIXME(zimbatm): why does this start to count at 1?
Example:
-
imap (i: v: "${v}-${toString i}") ["a" "b"]
=> [ "a-1" "b-2" ]
*/
-
imap = f: list: genList (n: f (n + 1) (elemAt list n)) (length list);
/* Map and concatenate the result.
···
*/
foldl' = builtins.foldl' or foldl;
+
/* Map with index starting from 0
+
Example:
+
imap0 (i: v: "${v}-${toString i}") ["a" "b"]
+
=> [ "a-0" "b-1" ]
+
*/
+
imap0 = f: list: genList (n: f n (elemAt list n)) (length list);
+
+
/* Map with index starting from 1
Example:
+
imap1 (i: v: "${v}-${toString i}") ["a" "b"]
=> [ "a-1" "b-2" ]
*/
+
imap1 = f: list: genList (n: f (n + 1) (elemAt list n)) (length list);
/* Map and concatenate the result.
+1 -1
lib/modules.nix
···
/* Close a set of modules under the ‘imports’ relation. */
closeModules = modules: args:
let
-
toClosureList = file: parentKey: imap (n: x:
if isAttrs x || isFunction x then
let key = "${parentKey}:anon-${toString n}"; in
unifyModuleSyntax file key (unpackSubmodule (applyIfFunction key) x args)
···
/* Close a set of modules under the ‘imports’ relation. */
closeModules = modules: args:
let
+
toClosureList = file: parentKey: imap1 (n: x:
if isAttrs x || isFunction x then
let key = "${parentKey}:anon-${toString n}"; in
unifyModuleSyntax file key (unpackSubmodule (applyIfFunction key) x args)
+2 -2
lib/strings.nix
···
concatImapStrings (pos: x: "${toString pos}-${x}") ["foo" "bar"]
=> "1-foo2-bar"
*/
-
concatImapStrings = f: list: concatStrings (lib.imap f list);
/* Place an element between each element of a list
···
concatImapStringsSep "-" (pos: x: toString (x / pos)) [ 6 6 6 ]
=> "6-3-2"
*/
-
concatImapStringsSep = sep: f: list: concatStringsSep sep (lib.imap f list);
/* Construct a Unix-style search path consisting of each `subDir"
directory of the given list of packages.
···
concatImapStrings (pos: x: "${toString pos}-${x}") ["foo" "bar"]
=> "1-foo2-bar"
*/
+
concatImapStrings = f: list: concatStrings (lib.imap1 f list);
/* Place an element between each element of a list
···
concatImapStringsSep "-" (pos: x: toString (x / pos)) [ 6 6 6 ]
=> "6-3-2"
*/
+
concatImapStringsSep = sep: f: list: concatStringsSep sep (lib.imap1 f list);
/* Construct a Unix-style search path consisting of each `subDir"
directory of the given list of packages.
+4 -4
lib/types.nix
···
description = "list of ${elemType.description}s";
check = isList;
merge = loc: defs:
-
map (x: x.value) (filter (x: x ? value) (concatLists (imap (n: def:
if isList def.value then
-
imap (m: def':
(mergeDefinitions
(loc ++ ["[definition ${toString n}-entry ${toString m}]"])
elemType
···
if isList def.value then
{ inherit (def) file;
value = listToAttrs (
-
imap (elemIdx: elem:
{ name = elem.name or "unnamed-${toString defIdx}.${toString elemIdx}";
value = elem;
}) def.value);
···
name = "loaOf";
description = "list or attribute set of ${elemType.description}s";
check = x: isList x || isAttrs x;
-
merge = loc: defs: attrOnly.merge loc (imap convertIfList defs);
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name?>"]);
getSubModules = elemType.getSubModules;
substSubModules = m: loaOf (elemType.substSubModules m);
···
description = "list of ${elemType.description}s";
check = isList;
merge = loc: defs:
+
map (x: x.value) (filter (x: x ? value) (concatLists (imap1 (n: def:
if isList def.value then
+
imap1 (m: def':
(mergeDefinitions
(loc ++ ["[definition ${toString n}-entry ${toString m}]"])
elemType
···
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);
···
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);
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name?>"]);
getSubModules = elemType.getSubModules;
substSubModules = m: loaOf (elemType.substSubModules m);
+1 -1
nixos/modules/misc/meta.nix
···
# }
merge = loc: defs:
zipAttrs
-
(flatten (imap (n: def: imap (m: def':
maintainer.merge (loc ++ ["[${toString n}-${toString m}]"])
[{ inherit (def) file; value = def'; }]) def.value) defs));
};
···
# }
merge = loc: defs:
zipAttrs
+
(flatten (imap1 (n: def: imap1 (m: def':
maintainer.merge (loc ++ ["[${toString n}-${toString m}]"])
[{ inherit (def) file; value = def'; }]) def.value) defs));
};
+1 -1
nixos/modules/services/cluster/kubernetes.nix
···
cniConfig = pkgs.buildEnv {
name = "kubernetes-cni-config";
-
paths = imap (i: entry:
pkgs.writeTextDir "${toString (10+i)}-${entry.type}.conf" (builtins.toJSON entry)
) cfg.kubelet.cni.config;
};
···
cniConfig = pkgs.buildEnv {
name = "kubernetes-cni-config";
+
paths = imap1 (i: entry:
pkgs.writeTextDir "${toString (10+i)}-${entry.type}.conf" (builtins.toJSON entry)
) cfg.kubelet.cni.config;
};
+1 -1
nixos/modules/services/networking/libreswan.nix
···
trim = chars: str: let
nonchars = filter (x : !(elem x.value chars))
-
(imap (i: v: {ind = (sub i 1); value = v;}) (stringToCharacters str));
in
if length nonchars == 0 then ""
else substring (head nonchars).ind (add 1 (sub (last nonchars).ind (head nonchars).ind)) str;
···
trim = chars: str: let
nonchars = filter (x : !(elem x.value chars))
+
(imap0 (i: v: {ind = i; value = v;}) (stringToCharacters str));
in
if length nonchars == 0 then ""
else substring (head nonchars).ind (add 1 (sub (last nonchars).ind (head nonchars).ind)) str;
+1 -1
nixos/modules/services/networking/networkmanager.nix
···
{ source = overrideNameserversScript;
target = "NetworkManager/dispatcher.d/02overridedns";
}
-
++ lib.imap (i: s: {
inherit (s) source;
target = "NetworkManager/dispatcher.d/${dispatcherTypesSubdirMap.${s.type}}03userscript${lib.fixedWidthNumber 4 i}";
}) cfg.dispatcherScripts;
···
{ source = overrideNameserversScript;
target = "NetworkManager/dispatcher.d/02overridedns";
}
+
++ lib.imap1 (i: s: {
inherit (s) source;
target = "NetworkManager/dispatcher.d/${dispatcherTypesSubdirMap.${s.type}}03userscript${lib.fixedWidthNumber 4 i}";
}) cfg.dispatcherScripts;
+1 -1
nixos/modules/services/x11/xserver.nix
···
name = "multihead${toString num}";
inherit config;
};
-
in imap mkHead cfg.xrandrHeads;
xrandrDeviceSection = let
monitors = flip map xrandrHeads (h: ''
···
name = "multihead${toString num}";
inherit config;
};
+
in imap1 mkHead cfg.xrandrHeads;
xrandrDeviceSection = let
monitors = flip map xrandrHeads (h: ''
+1 -1
pkgs/games/uqm/default.nix
···
inherit stdenv requireFile writeText fetchurl haskellPackages;
};
-
remixPacks = imap (num: sha256: fetchurl rec {
name = "uqm-remix-disc${toString num}.uqm";
url = "mirror://sourceforge/sc2/${name}";
inherit sha256;
···
inherit stdenv requireFile writeText fetchurl haskellPackages;
};
+
remixPacks = imap1 (num: sha256: fetchurl rec {
name = "uqm-remix-disc${toString num}.uqm";
url = "mirror://sourceforge/sc2/${name}";
inherit sha256;