NixOS Installation Guide
This guide describes various ways of installing NixOS. The official manual should cover most usual use-cases.
Installing from USB
Since NixOS 14.11 the installer ISO is hybrid. This means it is bootable on both CD and USB drives. It also boots on EFI systems, like most modern motherboards and Mac* systems. Please read the #Hybrid image section on how to burn the ISO.
For NixOS 14.04 and earlier, please read #Using Unetbootin and the other sections below.
Hybrid image
First, download a NixOS ISO image or create a custom ISO. Then plug in a USB stick large enough to accomodate the image. Then follow the platform instructions:
On Linux:
- Find the right device with
fdisk -l
, let's say/dev/sdb
. - Burn with:
cp nixos-xxx.iso /dev/sdb
On OS X:
- Find the right device with
diskutil list
, let's saydisk2
. - Unmount with
diskutil unmountDisk disk2
. - Burn with:
sudo dd if=path_to_nixos.iso of=/dev/rdisk2 bs=64k
- Note the use of rdisk instead of disk, this makes a large speed difference. You can check the write speed with
iostat 2
in another terminal.
You could use Disk Utility to write the image but then you'd need to convert it first to a compatible format somehow.
On Windows:
- Download USBwriter.
- Start USBwriter.
- Choose the downloaded ISO as 'Source'
- Choose the USB drive as 'Target'
- Click 'Write'
- When USBwriter has finished writing, safely unplug the USB drive.
This should suffice to boot the installer. Otherwise look for other alternatives below.
UEFI note
The below is for BIOS installation. For UEFI installation see the manual.
Using Unetbootin
It is possible to install NixOS from a USB stick, rather than from a CD. This is useful if you want to install NixOS on a machine that doesn't have a CD-ROM drive (such as most netbooks), or if you don't want to waste a blank CD. Here is how to do it:
- Download a NixOS ISO image or #Build a custom installation image.
- Obtain a USB stick formatted with the VFAT/FAT32 filesystem with enough free disk space to hold the contents of the ISO image.
- Install UNetbootin, a tool that allows you to create a bootable USB stick from an ISO image. UNetbootin runs on both Linux and Windows. If you already have Nix/NixOS, you can install it by running nix-env -i unetbootin. Other tools may also work.
- Insert the USB stick, start UNetbootin, select the ISO file and target USB drive, and press Ok. This copies the contents of the ISO to the USB stick and installs the GRUB boot loader.
- You should now be able to boot NixOS from the USB stick, and perform the installation as usual.
Using syslinux
Using syslinux.cfg
If you have trouble booting from a USB drive or sdcard with Unetbootin (I just got a blinking cursor instead of a bootloader on an Asus eee 1000) the procedure described on http://knoppix.net/wiki/Bootable_USB_Key should work. After preparing the sdcard with mkdiskimage and syslinux you can mount it and copy all the content from the NixOS iso:
$ mount -o loop ~/Downloads/nixos-graphical-0.1pre27337-i686-linux.iso /media/iso
$ rsync -av --progress /media/iso/ /media/sd-card/
The syslinux.cfg needs to be created manually, but the details can easily be derived from the grub.cfg on the NixOS install cd.
I specified the root device by UUID, to get the UUID:
blkid /dev/mmcblk0p1
The relevant section from the live cd: /media/sd-card/boot/grub/grub.cfg
menuentry "NixOS Installer / Rescue" { linux /boot/bzImage init=/nix/store/r7xhnzymi1ll49r4glf1dwr5y1alx0bl-system/init root=LABEL=NIXOS_INSTALL_CD_0.1pre27337 splash=verbose vga=0x317 initrd /boot/initrd }
can be used in the syslinux config file: /media/sd-card/syslinux.cfg (you should just need to update the init path and the root UUID / LABEL or device path)
DEFAULT linux LABEL linux SAY Now booting the kernel from SYSLINUX... KERNEL /boot/bzImage APPEND init=/nix/store/r7xhnzymi1ll49r4glf1dwr5y1alx0bl-system/init root=UUID=509C-63E2 ro initrd=/boot/initrd splash=verbose
Note: in place of using mkdiskimage, you can simply format your SDCard as a fat32 system and install syslinux on it:
mkdosfs -F32 /dev/mmcblk0p1 syslinux -s /dev/mmcblk0p1
Using Command-line Tools
- Download the ISO image from http://nixos.org/nixos/download.html.
- Prepare your USB stick. If it isn't yet partitioned to your liking, create a bootable partition on
/dev/sdb1
:fdisk /dev/sdb
And format it with:
mkdosfs /dev/sdb1
Add ext2 partitions if you like. If you want both 32-bit and 64-bit NixOS, you need at least two partitions. You can directly use the entire disk without partition if that's what you like. Once you have your VFAT (dosfs) block device, you should give it a label, within a 11-character limit, e.g.:
dosfslabel /dev/sdb1 NIXBOOT
- Mount the bootable USB partition with:
mkdir -p /media/NIXBOOT mount /dev/sdb1 /media/NIXBOOT
- Mount the ISO image with:
mkdir -p /media/iso mount -o loop ~/Downloads/nixos-graphical-0.2pre4463_5e88e9b-c877f45-x86_64-linux.iso /media/iso
- Copy the contents of the NixOS ISO (
/media/iso
) to your USB stick (/media/NIXBOOT
) with:rsync -av --progress /media/iso/ /media/NIXBOOT/
- Install a complete and recent GRUB to the USB stick with:
grub-install /dev/sdb --root-directory=/media/NIXBOOT/
- Edit the file
/media/NIXBOOT/boot/grub/grub.cfg
. Start from an existing grub.cfg, say from your hard drive or from another working bootable USB stick (e.g., from GRML.org). Then, merge in the entry for NixOS from/media/iso/boot/grub/grub.cfg
, modifying the LABEL to be that of your VFAT partition:menuentry "NixOS Installer / Rescue" { linux /boot/bzImage init=/nix/store/p94ckcksmhj90cr868cpcajrqgzwy57w-nixos-0.2pre4463_5e88e9b-c877f45/init root=LABEL=NIXBOOT initrd /boot/initrd }
- To share your USB stick with other distributions (e.g., GRML, or both 32- and 64- bit NixOS), you can move NixOS's bzImage and initrd to different location (e.g.,
/boot/nix32/
and/boot/nix64/
), and you appropriately edit the entries in your grub.cfg. If you have both nix32 and nix64, only one can have itsnix-store.squashfs
in the root of any given partition. That's where you can use a second partition, for the second one. For instance, copy the contents of your 64-bit NixOS to the first partition, with labelNIX64
, and copy the nix-store.squashfs of your 32-bit NixOS to the second partition with labelNIX32
(and create an empty nix/store). Copy the 32-bit kernel and initrd to/boot/nix32/
on the bootable partition, and edit the grub.cfg of the bootable partition accordingly. - There you go: you have a bootable NixOS USB drive, possibly allowing you to boot NixOS 64-bit, NixOS 32-bit, GRML 64-bit, GRML 32-bit, FreeBSD, FreeDOS, boot-sector-tetris, etc. Just you convince your BIOS to let you boot off it.
Build a custom installation image
NixOS provides an easy way to build a custom variant of the installation image. This might be useful to embed your own ssh key or enable additional features like zfs support. It allows to specify a custom configuration.nix
which is used to build the image
This is explained in Creating a NixOS live CD.
Installing on VPS & Cloud Providers
Not all providers allow to upload own images or provide NixOS images for installation. However there are a few ways to install NixOS anyhow. nixos-in-place and nixos-infect are generic approaches to install NixOS from an existing Linux. A different approach is to build an kexec-based image to start the installer from an booted linux as shown in this repo.
For no-hassle NixOS installations check out NixOS friendly hosters. The NixOps project also provides interfaces to different cloud providers.
Here we list methods known to work on specific providers:
Hetzner
Hetzner provides an interface to upload your own ISO-images. However you may want to build your own iso-image, which has openssh with ssh keys due the lack of a remote console. An easier method to install NixOS on Hetzner, is to use the existing integration into NixOps.
Amazon EC2
see NixOS on Amazon EC2.
Digital Ocean
On Digitalocean the project nixos-infect can be used to transform an existing installation into NixOS. This method will be also used in NixOps.
Netcup
You can mount your own images similar to hetzner, see the official netcup documentation.
Linode
NixOS is official supported by Linode. Up to date documentation can be obtained from their manual
CloudAtCost
Scaleway
The kexec
method above works well, but bear in mind it only works with the 'virtual' class of servers, as they run under a hypervisor which attaches the disks before the kernel boots. The 'bare-metal' servers rely on a special Linux kernel booting to attach network /dev/nbdX
drives which works only with Scaleway supplied images.
Rackspace
See Install NixOS on Rackspace Cloud Servers.
Google Compute Engine
See Install NixOS on GCE.
Online.net
Installing from Linux
You already have a running linux with GRUB on your primary partition and you don't want to waste a CD-R? Right. We also assume that you have a spare partition where to install NixOS ready.
To simplify, we will be using these abbreviations:
- /dev/sda1
- your boot partition, containing already working grub
- /dev/sda2
- your linux root partition, containing your currently working OS (/dev/sda1 and /dev/sda2 *can* be the same partition!)
- /dev/sda3
- your spare partition to where you will install NixOS
- /boot
- the boot directory, where grub is installed
- ~/inst
- directory where the ISO is (loop) mounted
The following sections outline two installation methods. Choose the one that suits you best.
Installing through a chroot
In this section we will install NixOS by unpacking the ISO and then chrooting into its contents to start the final install.
Preparations
Get a NixOS ISO and unpack the squashfs filesystem containing the nix store. A tool from squashfs-tools is required, so install that package first.
The following commands create two directories in your home directory, one for the ISO mountpoint and one for the host system. Then the ISO is downloaded (substitute as you please), mounted and the nix store extracted.
# cd ~
# mkdir -p inst host/nix
# wget https://d3g5gsiof5omrk.cloudfront.net/nixos/17.03/nixos-17.03.1449.2e983f14f6/nixos-minimal-17.03.1449.2e983f14f6-x86_64-linux.iso
# modprobe loop
# mount -o loop nixos-minimal-17.03.1449.2e983f14f6-x86_64-linux.iso inst
# unsquashfs -d host/nix/store inst/nix-store.squashfs '*'
To have a working network connection, copy /etc/resolv.conf to host/nix/etc. For a working chroot, you also need to bind /dev, /proc and /sys directories to the target system.
# cd host
# mkdir -p etc dev proc sys
# cp /etc/resolv.conf etc
# for fn in dev proc sys; do mount --bind /$fn $fn; done
Circumventing regular init sequence
To properly chroot into the host system you must locate the packages named nixos and bash. The following commands may prove helpful:
# INIT=$(find . -type f -path '*nixos*/init')
# echo $INIT
./nix/store/abwlkvzyjd2i39b1l1wfv7v9ilx88fwi-nixos-0.1pre34067-34077/init
# BASH=$(find . -type f -path '*/bin/bash' | tail -n 1)
# echo $BASH
./nix/store/bmgq2jrn6719r6j55gs4rzfp0azcbazy-bash-4.2-p24/bin/bash
Replace further mentions of these files with your own results! Note the missing prefix in some uses.
Next we have to edit the target system init script to start a bash session instead of systemd. As that is the last thing the script does, adding an interactive program does not pose a problem.
# sed -i "s,exec systemd,exec /$BASH," $INIT
Starting the install
Now we are able to chroot into the NixOS system used for installation. You may get several warnings and errors, but you can safely ignore them unless they are fatal. If you're installing from Arch, you may need to remove the mount call that tries to make / private in the same init script.
# chroot . /$INIT
Further commands to be executed in the chrooted environment will be prefixed with "# (chroot)".
The next steps are the same as with classical installation methods. Create a suitable NixOS configuration and start the build.
# (chroot) mount /dev/sda3 /mnt
# (chroot) nixos-generate-config --root /mnt
# (chroot) nano /etc/nixos/configuration.nix
Before executing nixos-install you might want to run nixos-checkout to obtain the most recent version of packages to be installed into your NixOS system. You might need to copy /etc/resolv.conf from the host to the target system.
# (chroot) nixos-checkout
# (chroot) nixos-install
Now your system should be set up. Be sure to change the root password to be able to login and double check that /mnt/boot/grub/menu.lst is present and looks sensible.
# (chroot) passwd
Finally, if you instructed nix not to overwrite your existing boot loader, manually copy the menu.lst entries into your existing system's GRUB menu.lst.
Once you're done tweaking, exit the chroot (exit or ctrl-d will) and reboot. With any luck, you will have new choices in the GRUB menu to choose from.
Running the LiveCD installer from disk
In this section we will install NixOS by transfering the installer from the ISO to a bootable partition and then running it.
WARNING
This approach relies on merging the LiveCD image with the contents of your working partition, so several limitations apply:
- the target partition needs to have enough free space for both the working partition (/dev/sda2) and the uncompressed nix store from the LiveCD
- /etc will be merged, so your current settings can interfere with the final installation steps
Unpacking the ISO image
# wget https://d3g5gsiof5omrk.cloudfront.net/nixos/17.03/nixos-17.03.1449.2e983f14f6/nixos-minimal-17.03.1449.2e983f14f6-x86_64-linux.iso # get the latest iso (URL might change!)
# modprobe loop
# mount -o loop nixos-minimal-17.03.1449.2e983f14f6-x86_64-linux.iso ~/inst
# # mount /dev/sda1 # mount the /boot partition if you have it
# cp ~/inst/boot/bzImage /boot/nixos-livecd-bzImage
# cp ~/inst/boot/initrd /boot/nixos-livecd-initrd
# cp ~/inst/nix-store.squashfs /nix-store.squashfs
Modifying your bootloader's config
Look at some_dir/boot/grub/grub.cfg. This is Grub-2 main config file of the ISO. Locate the NixOS menuentry section:
menuentry "NixOS Installer / Rescue" { linux /boot/bzImage init=/nix/store/p5n72ay1c1wx4wry90zabr8jnljpdzgx-nixos-0.2pre4601_1def5ba-48a4e91/init root=LABEL=NIXOS_0.2pre4601_1def5ba-48a4e91 initrd /boot/initrd }
The goal is to tell your bootloader to boot /nixos-livecd-bzImage with correct init argument.
GRUB 1
To setup grub-1, edit your /boot/grub/menu.lst (or equivalent). Add following lines to the config:
title NixOS LiveCD kernel /nixos-livecd-bzImage init=/nix/store/p5n72ay1c1wx4wry90zabr8jnljpdzgx-nixos-0.2pre4601_1def5ba-48a4e91/init root=/dev/sda2 splash=verbose vga=0x317 initrd /nixos-livecd-initrd
Note, that hash should match with what you have seen in some_dir/grub/grub.cfg
Go to the reboot section
GRUB 2
A grub entry for ubuntu might look like this:
menuentry "NixOS Installer / Rescue" { linux /nixos-livecd-bzImage init=/nix/store/p5n72ay1c1wx4wry90zabr8jnljpdzgx-nixos-0.2pre4601_1def5ba-48a4e91/init root=/dev/sda2 initrd /nixos-lived-initrd }
Other bootloaders
Should also work. Please add instructions here.
Booting into LiveCD
Reboot. Select "NixOS LiveCD" from the bootloader menu. If everything is OK, you will see Login prompt asking you to login as root with empty password. DO NOT TRUST IT BLINDLY. You probably have your /etc mounted from /dev/sda2 so it contains your old passwd (as well as LiveCD stuff merged into by the means of UnionFS). So if empty password is not working, try your old root password.
Thats it. Now follow the manual (Alt-F8), mount /dev/sda3 as /mnt, do nixos-option and so on.
Special hardware
- Blog post how to install NixOS on a Dell 9560
- for embedded Hardware like the Raspberry Pi2 see NixOS on ARM
- Brand servers may require extra kernel modules be included into initrd (boot.initrd.extraKernelModules in configuration.nix) For example HP Proliant needs "hpsa" module to see the disk drive.