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; lib = pkgs.lib;
pkgs-unstable = nixpkgs-unstable.legacyPackages.${controllerArch}; pkgs-unstable = nixpkgs-unstable.legacyPackages.${controllerArch};
machines = (lib.modules.evalModules { modules = [ (import ./nixos/machines) ]; }).config.machines; 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: mkNixosSystems = systemDef:
builtins.mapAttrs builtins.mapAttrs
(name: machine: (name: machine:

View file

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

View file

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

View file

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

View file

@ -57,7 +57,7 @@
microvm.vms = microvm.vms =
let 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 in
builtins.mapAttrs builtins.mapAttrs
(name: vm: (name: vm:

View file

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