NixOS Installation Guide: Difference between revisions

From NixOS Wiki
imported>Volth
imported>Ixxie
Merged the USB installation guide.
Line 1: Line 1:
For commodity PC hardware the [https://nixos.org/nixos/manual/index.html#ch-installation official manual] should covers the most use cases.
For commodity PC hardware the [https://nixos.org/nixos/manual/index.html#ch-installation official manual] should covers the most use cases.
== Installing NixOS 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 ==
{{Note|This section applies to NixOS 14.11. For NixOS 14.04, please refer to [[#Using Unetbootin]].}}
First, download a [http://nixos.org/nixos/download.html NixOS ISO image] or [[Creating a Nix OS live CD|create a custom ISO]]. Then plug in a USB stick large enough to accomodate the image. Then follow the platform instructions:
<ul>
<li>On Linux:
<ol>
<li>Find the right device with <code>fdisk -l</code>, let's say <code>/dev/sdb</code>.</li>
<li>Burn with: <code>cp nixos-xxx.iso /dev/sdb</code></li> 
</ol>
{{Note|do not use /dev/sdb1 or partitions of the disk, use the whole disk /dev/sdb.}}
</li>
<li>On OS X:
<ol>
<li>Find the right device with <code>diskutil list</code>, let's say <code>disk<b>2</b></code>.</li>
<li>Unmount with <code>diskutil unmountDisk disk<b>2</b></code>.</li>
<li>Burn with: <code>sudo dd if=<b>path_to_nixos.iso</b> of=/dev/<b>r</b>disk<b>2</b> bs=64k</code></li>
<li>Note the use of rdisk instead of disk, this makes a large speed difference. You can check the write speed with <code>iostat 2</code> in another terminal.</li>
</ol>
You could use Disk Utility to write the image but then you'd need to convert it first to a compatible format somehow.
</li>
<li>On Windows:
<ol>
<li>Download [http://sourceforge.net/projects/usbwriter/ USBwriter].</li>
<li>Start USBwriter.</li>
<li>Choose the downloaded ISO as 'Source'</li>
<li>Choose the USB drive as 'Target'</li>
<li>Click 'Write'</li>
<li>When USBwriter has finished writing, safely unplug the USB drive.</li>
</ol>
</li>
</ul>
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 [http://nixos.org/nixos/manual/#sec-uefi-installation 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:
<ol>
<li>Download a [http://nixos.org/nixos/download.html NixOS ISO image] or [[Creating a Nix OS live CD|create a custom ISO]].</li>
<li>Obtain a USB stick formatted with the VFAT/FAT32 filesystem with enough free disk space to hold the contents of the ISO image.  {{Note|It's not necessary to erase the USB stick.}}</li>
<li>Install [http://unetbootin.sourceforge.net/ 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 <tt>nix-env -i unetbootin</tt>.  [http://en.wikipedia.org/wiki/List_of_tools_to_create_Live_USB_systems Other tools] may also work.</li>
<li>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.</li>
<li>You should now be able to boot NixOS from the USB stick, and perform the installation as usual.</li>
</ol>
{{Note|For EFI support you may need to change the label of the FAT filesystem. You can do this on Linux with mlabel, and on OS X with diskutil: `sudo diskutil rename OLDNAME NIXOS_ISO`.}}
== 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:
<syntaxhighlight lang="console">
$ mount -o loop ~/Downloads/nixos-graphical-0.1pre27337-i686-linux.iso /media/iso
$ rsync -av --progress /media/iso/ /media/sd-card/
</syntaxhighlight>
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:
<pre>
blkid /dev/mmcblk0p1
</pre>
The relevant section from the live cd: /media/sd-card/boot/grub/grub.cfg
<pre>
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
}
</pre>
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)
<pre>
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
</pre>
'''Note''': in place of using mkdiskimage, you can simply format your SDCard as a fat32 system and install syslinux on it:
<pre>
mkdosfs -F32 /dev/mmcblk0p1
syslinux -s /dev/mmcblk0p1
</pre>
== Alternate method ==
<ol>
<li>Download the ISO image from http://nixos.org/nixos/download.html.</li>
<li>Prepare your USB stick. If it isn't yet partitioned to your liking, create a bootable partition on <code>/dev/sdb1</code>:
<pre>
fdisk /dev/sdb
</pre>
And format it with:
<pre>
mkdosfs /dev/sdb1
</pre>
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.:
<pre>
dosfslabel /dev/sdb1 NIXBOOT
</pre>
</li>
<li>Mount the bootable USB partition with:
<pre>
mkdir -p /media/NIXBOOT
mount /dev/sdb1 /media/NIXBOOT
</pre>
</li>
<li>Mount the ISO image with:
<pre>
mkdir -p /media/iso
mount -o loop ~/Downloads/nixos-graphical-0.2pre4463_5e88e9b-c877f45-x86_64-linux.iso /media/iso
</pre>
</li>
<li>Copy the contents of the NixOS ISO (<code>/media/iso</code>) to your USB stick (<code>/media/NIXBOOT</code>) with:
<pre>
rsync -av --progress /media/iso/ /media/NIXBOOT/
</pre>
</li>
<li>Install a complete and recent GRUB to the USB stick with:
<pre>
grub-install /dev/sdb --root-directory=/media/NIXBOOT/
</pre>
</li>
<li>Edit the file <code>/media/NIXBOOT/boot/grub/grub.cfg</code>. 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 <code>/media/iso/boot/grub/grub.cfg</code>, modifying the LABEL to be that of your VFAT partition:
<pre>
menuentry "NixOS Installer / Rescue" {
  linux /boot/bzImage init=/nix/store/p94ckcksmhj90cr868cpcajrqgzwy57w-nixos-0.2pre4463_5e88e9b-c877f45/init root=LABEL=NIXBOOT
  initrd /boot/initrd
}
</pre>
</li>
<li>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., <code>/boot/nix32/</code> and <code>/boot/nix64/</code>), and you appropriately edit the entries in your grub.cfg. If you have both nix32 and nix64, only one can have its <code>nix-store.squashfs</code> 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 label <code>NIX64</code>, and copy the nix-store.squashfs of your 32-bit NixOS to the second partition with label <code>NIX32</code> (and create an empty nix/store). Copy the 32-bit kernel and initrd to <code>/boot/nix32/</code> on the bootable partition, and edit the grub.cfg of the bootable partition accordingly.</li>
<li>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.</li>
</ol>


== Build a custom installation image ==
== Build a custom installation image ==
Line 55: Line 245:
* for embedded Hardware like the Raspberry Pi2 see [[NixOS on ARM]]
* 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.
* 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.
[[Category:Guide]]
[[Category:Installation]]

Revision as of 21:05, 6 October 2017

For commodity PC hardware the official manual should covers the most use cases.

Installing NixOS from USB

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:

  1. Download a NixOS ISO image or create a custom ISO.
  2. Obtain a USB stick formatted with the VFAT/FAT32 filesystem with enough free disk space to hold the contents of the ISO image.
    Note: It's not necessary to erase the USB stick.
  3. 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.
  4. 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.
  5. You should now be able to boot NixOS from the USB stick, and perform the installation as usual.
Note: For EFI support you may need to change the label of the FAT filesystem. You can do this on Linux with mlabel, and on OS X with diskutil: `sudo diskutil rename OLDNAME NIXOS_ISO`.

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

Alternate method

  1. Download the ISO image from http://nixos.org/nixos/download.html.
  2. 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
    
  3. Mount the bootable USB partition with:
    mkdir -p /media/NIXBOOT
    mount /dev/sdb1 /media/NIXBOOT
    
  4. 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
    
  5. Copy the contents of the NixOS ISO (/media/iso) to your USB stick (/media/NIXBOOT) with:
    rsync -av --progress /media/iso/ /media/NIXBOOT/
    
  6. Install a complete and recent GRUB to the USB stick with:
    grub-install /dev/sdb --root-directory=/media/NIXBOOT/
    
  7. 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
    }
    
  8. 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 its nix-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 label NIX64, and copy the nix-store.squashfs of your 32-bit NixOS to the second partition with label NIX32 (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.
  9. 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.

Install Nixos on VPS/Cloud-Provider

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

Installation Stories

Add the code you needed to run in order to deploy NixOS at $provider .

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

Digitalocean

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.

Online.net

See Install NixOS on Online.Net.

<your vps provider here>

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.