summary refs log tree commit diff
path: root/lib/attrsets.nix
diff options
context:
space:
mode:
authorfigsoda <figsoda@pm.me>2022-11-16 12:20:38 -0500
committerfigsoda <figsoda@pm.me>2022-11-17 10:41:53 -0500
commitf993f8a18659bb15bc5697b6875caf0cba8b1825 (patch)
treed671b3289bac8aaad0c867096808d99b338dd208 /lib/attrsets.nix
parent4536ebad69010f41df3c67c18bff3d85513faf86 (diff)
downloadnixpkgs-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar
nixpkgs-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.gz
nixpkgs-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.bz2
nixpkgs-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.lz
nixpkgs-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.xz
nixpkgs-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.zst
nixpkgs-f993f8a18659bb15bc5697b6875caf0cba8b1825.zip
lib/attrsets: add concatMapAttrs
Diffstat (limited to 'lib/attrsets.nix')
-rw-r--r--lib/attrsets.nix21
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix
index 0a4c3c8ebcf..8b5c0ef4cea 100644
--- a/lib/attrsets.nix
+++ b/lib/attrsets.nix
@@ -3,7 +3,7 @@
 
 let
   inherit (builtins) head tail length;
-  inherit (lib.trivial) id;
+  inherit (lib.trivial) flip id mergeAttrs pipe;
   inherit (lib.strings) concatStringsSep concatMapStringsSep escapeNixIdentifier sanitizeDerivationName;
   inherit (lib.lists) foldr foldl' concatMap concatLists elemAt all partition groupBy take foldl;
 in
@@ -77,6 +77,25 @@ rec {
     let errorMsg = "cannot find attribute `" + concatStringsSep "." attrPath + "'";
     in attrByPath attrPath (abort errorMsg);
 
+  /* Map each attribute in the given set and merge them into a new attribute set.
+
+     Type:
+       concatMapAttrs ::
+         (String -> a -> AttrSet)
+         -> AttrSet
+         -> AttrSet
+
+     Example:
+       concatMapAttrs
+         (name: value: {
+           ${name} = value;
+           ${name + value} = value;
+         })
+         { x = "a"; y = "b"; }
+       => { x = "a"; xa = "a"; y = "b"; yb = "b"; }
+  */
+  concatMapAttrs = f: flip pipe [ (mapAttrs f) attrValues (foldl' mergeAttrs { }) ];
+
 
   /* Update or set specific paths of an attribute set.