Allow running the VM as root

This commit is contained in:
Manuel Thalmann 2024-05-01 00:27:07 +02:00
parent 0480f0d4a4
commit cf051abbdf

View file

@ -1,53 +1,53 @@
{ config, lib, options, pkgs, ... }: { config, lib, options, pkgs, ... }: {
let options = {
vmOptions = { }; virtualisation = {
in { runAsRoot = lib.mkOption {
options = { type = lib.types.bool;
virtualisation = { default = false;
vmVariant = vmOptions;
vmVariantWithBootLoader = vmOptions;
}; };
}; };
};
config = { config = {
# Replace native `qemu` with `remote-viewer` # Replace native `qemu` with `remote-viewer`
system.build = system.build =
{ {
vm = vm =
let let
packageName = "custom-nixos-vm"; packageName = "custom-nixos-vm";
mergedSystem = mergedSystem =
with options.system; with options.system;
lib.mergeDefinitions lib.mergeDefinitions
build.loc build.loc
build.type build.type
(lib.lists.forEach (lib.lists.forEach
( (
builtins.filter builtins.filter
(item: (item:
!(lib.path.hasPrefix ./. (/. + item.file))) !(lib.path.hasPrefix ./. (/. + item.file)))
build.definitionsWithLocations) build.definitionsWithLocations)
(item: { inherit (item) file value; })); (item: { inherit (item) file value; }));
in in
lib.mkForce ( lib.mkForce (
if (mergedSystem.mergedValue.vm.name == packageName) if (mergedSystem.mergedValue.vm.name == packageName)
then then
mergedSystem.mergedValue.vm mergedSystem.mergedValue.vm
else else
let let
wrapped = pkgs.writeShellApplication { prefix = lib.concatStringsSep " " (lib.optional config.virtualisation.runAsRoot "sudo");
name = "run-${config.system.name}-vm"; wrapped = pkgs.writeShellApplication {
text = '' name = "run-${config.system.name}-vm";
${mergedSystem.mergedValue.vm}/bin/run-${config.system.name}-vm text = ''
''; ${prefix} ${mergedSystem.mergedValue.vm}/bin/run-${config.system.name}-vm
}; '';
in };
# Rename package to `nixos-vm` in
pkgs.symlinkJoin { # Rename package to `nixos-vm`
name = packageName; pkgs.symlinkJoin {
paths = [ wrapped ]; name = packageName;
}); paths = [ wrapped ];
}; });
}; };
} };
}