add derived isPhysical and isVirtual values for machines

This commit is contained in:
Pim Kunis 2024-02-29 20:51:52 +01:00
parent 960d3f6075
commit 0fe9aef9f5
6 changed files with 20 additions and 9 deletions

View file

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

View file

@ -6,8 +6,8 @@
disko.nixosModules.disko
agenix.nixosModules.default
]
++ lib.lists.optional (machine.kind == "physical") ./physical.nix
++ lib.lists.optional (machine.kind == "virtual") ./virtual;
++ lib.lists.optional machine.isPhysical ./physical.nix
++ lib.lists.optional machine.isVirtual ./virtual;
config = {
time.timeZone = "Europe/Amsterdam";

View file

@ -1,8 +1,7 @@
{ lib, ... }:
let
machineOpts = { ... }: {
machineOpts = { config, ... }: {
options = {
# TODO: rename to kind?
kind = lib.mkOption {
type = lib.types.enum [ "physical" "virtual" ];
description = ''
@ -36,6 +35,18 @@ let
type = lib.types.bool;
};
# Derived value
isPhysical = lib.mkOption {
default = config.kind == "physical";
type = lib.types.bool;
};
# Derived value
isVirtual = lib.mkOption {
default = config.kind == "virtual";
type = lib.types.bool;
};
nixosModule = lib.mkOption {
default = { ... }: { };
type = lib.types.anything;

View file

@ -48,7 +48,7 @@ in {
config = {
networking = {
domain = if machine.kind == "physical" then "hyp" else "dmz";
domain = if machine.isPhysical then "hyp" else "dmz";
nftables.enable = true;
useDHCP = false;
@ -117,7 +117,7 @@ in {
networkConfig.Bridge = cfg.dmz.bridgeName;
};
})
(lib.optionalAttrs (machine.kind == "virtual") {
(lib.optionalAttrs machine.isVirtual {
"30-main-nic" = {
matchConfig.Name = "en*";

View file

@ -57,7 +57,7 @@
microvm.vms =
let
vmsForHypervisor = lib.filterAttrs (n: v: v.kind == "virtual" && v.hypervisorName == config.networking.hostName) machines;
vmsForHypervisor = lib.filterAttrs (n: v: v.isVirtual && v.hypervisorName == config.networking.hostName) machines;
in
builtins.mapAttrs
(name: vm:

View file

@ -12,7 +12,7 @@ in
};
config = lib.mkIf cfg.enable {
lab.vm.shares = lib.mkIf (machine.kind == "virtual") [{
lab.vm.shares = lib.mkIf machine.isVirtual [{
name = "docker";
mountPoint = "/var/lib/docker";
}];