From 1f8b3f5d1a4ecff870315c8afa73e40fe67fb8a0 Mon Sep 17 00:00:00 2001 From: Christopher Bacher Date: Fri, 14 Oct 2022 01:29:48 +0200 Subject: [PATCH] lib(util): add functions for finding and acessing fragments ("paths") through a tree of attrSets --- flake.nix | 1 + lib/util.nix | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 lib/util.nix diff --git a/flake.nix b/flake.nix index 50153e1..fbe630d 100644 --- a/flake.nix +++ b/flake.nix @@ -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"; diff --git a/lib/util.nix b/lib/util.nix new file mode 100644 index 0000000..ee4b79c --- /dev/null +++ b/lib/util.nix @@ -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 + ; +}