lib(util): add functions for finding and acessing fragments ("paths") through a tree of attrSets
This commit is contained in:
parent
1a4551ff17
commit
1f8b3f5d1a
2 changed files with 23 additions and 0 deletions
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
outputs = { self, nixpkgs, flake-utils }: with flake-utils.lib; eachSystem (with system; [ x86_64-linux aarch64-linux ]) (curSystem:
|
outputs = { self, nixpkgs, flake-utils }: with flake-utils.lib; eachSystem (with system; [ x86_64-linux aarch64-linux ]) (curSystem:
|
||||||
let
|
let
|
||||||
|
util = import ./lib/util.nix;
|
||||||
pkgs = nixpkgs.legacyPackages.${curSystem};
|
pkgs = nixpkgs.legacyPackages.${curSystem};
|
||||||
|
|
||||||
imageName = "pihole/pihole";
|
imageName = "pihole/pihole";
|
||||||
|
|
22
lib/util.nix
Normal file
22
lib/util.nix
Normal 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
|
||||||
|
;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue