Quickly spin up a VM containing non-NixOS

The internet has much to say on the topic of Nix and virtualization, too much for me to find a quick recipe for my simple use case: I am running NixOS and want to explore the process of installing the nix package manager as a non-nix-user.

So I would like to spin up a VM running a fairly minimal installation of some Linux distribution, something like alpine or arch with working network connection and a non-root user account.

Is there a quick way of achieving this?

Does virtualization need to be enabled in the system configuration.nix, or can this all be done (perhaps in home-manager) as an unprivileged user?


I just use Vagrant when I want to spin up a simple VM of another distro on the fly.

If you enable libvirtd (virtualisation.libvirtd.enable = true;) you can use GNOME Boxes to run VMs with a low-effort clicky wizard, if that’s your kind of thing. I’m not aware of declarative options for specifying VMs.

1 Like

The following seems to be the simplest sequence of actions that I have found. (I’m not 100% certain that this is self-contained, as I might have mutated some state during my exploration which has escaped my notice.) It does require virtualisation to be enabled at the system level. Everything else can be done at the user level.

  1. Add the following to /etc/nixos/configuration.nix:

    boot.kernelModules = [ "kvm-amd" "kvm-intel" ];
  2. sudo nixos-rebuild switch

  3. mkdir -p vagrant/alpine38
    cd vagrant/alpine38
    nix-shell -p vagrant
    vagrant init generic/alpine38
    vagrant up
    vagrant ssh
    # play around inside Alpine Linux
    vagrant down
  4. If you want to reset the Alpine machine to a blank slate, all it takes is (from inside the vagrant/alpine38 directory:

    vagrant destroy
    vagrant up

Please let me know if you spot any mistakes, or a way to simplify the process.


There’s a mistake here:


should be

virtualisation.libvirtd.enable = true;

Unfortunately I can’t edit the original any more.

The above worked on 20.03. I am now on 20.09 and pretty much any use of the vagrant command errors with

Error while connecting to Libvirt: Error making a connection to libvirt URI qemu:///system?no_verify=1&keyfile=/home/myuser/.ssh/id_rsa:
Call to virConnectOpen failed: authentication unavailable: no polkit agent available to authenticate action 'org.libvirt.unix.manage'

Admittedly, I haven’t used this for months, so maybe I have made some other relevant changes to my system, but the biggest change is the upgrade from NixOS 20.03 to 20.09.

Does this ring any bells with anyone? Any suggestions on what to try to resolve it?

Adding “libvirtd” to the extraGroups of my user in configuration.nix seems
to have solved my problem:

users.users.<myuser>.extraGroups = [ "libvirtd" ];

I just tried this and it all worked, except for this line:

on Vagrant 2.2.14 the down command is not available, but vagrant halt did it.

(I used vagrant circa 2013, when the default was virtualbox, so I’m both happy that it works with libvirt and :man_facepalming: for not checking it more frequently and seeing they added libvirtd as a provider. Thanks for poisting this!)