Skip to main content

Run NixOS using Vagrant

·

In this tutorial, we’ll create a NixOS virtual machine using Vagrant. This setup can be helpful to set up an environment for learning NixOS. NixOS is a Linux distribution declaratively configured using a specification written in the Nix expression language describing the system’s desired state. 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 4 .

This tutorial will use NixOS 19.09 Loris.

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 pretty different from other distributions), see the Nix and NixOS documentation. I’ve had a fascinating time experimenting with NixOS, and I’ll likely publish more tutorials on this interesting Linux distribution.

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

Have fun learning NixOS! 😀

Revision #

2023-08-31 Revised language, remove unnecessary paragraphs