lib(util): add functions for finding and acessing fragments ("paths") through a tree of attrSets

This commit is contained in:
Christopher Bacher 2022-10-14 01:29:48 +02:00
parent 1a4551ff17
commit 1f8b3f5d1a
2 changed files with 23 additions and 0 deletions

View file

@ -9,6 +9,7 @@
outputs = { self, nixpkgs, flake-utils }: with flake-utils.lib; eachSystem (with system; [ x86_64-linux aarch64-linux ]) (curSystem:
let
util = import ./lib/util.nix;
pkgs = nixpkgs.legacyPackages.${curSystem};
imageName = "pihole/pihole";

22
lib/util.nix Normal file
View file

@ -0,0 +1,22 @@
{
collectAttrFragments = predicate: attrs: with builtins; let
_collectAttrFragments = attrs:
concatMap (key: _collectAttrFragmentsBelowKey key attrs.${key}) (attrNames attrs)
;
_collectAttrFragmentsBelowKey = key: value:
if predicate value then [ [key] ]
else if isAttrs value then
map (fragment: [key] ++ fragment) (_collectAttrFragments value)
else [ ]
;
in _collectAttrFragments attrs
;
accessValueOfFragment = attrs: fragment: with builtins; let
_accessValueOfFragment = value: fragment:
if fragment == [] then value
else _accessValueOfFragment (value.${head fragment}) (tail fragment)
;
in _accessValueOfFragment attrs fragment
;
}