The NixOS wiki is outdated and is being shut down. If you wish to add or improve NixOS documentation, please grab a ticket from the Move the wiki! milestone.

Installing NixOS in a VirtualBox guest

From Nix Wiki
(Redirected from InstallingOnVirtualBox)
Jump to: navigation, search

This pages describes how to install NixOS as a VirtualBox guest.

Base Instructions

  1. Download a NixOS ISO from our release page.
  2. Add a New Machine in VirtualBox with OS Type "Linux / Other Linux"
    • Base Memory Size: 768 MB or higher.
    • New Hard Disk of 8 GB or higher.
    • Mount the CD-ROM with the NixOS ISO (by clicking on CD/DVD-ROM)
    • Click on Settings / System / Processor and enable PAE/NX
    • Click on Settings / System / Acceleration and enable "VT-x/AMD-V" acceleration
  3. Save the setting and start the Virtual machine
  4. Boot into install shell
  5. Mount the disk as follows:
$ fdisk /dev/sda # Create a full partition, For quick setup use these commands in order: n, p, 1, <Enter>, <Enter>, w
$ mkfs.ext4 -j -L nixos /dev/sda1
$ mount LABEL=nixos /mnt
$ nixos-generate-config --root /mnt
  1. Change the configuration.nix open the main NixOS configuration file via nano:
$ nano /mnt/etc/nixos/configuration.nix

Basic Configuration

In the configuration.nix, enable the virtualbox guest service at the main block:

virtualisation.virtualbox.guest.enable = true;

Enable booting:

 boot.loader.grub.device = "/dev/sda";

Also remove the fsck that runs at startup. It will always fail to run, stopping your boot until you press *.

boot.initrd.checkJournalingFS = false;

Then do whatever installation tasks you normally do. Make sure you add a user and give the user a password. See the NixOS Manual for details.

You may also want to automatically log into a specific user when starting up the virtual machine, instead of having a login screen. Set xserver displayer manager properties as follows: = true; = "your-user";

When you are happy with your configuration file, run nixos-install and reboot.

$ nixos-install
$ reboot

Minimal Install (For Servers)

This section is about installing a minimal NixOS VirtualBox Guest for servers. It will be bare bones, except for allowing you to ssh into it.

First off, complete the steps listed in the Base Instructions section.

Then, in the VirtualBox settings, under networking, select "Attached to: NAT" and change the host-interface to "en1: Airport".

Replace the NixOS configuration file with the following:

{ config, pkgs, ...} :
  boot.loader.grub.device = "/dev/sda";

  # Enable virtualbox guest additions
  virtualisation.virtualbox.guest.enable = true;
  # Enable the sshd daemon so you can ssh
  # into the 
  services.openssh.enable = true;
  fileSystems = [
    { mountPoint = "/";
      label = "nixos";

Finally, run nixos-install, reboot, and you'll have a working NixOS system.

$ nixos-install
$ reboot


UPDATE: This can now all be done in the VirtualBox Settings GUI for the VM: Settings -> Network -> Advanced -> Port Forwarding.

However, you can't conveniently log in to ssh this way, so you'll need to set up port-forwarding. Doing that is explained on this site, all you have to do is create a script named "vbox-tunnel" that contains the following lines:

 cd /Applications/
 ./VBoxManage setextradata "$1" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/$2/Protocol" TCP
 ./VBoxManage setextradata "$1" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/$2/GuestPort" $3
 ./VBoxManage setextradata "$1" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/$2/HostPort" $4

Now execute the script:

vbox-tunnel Nix sshtunnel 22 2222

You should be able to log in to Nix now by running ssh -p 2222 localhost in your Terminal. Enjoy!

Alternative (independent of virtual network card chosen for the guest):

VBoxManage modifyvm "Nix" --natpf1 "sshtunnel,tcp,,2222,,22"

As before, you are now able to log in to Nix by running ssh -p 2222 localhost in your terminal.

Shared Folders

Shared folders can be given a name and a path in the host system in the VirtualBox settings (Machine / Settings / Shared Folders, then click on the "Add" icon). Add the following to the /etc/nixos/configuration.nix to auto-mount them:

{ config, pkgs, ...} :

  fileSystems."/virtualboxshare" = {
    fsType = "vboxsf";
    device = "nameofthesharedfolder";
    options = "rw";

The folder will be available directly under the root directory.

If you want to have symlinks you have to use something like VBoxManage setextradata VM_NAME VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME 1 to enable them.

As noted that vboxsf is quite slow, and NFS, CIFS or 9P might be a better choice if the host support these networking.

Vagrant boxes

Using Vagrant requires maintaining base boxes and some plugins. See Vagrant boxes about it.