From 927c8b318f03f9cbefdcf6a591565d5c3080648e Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Sat, 13 Dec 2025 21:59:28 +1000 Subject: [PATCH] define recursive mapping on attribute sets --- nib/std/attrs.nix | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/nib/std/attrs.nix b/nib/std/attrs.nix index 090495f..b65af4c 100644 --- a/nib/std/attrs.nix +++ b/nib/std/attrs.nix @@ -1,4 +1,4 @@ -rec { +{lists}: rec { nameValuePair = name: value: {inherit name value;}; listToAttrsIdentity = values: @@ -77,4 +77,25 @@ rec { ::: */ genAttrs' = xs: f: builtins.listToAttrs (map f xs); + + mapAttrsRecursiveCond = cond: f: set: let + recurse = path: + builtins.mapAttrs ( + name: value: + if builtins.isAttrs value && cond value + then recurse (path ++ [name]) value + else f (path ++ [name]) value + ); + in + recurse [] set; + + mapAttrsRecursive = f: set: mapAttrsRecursiveCond (as: true) f set; + + # form: attrValueAt :: xs -> path -> value + # given path as a list of strings, return that value of an + # attribute set at that path + attrValueAt = lists.foldl (l: r: + if l != null && builtins.hasAttr r l + then l.${r} + else null); }