Merge pull request #766 from Enzime/disko-vm

Add `virtualisation.vmVariantWithDisko` option
This commit is contained in:
lassulus 2024-09-12 08:57:52 +01:00 committed by GitHub
commit 495c2d7673
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 57 additions and 42 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,8 +16,6 @@ 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;
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
# uses the field "type" to find the correct type in the attrset # uses the field "type" to find the correct type in the attrset

View file

@ -1,12 +1,9 @@
{ nixosConfig # We need to specify extendModules here to ensure that it is available
, diskoLib # in args for makeDiskImages
, pkgs ? nixosConfig.pkgs { diskoLib, modulesPath, config, pkgs, lib, extendModules, ... }@args:
, name ? "${nixosConfig.config.networking.hostName}-disko-images"
, extraConfig ? { }
}:
let let
lib = pkgs.lib; vm_disko = (diskoLib.testLib.prepareDiskoConfig config diskoLib.testLib.devices).disko;
vm_disko = (diskoLib.testLib.prepareDiskoConfig nixosConfig.config diskoLib.testLib.devices).disko;
cfg_ = (lib.evalModules { cfg_ = (lib.evalModules {
modules = lib.singleton { modules = lib.singleton {
# _file = toString input; # _file = toString input;
@ -24,7 +21,7 @@ let
}).config; }).config;
disks = lib.attrValues cfg_.disko.devices.disk; disks = lib.attrValues cfg_.disko.devices.disk;
diskoImages = diskoLib.makeDiskImages { diskoImages = diskoLib.makeDiskImages {
nixosConfig = nixosConfig; nixosConfig = args;
copyNixStore = false; copyNixStore = false;
extraConfig = { extraConfig = {
disko.devices = cfg_.disko.devices; disko.devices = cfg_.disko.devices;
@ -47,34 +44,29 @@ let
driveExtraOpts.werror = "report"; driveExtraOpts.werror = "report";
}) })
(builtins.tail disks); (builtins.tail disks);
vm = (nixosConfig.extendModules {
modules = [ diskoBasedConfiguration = {
({ modulesPath, ... }: { # generated from disko config
imports = [ virtualisation.fileSystems = cfg_.disko.devices._config.fileSystems;
(modulesPath + "/virtualisation/qemu-vm.nix") boot = cfg_.disko.devices._config.boot or { };
]; swapDevices = cfg_.disko.devices._config.swapDevices or [ ];
}) };
{
virtualisation.useEFIBoot = nixosConfig.config.disko.tests.efi;
virtualisation.memorySize = nixosConfig.config.disko.memSize;
virtualisation.useDefaultFilesystems = false;
virtualisation.diskImage = null;
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.forceImportAll = true;
}
{
# generated from disko config
virtualisation.fileSystems = cfg_.disko.devices._config.fileSystems;
boot = cfg_.disko.devices._config.boot or { };
swapDevices = cfg_.disko.devices._config.swapDevices or [ ];
}
nixosConfig.config.disko.tests.extraConfig
];
}).config.system.build.vm;
in in
{ {
pure = pkgs.writers.writeDashBin "disko-vm" '' imports = [
(modulesPath + "/virtualisation/qemu-vm.nix")
diskoBasedConfiguration
];
virtualisation.useEFIBoot = config.disko.tests.efi;
virtualisation.memorySize = config.disko.memSize;
virtualisation.useDefaultFilesystems = false;
virtualisation.diskImage = null;
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.forceImportAll = true;
system.build.vmWithDisko = pkgs.writers.writeDashBin "disko-vm" ''
set -efux set -efux
export tmp=$(mktemp -d) export tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT trap 'rm -rf "$tmp"' EXIT
@ -84,6 +76,6 @@ in
-F qcow2 "$tmp"/${disk.name}.qcow2 -F qcow2 "$tmp"/${disk.name}.qcow2
'') disks} '') disks}
set +f set +f
${vm}/bin/run-*-vm ${config.system.build.vm}/bin/run-*-vm
''; '';
} }

View file

@ -7,6 +7,14 @@ 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 = extendModules {
modules = [
./lib/interactive-vm.nix
{ _module.args = { inherit diskoLib; }; }
config.disko.tests.extraConfig
];
};
in in
{ {
options.disko = { options.disko = {
@ -137,6 +145,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 +179,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;
};
}; };