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?

1 Like

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.

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.

1 Like

There’s a mistake here:


should be

virtualisation.libvirtd.enable = true;

Unfortunately I can’t edit the original any more.