set hostName and domain in nixos config directly

This commit is contained in:
Pim Kunis 2024-01-28 12:55:58 +01:00
parent 371b851ca5
commit bd5dc4b883
5 changed files with 55 additions and 45 deletions

View file

@ -40,18 +40,19 @@
pkgs-unstable = nixpkgs-unstable.legacyPackages.${system}; pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
machines = import ./nixos/machines; machines = import ./nixos/machines;
physicalMachines = lib.filterAttrs (n: v: v.type == "physical") machines; physicalMachines = lib.filterAttrs (n: v: v.type == "physical") machines;
# TODO: Maybe use mergeAttrLists # TODO: Use mergeAttrLists
mkNixosSystems = systemDef: mkNixosSystems = systemDef:
nixpkgs.lib.foldlAttrs nixpkgs.lib.foldlAttrs
(acc: name: machine: (acc: name: machine:
acc // { acc // {
"${name}" = nixpkgs.lib.nixosSystem (systemDef machine); "${name}" = nixpkgs.lib.nixosSystem (systemDef name machine);
}) })
{ } { }
physicalMachines; physicalMachines;
mkDeployNodes = nodeDef: mkDeployNodes = nodeDef:
# TODO: Use mergeAttrLists
nixpkgs.lib.foldlAttrs nixpkgs.lib.foldlAttrs
(acc: name: machine: acc // { "${name}" = nodeDef machine; }) (acc: name: machine: acc // { "${name}" = nodeDef name machine; })
{ } { }
physicalMachines; physicalMachines;
in in
@ -75,7 +76,7 @@
formatter.${system} = pkgs.nixfmt; formatter.${system} = pkgs.nixfmt;
nixosConfigurations = mkNixosSystems (machine: { nixosConfigurations = mkNixosSystems (name: machine: {
inherit system; inherit system;
specialArgs = { inherit machines machine kubenix dns microvm disko agenix; }; specialArgs = { inherit machines machine kubenix dns microvm disko agenix; };
modules = [ ./nixos ]; modules = [ ./nixos ];
@ -85,12 +86,11 @@
sshUser = "root"; sshUser = "root";
user = "root"; user = "root";
nodes = mkDeployNodes (machine: { nodes = mkDeployNodes (name: machine: {
# TODO: simply get this from nixos configuration? hostname = self.nixosConfigurations.${name}.config.networking.fqdn;
hostname = "${machine.hostName}.${machine.domain}";
profiles.system = { profiles.system = {
path = deploy-rs.lib.${system}.activate.nixos path = deploy-rs.lib.${system}.activate.nixos
self.nixosConfigurations.${machine.hostName}; self.nixosConfigurations.${name};
}; };
}); });
}; };

View file

@ -1,35 +1,44 @@
{ {
jefke = { jefke = {
# TODO: directly set and read this from nixos config. # TODO: model this in a nixos module?
hostName = "jefke";
domain = "hyp";
type = "physical"; type = "physical";
nixosModule.lab = { nixosModule = {
terraformDatabase.enable = true; networking = {
hostName = "jefke";
storage = { # TODO: generate this using type?
osDisk = "/dev/sda"; domain = "hyp";
dataPartition = "/dev/nvme0n1p1";
}; };
ssh = { lab = {
useCertificates = true; terraformDatabase.enable = true;
# TODO: automatically set this?
hostCert = builtins.readFile ./jefke_host_ed25519-cert.pub; storage = {
userCert = builtins.readFile ./jefke_user_ed25519-cert.pub; osDisk = "/dev/sda";
dataPartition = "/dev/nvme0n1p1";
};
ssh = {
useCertificates = true;
# TODO: automatically set this?
hostCert = builtins.readFile ./jefke_host_ed25519-cert.pub;
userCert = builtins.readFile ./jefke_user_ed25519-cert.pub;
};
}; };
}; };
}; };
atlas = { atlas = {
hostName = "atlas";
domain = "hyp";
type = "physical"; type = "physical";
nixosModule = { config, ... }: nixosModule = { config, ... }:
let inherit (config.lab.networking) dmzServicesIPv4 dmzServicesIPv6; in let inherit (config.lab.networking) dmzServicesIPv4 dmzServicesIPv6; in
{ {
networking = {
hostName = "atlas";
domain = "hyp";
};
lab = { lab = {
networking = { networking = {
# TODO: Ideally, we don't have to set this here. # TODO: Ideally, we don't have to set this here.
@ -53,11 +62,14 @@
}; };
lewis = { lewis = {
hostName = "lewis";
domain = "hyp";
type = "physical"; type = "physical";
nixosModule = { pkgs, ... }: { nixosModule = { pkgs, ... }: {
networking = {
hostName = "lewis";
domain = "hyp";
};
lab = { lab = {
dataHost.enable = true; dataHost.enable = true;
@ -76,8 +88,6 @@
}; };
my-microvm = { my-microvm = {
hostName = "my-microvm";
domain = "dmz";
type = "virtual"; type = "virtual";
hypervisorName = "lewis"; hypervisorName = "lewis";
@ -86,6 +96,11 @@
echo "Hello world from inside a virtual machine!!" | ${pkgs.lolcat}/bin/lolcat echo "Hello world from inside a virtual machine!!" | ${pkgs.lolcat}/bin/lolcat
''; '';
networking = {
hostName = "my-microvm";
domain = "dmz";
};
lab.vmMacAddress = "BA:DB:EE:F0:00:00"; lab.vmMacAddress = "BA:DB:EE:F0:00:00";
}; };
}; };

View file

@ -89,8 +89,6 @@ in {
config = { config = {
networking = { networking = {
hostName = machine.hostName;
domain = machine.domain;
nftables.enable = true; nftables.enable = true;
useDHCP = machine.type == "virtual"; useDHCP = machine.type == "virtual";

View file

@ -58,23 +58,20 @@
microvm.vms = microvm.vms =
let let
vmsForHypervisor = lib.attrValues (lib.filterAttrs (n: v: v.type == "virtual" && v.hypervisorName == machine.hostName) machines); vmsForHypervisor = lib.filterAttrs (n: v: v.type == "virtual" && v.hypervisorName == config.networking.hostName) machines;
in in
lib.attrsets.mergeAttrsList (map builtins.mapAttrs
(vm: (name: vm:
{ {
"${vm.hostName}" = { # TODO Simplify?
# TODO Simplify? specialArgs = { inherit agenix disko pkgs lib microvm; machine = vm; hypervisorConfig = config; };
specialArgs = { inherit agenix disko pkgs lib microvm; machine = vm; hypervisorConfig = config; }; config = {
config = { imports = [
imports = [ ./.
./. ];
];
};
}; };
} }
) )
vmsForHypervisor vmsForHypervisor;
);
}; };
} }

View file

@ -1,4 +1,4 @@
{ lib, config, machine, hypervisorConfig, ... }: { { lib, config, hypervisorConfig, ... }: {
options.lab.vmMacAddress = lib.mkOption { options.lab.vmMacAddress = lib.mkOption {
type = lib.types.str; type = lib.types.str;
description = '' description = ''
@ -19,7 +19,7 @@
interfaces = [{ interfaces = [{
type = "tap"; type = "tap";
id = "vm-${machine.hostName}"; id = "vm-${config.networking.hostName}";
mac = config.lab.vmMacAddress; mac = config.lab.vmMacAddress;
}]; }];
}; };