Linux Distros

How to Install NixOS

In the Linux world, there are many distributions, and these distributions usually differ in terms of package manager, environment, and packages. Once installed, you can find files in specific places in the file structure. Directories like /usr, /usr/local and /bin are used to store different files, and this standard makes it possible for an experienced Linux user to know where files are located and to run scripts that use these files over many distributions. To find out more, look up the LSB project.

While you can run applications under NixOS because they follow the above standard, the files are not where they would be in another system. The developers of NixOS and GNU Guix have strong opinions about this system, and they have come up with clever ways to comply with it.

A different system

Your software storage system affects functionality in a way that is much deeper than it seems at first glance. For the software to find the files it needs, NixOS uses symlinks. Each application has its own directory that contains the executable and links to the libraries that run it.

With this organisation system, you can have different files and versions installed at the same time. By default, all packages and their dependencies should compile during installation. However, it requires a lot of time and processing power to do so at every install, there are caches.

Downloading

With NixOS, there is always more than one way to do something. Like other distributions, with NixOS, you have an ISO on a USB stick. You have choices regarding how you want to install NixOS on your distribution. However, before we discuss this topic in more detail, it is important to understand that there are two slightly confusing parts of this process.

First, Nix is different from NixOS, and you must understand the difference between Nix, the package manager, and NixOS, which configures your system. You can download the Nix package manager and use it on your current system. With it, you can keep many versions of applications on your system without them interfering with each other.

Second, with NixOS, while you cannot not declare the partitioning scheme, everything else can be left in one file. Most users leave the automatically created hardware configuration file alone. When you first start out, you can keep your packages declared in the file, but over time, you will probably make separate files that you import into your configuration file.

Partitioning

Before installation, you must partition your drives. In other distributions, there are defaults you can accept; however, with NixOS, you must do your own partitioning. Partitioning is not very complex, but you can run into trouble when you have to set your configuration for the partitioning scheme you choose. It is important to understand that the instructions and scripts prefer if your file systems are labelled correctly.

The standard manual shows the partitioning commands. Note that the commands differ for a UEFI and an MBR disk, and setting the wrong values will cause many problems. The manual suggests using the values provided below for the initial installation, but it is really easy to test new values.

Standard partitions:
MBR:

parted /dev/sda -- mklabel msdos
parted /dev/sda -- mkpart primary 1MiB -8GiB
parted /dev/sda -- mkpart primary linux-swap -8GiB 100%

UEFI:

parted /dev/sda -- mklabel gpt
parted /dev/sda -- mkpart primary 512MiB -8GiB
parted /dev/sda -- mkpart primary linux-swap -8GiB 100%
parted /dev/sda -- mkpart ESP fat32 1MiB 512MiB
parted /dev/sda -- set 3 esp on

Mounting the partitions in MBR:

mkswap -L swap /dev/sda2
mount /dev/disk/by-label/nixos

Mounting the partitions in UEFI:

mount /dev/disk/by-label/nixos /mnt
mkdir -p /mnt/boot
mount /dev/disk/by-label/boot /mnt/boot

The next section will show you how to create your configuration file.

The Config File

Once you have your disks set up, you can start the configuration process. With NixOS, you configure first and then install. The following instructions assume that you have booted using the ISO, but you could boot with chroot.

With nixos-generate-config, the system generates a standard configuration file.

$ nixos-generate-config –root /mnt

This command creates two files: /mnt/etc/nixos/hardware-configuration.nix (you do not change this file) and /mnt/etc/nixos/configuration.nix. You can edit the second file in your favourite editor.

Usually, the options do not change depending on the method used to boot. You can use grub or another boot configuration. There are many options, but here are some standards.

Add this line for MBR only:

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

Add these lines for UEFI only:

boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;

Change the config files as little as possible to start. These values are all in the original file: just un-comment them and make changes to fit your needs.

 networking.hostName = "nixos";
users.user.nixos = {
  isNormalUser = true;
  extraGroups = " wheel"
}

environment.systemPackages = with pkgs [
  wget vim
];

services.openssh.enable = true;

Add the packages you want to use as standard packages. All standard packages go in the square brackets with wget and vim. You can install more packages once the system is running.

Building

Once your configuration file is correct, you can run the install.

$ nixos-install

Next, the installer will ask for a root password that will be used on the real system. All programs will be compiled or downloaded from cache.nixpkgs.org and then installed in the nix store on your computer. Then, you can reboot, and you should get a login prompt.

$ reboot

Now, provide a password for your user using root. The user you defined in the configuration file will also have a home directory.

New Config

Once you have completed the above steps, you can play around with the configuration file. When you change something, try it out without installing it as follows:

$ nixos-rebuild test

Once you have new values that work well, run the rebuild command:

$ nixos-rebuild switch

Now, you will see if you have set the boot values correctly. It is important to note that the changes to the configuration are reversible. You can simply reboot and choose an older version, which is called a generation, and try again. However, each configuration does require disk space, so make sure you are familiar with the garbage collection function.

Conclusion

NixOS requires a few manual steps to set up, but you can return to a running system much quicker than with other systems. Furthermore, version control is easier if you need many versions of the same application or if you need many versions of the same libraries. At first glance, there may seem to be many limitations, but these limitations can be overcome with the more advanced parts of the system.

About the author

Mats Tage Axelsson

I am a freelance writer for Linux magazines. I enjoy finding out what is possible under Linux and how we can all chip in to improve it. I also cover renewable energy and the new way the grid operates. You can find more of my writing on my blog.