Install VirtualBox on NixOS

2018-02-24

I use VirtualBox to do almost all of my development. I rarely install programming environments on my host machine. I have recently switched to using NixOS, so I needed to figure out how to install VirtualBox on NixOS.

Installing VirtualBox is relatively straight-forward on NixOS, but there are a few things to watch out for if you want to use the Oracle Extension Pack.

The following instructions are for NixOS version 18.03.

Installing VirtualBox normally

It is easy to install VirtualBox.

First, add the virtualisation.virtualbox.host.enable setting to your /etc/nixos/configuration.nix:

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      /etc/nixos/hardware-configuration.nix
    ];

  # Enable virtualbox.
  virtualisation.virtualbox.host.enable = true;

  ...
}

Then, rebuild:

$ sudo nixos-rebuild switch

This will install the VirtualBox executable and kernel modules.1

Running VirtualBox will bring up the VirtualBox application as expected.

Note that you do not need to add virtualbox to your environment.systemPackages. The virtualisation.virtualbox.host.enable setting will make sure this happens automatically.

Installing VirtualBox with the Oracle Extension Pack

(Note: these instructions are for NixOS 18.03. Instructions for 18.09 and up can be found here. Versions of NixOS before 18.03 may or may not work.)

Installing VirtualBox with the Oracle Extension Pack is a little more difficult.

First, you need to add the enableExtensionPack setting to your configuration.nix:

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      /etc/nixos/hardware-configuration.nix
    ];

  # Enable virtualbox.
  virtualisation.virtualbox.host.enable = true;

  # Enable the Oracle Extension Pack.
  nixpkgs.config.virtualbox.enableExtensionPack = true;

  ...
}

Then, run nixos-rebuild switch:

$ sudo nixos-rebuild switch

This should fail with a message like the following:

***
In order to use the extension pack, you need to comply with the VirtualBox Personal Use
and Evaluation License (PUEL) available at:

https://www.virtualbox.org/wiki/VirtualBox_PUEL

Once you have read and if you agree with the license, please use the
following command and re-run the installation:

nix-prefetch-url http://download.virtualbox.org/virtualbox/5.1.26/Oracle_VM_VirtualBox_Extension_Pack-5.1.26-117224.vbox-extpack

***

This is saying that you must manually download the Oracle Extension Pack.

It can be downloaded with the command provided:

$ nix-prefetch-url \
    http://download.virtualbox.org/virtualbox/5.1.26/Oracle_VM_VirtualBox_Extension_Pack-5.1.26-117224.vbox-extpack

After downloading the Extension Pack, run nixos-rebuild switch again. It should succeed this time.

Unfortunately, the VirtualBox derivation is setup in such a way that enabling the Extension Pack causes VirtualBox to be recompiled from source. This takes quite a while. You are not able to make use of a binary cache.

Also, there is currently no way to automate the downloading of the Extension Pack. You will have to manually download it every time you update VirtualBox.

I think this is quite an unfortunate situation. As an end-user, I love NixOS because of the ability to run one command to have my entire system setup. I created an issue about this on the nixpkgs Github, but there does not currently seem to be any solution to this problem[^3].

Installing VirtualBox from unstable

It would be nice to be able to install VirtualBox from the nixos-unstable channel.

However, I have not been able to figure this out. I have created a question on Stack Overflow, as well as a related issue on Github, but this has not yet been resolved.

For now, it does not look like there is any way to install VirtualBox from unstable.

Conclusion

Installing VirtualBox is quite simple on NixOS. Just one line in your configuration.nix. However, installing the Oracle Extension Pack requires some manual work.

Footnotes


  1. It could be possible that a reboot is required after running nixos-rebuild switch, if the VirtualBox kernel modules are being built for a kernel different from the one you are currently running. In most cases a reboot will not be required.↩︎

tags: nixos