2024-07-25 00:25:21 +02:00
|
|
|
{ lib, config, self, inputs, withSystem, ... }:
|
|
|
|
|
|
|
|
let
|
2024-07-29 01:11:32 +02:00
|
|
|
inherit (config.lib)
|
2024-07-25 00:25:21 +02:00
|
|
|
and
|
|
|
|
hasFiles
|
|
|
|
hasDirectories
|
|
|
|
recurseDir
|
2024-08-13 14:53:14 +02:00
|
|
|
kebabToCamel
|
2024-09-01 03:29:32 +02:00
|
|
|
configuration-type-to-outputs-modules;
|
2024-07-25 00:25:21 +02:00
|
|
|
in
|
|
|
|
let
|
2024-07-27 12:04:48 +02:00
|
|
|
# Configuration helpers
|
|
|
|
|
2024-07-29 00:35:14 +02:00
|
|
|
# `pkgs` with flake's overlays
|
|
|
|
# NOTE: done here to avoid infinite recursion
|
|
|
|
pkgs' = system:
|
|
|
|
(withSystem system ({ pkgs, ... }: pkgs)).extend
|
|
|
|
(final: prev: inputs.self.packages.${system});
|
|
|
|
|
2024-08-13 14:53:14 +02:00
|
|
|
genUsers = configurationFiles:
|
|
|
|
lib.pipe configurationFiles [
|
|
|
|
(cf: cf."home" or { })
|
|
|
|
builtins.attrNames
|
|
|
|
(builtins.map
|
|
|
|
(lib.strings.removeSuffix ".nix"))
|
|
|
|
];
|
|
|
|
|
|
|
|
homeManagerModule = { root, meta, users ? null }: {
|
2024-07-25 00:25:21 +02:00
|
|
|
home-manager = {
|
|
|
|
# Use same `pkgs` instance as system (i.e. carry over overlays)
|
|
|
|
useGlobalPkgs = true;
|
|
|
|
# Do not keep packages in ${HOME}
|
|
|
|
useUserPackages = true;
|
2024-07-25 11:21:59 +02:00
|
|
|
# Default import all of our exported `home-manager` modules
|
|
|
|
sharedModules = builtins.attrValues config.flake.${configuration-type-to-outputs-modules "home-manager"};
|
2024-08-13 14:53:14 +02:00
|
|
|
# Pass in `inputs`, `hostname` and `meta`
|
2024-07-25 00:25:21 +02:00
|
|
|
extraSpecialArgs = {
|
2024-07-29 00:35:14 +02:00
|
|
|
inherit inputs;
|
2024-08-13 14:53:14 +02:00
|
|
|
inherit meta;
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
} // (if users == null then {
|
|
|
|
# nixOnDroid
|
|
|
|
config = "${root}/home.nix";
|
|
|
|
} else {
|
|
|
|
# Not nixOnDroid
|
2024-07-27 12:04:48 +02:00
|
|
|
users =
|
|
|
|
lib.attrsets.genAttrs
|
|
|
|
users
|
2024-07-25 00:25:21 +02:00
|
|
|
(user: import "${root}/home/${user}.nix");
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-08-13 14:53:14 +02:00
|
|
|
mkNixosHost = args @ { root, meta, users }: inputs.nixpkgs.lib.nixosSystem {
|
|
|
|
inherit (meta) system;
|
|
|
|
pkgs = pkgs' meta.system;
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
modules = [
|
|
|
|
# Main configuration
|
|
|
|
"${root}/configuration.nix"
|
|
|
|
# Home Manager
|
|
|
|
inputs.home-manager.nixosModules.home-manager
|
|
|
|
(homeManagerModule args)
|
|
|
|
# (r)agenix && agenix-rekey
|
|
|
|
inputs.ragenix.nixosModules.default
|
|
|
|
inputs.agenix-rekey.nixosModules.default
|
|
|
|
# nix-topology
|
|
|
|
inputs.nix-topology.nixosModules.default
|
|
|
|
# Sane default `networking.hostName`
|
|
|
|
{
|
2024-08-13 14:53:14 +02:00
|
|
|
networking.hostName = lib.mkDefault meta.hostname;
|
2024-07-25 00:25:21 +02:00
|
|
|
}
|
2024-08-13 14:53:14 +02:00
|
|
|
# TODO: lib.optionals
|
2024-07-25 11:21:59 +02:00
|
|
|
] ++ (builtins.attrValues config.flake.${configuration-type-to-outputs-modules "nixos"});
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
specialArgs = {
|
2024-07-29 00:35:14 +02:00
|
|
|
inherit inputs;
|
2024-08-13 14:53:14 +02:00
|
|
|
inherit meta;
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-13 14:53:14 +02:00
|
|
|
mkNixOnDroidHost = args @ { root, meta }: inputs.nix-on-droid.lib.nixOnDroidConfiguration {
|
2024-07-25 00:25:21 +02:00
|
|
|
# NOTE: inferred by `pkgs.system`
|
|
|
|
# inherit system;
|
2024-08-13 14:53:14 +02:00
|
|
|
pkgs = pkgs' meta.system;
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
modules = [
|
|
|
|
# Main configuration
|
|
|
|
"${root}/configuration.nix"
|
|
|
|
# Home Manager
|
|
|
|
(homeManagerModule args)
|
2024-07-25 11:21:59 +02:00
|
|
|
] ++ (builtins.attrValues config.flake.${configuration-type-to-outputs-modules "nix-on-droid"});
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
extraSpecialArgs = {
|
2024-07-29 00:35:14 +02:00
|
|
|
inherit inputs;
|
2024-08-13 14:53:14 +02:00
|
|
|
inherit meta;
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
home-manager-path = inputs.home-manager.outPath;
|
|
|
|
};
|
|
|
|
|
2024-08-13 14:53:14 +02:00
|
|
|
mkNixDarwinHost = args @ { root, meta, users }: inputs.nix-darwin.lib.darwinSystem {
|
|
|
|
inherit (meta) system;
|
|
|
|
pkgs = pkgs' meta.system;
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
modules = [
|
|
|
|
# Main configuration
|
|
|
|
"${root}/configuration.nix"
|
|
|
|
# Home Manager
|
|
|
|
inputs.home-manager.darwinModules.home-manager
|
|
|
|
(homeManagerModule args)
|
|
|
|
# # Set `nixpkgs.hostPlatform`
|
|
|
|
# {
|
|
|
|
# nixpkgs.hostPlatform = system;
|
|
|
|
# }
|
2024-07-25 11:21:59 +02:00
|
|
|
] ++ (builtins.attrValues config.flake.${configuration-type-to-outputs-modules "nix-darwin"});
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
specialArgs = {
|
2024-07-29 00:35:14 +02:00
|
|
|
inherit inputs;
|
2024-08-13 14:53:14 +02:00
|
|
|
inherit meta;
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-13 14:53:14 +02:00
|
|
|
mkHomeManagerHost = args @ { root, meta }: inputs.home-manager.lib.homeManagerConfiguration {
|
|
|
|
inherit (meta) system;
|
|
|
|
pkgs = pkgs' meta.system;
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
modules = [
|
|
|
|
"${root}/home.nix"
|
2024-07-25 11:21:59 +02:00
|
|
|
] ++ (builtins.attrValues config.flake.${configuration-type-to-outputs-modules "home-manager"});
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
extraSpecialArgs = {
|
2024-07-29 00:35:14 +02:00
|
|
|
inherit inputs;
|
2024-08-13 14:53:14 +02:00
|
|
|
inherit meta;
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = let
|
|
|
|
inherit (lib) types;
|
|
|
|
in {
|
2024-09-01 03:29:32 +02:00
|
|
|
auto.configurations = lib.mkOption {
|
2024-07-25 00:25:21 +02:00
|
|
|
description = ''
|
|
|
|
Automagically generate configurations from walking directories with Nix files
|
|
|
|
'';
|
2024-08-13 14:53:14 +02:00
|
|
|
internal = true;
|
|
|
|
type = types.submodule (autoConfigurationsSubmodule: let
|
|
|
|
inherit (autoConfigurationsSubmodule.config)
|
|
|
|
configurationTypes
|
|
|
|
enableAll
|
|
|
|
baseDir
|
|
|
|
;
|
|
|
|
in {
|
2024-07-25 00:25:21 +02:00
|
|
|
options = {
|
2024-08-13 14:53:14 +02:00
|
|
|
enableAll = lib.mkEnableOption ''
|
|
|
|
Automatic ${builtins.toString (lib.attrValues configurationTypes)} configurations extraction
|
|
|
|
'';
|
2024-07-25 00:25:21 +02:00
|
|
|
baseDir = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Base directory of the contained configurations, used as a base for the rest of the options
|
|
|
|
'';
|
|
|
|
type = types.path;
|
2024-07-27 12:04:48 +02:00
|
|
|
default = "${self}/hosts";
|
|
|
|
defaultText = ''''${self}/hosts'';
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
2024-08-13 14:53:14 +02:00
|
|
|
configurationTypes = lib.mkOption {
|
|
|
|
type = types.attrsOf (types.submodule (configurationTypeSubmodule@{ name, ... }: let
|
|
|
|
inherit (configurationTypeSubmodule.config)
|
|
|
|
# enable
|
|
|
|
dir
|
|
|
|
predicate
|
|
|
|
mkHost
|
|
|
|
mkDeployNode
|
|
|
|
;
|
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
enable = lib.mkEnableOption "Automatic ${name} configurations extraction" // {
|
|
|
|
default = enableAll;
|
|
|
|
};
|
|
|
|
# NOTE: each can be read from a different directory
|
|
|
|
dir = lib.mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "${baseDir}/${name}";
|
|
|
|
};
|
|
|
|
hostsName = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Name of the `hosts` output
|
|
|
|
'';
|
|
|
|
type = types.str;
|
|
|
|
default = "${kebabToCamel name}Hosts";
|
|
|
|
};
|
|
|
|
configurationsName = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Name of the `configurations` output
|
|
|
|
'';
|
|
|
|
type = types.str;
|
|
|
|
default = "${kebabToCamel name}Configurations";
|
|
|
|
};
|
|
|
|
predicate = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Function for filtering configurations
|
|
|
|
'';
|
|
|
|
# FIXME: `merge` of `functionTo` type causes a stray `passthru` to attempt getting evaluated
|
|
|
|
# type = types.functionTo types.anything;
|
|
|
|
type = types.unspecified;
|
|
|
|
example = /* nix */ ''
|
|
|
|
{ root, host, configurationFiles, ... }:
|
|
|
|
# Utils from `./modules/flake/lib/default.nix`
|
|
|
|
and [
|
|
|
|
(! (host == "__template__"))
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" ]
|
|
|
|
configurationFiles)
|
|
|
|
(hasDirectories
|
|
|
|
[ "home" ]
|
|
|
|
configurationFiles)
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
mkHost = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Function for generating a configuration
|
|
|
|
'';
|
|
|
|
# type = types.functionTo types.anything;
|
|
|
|
type = types.unspecified;
|
|
|
|
example = /* nix */ ''
|
|
|
|
args @ { root, meta, users }: inputs.nixpkgs.lib.nixosSystem {
|
|
|
|
inherit (meta) system;
|
|
|
|
|
|
|
|
modules = [
|
|
|
|
# Main configuration
|
|
|
|
"''${root}/configuration.nix"
|
|
|
|
# Home Manager
|
|
|
|
inputs.home-manager.nixosModules.home-manager
|
|
|
|
(homeManagerModule args)
|
|
|
|
] ++ (builtins.attrValues config.flake.''${configuration-type-to-outputs-modules "nixos"});
|
|
|
|
|
|
|
|
specialArgs = {
|
|
|
|
inherit inputs;
|
|
|
|
inherit meta;
|
2024-07-29 00:35:14 +02:00
|
|
|
};
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
2024-08-13 14:53:14 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
mkDeployNode = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Function for generating a `deploy-rs` node (null to skip)
|
|
|
|
'';
|
|
|
|
type = types.nullOr (types.functionTo types.anything);
|
|
|
|
default = null;
|
|
|
|
# TODO: update
|
|
|
|
example = /* nix */ ''
|
|
|
|
args @ { root, host, meta, configuration }:
|
|
|
|
inputs.deploy-rs.''${meta.system}.activate.nixos configuration;
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
resultConfigurations = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
The resulting automatic configurations
|
|
|
|
'';
|
|
|
|
# TODO: specify
|
|
|
|
type = types.unspecified;
|
|
|
|
readOnly = true;
|
|
|
|
default =
|
|
|
|
lib.pipe dir [
|
|
|
|
recurseDir
|
|
|
|
(lib.concatMapAttrs
|
|
|
|
(host: configurationFiles:
|
|
|
|
let
|
|
|
|
root = "${dir}/${host}";
|
|
|
|
meta-path = "${root}/meta.nix";
|
|
|
|
meta = import meta-path;
|
|
|
|
deploy-config = meta.deploy or null;
|
|
|
|
has-mkDeployNode = mkDeployNode != null;
|
|
|
|
has-deploy-config = builtins.pathExists meta-path && deploy-config != null;
|
|
|
|
configuration-args = { inherit root host configurationFiles; };
|
|
|
|
valid = predicate configuration-args;
|
|
|
|
configuration = mkHost configuration-args;
|
|
|
|
deploy-args = { inherit root host meta configuration; };
|
|
|
|
deploy = mkDeployNode deploy-args;
|
|
|
|
in
|
|
|
|
lib.optionalAttrs valid {
|
|
|
|
${host} = {
|
|
|
|
inherit configuration;
|
|
|
|
} // lib.optionalAttrs (has-mkDeployNode && has-deploy-config) {
|
|
|
|
inherit deploy;
|
|
|
|
};
|
|
|
|
}))
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = {};
|
|
|
|
}));
|
|
|
|
# TODO: put in a more visible place
|
|
|
|
default = {
|
|
|
|
nixos = {
|
|
|
|
predicate = ({ root, host, configurationFiles, ... }:
|
|
|
|
and [
|
|
|
|
(! (host == "__template__"))
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" "meta.nix" ]
|
|
|
|
configurationFiles)
|
|
|
|
]);
|
|
|
|
mkHost = ({ root, host, configurationFiles, ... }: let
|
|
|
|
meta = import "${root}/meta.nix" // {
|
|
|
|
hostname = host;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
mkNixosHost {
|
|
|
|
inherit root;
|
|
|
|
inherit meta;
|
|
|
|
users = genUsers configurationFiles;
|
|
|
|
});
|
|
|
|
mkDeployNode = ({ root, host, meta, configuration }:
|
|
|
|
{
|
|
|
|
inherit (meta.deploy) hostname;
|
|
|
|
profiles.system = meta.deploy // {
|
|
|
|
path = inputs.deploy-rs.lib.${meta.system}.activate."nixos" configuration;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
nix-on-droid = {
|
|
|
|
predicate = ({ root, host, configurationFiles, ... }:
|
|
|
|
and [
|
|
|
|
(! (host == "__template__"))
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" "home.nix" "meta.nix" ]
|
|
|
|
configurationFiles)
|
|
|
|
]);
|
|
|
|
mkHost = ({ root, host, configurationFiles, ... }: let
|
|
|
|
meta = import "${root}/meta.nix" // {
|
|
|
|
hostname = host;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
mkNixOnDroidHost {
|
|
|
|
inherit root;
|
|
|
|
inherit meta;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
nix-darwin = {
|
|
|
|
hostsName = "darwinHosts";
|
|
|
|
configurationsName = "darwinConfigurations";
|
|
|
|
predicate = ({ root, host, configurationFiles, ... }:
|
|
|
|
and [
|
|
|
|
(! (host == "__template__"))
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" "meta.nix" ]
|
|
|
|
configurationFiles)
|
|
|
|
(hasDirectories
|
|
|
|
[ "home" ]
|
|
|
|
configurationFiles)
|
|
|
|
]);
|
|
|
|
mkHost = ({ root, host, configurationFiles, ... }: let
|
|
|
|
meta = import "${root}/meta.nix" // {
|
|
|
|
hostname = host;
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
2024-08-13 14:53:14 +02:00
|
|
|
in
|
|
|
|
mkNixDarwinHost {
|
|
|
|
inherit root;
|
|
|
|
inherit meta;
|
|
|
|
users = genUsers configurationFiles;
|
|
|
|
});
|
|
|
|
mkDeployNode = ({ root, host, meta, configuration }:
|
|
|
|
{
|
|
|
|
inherit (meta.deploy) hostname;
|
|
|
|
profiles.system = meta.deploy // {
|
|
|
|
path = inputs.deploy-rs.lib.${meta.system}.activate."darwin" configuration;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
home-manager = {
|
|
|
|
hostsName = "homeHosts";
|
|
|
|
configurationsName = "homeConfigurations";
|
|
|
|
predicate = ({ root, host, configurationFiles, ... }:
|
|
|
|
and [
|
|
|
|
(! (host == "__template__"))
|
|
|
|
(hasFiles
|
|
|
|
[ "home.nix" "meta.nix" ]
|
|
|
|
configurationFiles)
|
|
|
|
]);
|
|
|
|
mkHost = ({ root, host, configurationFiles, ... }: let
|
|
|
|
meta = import "${root}/meta.nix" // {
|
|
|
|
hostname = host;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
mkHomeManagerHost {
|
|
|
|
inherit root;
|
|
|
|
inherit meta;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
resultConfigurations = lib.mkOption {
|
|
|
|
readOnly = true;
|
|
|
|
default = lib.pipe configurationTypes [
|
|
|
|
(lib.mapAttrs'
|
|
|
|
(configurationType: configurationTypeConfig:
|
|
|
|
lib.nameValuePair
|
2024-09-01 03:29:32 +02:00
|
|
|
configurationTypeConfig.configurationsName
|
|
|
|
(lib.mapAttrs
|
|
|
|
(host: { configuration, ... }:
|
|
|
|
configuration)
|
|
|
|
configurationTypeConfig.resultConfigurations)))
|
2024-08-13 14:53:14 +02:00
|
|
|
];
|
|
|
|
};
|
|
|
|
resultDeployNodes = lib.mkOption {
|
|
|
|
readOnly = true;
|
|
|
|
default = lib.pipe configurationTypes [
|
|
|
|
(lib.concatMapAttrs
|
|
|
|
(configurationType: configurationTypeConfig:
|
|
|
|
(lib.concatMapAttrs
|
|
|
|
(host: { deploy ? null, ... }:
|
|
|
|
lib.optionalAttrs
|
|
|
|
(deploy != null)
|
|
|
|
{
|
|
|
|
${host} = deploy;
|
|
|
|
})
|
|
|
|
configurationTypeConfig.resultConfigurations)))
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2024-07-25 00:25:21 +02:00
|
|
|
});
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2024-08-13 14:53:14 +02:00
|
|
|
flake = let
|
2024-09-01 03:29:32 +02:00
|
|
|
configurations = config.auto.configurations.resultConfigurations;
|
2024-08-13 14:53:14 +02:00
|
|
|
deployNodes = {
|
2024-09-01 03:29:32 +02:00
|
|
|
deploy.nodes = config.auto.configurations.resultDeployNodes;
|
2024-08-13 14:53:14 +02:00
|
|
|
};
|
|
|
|
deployChecks = {
|
|
|
|
checks =
|
|
|
|
lib.mapAttrs
|
|
|
|
(system: deployLib:
|
|
|
|
deployLib.deployChecks
|
|
|
|
self.deploy)
|
|
|
|
inputs.deploy-rs.lib;
|
|
|
|
};
|
|
|
|
# TODO: lib.something for merging (asserting for no overwrites)
|
|
|
|
in configurations // deployNodes // deployChecks;
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
}
|