create module system for machines

This commit is contained in:
Pim Kunis 2024-02-29 00:28:38 +01:00
parent 9ee519055b
commit e7f35bf1bd
3 changed files with 186 additions and 147 deletions

View file

@ -35,7 +35,7 @@
pkgs = nixpkgs.legacyPackages.${controllerArch}; pkgs = nixpkgs.legacyPackages.${controllerArch};
lib = pkgs.lib; lib = pkgs.lib;
pkgs-unstable = nixpkgs-unstable.legacyPackages.${controllerArch}; pkgs-unstable = nixpkgs-unstable.legacyPackages.${controllerArch};
machines = import ./nixos/machines; machines = (lib.modules.evalModules { modules = [ (import ./nixos/machines) ]; }).config.machines;
physicalMachines = lib.filterAttrs (n: v: v.type == "physical") machines; physicalMachines = lib.filterAttrs (n: v: v.type == "physical") machines;
mkNixosSystems = systemDef: mkNixosSystems = systemDef:
builtins.mapAttrs builtins.mapAttrs

View file

@ -1,12 +1,64 @@
# TODO: Create a nixos module system for this. (mkMerge) { lib, ... }:
# That way, we don't have to specify isRaspberryPi on every machine... etc. let
machineOpts = { ... }: {
options = {
# TODO: rename to kind?
type = lib.mkOption {
type = lib.types.enum [ "physical" "virtual" ];
description = ''
Whether this machine is physical or virtual.
'';
};
hypervisorName = lib.mkOption {
default = null;
type = with lib.types; nullOr str;
description = ''
The host name of the hypervisor hosting this virtual machine.
'';
};
arch = lib.mkOption {
default = null;
type = with lib.types; nullOr str;
description = ''
CPU architecture of this machine.
'';
};
isRaspberryPi = lib.mkOption {
default = false;
type = lib.types.bool;
};
isHypervisor = lib.mkOption {
default = false;
type = lib.types.bool;
};
nixosModule = lib.mkOption {
default = { ... }: { };
type = lib.types.anything;
description = ''
Customized configuration for this machine in the form of a NixOS module.
'';
};
};
};
in
{ {
options = {
machines = lib.mkOption {
type = with lib.types; attrsOf (submodule machineOpts);
};
};
config = {
machines = {
warwick = { warwick = {
type = "physical"; type = "physical";
arch = "aarch64-linux"; arch = "aarch64-linux";
isRaspberryPi = true; isRaspberryPi = true;
isHypervisor = false;
isVirtualMachine = false;
nixosModule.lab = { nixosModule.lab = {
storage = { storage = {
@ -18,9 +70,7 @@
atlas = { atlas = {
type = "physical"; type = "physical";
arch = "x86_64-linux"; arch = "x86_64-linux";
isRaspberryPi = false;
isHypervisor = true; isHypervisor = true;
isVirtualMachine = false;
nixosModule.lab = { nixosModule.lab = {
storage = { storage = {
@ -39,9 +89,7 @@
jefke = { jefke = {
type = "physical"; type = "physical";
arch = "x86_64-linux"; arch = "x86_64-linux";
isRaspberryPi = false;
isHypervisor = true; isHypervisor = true;
isVirtualMachine = false;
nixosModule.lab = { nixosModule.lab = {
storage = { storage = {
@ -60,9 +108,7 @@
lewis = { lewis = {
type = "physical"; type = "physical";
arch = "x86_64-linux"; arch = "x86_64-linux";
isRaspberryPi = false;
isHypervisor = true; isHypervisor = true;
isVirtualMachine = false;
nixosModule.lab = { nixosModule.lab = {
backups.enable = true; backups.enable = true;
@ -85,22 +131,22 @@
hermes = { hermes = {
type = "virtual"; type = "virtual";
hypervisorName = "lewis"; hypervisorName = "lewis";
isRaspberryPi = false;
isVirtualMachine = true;
isHypervisor = false;
nixosModule = { config, ... }: { nixosModule = { config, ... }: {
lab = { lab = {
networking = { networking = {
dmz.services.enable = true; dmz.services.enable = true;
staticNetworking = true; staticNetworking = true;
staticIPv4 = config.lab.networking.dmz.ipv4.services; # TODO: This seems to cause infinite recursion? Really weird.
staticIPv6 = config.lab.networking.dmz.ipv6.services; # staticIPv4 = config.lab.networking.dmz.ipv4.services;
# staticIPv6 = config.lab.networking.dmz.ipv6.services;
staticIPv4 = "192.168.30.7";
staticIPv6 = "2a0d:6e00:1a77:30::7";
}; };
vm = { vm = {
# TODO: would be cool to create a check that a mac address is only ever assigned to one VM. # # TODO: would be cool to create a check that a mac address is only ever assigned to one VM.
# TODO: idea: what if we generated these IDs by hashing the host name and reducing that to the amount of hosts possible? # # TODO: idea: what if we generated these IDs by hashing the host name and reducing that to the amount of hosts possible?
id = 7; id = 7;
shares = [{ shares = [{
@ -115,9 +161,6 @@
maestro = { maestro = {
type = "virtual"; type = "virtual";
hypervisorName = "atlas"; hypervisorName = "atlas";
isRaspberryPi = false;
isVirtualMachine = false;
isHypervisor = false;
nixosModule = { config, ... }: { nixosModule = { config, ... }: {
microvm.balloonMem = 7680; microvm.balloonMem = 7680;
@ -135,9 +178,6 @@
bancomart = { bancomart = {
type = "virtual"; type = "virtual";
hypervisorName = "jefke"; hypervisorName = "jefke";
isRaspberryPi = false;
isVirtualMachine = false;
isHypervisor = false;
nixosModule = { nixosModule = {
microvm.balloonMem = 7680; microvm.balloonMem = 7680;
@ -152,9 +192,6 @@
vpay = { vpay = {
type = "virtual"; type = "virtual";
hypervisorName = "lewis"; hypervisorName = "lewis";
isRaspberryPi = false;
isVirtualMachine = false;
isHypervisor = false;
nixosModule = { nixosModule = {
microvm.balloonMem = 5120; microvm.balloonMem = 5120;
@ -165,4 +202,6 @@
}; };
}; };
}; };
};
};
} }

View file

@ -117,7 +117,7 @@ in {
networkConfig.Bridge = cfg.dmz.bridgeName; networkConfig.Bridge = cfg.dmz.bridgeName;
}; };
}) })
(lib.optionalAttrs machine.isVirtualMachine { (lib.optionalAttrs (machine.type == "virtual") {
"30-main-nic" = { "30-main-nic" = {
matchConfig.Name = "en*"; matchConfig.Name = "en*";