nixos-configs/lib/modules.nix

65 lines
1.5 KiB
Nix
Raw Normal View History

2023-03-20 22:35:11 +01:00
{ self, lib, ... }:
let
2024-08-20 22:58:24 +02:00
inherit (builtins)
attrValues
readDir
pathExists
concatLists
;
inherit (lib)
id
mapAttrsToList
filterAttrs
hasPrefix
hasSuffix
nameValuePair
removeSuffix
;
2023-03-20 22:35:11 +01:00
inherit (self.attrs) mapFilterAttrs;
in
rec {
2024-08-20 22:58:24 +02:00
mapModules =
dir: fn:
mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (
n: v:
let
path = "${toString dir}/${n}";
in
if v == "directory" && pathExists "${path}/default.nix" then
nameValuePair n (fn path)
else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n then
nameValuePair (removeSuffix ".nix" n) (fn path)
else
nameValuePair "" null
) (readDir dir);
2023-03-20 22:35:11 +01:00
2024-08-20 22:58:24 +02:00
mapModules' = dir: fn: attrValues (mapModules dir fn);
2023-03-20 22:35:11 +01:00
2024-08-20 22:58:24 +02:00
mapModulesRec =
dir: fn:
mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (
n: v:
let
path = "${toString dir}/${n}";
in
if v == "directory" then
nameValuePair n (mapModulesRec path fn)
else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n then
nameValuePair (removeSuffix ".nix" n) (fn path)
else
nameValuePair "" null
) (readDir dir);
2023-03-20 22:35:11 +01:00
2024-08-20 22:58:24 +02:00
mapModulesRec' =
dir: fn:
2023-03-20 22:35:11 +01:00
let
2024-08-20 22:58:24 +02:00
dirs = mapAttrsToList (k: _: "${dir}/${k}") (
filterAttrs (n: v: v == "directory" && !(hasPrefix "_" n)) (readDir dir)
);
2023-03-20 22:35:11 +01:00
files = attrValues (mapModules dir id);
paths = files ++ concatLists (map (d: mapModulesRec' d id) dirs);
in
map fn paths;
}