refactor(flake)!: flake.autoStuff -> auto.stuff

Avoids infinite recursion in `auto.configurations` and `auto.modules`
Can now fully dynamically define `configuration` and `module` types
This commit is contained in:
reo101 2024-09-01 04:29:32 +03:00
parent ea47ccd981
commit 7956cfd20a
Signed by: reo101
GPG key ID: 675AA7EF13964ACB
6 changed files with 118 additions and 105 deletions

View file

@ -11,6 +11,11 @@
"x86_64-darwin" "x86_64-darwin"
]; ];
# BUG: infinite recursion
# imports = [
# ./modules/flake/modules
# ] ++ inputs.self.flakeModules;
imports = [ imports = [
./modules/flake/lib ./modules/flake/lib
./modules/flake/pkgs ./modules/flake/pkgs
@ -23,6 +28,23 @@
./modules/flake/shells ./modules/flake/shells
]; ];
auto = {
# Automatic modules, see `./modules/flake/modules/default.nix`
modules.enableAll = true;
# Automatic configurations, see `./modules/flake/configurations/default.nix`
configurations.enableAll = true;
# Automatic packages, see `./modules/flake/packages/default/default.nix`
packages.enable = true;
# Automatic overlays, see `./modules/flake/overlays/default/default.nix`
overlays.enable = true;
# Automatic devShells, see `./modules/flake/shells/default/default.nix`
devShells.enable = true;
};
perSystem = { lib, pkgs, system, ... }: { perSystem = { lib, pkgs, system, ... }: {
# Apps (`nix run`) # Apps (`nix run`)
apps = import ./apps { inherit pkgs; }; apps = import ./apps { inherit pkgs; };
@ -34,21 +56,6 @@
flake = { flake = {
inherit (inputs) self; inherit (inputs) self;
# Automatic modules, see `./modules/flake/modules/default.nix`
autoModules.enableAll = true;
# Automatic configurations, see `./modules/flake/configurations/default.nix`
autoConfigurations.enableAll = true;
# Automatic packages, see `./modules/flake/packages/default/default.nix`
autoPackages.enable = true;
# Automatic overlays, see `./modules/flake/overlays/default/default.nix`
autoOverlays.enable = true;
# Automatic devShells, see `./modules/flake/shells/default/default.nix`
autoDevShells.enable = true;
# Templates # Templates
templates = import ./templates { templates = import ./templates {
inherit inputs; inherit inputs;

View file

@ -7,8 +7,7 @@ let
hasDirectories hasDirectories
recurseDir recurseDir
kebabToCamel kebabToCamel
configuration-type-to-outputs-modules configuration-type-to-outputs-modules;
configuration-type-to-outputs-configurations;
in in
let let
# Configuration helpers # Configuration helpers
@ -140,7 +139,7 @@ in
options = let options = let
inherit (lib) types; inherit (lib) types;
in { in {
flake.autoConfigurations = lib.mkOption { auto.configurations = lib.mkOption {
description = '' description = ''
Automagically generate configurations from walking directories with Nix files Automagically generate configurations from walking directories with Nix files
''; '';
@ -426,19 +425,10 @@ in
}; };
config = { config = {
# BUG: cannot iterate on `config.flake.autoConfigurations.resultConfigurations`
# because of infinite recursion
flake = let flake = let
ogConfigurationTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager"]; configurations = config.auto.configurations.resultConfigurations;
configurations = lib.pipe ogConfigurationTypes [
(lib.map
configuration-type-to-outputs-configurations)
(lib.flip lib.genAttrs
(configurationType:
config.flake.autoConfigurations.resultConfigurations.${configurationType}))
];
deployNodes = { deployNodes = {
deploy.nodes = config.flake.autoConfigurations.resultDeployNodes; deploy.nodes = config.auto.configurations.resultDeployNodes;
}; };
deployChecks = { deployChecks = {
checks = checks =

View file

@ -3,12 +3,10 @@
let let
inherit (config.lib) inherit (config.lib)
createThings createThings
configuration-type-to-outputs-modules; kebabToCamel;
in in
let let
# Modules helpers # Modules helpers
moduleTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager" "flake"];
createModules = baseDir: createModules = baseDir:
createThings { createThings {
inherit baseDir; inherit baseDir;
@ -19,11 +17,17 @@ in
options = let options = let
inherit (lib) types; inherit (lib) types;
in { in {
flake.autoModules = lib.mkOption { auto.modules = lib.mkOption {
description = '' description = ''
Automagically generate modules from walking directories with Nix files Automagically generate modules from walking directories with Nix files
''; '';
type = types.submodule (submodule: { type = types.submodule (autoModulesSubmodule: let
inherit (autoModulesSubmodule.config)
moduleTypes
enableAll
baseDir
;
in {
options = { options = {
enableAll = lib.mkEnableOption "Automatic ${builtins.toString moduleTypes} modules extraction"; enableAll = lib.mkEnableOption "Automatic ${builtins.toString moduleTypes} modules extraction";
baseDir = lib.mkOption { baseDir = lib.mkOption {
@ -34,29 +38,31 @@ in
default = "${self}/modules"; default = "${self}/modules";
defaultText = ''''${self}/modules''; defaultText = ''''${self}/modules'';
}; };
} // ( moduleTypes = lib.mkOption {
lib.pipe type = types.attrsOf (types.submodule (moduleTypeSubmodule@{ name, ... }: let
moduleTypes inherit (moduleTypeSubmodule.config)
[ enable
(builtins.map dir
# NOTE: create small submodule for every `moduleType` ;
(moduleType: in {
lib.nameValuePair
"${moduleType}"
(lib.mkOption {
type = types.submodule {
options = { options = {
# NOTE: each can be enabled (default global `enableAll`) # NOTE: each can be enabled (default global `enableAll`)
enable = lib.mkEnableOption "Automatic ${moduleType} modules extraction" // { enable = lib.mkEnableOption "Automatic ${name} modules extraction" // {
default = submodule.config.enableAll; default = enableAll;
}; };
# NOTE: each can be read from a different directory # NOTE: each can be read from a different directory
# (default global `baseDir` + `camelToKebab`-ed `moduleType`)
dir = lib.mkOption { dir = lib.mkOption {
type = types.path; type = types.path;
default = "${submodule.config.baseDir}/${moduleType}"; default = "${baseDir}/${name}";
}; };
result = lib.mkOption { modulesName = lib.mkOption {
description = ''
Name of the `modules` output
'';
type = types.str;
default = "${kebabToCamel name}Modules";
};
resultModules = lib.mkOption {
description = '' description = ''
The resulting automatic packages The resulting automatic packages
''; '';
@ -66,15 +72,37 @@ in
internal = true; internal = true;
default = default =
lib.optionalAttrs lib.optionalAttrs
config.flake.autoModules.${moduleType}.enable enable
(createModules config.flake.autoModules.${moduleType}.dir); (createModules dir);
}; };
}; };
config = {};
}));
# TODO: put in a more visible place
default = {
nixos = {};
nix-on-droid = {};
nix-darwin = {
modulesName = "darwinModules";
};
home-manager = {};
flake = {};
};
};
resultModules = lib.mkOption {
readOnly = true;
default = lib.pipe moduleTypes [
(lib.mapAttrs'
(moduleType: moduleTypeConfig:
lib.nameValuePair
moduleTypeConfig.modulesName
(lib.mapAttrs
(host: module:
module)
moduleTypeConfig.resultModules)))
];
};
}; };
default = {};
})))
builtins.listToAttrs
]);
}); });
default = {}; default = {};
}; };
@ -82,19 +110,7 @@ in
config = { config = {
flake = let flake = let
autoModules = autoModules = config.auto.modules.resultModules;
lib.pipe
moduleTypes
[
(builtins.map
(moduleType:
let
name = "${configuration-type-to-outputs-modules moduleType}";
value = config.flake.autoModules.${moduleType}.result;
in
lib.nameValuePair name value))
builtins.listToAttrs
];
in autoModules; in autoModules;
}; };
} }

View file

@ -15,7 +15,7 @@ in
options = let options = let
inherit (lib) types; inherit (lib) types;
in { in {
flake.autoOverlays = lib.mkOption { auto.overlays = lib.mkOption {
description = '' description = ''
Automagically generate overlays from walking directories with Nix files Automagically generate overlays from walking directories with Nix files
''; '';
@ -39,8 +39,8 @@ in
internal = true; internal = true;
default = default =
lib.optionalAttrs lib.optionalAttrs
config.flake.autoOverlays.enable config.auto.overlays.enable
(createOverlays config.flake.autoOverlays.dir); (createOverlays config.auto.overlays.dir);
}; };
}; };
}); });
@ -52,7 +52,7 @@ in
flake = let flake = let
overlays = overlays =
lib.pipe lib.pipe
config.flake.autoOverlays.result config.auto.overlays.result
[ [
(lib.mapAttrs (lib.mapAttrs
(name: overlay: (name: overlay:

View file

@ -19,7 +19,7 @@ in
options = let options = let
inherit (lib) types; inherit (lib) types;
in { in {
flake.autoPackages = lib.mkOption { auto.packages = lib.mkOption {
description = '' description = ''
Automagically generate packages from walking directories with Nix files Automagically generate packages from walking directories with Nix files
''; '';
@ -46,8 +46,8 @@ in
internal = true; internal = true;
default = default =
lib.optionalAttrs lib.optionalAttrs
config.flake.autoPackages.enable config.auto.packages.enable
(createPackages config.flake.autoPackages.dir); (createPackages config.auto.packages.dir);
}; };
}; };
}); });
@ -59,7 +59,7 @@ in
perSystem = { lib, pkgs, system, ... }: let perSystem = { lib, pkgs, system, ... }: let
packages = packages =
lib.pipe lib.pipe
config.flake.autoPackages.result config.auto.packages.result
[ [
(lib.filterAttrs (lib.filterAttrs
(name: { package, systems }: (name: { package, systems }:

View file

@ -19,7 +19,7 @@ in
options = let options = let
inherit (lib) types; inherit (lib) types;
in { in {
flake.autoDevShells = lib.mkOption { auto.devShells = lib.mkOption {
description = '' description = ''
Automagically generate devShells from walking directories with Nix files Automagically generate devShells from walking directories with Nix files
''; '';
@ -46,8 +46,8 @@ in
internal = true; internal = true;
default = default =
lib.optionalAttrs lib.optionalAttrs
config.flake.autoDevShells.enable config.auto.devShells.enable
(createDevShells config.flake.autoDevShells.dir); (createDevShells config.auto.devShells.dir);
}; };
}; };
}); });
@ -59,7 +59,7 @@ in
perSystem = { lib, pkgs, system, ... }: let perSystem = { lib, pkgs, system, ... }: let
devShells = devShells =
lib.pipe lib.pipe
config.flake.autoDevShells.result config.auto.devShells.result
[ [
(lib.filterAttrs (lib.filterAttrs
(name: { devShell, systems }: (name: { devShell, systems }: