NixOs box for Vagrant
Recently I’ve become interested in the NixOS project. While still young with some rough edges it seems like something that has the potential to replace the need for Configuration Mangement systems (like Puppet, Ansible, SaltStack, etc…).
It may also provide a very powerfull development and testing subsitute environment for language mangement toolkits (like rbenv, pyenv, plenv, etc..), for any language and native libraries.
It could also replace the library management tools (like Ruby’s bundler, Perl’s Carton, etc..) and even potentially provide a way to handle the package menagement systems (Perl’s CPAN, Ruby’s Gems, NodeJS’s NPM, etc…) in a similar fashion by Writing Nix Expressions.
This is a big project with big goals. Check it out at http://nixos.org/, if you’re interested.
This article is a writeup of a (mostly) working process to build a Vagrant boxes (running on VirtualBox) using Packer so that I have and environment to play around with the OS.
Short description
- Install git (not covered in the article)
- Install cURL (not covered in the article)
- Install Vagrant.
- Install VirtualBox.
- Install Packer.
- Create a Packer template.
- Build the Vagrant box.
- Import the box into Vagrant.
- Test things out.
The Details
To install Vagrant visit the download page and follow the directions for your platform.
To install VirtualBox visit the download page and follow the directions for your platform.
To install Packer visit the download page and follow the directions for your platform.
While the Vagrant and VirtualBox installers seems to have configured the binaries to be in the path, Packer did not.
To add the packer environment to your run time path:
echo "PACKER_HOME=/path/to/packer/install" >> ~/.bash_profile
echo "export PATH="$PATH:$PACKER_HOME/bin" >> ~/.bash_profile
Then reload the environment (by souring the configuration or opening another shell).
After some investigation I ran across a set of Packer templates for NixOS located at: https://github.com/oxdi/nixos. This package requires the use of a Vagrant plugin - vagrant-nixos.
To install the plugin:
vagrant plugin install vagrant-nixos
Next downlod the Packer templates:
git clone https://github.com/oxdi/nixos.git
Then try building the system:
cd nixos
packer build template.json
The build first failed because the template is pointing to an out of date ISO. I changed the following lines in the template.json:
{
"builders": [
{
"iso_checksum": "48395b4bd21675703c87cfae1feb68ed",
"iso_checksum_type": "md5",
"iso_url": "http://releases.nixos.org/nixos/unstable/nixos-14.02pre38596.1dd0e05/nixos-minimal-14.02pre38596.1dd0e05-x86_64-linux.iso",
}],
"post-processors": [
{
"type": "vagrant",
"output": "nixos-14.02-x86_64-{{.Provider}}.box"
}
]
}
to the following:
{
"builders": [{
"iso_checksum": "860db3ea550f2f2cf2a9b2dd55f0849a9a35365536fff594cd723ae811ee50f2",
"iso_checksum_type": "sha256",
"iso_url": "http://releases.nixos.org/nixos/14.12/nixos-14.12.801.221101a/nixos-minimal-14.12.801.221101a-x86_64-linux.iso",
}],
"post-processors": [
{
"type": "vagrant",
"output": "nixos-14.12-x86_64-{{.Provider}}.box"
}
]
}
in the builders
and post-processors
sections, using the the information from the NixOS release sites. Since I simply want to test the using the latest stable build I chose to update to the latest 14.12 (at the time of this writing).
The build still failed with errors similar to the following:
virtualbox-iso: error: file ‘nixpkgs/nixos/modules/installer/tools/nixos-rebuild.sh’ was not found in the Nix search path (add it using $NIX_PATH or -I)
virtualbox-iso: building Nix...
virtualbox-iso: error: file ‘nixpkgs/nixos’ was not found in the Nix search path (add it using $NIX_PATH or -I)
virtualbox-iso: error: file ‘nixpkgs/nixos’ was not found in the Nix search path (add it using $NIX_PATH or -I)
virtualbox-iso: error: file ‘nixpkgs’ was not found in the Nix search path (add it using $NIX_PATH or -I)
After a bit of investigation the issues turned out to be the following line in the scripts/postinstall.sh script:
nix-channel --remove nixos
I assume that this was an attempt to provide a clean base box. Since I’m just need a box to play with I commented this out, and it seems to allow the build to complete.
Next install the box:
vagrant box add nixos-14.12-x86_64 nixos-14.12-x86_64-virtualbox.box
And create a Vagrant environment:
mkdir vagrant-test && cd vagrant-test
vagrant init
Then change the following in the created Vagrantfile
from:
config.vm.box = "base"
to:
config.vm.box = "nixos-14.12-x86_64"
save the file and fire it up and test out the VM:
vagrant up
vagrant ssh
uname -a
nixos-version
exit
vagrant halt
which should display something similar to the following:
Linux nixos 3.14.47 #1-NixOS SMP Thu Jan 1 00:00:01 UTC 1970 x86_64 GNU/Linux
14.12.801.221101a (Caterpillar)
I’ve had quite a bit of experience with the tools invovled (bascially everything mentioned except NixOs), getting this far took me around 5 hours.