Working on modularizing stuff

This commit is contained in:
Chris Kruining 2024-07-21 00:07:19 +02:00
parent 835faf218d
commit 8349809fec
15 changed files with 256 additions and 41 deletions

36
lib/modules.nix Normal file
View file

@ -0,0 +1,36 @@
{ lib, self, ... }:
let
inherit (builtins) attrValues readDir pathExists concatLists;
inherit (lib.attrsets) mapAttrsToList filterAttrs nameValuePair;
inherit (lib.strings) hasPrefix hasSuffix removeSuffix;
inherit (lib.trivial) id;
inherit (self.attrs) mapFilterAttrs;
in rec
{
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);
mapModules' = dir: fn: attrValues (mapModules dir fn);
mapModulesRec = 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 (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);
mapModulesRec' = dir: fn: let
dirs = mapAttrsToList (k: _: "${dir}/${k}") (filterAttrs (n: v: v == "directory" && !(hasPrefix "_" n)) (readDir dir));
files = attrValues (mapModules dir id);
paths = files ++ contactLists (map (d: mapModulesRec' d id) dirs);
in
map fn paths;
}