mirror of
https://gitlab.com/rensa-nix/utils.git
synced 2025-12-12 07:30:13 +01:00
feat: add utility functions for disko and module discovery/loading
This commit is contained in:
parent
901ab49e43
commit
3363059b65
1 changed files with 84 additions and 1 deletions
|
|
@ -1,5 +1,88 @@
|
||||||
args: {
|
{lib, ...} @ args: let
|
||||||
|
inherit
|
||||||
|
(lib)
|
||||||
|
pipe
|
||||||
|
mapAttrs
|
||||||
|
flatten
|
||||||
|
hasSuffix
|
||||||
|
removeSuffix
|
||||||
|
removePrefix
|
||||||
|
splitString
|
||||||
|
setAttrByPath
|
||||||
|
fold
|
||||||
|
recursiveUpdate
|
||||||
|
;
|
||||||
|
in rec {
|
||||||
mkSystem = import ./mkSystem.nix args;
|
mkSystem = import ./mkSystem.nix args;
|
||||||
mkHome = import ./mkHome.nix args;
|
mkHome = import ./mkHome.nix args;
|
||||||
mkDisk = import ./mkDisk.nix args;
|
mkDisk = import ./mkDisk.nix args;
|
||||||
|
|
||||||
|
# collects all disks from the cell, can be passed to disko.devices:
|
||||||
|
#
|
||||||
|
# disko.devices = utils.collectDisks cell.disks;
|
||||||
|
collectDisks = disks:
|
||||||
|
fold (acc: item: recursiveUpdate acc item) {}
|
||||||
|
(map (dev: builtins.removeAttrs dev.userConfig ["ren"])
|
||||||
|
(builtins.attrValues disks));
|
||||||
|
|
||||||
|
# find modules/files in directory, return them all as a list of paths to import
|
||||||
|
findModules = {
|
||||||
|
dir,
|
||||||
|
currentFile ? "default.nix",
|
||||||
|
relative ? false,
|
||||||
|
}: let
|
||||||
|
processDir = subDir: first:
|
||||||
|
pipe "${dir}${subDir}" [
|
||||||
|
builtins.readDir
|
||||||
|
(mapAttrs (
|
||||||
|
n: v: let
|
||||||
|
p =
|
||||||
|
if relative
|
||||||
|
then "${subDir}/${n}"
|
||||||
|
else "${dir}${subDir}/${n}";
|
||||||
|
in
|
||||||
|
if v == "directory"
|
||||||
|
then (processDir "${subDir}/${n}" false)
|
||||||
|
else
|
||||||
|
# filter out the current file to prevent recursive import
|
||||||
|
if first && n == currentFile
|
||||||
|
then []
|
||||||
|
else p
|
||||||
|
))
|
||||||
|
builtins.attrValues
|
||||||
|
flatten
|
||||||
|
(builtins.filter (path: hasSuffix ".nix" path))
|
||||||
|
];
|
||||||
|
in
|
||||||
|
processDir "" true;
|
||||||
|
|
||||||
|
# find, then import, then merge all modules in a dir
|
||||||
|
importModules = {
|
||||||
|
dir,
|
||||||
|
args ? {},
|
||||||
|
currentFile ? "default.nix",
|
||||||
|
usePathAsKeys ? false,
|
||||||
|
}: let
|
||||||
|
allNixFiles = findModules {
|
||||||
|
inherit dir currentFile;
|
||||||
|
relative = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
filePathToAttrPath = filePath: let
|
||||||
|
relativePath = removeSuffix ".nix" (removePrefix "/" filePath);
|
||||||
|
in
|
||||||
|
splitString "/" relativePath;
|
||||||
|
|
||||||
|
importAndProcessFile = file: let
|
||||||
|
importedData = import "${dir}/${file}" args;
|
||||||
|
attrPath = filePathToAttrPath file;
|
||||||
|
in
|
||||||
|
if usePathAsKeys
|
||||||
|
then setAttrByPath attrPath importedData
|
||||||
|
else importedData;
|
||||||
|
|
||||||
|
importedContents = map importAndProcessFile allNixFiles;
|
||||||
|
mergedFiles = fold (acc: item: recursiveUpdate acc item) {} importedContents;
|
||||||
|
in
|
||||||
|
mergedFiles;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue