2024-07-25 00:25:21 +02:00
|
|
|
{ lib, config, self, inputs, withSystem, ... }:
|
|
|
|
|
|
|
|
let
|
2024-07-29 00:35:14 +02:00
|
|
|
inherit (import ../../nix/utils.nix { inherit lib config self; })
|
2024-07-25 00:25:21 +02:00
|
|
|
and
|
|
|
|
hasFiles
|
|
|
|
hasDirectories
|
|
|
|
recurseDir
|
2024-07-29 00:35:14 +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
|
|
|
|
configurationTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager"];
|
|
|
|
|
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-07-25 00:25:21 +02:00
|
|
|
homeManagerModule = { root, system, hostname, users ? null }: {
|
|
|
|
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-07-25 00:25:21 +02:00
|
|
|
# Pass in `inputs`, `outputs` and maybe `meta`
|
|
|
|
extraSpecialArgs = {
|
2024-07-29 00:35:14 +02:00
|
|
|
inherit inputs;
|
2024-07-25 00:25:21 +02:00
|
|
|
# TODO: meta?
|
|
|
|
inherit hostname;
|
|
|
|
};
|
|
|
|
} // (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-07-27 12:14:58 +02:00
|
|
|
mkNixosHost = args @ { root, system, hostname, users }: inputs.nixpkgs.lib.nixosSystem {
|
2024-07-25 00:25:21 +02:00
|
|
|
inherit system;
|
2024-07-29 00:35:14 +02:00
|
|
|
pkgs = pkgs' 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`
|
|
|
|
{
|
|
|
|
networking.hostName = lib.mkDefault hostname;
|
|
|
|
}
|
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-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkNixOnDroidHost = args @ { root, system, hostname }: inputs.nix-on-droid.lib.nixOnDroidConfiguration {
|
|
|
|
# NOTE: inferred by `pkgs.system`
|
|
|
|
# inherit system;
|
2024-07-29 00:35:14 +02:00
|
|
|
pkgs = pkgs' 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-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
home-manager-path = inputs.home-manager.outPath;
|
|
|
|
};
|
|
|
|
|
|
|
|
mkNixDarwinHost = args @ { root, system, hostname, users }: inputs.nix-darwin.lib.darwinSystem {
|
|
|
|
inherit system;
|
2024-07-29 00:35:14 +02:00
|
|
|
pkgs = pkgs' 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-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkHomeManagerHost = args @ { root, system, hostname }: inputs.home-manager.lib.homeManagerConfiguration {
|
|
|
|
inherit system;
|
2024-07-29 00:35:14 +02:00
|
|
|
pkgs = pkgs' 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-07-25 00:25:21 +02:00
|
|
|
inherit hostname;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
createConfigurations =
|
2024-07-27 12:04:48 +02:00
|
|
|
pred: mkHost: hosts:
|
2024-07-25 00:25:21 +02:00
|
|
|
lib.foldAttrs
|
|
|
|
lib.const
|
|
|
|
[ ]
|
|
|
|
(builtins.attrValues
|
|
|
|
(builtins.mapAttrs
|
|
|
|
(system: hosts:
|
|
|
|
lib.concatMapAttrs
|
|
|
|
(host: configurationFiles:
|
|
|
|
lib.optionalAttrs
|
|
|
|
(and [
|
|
|
|
(host != "__template__")
|
|
|
|
(pred { inherit system host configurationFiles; })
|
|
|
|
])
|
|
|
|
{
|
|
|
|
${host} = mkHost { inherit system host configurationFiles; };
|
|
|
|
})
|
|
|
|
hosts)
|
2024-07-27 12:04:48 +02:00
|
|
|
hosts));
|
2024-07-25 00:25:21 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = let
|
|
|
|
inherit (lib) types;
|
|
|
|
in {
|
|
|
|
flake.autoConfigurations = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Automagically generate configurations from walking directories with Nix files
|
|
|
|
'';
|
|
|
|
type = types.submodule (submodule: {
|
|
|
|
options = {
|
|
|
|
enableAll = lib.mkEnableOption "Automatic ${builtins.toString configurationTypes} configurations extraction";
|
|
|
|
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
|
|
|
};
|
|
|
|
} // (
|
|
|
|
lib.pipe
|
|
|
|
configurationTypes
|
|
|
|
[
|
|
|
|
(builtins.map
|
|
|
|
# NOTE: create small submodule for every `configurationType`
|
|
|
|
(configurationType:
|
|
|
|
lib.nameValuePair
|
|
|
|
"${configurationType}"
|
|
|
|
(lib.mkOption {
|
|
|
|
type = types.submodule {
|
|
|
|
options = {
|
|
|
|
# NOTE: each can be enabled (default global `enableAll`)
|
|
|
|
enable = lib.mkEnableOption "Automatic ${configurationType} configurations extraction" // {
|
|
|
|
default = submodule.config.enableAll;
|
|
|
|
};
|
|
|
|
# NOTE: each can be read from a different directory
|
|
|
|
# (default global `baseDir` + `camelToKebab`-ed `configurationType`)
|
|
|
|
dir = lib.mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "${submodule.config.baseDir}/${configurationType}";
|
|
|
|
};
|
2024-07-29 00:35:14 +02:00
|
|
|
# TODO: split hosts and configurations?
|
|
|
|
resultHosts = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
The resulting automatic packages
|
|
|
|
'';
|
|
|
|
# TODO: specify
|
|
|
|
type = types.unspecified;
|
|
|
|
readOnly = true;
|
|
|
|
internal = true;
|
|
|
|
default =
|
|
|
|
lib.optionalAttrs
|
|
|
|
config.flake.autoConfigurations.${configurationType}.enable
|
|
|
|
(recurseDir config.flake.autoConfigurations.${configurationType}.dir);
|
|
|
|
};
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
default = {};
|
|
|
|
})))
|
|
|
|
builtins.listToAttrs
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2024-07-29 00:35:14 +02:00
|
|
|
flake = {
|
2024-07-25 00:25:21 +02:00
|
|
|
# Configurations
|
|
|
|
nixosConfigurations =
|
|
|
|
createConfigurations
|
|
|
|
({ system, host, configurationFiles, ... }:
|
|
|
|
and
|
|
|
|
[
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" ]
|
|
|
|
configurationFiles)
|
|
|
|
# (hasDirectories
|
|
|
|
# [ "home" ]
|
|
|
|
# config)
|
|
|
|
])
|
|
|
|
({ system, host, configurationFiles, ... }:
|
|
|
|
mkNixosHost {
|
|
|
|
root = "${config.flake.autoConfigurations.nixos.dir}/${system}/${host}";
|
|
|
|
inherit system;
|
|
|
|
hostname = host;
|
|
|
|
users = (builtins.map
|
|
|
|
(lib.strings.removeSuffix ".nix")
|
|
|
|
(builtins.attrNames (configurationFiles."home" or { })));
|
|
|
|
})
|
2024-07-29 00:35:14 +02:00
|
|
|
config.flake.autoConfigurations.nixos.resultHosts;
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
nixOnDroidConfigurations =
|
|
|
|
createConfigurations
|
|
|
|
({ system, host, configurationFiles, ... }:
|
|
|
|
and
|
|
|
|
[
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" "home.nix" ]
|
|
|
|
configurationFiles)
|
|
|
|
])
|
|
|
|
({ system, host, configurationFiles, ... }:
|
|
|
|
mkNixOnDroidHost {
|
|
|
|
root = "${config.flake.autoConfigurations.nix-on-droid.dir}/${system}/${host}";
|
|
|
|
inherit system;
|
|
|
|
hostname = host;
|
|
|
|
})
|
2024-07-29 00:35:14 +02:00
|
|
|
config.flake.autoConfigurations.nix-on-droid.resultHosts;
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
darwinConfigurations =
|
|
|
|
createConfigurations
|
|
|
|
({ system, host, configurationFiles, ... }:
|
|
|
|
and
|
|
|
|
[
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" ]
|
|
|
|
configurationFiles)
|
|
|
|
(hasDirectories
|
|
|
|
[ "home" ]
|
|
|
|
configurationFiles)
|
|
|
|
])
|
|
|
|
({ system, host, configurationFiles, ... }:
|
|
|
|
mkNixDarwinHost {
|
|
|
|
root = "${config.flake.autoConfigurations.nix-darwin.dir}/${system}/${host}";
|
|
|
|
inherit system;
|
|
|
|
hostname = host;
|
|
|
|
users = (builtins.map
|
|
|
|
(lib.strings.removeSuffix ".nix")
|
|
|
|
(builtins.attrNames (configurationFiles."home" or { })));
|
|
|
|
})
|
2024-07-29 00:35:14 +02:00
|
|
|
config.flake.autoConfigurations.nix-darwin.resultHosts;
|
2024-07-25 00:25:21 +02:00
|
|
|
|
|
|
|
homeConfigurations =
|
|
|
|
createConfigurations
|
|
|
|
({ system, host, configurationFiles, ... }:
|
|
|
|
and
|
|
|
|
[
|
|
|
|
(hasFiles
|
|
|
|
[ "home.nix" ]
|
|
|
|
configurationFiles)
|
|
|
|
])
|
|
|
|
({ system, host, configurationFiles, ... }:
|
|
|
|
mkHomeManagerHost {
|
2024-07-28 01:36:34 +02:00
|
|
|
root = "${config.flake.autoConfigurations.home-manager.dir}/${system}/${host}";
|
2024-07-25 00:25:21 +02:00
|
|
|
inherit system;
|
|
|
|
hostname = host;
|
|
|
|
})
|
2024-07-29 00:35:14 +02:00
|
|
|
config.flake.autoConfigurations.home-manager.resultHosts;
|
2024-07-25 00:25:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|