Add virtualisation.vmVariantWithDisko option

This commit is contained in:
Michael Hoang 2024-09-08 02:35:08 +10:00
parent e55f9a8678
commit ba66c22ec1
4 changed files with 63 additions and 46 deletions

View file

@ -12,6 +12,16 @@ afterwards you can run the interactive VM with:
result/bin/disko-vm result/bin/disko-vm
``` ```
You can configure the VM using the `virtualisation.vmVariantWithDisko` NixOS option:
```nix
{
virtualisation.vmVariantWithDisko = {
virtualisation.fileSystems."/persist".neededForBoot = true;
};
}
```
extraConfig that is set in disko.tests.extraConfig is also applied to the interactive VMs. extraConfig that is set in disko.tests.extraConfig is also applied to the interactive VMs.
imageSize of the VMs will be determined by the imageSize in the disk type in your disko config. imageSize of the VMs will be determined by the imageSize in the disk type in your disko config.
memorySize is set by disko.memSize memorySize is set by disko.memSize

View file

@ -16,7 +16,7 @@ let
# a version of makeDiskImage which runs outside of the store # a version of makeDiskImage which runs outside of the store
makeDiskImagesScript = args: (import ./make-disk-image.nix ({ inherit diskoLib; } // args)).impure; makeDiskImagesScript = args: (import ./make-disk-image.nix ({ inherit diskoLib; } // args)).impure;
makeVMRunner = args: (import ./interactive-vm.nix ({ inherit diskoLib; } // args)).pure; makeVMRunner = args: import ./interactive-vm.nix ({ inherit diskoLib; } // args);
testLib = import ./tests.nix { inherit lib makeTest eval-config; }; testLib = import ./tests.nix { inherit lib makeTest eval-config; };
# like lib.types.oneOf but instead of a list takes an attrset # like lib.types.oneOf but instead of a list takes an attrset

View file

@ -1,8 +1,6 @@
{ nixosConfig { nixosConfig
, diskoLib , diskoLib
, pkgs ? nixosConfig.pkgs , pkgs ? nixosConfig.pkgs
, name ? "${nixosConfig.config.networking.hostName}-disko-images"
, extraConfig ? { }
}: }:
let let
lib = pkgs.lib; lib = pkgs.lib;
@ -47,43 +45,40 @@ let
driveExtraOpts.werror = "report"; driveExtraOpts.werror = "report";
}) })
(builtins.tail disks); (builtins.tail disks);
vm = (nixosConfig.extendModules { in nixosConfig.extendModules {
modules = [ modules = [
({ modulesPath, ... }: { ({ modulesPath, config, pkgs, ... }: {
imports = [ imports = [
(modulesPath + "/virtualisation/qemu-vm.nix") (modulesPath + "/virtualisation/qemu-vm.nix")
]; ];
})
{ virtualisation.useEFIBoot = config.disko.tests.efi;
virtualisation.useEFIBoot = nixosConfig.config.disko.tests.efi; virtualisation.memorySize = config.disko.memSize;
virtualisation.memorySize = nixosConfig.config.disko.memSize; virtualisation.useDefaultFilesystems = false;
virtualisation.useDefaultFilesystems = false; virtualisation.diskImage = null;
virtualisation.diskImage = null; virtualisation.qemu.drives = [ rootDisk ] ++ otherDisks;
virtualisation.qemu.drives = [ rootDisk ] ++ otherDisks; boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms
boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms boot.zfs.forceImportAll = true;
boot.zfs.forceImportAll = true;
} system.build.vmWithDisko = pkgs.writers.writeDashBin "disko-vm" ''
{ set -efux
# generated from disko config export tmp=$(mktemp -d)
virtualisation.fileSystems = cfg_.disko.devices._config.fileSystems; trap 'rm -rf "$tmp"' EXIT
boot = cfg_.disko.devices._config.boot or { }; ${lib.concatMapStringsSep "\n" (disk: ''
swapDevices = cfg_.disko.devices._config.swapDevices or [ ]; ${pkgs.qemu}/bin/qemu-img create -f qcow2 \
} -b ${diskoImages}/${disk.name}.qcow2 \
nixosConfig.config.disko.tests.extraConfig -F qcow2 "$tmp"/${disk.name}.qcow2
]; '') disks}
}).config.system.build.vm; set +f
in ${config.system.build.vm}/bin/run-*-vm
{ '';
pure = pkgs.writers.writeDashBin "disko-vm" '' })
set -efux {
export tmp=$(mktemp -d) # generated from disko config
trap 'rm -rf "$tmp"' EXIT virtualisation.fileSystems = cfg_.disko.devices._config.fileSystems;
${lib.concatMapStringsSep "\n" (disk: '' boot = cfg_.disko.devices._config.boot or { };
${pkgs.qemu}/bin/qemu-img create -f qcow2 \ swapDevices = cfg_.disko.devices._config.swapDevices or [ ];
-b ${diskoImages}/${disk.name}.qcow2 \ }
-F qcow2 "$tmp"/${disk.name}.qcow2 nixosConfig.config.disko.tests.extraConfig
'') disks} ];
set +f
${vm}/bin/run-*-vm
'';
} }

View file

@ -7,6 +7,11 @@ let
eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix"); eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix");
}; };
cfg = config.disko; cfg = config.disko;
vmVariantWithDisko = diskoLib.makeVMRunner {
inherit pkgs;
nixosConfig = args;
};
in in
{ {
options.disko = { options.disko = {
@ -137,6 +142,16 @@ in
}; };
}; };
}; };
options.virtualisation.vmVariantWithDisko = lib.mkOption {
description = ''
Machine configuration to be added for the vm script available at `.system.build.vmWithDisko`.
'';
inherit (vmVariantWithDisko) type;
default = {};
visible = "shallow";
};
config = lib.mkIf (cfg.devices.disk != { }) { config = lib.mkIf (cfg.devices.disk != { }) {
system.build = (cfg.devices._scripts { inherit pkgs; checked = cfg.checkScripts; }) // { system.build = (cfg.devices._scripts { inherit pkgs; checked = cfg.checkScripts; }) // {
@ -161,10 +176,7 @@ in
extraTestScript = cfg.tests.extraChecks; extraTestScript = cfg.tests.extraChecks;
}; };
vmWithDisko = diskoLib.makeVMRunner { vmWithDisko = lib.mkDefault config.virtualisation.vmVariantWithDisko.system.build.vmWithDisko;
inherit pkgs;
nixosConfig = args;
};
}; };