lib/path: add scanPaths

This commit is contained in:
Lu Wang 2024-11-27 23:29:46 +08:00
parent d0960631e3
commit a5973d50b8
Signed by: rebmit
SSH key fingerprint: SHA256:3px8QV1zEerIrEWHaqtH5rR9kjetyRST5EipOPrd+bU

View file

@ -1,5 +1,6 @@
# Portions of this file are sourced from # Portions of this file are sourced from
# https://github.com/linyinfeng/dotfiles/tree/b618b0fd16fb9c79ab7199ed51c4c0f98a392cea/lib # https://github.com/linyinfeng/dotfiles/tree/b618b0fd16fb9c79ab7199ed51c4c0f98a392cea/lib
# https://github.com/spikespaz/bird-nix-lib/blob/3f44018dde966a00193081470f85a59458916986/lib/scaffold.nix
{ {
inputs, inputs,
lib, lib,
@ -36,7 +37,35 @@ let
lib.listToAttrs (flattenTree' "" tree); lib.listToAttrs (flattenTree' "" tree);
buildModuleList = dir: lib.attrValues (flattenTree (rakeLeaves dir)); 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 in
{ {
inherit rakeLeaves flattenTree buildModuleList; inherit
rakeLeaves
flattenTree
buildModuleList
scanPaths
;
} }