Improve readability of custom-build-vm code

This commit is contained in:
Manuel Thalmann 2024-05-01 15:06:31 +02:00
parent 3a2ce0ed87
commit ea620730a6

View file

@ -118,17 +118,21 @@ in {
};
};
virtualisation = config.virtualisation;
inherit (config.virtualisation)
vmVariant
vmVariantWithBootLoader
;
in {
vmVariant = extendVMConfig virtualisation.vmVariant;
vmVariantWithBootLoader = extendVMConfig virtualisation.vmVariantWithBootLoader;
vmVariant = extendVMConfig vmVariant;
vmVariantWithBootLoader = extendVMConfig vmVariantWithBootLoader;
};
system.build =
{
vm = lib.mkForce (
(
vm:
let
vm = vanillaVM;
in
if (vm.name == packageName)
then
vm
@ -141,45 +145,47 @@ in {
lib.concatStringsSep " " (
lib.optional config.virtualisation.qemu.runInBackground "&");
shellApp = pkgs.writeShellApplication {
vmRunner = pkgs.writeShellApplication {
name = "run-${config.system.name}-vm";
text = lib.strings.concatLines (
[
"${originalCommand} ${suffix}"
] ++ (
let
# Run `remote-viewer` as normal user to limit access
viewerPrefix = "sudo -Eu\"#$SUDO_UID\" ";
spice = config.virtualisation.qemu.spice;
in
(
lib.optionals
config.virtualisation.virt-viewer
config.virtualisation.virt-viewer (
let
spice = config.virtualisation.qemu.spice;
remoteAddress = "spice://${lib.escapeShellArg spice.bindAddress}:${toString spice.port}";
viewerPrefix = "sudo -Eu\"#$SUDO_UID\" ";
in
[
"${viewerPrefix}${pkgs.virt-viewer}/bin/remote-viewer spice://${lib.escapeShellArg spice.bindAddress}:${toString spice.port}"
"${viewerPrefix}${pkgs.virt-viewer}/bin/remote-viewer ${remoteAddress}"
# Kill QEMU after `remote-viewer` finished running
"kill %1"
])));
]))));
};
# Run VM as root if requested
wrapped =
if !config.virtualisation.runAsRoot
then
shellApp
vmRunner
else
pkgs.writeShellApplication {
name = shellApp.name;
inherit (vmRunner) name;
text = ''
sudo -E ${shellApp}/bin/${shellApp.name}
sudo -E "${vmRunner}/bin/${vmRunner.name}"
'';
};
in
pkgs.symlinkJoin {
name = packageName;
paths = [ wrapped ];
})
vanillaVM);
});
};
};
}