diff options
| author | Jon Santmyer <jon@jonsantmyer.com> | 2024-01-07 12:00:08 -0500 |
|---|---|---|
| committer | Jon Santmyer <jon@jonsantmyer.com> | 2024-01-07 12:00:08 -0500 |
| commit | 7b79203f54853733d4fae88943829d0e24e6e49f (patch) | |
| tree | a352d199fad7453332b9308a6295164157729f27 /lib.nix | |
| parent | 184bd30bcb303104a4981ac742d8f8961c5477e7 (diff) | |
| download | nix-config-7b79203f54853733d4fae88943829d0e24e6e49f.tar.gz nix-config-7b79203f54853733d4fae88943829d0e24e6e49f.tar.bz2 nix-config-7b79203f54853733d4fae88943829d0e24e6e49f.zip | |
massive overhaul
Diffstat (limited to 'lib.nix')
| -rw-r--r-- | lib.nix | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -0,0 +1,39 @@ +# functions to map a function over modules +# used to import all modules at once +# from hlissner +lib: rec { + mapModules = dir: func: + lib.filterAttrs (_: v: v != null) (lib.mapAttrs' + (n: v: let + path = "${toString dir}/${n}"; + in + if v == "directory" && builtins.pathExists "${path}/default.nix" + then lib.nameValuePair n (func path) + else if v == "regular" && n != "default.nix" && lib.hasSuffix ".nix" n + then lib.nameValuePair (lib.removeSuffix ".nix" n) (func path) + else lib.nameValuePair "" null) + (builtins.readDir dir)); + + mapModules' = dir: func: builtins.attrValues (mapModules dir func); + + mapModulesRec = dir: func: + lib.filterAttrs (_: v: v != null) (lib.mapAttrs' + (n: v: let + path = "${toString dir}/${n}"; + in + if v == "directory" + then lib.nameValuePair n (mapModulesRec path func) + else if v == "regular" && n != "default.nix" && lib.hasSuffix ".nix" n + then lib.nameValuePair (lib.removeSuffix ".nix" n) (func path) + else lib.nameValuePair "" null) + (builtins.readDir dir)); + + mapModulesRec' = dir: func: let + dirs = + lib.mapAttrsToList (k: _: "${dir}/${k}") + (lib.filterAttrs (_: v: v == "directory") (builtins.readDir dir)); + files = builtins.attrValues (mapModules dir lib.id); + paths = files ++ builtins.concatLists (builtins.map (d: mapModulesRec' d lib.id) dirs); + in + builtins.map func paths; +} |
