In this tutorial we’ll create a NixOS virtual machine using Vagrant. This setup can be useful to setup an environment for learning NixOS. NixOS is a Linux distribution which is declaratively configured using a specification written in the Nix expression language describing the desired state of the system. It supports automatic upgrades and rollbacks. If something goes wrong, just revert to the working state. NixOS runs on common computers and architectures, and can be used both on laptops or workstations with a desktop environment, or on servers, both x86_64 and ARM64, like on the Raspberry Pi 3.

We’ll be using NixOS 19.09 Loris in this tutorial.

This tutorial assumes you have a working installation of macOS or Ubuntu running on a Mac or other client computer, and that you have VirtualBox installed with the VirtualBox Extension Pack. We’ll use the Nix community project nixbox to build our Vagrant box (please note that nixbox currently does not support ARM64, I’ll try to submit a PR to add this support if I can find the time).

Install Vagrant and Packer

These instructions are different depending on your client OS.

On macOS, install Homebrew and install Vagrant and Packer:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install vagrant packer

On Ubuntu:

$ sudo apt install vagrant packer

Create the NixOS Vagrant Box

Clone the nixbox Git repository:

$ git clone https://github.com/nix-community/nixbox.git

Build the NixOS Vagrant box:

$ packer build --only=virtualbox-iso nixos-x86_64.json

This will download (538.00 MiB) the latest version of NixOS (at the time of writing version 19.09 Loris) and then use it to build the Vagrant box for the VirtualBox provider. nixbox also supports QEMU/libvirt and VMware as providers.

Add the NixOS Boxes to Vagrant

$ vagrant box add nixbox64 packer_virtualbox-iso_virtualbox.box

Start NixOS in Vagrant

To create a new Vagrantfile with NixOS as the image, simply run:

$ vagrant init nixbox64

Next, we’ll need to add a line to our Vagrantfile to disable syncing the current directory to the Vagrant virtual machine (otherwise boot will likely fail):

$ vim Vagrantfile

Add this line under the line config.vm.box = "nixbox64":

config.vm.synced_folder ".", "/vagrant", disabled: true

Save and exit.

Finally, start the Vagrant virtual machine:

$ vagrant up

You can enter your Vagrant NixOs virtual machine by running:

$ vagrant ssh

Last Words

That’s all! To learn how to configure and use NixOS (it’s quite different from other distributions), see the Nix and NixOS documentation. I’ve had a really exciting time experimenting with NixOS and I’ll most likely publish more tutorials on this interesting Linux distribution.

When this article was published macOS Catalina had already been released. I’ll update the site with instructions on how to run Catalina as well. I’ll also post a tutorial on using Ansible to configure macOS.

If you’d like to learn more Vagrant, I’d recommend their documentation, or reading a book or two:

Have fun learning NixOS! 😀