Install NixOS on Linode

From NixOS Wiki
Revision as of 10:27, 30 June 2017 by imported>Fadenb (Created page with "The high level approach that these directions will take is to boot into the Rescue Mode, then mount the partitions we'll install on, install NixOS and NixPkgs on the Rescue Di...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The high level approach that these directions will take is to boot into the Rescue Mode, then mount the partitions we'll install on, install NixOS and NixPkgs on the Rescue Disk and then have it install to the mounted partition we actually want it installed on.

Setup your Disk Image partions.

Create Disk Images in Linode Manager

You will need:

  • A small partition we will call 'boot', it should be ~200 mb this will be /dev/xvda
  • Make the rest of your disk space one other parition, we'll call this 'nixos' this will be /dev/xvdb

Additionally, be sure to choose the "pv_grub-<arch>" kernel to actually use the kernel supplied from the nixos installation.

Boot into Rescue Mode and use LISH to connect to your machine

What will become /boot needs to be ext2, I make my main partition ext4:

mkfs.ext2 /dev/xvda
mkfs.ext4 /dev/xvdb

Add a filesystem label to the NixOS Installation Disk

This is optional. The label is used in configuration.nix later so make sure to adapt it to your needs.

e2label /dev/xvdb nixos

Mount the Partitions

mount /dev/xvdb /mnt
mkdir /mnt/boot
mount /dev/xvda /mnt/boot

Install Prerequisites

Install Nix in the Rescue System

Installing Nix as root doesn't work, so create an user just to do that:

adduser nix # set password

We also need the Nix build users to exist:

groupadd -r nixbld
for n in $(seq 1 10); do useradd -c "Nix build user $n" \
    -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" \
    nixbld$n; done

Now do the Nix install:

su - nix
bash <(curl https://nixos.org/nix/install)
exit

And then for the rest we can be root again.

. ~nix/.nix-profile/etc/profile.d/nix.sh 
nix-channel --remove nixpkgs
nix-channel --add http://nixos.org/channels/nixos-17.03 nixos
nix-channel --update

Install vim for editing (Optional)

nix-env -i vim

Install nixos-install, nixos-option, and nixos-hardware-schan

cat <<EOF > configuration.nix
{ fileSystems."/" = {};
  boot.loader.grub.enable = false;
} 
EOF
export NIX_PATH=nixpkgs=/root/.nix-defexpr/channels/nixos:nixos=/root/.nix-defexpr/channels/nixos/nixos
export NIXOS_CONFIG=/root/configuration.nix
nix-env -i -A config.system.build.nixos-install -A config.system.build.nixos-option -A config.system.build.nixos-generate-config -f "<nixos>"

Configure your system

Generate configuration.nix template in /mnt/etc/nixos/

export NIX_PATH=nixpkgs=/root/.nix-defexpr/channels/nixos:nixos=/root/.nix-defexpr/channels/nixos/nixos
nixos-generate-config --root /mnt

Edit your /mnt/etc/nixos/configuration.nix

These are the minimal changes you'll have to make, you probably also want to turn sshd on, and perhaps create a user. That disk and swap settings in hardware-configuration.nix should be correct but you should check to confirm.

 # Use the GRUB 1 boot loader.
 boot.loader.grub = {
   enable = true;
   version = 1;
   extraPerEntryConfig = "root (hd0)";
   device = "nodev";
 };

If you want a swap file, add something like:

 swapDevices = [
   { device = "/var/swapfile";
     size = 2048; # in MB
   }
 ];

You could also use a swap partition, but there's no reason to since the performance is the same[1] and swap partitions are less flexible.

Install NixOS!

unset NIXOS_CONFIG
nixos-install

Fix Grub

Because of how Linode will try to load grub on the /boot partition, we need to make this symlink so it can find it:

mkdir -p /mnt/boot/boot/grub
cd /mnt/boot/boot/grub
ln -sv ../../grub/menu.lst /mnt/boot/boot/grub

Set Root Password

You should set the root password here. If you forget, you can use Linode's "Reset Root Password" option in the Rescue tab after finishing the install.

Reboot to NixOS

The installation is finished. The last steps before booting the system is creating a configuration profile in the Linode Manager:

  • Boot Settings
    • Kernel: pv-grub-x86_64
  • Block Device Assignment
    • /dev/xvda: Your 'boot' image
    • /dev/xvdb: nixos
    • root device: /dev/xvdb

Change other configuration options to your preference.

Now hit boot in the Linode Manager and wait for NixOS to boot. You can check progress in the Remote Access Console.

Update Channels to finish installation

nix-channel --update

You now should have a fully functional NixOS install on Linode, enjoy!