NixOS module for managing non NixOS QEMU vms

I’ve already used the containers module but:

  • it uses systemd-nspawn
  • containers are necessarily NixOS

These things are not bad, I really enjoy them but now I wanted to create a non-NixOS virtual machine and wanted to avoid systemd-nspawn, was thinking about QEMU (KVM) but it’s not a strict requirement.

Isnt’t there a NixOS module that let you declare QEMU virtual machines as the containers module (i.e. systemd unit files) and gives you a convenient CLI to imperatively interact with them?
I looked for it in nixpkgs without luck. Probably if I can’t find a better solution I’ll end up writing a systemd unit that executes my virtual machine but a well refined more general module that behaves like containers in nixpkgs would be great in my opinion.

2 Likes

Just to clarify, you want a VM with e.g. Debian in it, but build it from a Nix derivation and manage it via systemd on NixOS?

I don’t think there is a one-step solution yet, but starting from nixpkgs/qemu-vm.nix at e077b75a15b3cef3412909a985848580e0ae6232 · NixOS/nixpkgs · GitHub it should be possible to pass custom boot/root filesystems (e.g. a basic Debian) and afterwards connect to the VM to set it up using terminal commands (or Ansible, Bundlewrap, …)

(That is, you are not looking for this or this)

Just to clarify, you want a VM with e.g. Debian in it, but build it from a Nix derivation and manage it via systemd on NixOS?

Wait, build what with a derivation? If you mean building the vm image then no (how?). I mean a NixOS module that, given something like:

generic-containers = {
  foo = {
    memory = "2G";
    cpu = 2;
    disks = {
      bar = "/var/lib/foo/bar.qcow2";
    };
    cdrom = "debian-live.iso"; # you will be able to delete this line after you installed it
    # ...
  };
};

Creates a systemd service that “composes” the needed args for qemu. So, if there is a derivation involved, it’s the derivation used to build the unit file, but you wouldn’t explicitly write it.

It would be also interesting having a simple CLI util to stop/restart/list these containers, like nixos-container.

I’ve heard ideas of something like a libvirt module that would effectively be this, akin to the terraform module out there for libvirt.

That being said, without restricting what types of VMs are supported (like to Linux or Unix) post-provisioning VM interactions are difficult as console support for operating systems varies wildly. As I understand it, QubesOS has to do a decent amount to its templates to get the cross-vm capabilities on top of the Xen hypervisor. It’s definitely possible, but I’m not too sure it’d be worth the effort :sweat:

Maybe something more aligned would be “micro-vms” :thinking:? I’ve heard of some efforts like SpectrumOS out there in that vein

Ah, then you are probably looking for https://virt-manager.org/ to set up VMs.
To generate the launch command, a quick hack would be:

  1. Create a NixOS-VM with the desired properties (as in NixOS virtual machines — nix.dev documentation)
  2. Pick the launch script and patch the paths to the custom disk image
  3. Use systemd + virt-manager to start/stop the VM.