From a5973d50b8660c3a87224a58ffd38bedff9559bb Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Wed, 27 Nov 2024 23:29:46 +0800 Subject: [PATCH] lib/path: add scanPaths --- lib/path.nix | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/path.nix b/lib/path.nix index d452a77..3707918 100644 --- a/lib/path.nix +++ b/lib/path.nix @@ -1,5 +1,6 @@ # Portions of this file are sourced from # https://github.com/linyinfeng/dotfiles/tree/b618b0fd16fb9c79ab7199ed51c4c0f98a392cea/lib +# https://github.com/spikespaz/bird-nix-lib/blob/3f44018dde966a00193081470f85a59458916986/lib/scaffold.nix { inputs, lib, @@ -36,7 +37,35 @@ let lib.listToAttrs (flattenTree' "" tree); buildModuleList = dir: lib.attrValues (flattenTree (rakeLeaves dir)); + + scanPaths = + path: keep: + let + inherit (lib) types; + pred = + if keep == null then + (_: _: true) + else if types.singleLineStr.check keep then + (name: _: !(name == keep)) + else if lib.isFunction keep then + keep + else if (types.listOf types.singleLineStr).check keep then + (name: _: !(builtins.elem name keep)) + else + throw "importDir predicate should be a string, function, or list of strings"; + isNix = + name: type: + (type == "regular" && lib.hasSuffix ".nix" name) + || (lib.pathIsRegularFile "${path}/${name}/default.nix"); + pred' = name: type: (isNix name type) && (pred name type); + in + map (name: path + "/${name}") (builtins.attrNames (lib.filterAttrs pred' (builtins.readDir path))); in { - inherit rakeLeaves flattenTree buildModuleList; + inherit + rakeLeaves + flattenTree + buildModuleList + scanPaths + ; }