1
1
Fork 1
mirror of https://github.com/oddlama/nix-config.git synced 2025-10-11 07:10:39 +02:00

chore: fix toplevel microvm attrset merging

This commit is contained in:
oddlama 2023-05-14 01:48:13 +02:00
parent 70f7ef3023
commit c03d1a1c8f
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A

View file

@ -24,30 +24,13 @@
mkIf mkIf
mkMerge mkMerge
mkOption mkOption
optionalAttrs optional
recursiveUpdate recursiveUpdate
types types
; ;
cfg = config.extra.microvms; cfg = config.extra.microvms;
# Base configuration required for the host
hostConfig = {
assertions = let
duplicateMacs = extraLib.duplicates (mapAttrsToList (_: vmCfg: vmCfg.mac) cfg);
in [
{
assertion = duplicateMacs == [];
message = "Duplicate MicroVM MAC addresses: ${concatStringsSep ", " duplicateMacs}";
}
];
microvm = {
declarativeUpdates = true;
restartIfChanged = true;
};
};
# Configuration for each microvm # Configuration for each microvm
microvmConfig = vmName: vmCfg: { microvmConfig = vmName: vmCfg: {
# Add the required datasets to the disko configuration of the machine # Add the required datasets to the disko configuration of the machine
@ -56,6 +39,11 @@
extraLib.disko.zfs.filesystem "${vmCfg.zfs.mountpoint}"; extraLib.disko.zfs.filesystem "${vmCfg.zfs.mountpoint}";
}; };
# TODO not cool, this might change or require more creation options.
# TODO better to only add disko and a mount point requirement.
# TODO the user can do the rest if required.
# TODO needed for boot false
# When installing a microvm, make sure that its persitent zfs dataset exists # When installing a microvm, make sure that its persitent zfs dataset exists
systemd.services."install-microvm-${vmName}".preStart = let systemd.services."install-microvm-${vmName}".preStart = let
poolDataset = "${vmCfg.zfs.pool}/${vmCfg.zfs.dataset}"; poolDataset = "${vmCfg.zfs.pool}/${vmCfg.zfs.dataset}";
@ -106,7 +94,7 @@
} }
] ]
# Mount persistent data from the host # Mount persistent data from the host
++ optionalAttrs vmCfg.zfs.enable { ++ optional vmCfg.zfs.enable {
source = vmCfg.zfs.mountpoint; source = vmCfg.zfs.mountpoint;
mountPoint = "/persist"; mountPoint = "/persist";
tag = "persist"; tag = "persist";
@ -114,7 +102,8 @@
}; };
}; };
fileSystems."/persist".neededForBoot = true; # FIXME this should be changed in microvm.nix to mkDefault instead of mkForce here
fileSystems."/persist".neededForBoot = mkForce true;
# Add a writable store overlay, but since this is always ephemeral # Add a writable store overlay, but since this is always ephemeral
# disable any store optimization from nix. # disable any store optimization from nix.
@ -148,7 +137,15 @@ in {
imports = [ imports = [
# Add the host module, but only enable if it necessary # Add the host module, but only enable if it necessary
microvm.host microvm.host
# This is opt-out, so we can't put this into the mkIf below
{microvm.host.enable = cfg != {};} {microvm.host.enable = cfg != {};}
# This module requires declarativeUpdates and restartIfChanged.
{
microvm = mkIf (cfg != {}) {
declarativeUpdates = true;
restartIfChanged = true;
};
}
]; ];
options.extra.microvms = mkOption { options.extra.microvms = mkOption {
@ -211,8 +208,21 @@ in {
}); });
}; };
config = config = mkIf (cfg != {}) (
mkIf (cfg != {}) {
(extraLib.mkMergeTopLevel ["assertions" "disko" "systemd" "microvm"] assertions = let
([hostConfig] ++ mapAttrsToList microvmConfig cfg)); duplicateMacs = extraLib.duplicates (mapAttrsToList (_: vmCfg: vmCfg.mac) cfg);
in [
{
assertion = duplicateMacs == [];
message = "Duplicate MicroVM MAC addresses: ${concatStringsSep ", " duplicateMacs}";
}
];
}
// lib.genAttrs ["disko" "microvm" "systemd"]
(attr:
mkMerge (map
(c: c.${attr})
(mapAttrsToList microvmConfig cfg)))
);
} }