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"
];
# BUG: infinite recursion
# imports = [
# ./modules/flake/modules
# ] ++ inputs.self.flakeModules;
imports = [
./modules/flake/lib
./modules/flake/pkgs
@ -23,6 +28,23 @@
./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, ... }: {
# Apps (`nix run`)
apps = import ./apps { inherit pkgs; };
@ -34,21 +56,6 @@
flake = {
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 = import ./templates {
inherit inputs;

View file

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

View file

@ -3,12 +3,10 @@
let
inherit (config.lib)
createThings
configuration-type-to-outputs-modules;
kebabToCamel;
in
let
# Modules helpers
moduleTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager" "flake"];
createModules = baseDir:
createThings {
inherit baseDir;
@ -19,11 +17,17 @@ in
options = let
inherit (lib) types;
in {
flake.autoModules = lib.mkOption {
auto.modules = lib.mkOption {
description = ''
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 = {
enableAll = lib.mkEnableOption "Automatic ${builtins.toString moduleTypes} modules extraction";
baseDir = lib.mkOption {
@ -34,47 +38,71 @@ in
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}/${moduleType}";
};
result = lib.mkOption {
description = ''
The resulting automatic packages
'';
# TODO: specify
type = types.unspecified;
readOnly = true;
internal = true;
default =
lib.optionalAttrs
config.flake.autoModules.${moduleType}.enable
(createModules config.flake.autoModules.${moduleType}.dir);
};
};
};
default = {};
})))
builtins.listToAttrs
]);
moduleTypes = lib.mkOption {
type = types.attrsOf (types.submodule (moduleTypeSubmodule@{ name, ... }: let
inherit (moduleTypeSubmodule.config)
enable
dir
;
in {
options = {
# NOTE: each can be enabled (default global `enableAll`)
enable = lib.mkEnableOption "Automatic ${name} modules extraction" // {
default = enableAll;
};
# NOTE: each can be read from a different directory
dir = lib.mkOption {
type = types.path;
default = "${baseDir}/${name}";
};
modulesName = lib.mkOption {
description = ''
Name of the `modules` output
'';
type = types.str;
default = "${kebabToCamel name}Modules";
};
resultModules = lib.mkOption {
description = ''
The resulting automatic packages
'';
# TODO: specify
type = types.unspecified;
readOnly = true;
internal = true;
default =
lib.optionalAttrs
enable
(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 = {};
};
@ -82,19 +110,7 @@ in
config = {
flake = let
autoModules =
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
];
autoModules = config.auto.modules.resultModules;
in autoModules;
};
}

View file

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

View file

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

View file

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