I am currently using pkgs.nixosTest for testing my application in the context of a nixos install. I would love to re-use this infrastructure on a different VM machine image (for e.g. debian). The problem is, due to the way that nix works everything is very indirect and I have a hard time finding out how it exactly works.
So my question would be, how exactly does pkgs.nixosTest build up a derivation that ultimately calls QEMU and what kind of image does it use (and how could it be changed?).
What you really want is probably pkgs.vmTest, a low-level function (that does not exist yet) which takes as inputs: a test script, a QCOW2 image, maybe QEMU options or some smart NixOS module evaluation with only the virtualisation part and do the right thing, i.e. assemble the test driver for this test.
If you want support of certain test driver features, a instrumentation mechanism has to be baked into the guest (QEMU Guest Agent, backdoor.service, etc.), this could be declared by the user or asserted in the test driver.
If you don’t need it, virtually, any disk image can be used and ~“read-only” tests can be performed via grepping the console text if it does any serial output.
The patch has been merged upstream in the meantime, so with nixos-unstable or nixos-23.05 it works out of the box.
For the VM test framework to work, you still need some things to be set up in the guest, most importantly the backdoor service that allows the test-driver to communicate with a root shell on the VM. I’ve done the required bits for ubuntu cloud images, and I would like to generalise this further to other distros at some point.
VLANs are not working yet in the code in system-manager, since they also require setup on the guest side and I haven’t had the time to implement this yet.
I have been meaning to write all of this up in a blog post, hopefully I’ll find the time for that sometime soon.