refactor(flake)!: machines -> hosts

This commit is contained in:
reo101 2024-07-27 13:04:48 +03:00
parent 703cd0264b
commit a881c5d7e1
Signed by: reo101
GPG key ID: 675AA7EF13964ACB
33 changed files with 52 additions and 45 deletions

View file

@ -36,7 +36,6 @@
# keep-derivations = true # keep-derivations = true
# ''; # '';
settings = { settings = {
# Enable flakes and new 'nix' command # Enable flakes and new 'nix' command
experimental-features = [ experimental-features = [

View file

@ -12,9 +12,16 @@
./mindustry.nix ./mindustry.nix
# ./home-assistant # ./home-assistant
./samba.nix ./samba.nix
# ./steam.nix
# ./ollama.nix # ./ollama.nix
# ./sunshine.nix
# ./photoprism.nix
# ./immich.nix
# ./nextcloud.nix
]; ];
# services.kanidm = { };
age.rekey = { age.rekey = {
hostPubkey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPopSTZ81UyKp9JSljCLp+Syk51zacjh9fLteqxQ6/aB"; hostPubkey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPopSTZ81UyKp9JSljCLp+Syk51zacjh9fLteqxQ6/aB";
# masterIdentities = [ "${inputs.self}/secrets/privkey.age" ]; # masterIdentities = [ "${inputs.self}/secrets/privkey.age" ];

View file

@ -53,4 +53,6 @@
linkConfig.Name = "wan0"; # "wlp15s0"; linkConfig.Name = "wan0"; # "wlp15s0";
}; };
}; };
# systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
} }

View file

@ -1,6 +1,13 @@
{ inputs, outputs, lib, pkgs, config, ... }: { inputs, outputs, lib, pkgs, config, ... }:
{ {
# age.secrets."nextcloud.adminpass" = {
# rekeyFile = "${inputs.self}/secrets/home/jeeves/nextcloud/adminpass.age";
# mode = "770";
# owner = "nextcloud";
# group = "nextcloud";
# };
environment.systemPackages = [ environment.systemPackages = [
# config.services.nextcloud.package # config.services.nextcloud.package
]; ];
@ -10,7 +17,7 @@
services.nginx = { services.nginx = {
enable = true; enable = true;
package = pkgs.openresty; package = pkgs.openresty;
# virtualHosts."_.jeeves.local" = { # virtualHosts."_.${config.networking.hostName}.local" = {
# # listen = [ # # listen = [
# # { # # {
# # addr = "127.0.0.1"; # # addr = "127.0.0.1";

View file

@ -8,9 +8,12 @@ let
hasDirectories hasDirectories
recurseDir recurseDir
configuration-type-to-outputs-modules configuration-type-to-outputs-modules
configuration-type-to-outputs-machines; configuration-type-to-outputs-hosts;
in in
let let
# Configuration helpers
configurationTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager"];
homeManagerModule = { root, system, hostname, users ? null }: { homeManagerModule = { root, system, hostname, users ? null }: {
home-manager = { home-manager = {
# Use same `pkgs` instance as system (i.e. carry over overlays) # Use same `pkgs` instance as system (i.e. carry over overlays)
@ -30,15 +33,13 @@ let
config = "${root}/home.nix"; config = "${root}/home.nix";
} else { } else {
# Not nixOnDroid # Not nixOnDroid
users = lib.attrsets.genAttrs users =
users lib.attrsets.genAttrs
users
(user: import "${root}/home/${user}.nix"); (user: import "${root}/home/${user}.nix");
}); });
}; };
# Configuration helpers
configurationTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager"];
mkNixosHost = args @ { root, system, hostname, users }: lib.nixosSystem { mkNixosHost = args @ { root, system, hostname, users }: lib.nixosSystem {
inherit system; inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs); pkgs = withSystem system ({ pkgs, ... }: pkgs);
@ -120,7 +121,7 @@ let
}; };
createConfigurations = createConfigurations =
pred: mkHost: machines: pred: mkHost: hosts:
lib.foldAttrs lib.foldAttrs
lib.const lib.const
[ ] [ ]
@ -138,7 +139,7 @@ let
${host} = mkHost { inherit system host configurationFiles; }; ${host} = mkHost { inherit system host configurationFiles; };
}) })
hosts) hosts)
machines)); hosts));
in in
{ {
options = let options = let
@ -156,8 +157,8 @@ in
Base directory of the contained configurations, used as a base for the rest of the options Base directory of the contained configurations, used as a base for the rest of the options
''; '';
type = types.path; type = types.path;
default = "${self}/machines"; default = "${self}/hosts";
defaultText = ''''${self}/machines''; defaultText = ''''${self}/hosts'';
}; };
} // ( } // (
lib.pipe lib.pipe
@ -194,29 +195,20 @@ in
config = { config = {
flake = let flake = let
autoMachines = autoHosts =
lib.pipe lib.pipe
configurationTypes configurationTypes
[ [
(builtins.map (builtins.map
(configurationType: (configurationType:
lib.nameValuePair lib.nameValuePair
"${configuration-type-to-outputs-machines configurationType}" "${configuration-type-to-outputs-hosts configurationType}"
(if config.flake.autoConfigurations.${configurationType}.enable (if config.flake.autoConfigurations.${configurationType}.enable
then recurseDir config.flake.autoConfigurations.${configurationType}.dir then recurseDir config.flake.autoConfigurations.${configurationType}.dir
else { }))) else { })))
builtins.listToAttrs builtins.listToAttrs
]; ];
in { in autoHosts // {
# Machines
# NOTE: manually inheriting generated machines to avoid recursion
# (`autoMachines` depends on `config.flake` itself)
inherit (autoMachines)
nixosMachines
darwinMachines
nixOnDroidMachines
homeManagerMachines;
# Configurations # Configurations
nixosConfigurations = nixosConfigurations =
createConfigurations createConfigurations
@ -239,7 +231,7 @@ in
(lib.strings.removeSuffix ".nix") (lib.strings.removeSuffix ".nix")
(builtins.attrNames (configurationFiles."home" or { }))); (builtins.attrNames (configurationFiles."home" or { })));
}) })
config.flake.${configuration-type-to-outputs-machines "nixos"}; self.${configuration-type-to-outputs-hosts "nixos"};
nixOnDroidConfigurations = nixOnDroidConfigurations =
createConfigurations createConfigurations
@ -256,7 +248,7 @@ in
inherit system; inherit system;
hostname = host; hostname = host;
}) })
config.flake.${configuration-type-to-outputs-machines "nix-on-droid"}; self.${configuration-type-to-outputs-hosts "nix-on-droid"};
darwinConfigurations = darwinConfigurations =
createConfigurations createConfigurations
@ -279,7 +271,7 @@ in
(lib.strings.removeSuffix ".nix") (lib.strings.removeSuffix ".nix")
(builtins.attrNames (configurationFiles."home" or { }))); (builtins.attrNames (configurationFiles."home" or { })));
}) })
config.flake.${configuration-type-to-outputs-machines "nix-darwin"}; self.${configuration-type-to-outputs-hosts "nix-darwin"};
homeConfigurations = homeConfigurations =
createConfigurations createConfigurations
@ -296,7 +288,7 @@ in
inherit system; inherit system;
hostname = host; hostname = host;
}) })
config.flake.${configuration-type-to-outputs-machines "home-manager"}; self.${configuration-type-to-outputs-hosts "home-manager"};
}; };
}; };
} }

View file

@ -2,13 +2,13 @@
let let
inherit (import ../../nix/utils.nix { inherit lib self; }) inherit (import ../../nix/utils.nix { inherit lib self; })
accumulateMachines accumulateHosts
configuration-type-to-deploy-type; configuration-type-to-deploy-type;
in in
{ {
flake = { flake = {
deploy.nodes = deploy.nodes =
accumulateMachines accumulateHosts
# TODO: nix-on-droid # TODO: nix-on-droid
["nixos" "nix-darwin"] ["nixos" "nix-darwin"]
({ host, system, configuration-type, configuration }: ({ host, system, configuration-type, configuration }:

View file

@ -55,19 +55,19 @@ rec {
(builtins.throw (builtins.throw
(mkError configuration-type)); (mkError configuration-type));
# TODO: abstract away `_Machines` and `_Modules` # TODO: abstract away `_Hosts` and `_Modules`
configuration-type-to-outputs-machines = configuration-type-to-outputs-hosts =
gen-configuration-type-to gen-configuration-type-to
{ {
nixos = "nixosMachines"; nixos = "nixosHosts";
nix-on-droid = "nixOnDroidMachines"; nix-on-droid = "nixOnDroidHosts";
nix-darwin = "darwinMachines"; nix-darwin = "darwinHosts";
home-manager = "homeManagerMachines"; home-manager = "homeManagerHosts";
} }
(configuration-type: (configuration-type:
builtins.throw builtins.throw
"Invaild configuration-type \"${configuration-type}\" for flake outputs' machines"); "Invaild configuration-type \"${configuration-type}\" for flake outputs' hosts");
configuration-type-to-outputs-modules = configuration-type-to-outputs-modules =
gen-configuration-type-to gen-configuration-type-to
@ -104,18 +104,18 @@ rec {
builtins.throw builtins.throw
"Invaild configuration-type \"${configuration-type}\" for deploy-rs deployment"); "Invaild configuration-type \"${configuration-type}\" for deploy-rs deployment");
accumulateMachines = configuration-types: host-system-configuration-type-configuration-fn: accumulateHosts = configuration-types: host-system-configuration-type-configuration-fn:
lib.flip lib.concatMapAttrs lib.flip lib.concatMapAttrs
(lib.genAttrs (lib.genAttrs
configuration-types configuration-types
(configuration-type: (configuration-type:
let let
machines = configuration-type-to-outputs-machines configuration-type; hosts = configuration-type-to-outputs-hosts configuration-type;
in in
self.${machines})) self.${hosts}))
(configuration-type: machines: (configuration-type: hosts:
lib.pipe lib.pipe
machines hosts
[ [
# Filter out nondirectories # Filter out nondirectories
(lib.filterAttrs (lib.filterAttrs