NixOS

From Leo's Notes
This page was last edited on 26 April 2020, at 22:32.

NixOS is a Linux distribution based upon the Nix package manager. The idea is to build a immutable system from a modular and purely functional specification. Software, configuration files, scripts, etc. are generated from a specification. This method of creating a system ensures that everything comes up deterministically and reproducibly. Alterations to configuration files are also defined in this specification, allowing for easy tracability of configuration file changes. Upgrades involve updating the specification file and then rebuilding the system which in essence allows software to update atomically.

This is in contrast to the conventional way of managing a system: an imperative way where where packages are installed and updated in place and configuration files are created and edited after installation.

Nix

Central to the idea of NixOS is the Nix package manager. Packages (also known as derivations) are stored under /nix/store/hash-name-version, where a cryptographic hash uniquely identifies the derivation. All files under the Nix store are immutable. For example, bash would be in /nix/store/s4zia7hhqkin1di0f187b79sa2srhv6k-bash-4.2-p45/, under its own bin/bash directory. NIX will arrange binaries in your PATH appropriately.

This method of handling software packages allows for multiple versions of the same software package to be installed. Any dependencies are handled by installing the exact version of the dependency and then ensuring the proper link. This means that you could run mysql 5.2 with glibc-2.18, and mysql 5.5 with glibc-2.19 all in the same system.

Package upgrades are handled by installing the newer version on the system and then switching to that newer version.

The Nix database is stored at /nix/var/nix/db

Profile

A profile in Nix gathers separate components spread among different paths into a unified path. They are versioned (known as a generation) that allows for easy roll back.

Profiles are stored under ~/.nix-profile which is a symlink to /nix/var/nix/profiles/default. Different generations of the profile are stored /nix/var/nix/profiles/.

nix-env handles the profiles and all the symlinks in this location. paths are also merged by nix-env.

Introduction

Various commands that were introduced in Chapter 3 of the Nix Pills.

Command Description
nix-env Manages environments, profiles, and their generations
nix-env -i hello installs the hello environment
nix-env --list-generations lists all environment generations
nix-env -q lists all derivations
nix-env -q --references `which hello` shows all derivations that are required by the given binary
nix-env -q --referrers `which hello` shows all derivations that are dependent on the given binary
nix-env --rollback rolls back the generation by one
nix-env -G 3 sets the environment generation to the given generation (3)
nix-env -u upgrades all packages in the environment
nix-env -e '*' Uninstalls everything from the current environment. You can recover this by running nix-env from the nix store and reinstalling the nix-env derivative or roll back the environment generation.

Closures of a derivation is a recursive list of all dependencies that are required. nix-store -qR `which man` or nix-store -q --tree `which man`


Channels are basically like repos.

Command Description
nix-channel --list lists all channels. This is configured in ~/.nix-channels
nix-channel --update Synchronizes the channel repo data


Installing NixOS

The installation steps are listed in section 2.3 of the manual.

After downloading the NixOS install ISO and booting it, you will be greeted with a KDE login screen. Log in as nixos without a password. Open a terminal and sudo su into root. From there, configure the primary disk (/dev/sda) with a single partition and format it to ext4. Mount the partition in /mnt and then run:

If you want to use emacs to edit the initial config, run nix-env -f '<nixpkgs>' -iA emacs first.

# fdisk /dev/sda  # Create a partition for the system, another for swap if desired
# mkfs.ext4 -L nixos /dev/sda1
# mount /dev/sda1 /mnt
# nixos-generate-config --root /mnt 
# cd /mnt   # You can take a look at what's generated

## Review the configuration file. 
## BIOS systems: ensure that boot.loader.grub.device is set
## UEFI systems: ensure that boot.loader.systemd-boot.enable is set to true
# vi /mnt/etc/nixos/configuration.nix

## When you're ready, set up the system
# nixos-install

The nixos-install command will download all the required derivations, set up the boot loader, etc. The last step of the command will prompt for a root password. Once complete, you should be able to reboot into the new installation without the ISO image.

After the system comes up, you should be able to log in as root with the password you provided in the installation step. You can change the system by editing the same Template:/etc/nixos/configuration.nix file as you did originally. For changes to apply, run nixos-rebuild switch. Any system services that are affected should be automatically restarted by this rebuild process. If so desired, you can make a separate profile for the rebuild with the -p profile flag so that it shows up as a separate grub option.

You may also use nixos-rebuild test to test the configuration without making it boot by default. You may also use nixos-rebuild boot to do everything as switch did but without switching over until the next reboot.