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?
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.
Add the following to /etc/nixos/configuration.nix:
mkdir -p vagrant/alpine38
cd vagrant/alpine38
nix-shell -p vagrant
vagrant init generic/alpine38
vagrant up
vagrant ssh
# play around inside Alpine Linux
exit
vagrant down
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.
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?
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 for not checking it more frequently and seeing they added libvirtd as a provider. Thanks for poisting this!)
Yes, but it’s a bit more involved than just running qemu-kvm… Just ran it on an Arch iso, but it hang indefinitely, and one will also have to read a lot on how to automate VM provisioning where one can just SSH into - whereas with Vagrant, it’s just a couple of commands. The whole topic is very confusing, and not sure where to even start; here a couple stack exchange questions for example: