2024-05-01 00:07:12 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
|
|
|
let
|
|
|
|
packageName = "custom-nixos-vm";
|
|
|
|
|
|
|
|
mergedSystemOption =
|
|
|
|
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; }));
|
|
|
|
|
|
|
|
vanillaVM = mergedSystemOption.mergedValue.vm;
|
|
|
|
in {
|
2024-04-30 23:38:03 +00:00
|
|
|
options =
|
|
|
|
let
|
|
|
|
vmVariantOptions = {
|
|
|
|
virtualisation = {
|
|
|
|
runAsRoot = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
2024-04-30 22:50:02 +00:00
|
|
|
|
2024-04-30 23:38:03 +00:00
|
|
|
sharedHostKeys = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
2024-04-30 23:22:21 +00:00
|
|
|
|
2024-04-30 23:38:03 +00:00
|
|
|
qemu.runInBackground = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
2024-04-30 22:50:02 +00:00
|
|
|
};
|
2024-04-30 22:03:35 +00:00
|
|
|
};
|
2024-04-30 23:38:03 +00:00
|
|
|
in {
|
|
|
|
virtualisation = {
|
|
|
|
vmVariant = vmVariantOptions;
|
|
|
|
vmVariantWithBootLoader = vmVariantOptions;
|
|
|
|
};
|
2024-04-30 22:27:07 +00:00
|
|
|
};
|
2024-04-30 11:08:18 +00:00
|
|
|
|
2024-04-30 22:27:07 +00:00
|
|
|
config = {
|
2024-04-30 22:40:00 +00:00
|
|
|
virtualisation =
|
|
|
|
let
|
2024-04-30 23:22:21 +00:00
|
|
|
extendVMConfig =
|
|
|
|
vmVariant: {
|
|
|
|
boot.loader.efi.efiSysMountPoint = lib.mkVMOverride "/boot";
|
|
|
|
|
2024-04-30 23:38:03 +00:00
|
|
|
virtualisation = {
|
|
|
|
runAsRoot = lib.mkIf vmVariant.virtualisation.sharedHostKeys true;
|
|
|
|
|
|
|
|
sharedDirectories = lib.optionalAttrs (vmVariant.virtualisation.sharedHostKeys) {
|
|
|
|
hostKeys =
|
|
|
|
let
|
|
|
|
path = "/etc/ssh";
|
|
|
|
in {
|
|
|
|
source = path;
|
|
|
|
target = path;
|
|
|
|
};
|
2024-04-30 23:22:21 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
virtualisation = config.virtualisation;
|
2024-04-30 22:40:00 +00:00
|
|
|
in {
|
2024-04-30 23:22:21 +00:00
|
|
|
vmVariant = extendVMConfig virtualisation.vmVariant;
|
|
|
|
vmVariantWithBootLoader = extendVMConfig virtualisation.vmVariantWithBootLoader;
|
2024-04-30 22:40:00 +00:00
|
|
|
};
|
|
|
|
|
2024-04-30 22:27:07 +00:00
|
|
|
# Replace native `qemu` with `remote-viewer`
|
|
|
|
system.build =
|
|
|
|
{
|
2024-05-01 00:55:42 +00:00
|
|
|
vm = lib.mkForce (
|
|
|
|
(
|
|
|
|
vm:
|
|
|
|
if (vm.name == packageName)
|
|
|
|
then
|
|
|
|
vm
|
|
|
|
else
|
|
|
|
let
|
|
|
|
prefix =
|
|
|
|
lib.concatStringsSep " " (
|
|
|
|
lib.optionals config.virtualisation.runAsRoot (
|
|
|
|
["sudo"] ++
|
|
|
|
lib.optional config.virtualisation.qemu.runInBackground "-b"));
|
|
|
|
|
|
|
|
suffix =
|
|
|
|
lib.concatStringsSep " " (
|
|
|
|
lib.optional
|
|
|
|
(!config.virtualisation.runAsRoot && config.virtualisation.qemu.runInBackground)
|
|
|
|
"&");
|
|
|
|
|
|
|
|
wrapped = pkgs.writeShellApplication {
|
|
|
|
name = "run-${config.system.name}-vm";
|
|
|
|
text = ''
|
|
|
|
${prefix} ${vm}/bin/run-${config.system.name}-vm ${suffix}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
in
|
|
|
|
pkgs.symlinkJoin {
|
|
|
|
name = packageName;
|
|
|
|
paths = [ wrapped ];
|
|
|
|
})
|
|
|
|
vanillaVM);
|
2024-04-30 22:27:07 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|