NixOSConfig/lib/config/custom-build-vm.nix

65 lines
1.8 KiB
Nix
Raw Normal View History

2024-04-30 22:27:07 +00:00
{ config, lib, options, pkgs, ... }: {
options = {
virtualisation = {
runAsRoot = lib.mkOption {
type = lib.types.bool;
default = false;
};
};
2024-04-30 22:27:07 +00:00
};
2024-04-30 22:27:07 +00:00
config = {
2024-04-30 22:40:00 +00:00
virtualisation =
let
vmConfig = {
boot.loader.efi.efiSysMountPoint = lib.mkVMOverride "/boot";
};
in {
vmVariant = vmConfig;
vmVariantWithBootLoader = vmConfig;
};
2024-04-30 22:27:07 +00:00
# Replace native `qemu` with `remote-viewer`
system.build =
{
vm =
let
packageName = "custom-nixos-vm";
2024-04-30 22:27:07 +00:00
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 (
2024-04-30 22:43:41 +00:00
with { inherit (mergedSystem.mergedValue) vm; };
if (vm.name == packageName)
then
vm
else
let
prefix = lib.concatStringsSep " " (lib.optional config.virtualisation.runAsRoot "sudo");
wrapped = pkgs.writeShellApplication {
name = "run-${config.system.name}-vm";
text = ''
${prefix} ${vm}/bin/run-${config.system.name}-vm
'';
};
in
# Rename package to `nixos-vm`
pkgs.symlinkJoin {
name = packageName;
paths = [ wrapped ];
});
2024-04-30 22:27:07 +00:00
};
};
}