From cf051abbdf011d4844d0dc7cf79a568830cb43fa Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 1 May 2024 00:27:07 +0200 Subject: [PATCH] Allow running the VM as root --- lib/config/custom-build-vm.nix | 98 +++++++++++++++++----------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/lib/config/custom-build-vm.nix b/lib/config/custom-build-vm.nix index 5f56e91..7acaae7 100644 --- a/lib/config/custom-build-vm.nix +++ b/lib/config/custom-build-vm.nix @@ -1,53 +1,53 @@ -{ config, lib, options, pkgs, ... }: - let - vmOptions = { }; - in { - options = { - virtualisation = { - vmVariant = vmOptions; - vmVariantWithBootLoader = vmOptions; +{ config, lib, options, pkgs, ... }: { + options = { + virtualisation = { + runAsRoot = lib.mkOption { + type = lib.types.bool; + default = false; }; }; + }; - config = { - # Replace native `qemu` with `remote-viewer` - system.build = - { - vm = - let - packageName = "custom-nixos-vm"; + config = { + # Replace native `qemu` with `remote-viewer` + system.build = + { + vm = + let + packageName = "custom-nixos-vm"; - mergedSystem = - with options.system; - lib.mergeDefinitions - build.loc - build.type - (lib.lists.forEach - ( - builtins.filter - (item: - !(lib.path.hasPrefix ./. (/. + item.file))) - build.definitionsWithLocations) - (item: { inherit (item) file value; })); - in - lib.mkForce ( - if (mergedSystem.mergedValue.vm.name == packageName) - then - mergedSystem.mergedValue.vm - else - let - wrapped = pkgs.writeShellApplication { - name = "run-${config.system.name}-vm"; - text = '' - ${mergedSystem.mergedValue.vm}/bin/run-${config.system.name}-vm - ''; - }; - in - # Rename package to `nixos-vm` - pkgs.symlinkJoin { - name = packageName; - paths = [ wrapped ]; - }); - }; - }; - } + mergedSystem = + with options.system; + lib.mergeDefinitions + build.loc + build.type + (lib.lists.forEach + ( + builtins.filter + (item: + !(lib.path.hasPrefix ./. (/. + item.file))) + build.definitionsWithLocations) + (item: { inherit (item) file value; })); + in + lib.mkForce ( + if (mergedSystem.mergedValue.vm.name == packageName) + then + mergedSystem.mergedValue.vm + else + let + prefix = lib.concatStringsSep " " (lib.optional config.virtualisation.runAsRoot "sudo"); + wrapped = pkgs.writeShellApplication { + name = "run-${config.system.name}-vm"; + text = '' + ${prefix} ${mergedSystem.mergedValue.vm}/bin/run-${config.system.name}-vm + ''; + }; + in + # Rename package to `nixos-vm` + pkgs.symlinkJoin { + name = packageName; + paths = [ wrapped ]; + }); + }; + }; +}