feat(modules)!: modularize over machines dir

This commit is contained in:
reo101 2024-07-25 00:45:23 +03:00
parent 5590ef9cdc
commit c6432c7dab
Signed by: reo101
GPG key ID: 675AA7EF13964ACB
3 changed files with 104 additions and 9 deletions

View file

@ -172,6 +172,8 @@
flake = { flake = {
inherit self; inherit self;
autoModules.enableAll = true;
# Templates # Templates
templates = import ./templates { templates = import ./templates {
inherit inputs; inherit inputs;

View file

@ -5,10 +5,12 @@ let
inherit (import ./utils.nix { inherit lib self; }) inherit (import ./utils.nix { inherit lib self; })
eq eq
and and
hasFiles; hasFiles
camelToKebab;
in in
let let
# Modules helpers # Modules helpers
moduleTypes = ["nixos" "nixOnDroid" "nixDarwin" "homeManager" "flake"];
createModules = baseDir: { passthru ? { inherit inputs outputs; }, ... }: createModules = baseDir: { passthru ? { inherit inputs outputs; }, ... }:
lib.pipe baseDir [ lib.pipe baseDir [
# Read given directory # Read given directory
@ -17,7 +19,9 @@ let
(lib.mapAttrs' (lib.mapAttrs'
(name: type: (name: type:
let let
moduleDir = lib.path.append baseDir "${name}"; # BUG: cannot use `append` because of `${self}` (not a path)
# moduleDir = lib.path.append baseDir "${name}";
moduleDir = "${baseDir}/${name}";
in in
if and [ if and [
(type == "directory") (type == "directory")
@ -59,12 +63,81 @@ let
]; ];
in in
{ {
flake = { options = let
# Modules inherit (lib) types;
nixosModules = createModules ../modules/nixos { }; in {
nixOnDroidModules = createModules ../modules/nix-on-droid { }; flake.autoModules = lib.mkOption {
nixDarwinModules = createModules ../modules/nix-darwin { }; description = ''
homeManagerModules = createModules ../modules/home-manager { }; Automagivally generate modules from walking directories with Nix files
flakeModules = createModules ../modules/flake { }; '';
type = types.submodule (submodule: {
options = {
enableAll = lib.mkEnableOption "Automatic ${builtins.toString moduleTypes} modules extraction";
baseDir = lib.mkOption {
description = ''
Base directory of the contained modules, used as a base for the rest of the options
'';
type = types.path;
default = "${self}/modules";
defaultText = ''''${self}/modules'';
};
} // (
lib.pipe
moduleTypes
[
(builtins.map
# NOTE: create small submodule for every `moduleType`
(moduleType:
lib.nameValuePair
"${moduleType}"
(lib.mkOption {
type = types.submodule {
options = {
# NOTE: each can be enabled (default global `enableAll`)
enable = lib.mkEnableOption "Automatic ${moduleType} modules extraction" // {
default = submodule.config.enableAll;
};
# NOTE: each can be read from a different directory
# (default global `baseDir` + `camelToKebab`-ed `moduleType`)
dir = lib.mkOption {
type = types.path;
default = "${submodule.config.baseDir}/${camelToKebab moduleType}";
};
};
};
default = {};
})))
builtins.listToAttrs
]);
});
default = {};
};
};
config = {
flake = let
autoModules =
lib.pipe
moduleTypes
[
(builtins.map
(moduleType:
lib.nameValuePair
"${moduleType}Modules"
(if config.flake.autoModules.${moduleType}.enable
then createModules config.flake.autoModules.${moduleType}.dir { }
else { })))
builtins.listToAttrs
];
in {
# NOTE: manually inheriting generated modules to avoid recursion
# (`autoModules` depends on `config.flake` itself)
inherit (autoModules)
nixosModules
nixOnDroidModules
nixDarwinModules
homeManagerModules
flakeModules;
};
}; };
} }

View file

@ -30,6 +30,26 @@ rec {
# NOTE: Implying last argument is the output of `recurseDir` # NOTE: Implying last argument is the output of `recurseDir`
hasDirectories = allSatisfy lib.isAttrs; hasDirectories = allSatisfy lib.isAttrs;
camelToKebab =
lib.stringAsChars
(c: if c == lib.toUpper c then "-${lib.toLower c}" else c);
# NOTE: from Tweag's Nix Hour 76 - <https://github.com/tweag/nix-hour/blob/c4fd0f2fc3059f057571bbfd74f3c5e4021f526c/code/76/default.nix#L4-L22>
mutFirstChar =
f: s:
let
firstChar = f (lib.substring 0 1 s);
rest = lib.substring 1 (-1) s;
in firstChar + rest;
kebabToCamel =
s:
mutFirstChar lib.toLower (
lib.concatMapStrings (mutFirstChar lib.toUpper) (
lib.splitString "-" s
)
);
gen-configuration-type-to = mappings: mkError: configuration-type: gen-configuration-type-to = mappings: mkError: configuration-type:
mappings.${configuration-type} or mappings.${configuration-type} or
(builtins.throw (builtins.throw