Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Install NixOS on Linode

This tutorial is written for people who want to run NixOS on a Linode instance. The installation is pretty straightforward, but it involves some bootstrapping using Linode's tools.

In this tutorial, we will show you how to set up NixOS on Linode by setting up disk, installing Nix onto the rescue OS, and use Nix to install NixOS onto your disks.

Create a Linode

To start, create a Linode of any size and in any data center. Linode will then direct you to the Dashboard tab of the (Linode Manager) [manager.linode.com].

Setup Disks

Since NixOS isn't officially supported by Linode we will have to bootstrap the installation.

Create disk images

In the dashboard there is an option Create a new disk that you can use to create a disk. You will want to create an ext4 disk that is at least 4GB. You can do this by selecting ext4 in the type menu.

If you want to use a swap disk you will need to create another disk. For this disk you will want to choose the swap type from the menu. Linode recommends using a 256MB swap disk, but you can make it as large as you want.

Boot into Finnix

Now that we have some disks, we need to boot the Linode into Finnix. Finnix is the recovery mode that Linode uses to allow you to rescue and repair your disks, but we will be using it to install NixOS on the disks we made.

You can boot Finnix by going to the Rescue tab in the Linode Manager. To follow along with this guide you will want to make sure that your ext4 disk is selected for /dev/sda and if you have a swap disk that it is selected as /dev/sdb.

Once you boot into Finnix you will need to connect to your Linode. Linode offers a serial console called LISH that you can use for this, since Finnix doesn't boot with an ssh daemon running.

You can access LISH by going to the Remote Access tab of the Linode Manager. You have two options for LISH, the web console which can be accessed through the Launch Lish Console link or through SSH which can be accessed through the Lish via SSH link. If you use SSH you will be asked for a password which is the password for the Linode Manager.

The LISH console may have some strange behavior as it is a simple serial console. It is only intended for recovery and repair purpose, so you may encounter some frustration with scrollback and very long lines.

Filesytems

All of Linode's tools assume that you are using a partitionless disk, so if you partition your disk in any way these tools will fail. Since these tools, such as their backup service and root password resets, are an important part of Linode's platform we should try and set up our disks to support them.

If you did want to use a partitioning scheme, we recommend that you use additional disks instead of partitions. For example, you can create another disk to use as /var instead of another partition. Since all the disks are virtual creating new disks is simple and there isn't much point in partitioning.

So since we are going to use the disk as a partitionless disk everything is already set up. You only need to mount your disks. If you have swap you will want to turn it on now.

mount /dev/sda /mnt
swapon /dev/sdb

Install Nix

Now we need to install Nix on Finnix to bootstrap our NixOS installation.

Create a temporary user

Installing nix as root isn't currently supported, so we will need to create a user to install it as. This user won't exist on the installed NixOS system.

useradd nix -s /bin/bash -m

We will also need to create the nixbld group and some build users.

groupadd -r nixbld
for i in {1..10}; do
  useradd -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" \
  nixbld$i
done

Download and Build

To install nix itself we will need to login as the temporary user and run the installer.

update-ca-certificates
mkdir /nix && chown -R nix /nix
su -l nix
curl https://nixos.org/nix/install | bash
exit

Yes, the nix installer is a pipe installer. If you find that objectionable you can instead download and review the scripe yourself by replacing that line with

curl -O https://nixos.org/nix/install
less install

Create a Nix Profile

Now we will need to set up a Nix profile for root, and switch to the nixos channel.

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

Optional: Install a text editor

If you want to use either vim or emacs instead of nano, you will have to install it on Finnix.

For vim users:

nix-env -i vim

For emacs users:

nix-env -i emacs

Get the NixOS installer

Now we need to get the NixOS installer.

cat <<EOF > /root/configuration.nix
{
  fileSystems."/" = {};
  boot.loader.grub.enable = false;
} 
EOF

#export
export NIX_PATH=nixpkgs=/root/.nix-defexpr/channels/nixos:nixos=/root/.nix-defexpr/channels/nixos/nixos
NIX_PATH=nixpkgs=/root/.nix-defexpr/channels/nixos
NIX_PATH+=:nixos=/root/.nix-defexpr/channels/nixos/nixos
NIXOS_CONFIG=/root/configuration.nix
export NIX_PATH NIXOS_CONFIG

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 NixOS

We have the installation tools, so now we need to build the system configuration.

Generate configuration files

This will generate sample configuration files in /mnt/etc/nixos/.

nixos-generate-config --root /mnt

Linode specific configuration

Linode doesn't recommend using UUIDs to identify devices. Since Linode lets us easily control device identifiers, we will use these to mount our filesystems instead.

We will need to edit /mnt/etc/nixos/hardware-configuration.nix to replace the UUIDs used for filesystems.

fileSystems."/" = {
  device = "/dev/sda";
  fsType = "ext4";
};

If you created a swap disk earlier you will want to do the same thing for swap devices.

swapDevices =
[ { device = "/dev/sda"; }
];

You will also need to edit /mnt/etc/nixos/configuration.nix to tell grub which device to install the bootloader to. This will be our ext4 disk which is /dev/sda.

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

We will need to delcare some options to ensure that all the proper kernel modules and hardware configurations are used. The easiest way to do that is to create a new linode.nix file in /mnt/etc/nixos/.

#{- linode.nix -}#
{ config, pkgs, ... }:

{
  imports =
    [ <nixpkgs/nixos/modules/profiles/qemu-guest.nix>
    ];

  
  boot = {
    initrd.availableKernelModules = [
      "9p"
      "9pnet_virtio"
      "ata_piix"
      "virtio_blk"
      "virtio_net"
      "virtio_pci"
      "virtio_scsi"
    ];

    kernelParams = [ "console=ttyS0" ];

    loader.grub.extraConfig = ''
      serial; terminal_input serial; terminal_ouput serial
    '';
  };

  services = {
    longview = {
      enable = false;
      apiKey = "";

      apacheStatusUrl = "";
      nginxStatusUrl = "";

      mysqlUser = "";
      mysqlPassword = "";
    };
  };
}

You can then import this file by adding it to your imports in hardware-configuration.nix.

imports =
[ ./linode.nix
];

Other changes

Now that your Linode specific configuration is done, you can go ahead and declare anything else you may want. Take some time to set up your packages, services, and users.

Install NixOS

The moment of truth is here!

unset NIXOS_CONFIG
nixos-install

This will install NixOS onto the disk. If you didn't configure a root password in your configuration, you will want to do that before you reboot.

nixos-install --chroot
passwd
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.