summary refs log tree commit diff
path: root/lib/attrsets.nix
diff options
context:
space:
mode:
authorNikolay Amiantov <ab@fmap.me>2016-04-13 15:33:11 +0300
committerNikolay Amiantov <ab@fmap.me>2016-04-13 22:05:45 +0300
commit338340f993563551d8cb45941da987408abef65f (patch)
treefb0c34c5b2944a8bd4b039e68c6aacab996aa451 /lib/attrsets.nix
parent5ef56700b9e142978673db5889579a0838956d49 (diff)
downloadnixpkgs-338340f993563551d8cb45941da987408abef65f.tar
nixpkgs-338340f993563551d8cb45941da987408abef65f.tar.gz
nixpkgs-338340f993563551d8cb45941da987408abef65f.tar.bz2
nixpkgs-338340f993563551d8cb45941da987408abef65f.tar.lz
nixpkgs-338340f993563551d8cb45941da987408abef65f.tar.xz
nixpkgs-338340f993563551d8cb45941da987408abef65f.tar.zst
nixpkgs-338340f993563551d8cb45941da987408abef65f.zip
tryAttrs: init function
Diffstat (limited to 'lib/attrsets.nix')
-rw-r--r--lib/attrsets.nix18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix
index 4161fa546c8..70986195ae0 100644
--- a/lib/attrsets.nix
+++ b/lib/attrsets.nix
@@ -438,6 +438,24 @@ rec {
   overrideExisting = old: new:
     old // listToAttrs (map (attr: nameValuePair attr (attrByPath [attr] old.${attr} new)) (attrNames old));
 
+  /* Try given attributes in order. If no attributes are found, return
+     attribute list itself.
+
+     Example:
+       tryAttrs ["a" "b"] { a = 1; b = 2; }
+       => 1
+       tryAttrs ["a" "b"] { c = 3; }
+       => { c = 3; }
+  */
+  tryAttrs = allAttrs: set:
+    let tryAttrs_ = attrs:
+      if attrs == [] then set
+      else
+        (let h = head attrs; in
+         if hasAttr h set then getAttr h set
+         else tryAttrs_ (tail attrs));
+    in tryAttrs_ allAttrs;
+
 
   /*** deprecated stuff ***/