summaryrefslogtreecommitdiff
path: root/lib/attrsets.nix
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2023-10-10 19:49:17 +0200
committerGitHub <noreply@github.com>2023-10-10 19:49:17 +0200
commit5323fbf70331f8a7c47f1b4f49841cf74507f77f (patch)
tree633d4a4f8d2f36148370d73e413e05dc2ccbe23b /lib/attrsets.nix
parentMerge pull request #260249 from prusnak/gcc-arm-embedded-12 (diff)
parentlib.attrsets.attrsToList: add function (diff)
downloadnixpkgs-5323fbf70331f8a7c47f1b4f49841cf74507f77f.tar.gz
Merge pull request #254452 from flyingcircusio/lib-attrsToList
lib.attrsets.attrsToList: add function
Diffstat (limited to 'lib/attrsets.nix')
-rw-r--r--lib/attrsets.nix30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix
index b8960cf73f20..b0460ab139e8 100644
--- a/lib/attrsets.nix
+++ b/lib/attrsets.nix
@@ -542,6 +542,36 @@ rec {
attrs:
map (name: f name attrs.${name}) (attrNames attrs);
+ /*
+ Deconstruct an attrset to a list of name-value pairs as expected by [`builtins.listToAttrs`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-listToAttrs).
+ Each element of the resulting list is an attribute set with these attributes:
+ - `name` (string): The name of the attribute
+ - `value` (any): The value of the attribute
+
+ The following is always true:
+ ```nix
+ builtins.listToAttrs (attrsToList attrs) == attrs
+ ```
+
+ :::{.warning}
+ The opposite is not always true. In general expect that
+ ```nix
+ attrsToList (builtins.listToAttrs list) != list
+ ```
+
+ This is because the `listToAttrs` removes duplicate names and doesn't preserve the order of the list.
+ :::
+
+ Example:
+ attrsToList { foo = 1; bar = "asdf"; }
+ => [ { name = "bar"; value = "asdf"; } { name = "foo"; value = 1; } ]
+
+ Type:
+ attrsToList :: AttrSet -> [ { name :: String; value :: Any; } ]
+
+ */
+ attrsToList = mapAttrsToList nameValuePair;
+
/* Like `mapAttrs`, except that it recursively applies itself to
the *leaf* attributes of a potentially-nested attribute set: