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