aboutsummaryrefslogtreecommitdiff
path: root/lib/modules.nix
diff options
context:
space:
mode:
authorsefidel <contact@sefidel.net>2024-01-23 00:20:54 +0900
committersefidel <contact@sefidel.net>2024-01-23 01:05:04 +0900
commitb311d7f8459fe1e90f7b0cbf5862b22a2cbedff5 (patch)
treed7b8af8e685699a1e66afcb10b668363d604106e /lib/modules.nix
parentb670811db1c645c9c4effff1829e90b380318427 (diff)
downloadnixrc-b311d7f8459fe1e90f7b0cbf5862b22a2cbedff5.zip
feat(lib)!: refactor
Diffstat (limited to 'lib/modules.nix')
-rw-r--r--lib/modules.nix54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
new file mode 100644
index 0000000..ef7c289
--- /dev/null
+++ b/lib/modules.nix
@@ -0,0 +1,54 @@
+{ self, lib, ... }:
+
+let
+ inherit (builtins) attrValues readDir pathExists concatLists;
+ inherit (lib) id mapAttrsToList filterAttrs hasPrefix hasSuffix nameValuePair removeSuffix;
+ 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"
+ 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 ++ concatLists (map (d: mapModulesRec' d id) dirs);
+ in
+ map fn paths;
+}