Install NixOS using the minimal ISO

9 minutes read


Hey there! Are you ready to dive into the world of NixOS? Get ready to experience a unique Linux distribution that’s all about declarative configuration and package management. NixOS brings reproducibility, atomic upgrades and rollbacks, and package isolation to the table. Let’s walk through the installation process of NixOS and learn how to partition your disk using the parted command-line tool.


What is NixOS?

NixOS logo NixOS is an immutable OS.

An immutable system refers to a system where the state or configuration cannot be changed once it is set. In simpler terms, it means that once something is set up or configured in an immutable system, it cannot be modified or altered afterwards. It remains fixed and cannot be changed.

NixOS is not like a conventional distro with a package manager, and does not support the File Hierarchy Standard.

NixOS is an operating system that follows the concept of immutability. It is built on the Nix package manager, which allows for declarative and reproducible system configurations. In NixOS, the entire operating system, including the kernel, packages, and system settings, is defined in a configuration file. This configuration file specifies exactly how the system should be set up and what packages should be installed.

The key idea behind NixOS is that the system configuration is treated as a single, atomic entity. Any changes to the system are made by creating a new configuration file rather than modifying the existing one. This ensures that the system remains consistent and predictable, as any changes are applied in a controlled and reproducible manner.


Notes


Installing NixOS

Before we get started, it’s worth mentioning that installing NixOS differs a bit from the usual Linux distribution installation process. Instead of a traditional installer, we’ll be working with configuration files to set up our system. Let the installation begin!

Create a bootable USB drive:

Head over to the official NixOS website and grab the latest NixOS ISO. Once you have it, use a tool like dd or BalenaEtcher to create a bootable USB drive.

Boot into the NixOS live environment:

Plug in the USB drive and boot up your computer from it. You should see the NixOS boot menu. Go ahead and select the default option to boot into the live environment.

Partition your disk:

After entering the live environment, open up a terminal and fire up parted by running the command parted /dev/sda. Make sure to replace /dev/sda with the appropriate device name for your system.

Create partitions:

It’s time to create those partitions! Use parted to create the partitions you need. For example, if you want to create a root partition, try running this in your terminal:

(parted) mkpart primary ext4 1MiB 100GiB

This command creates a primary partition with the ext4 file system, starting from 1MiB and going up to 100GiB. Customize the size and file system to your liking. Repeat this step for any additional partitions you require, like a swap partition or a separate home partition.

Partitioning with parted

Set partition flags:

If you need to set specific flags for certain partitions, like the boot flag for an EFI partition, parted has got you covered. Use the set command to make it happen. Here’s an example:

(parted) set 1 boot on

This command sets the boot flag on partition 1.

Quit parted:

After you finish partitioning, it’s time to say goodbye to parted. Just type quit in the parted prompt, and you’ll be on your way.

Format the partitions:

Let’s format those partitions you just created with the appropriate file systems. To format the root partition as ext4, run the following command:

mkfs.ext4 /dev/sda1

Feel free to repeat this step for other partitions, if necessary.

The picture below shows my partitioning layout: (again, note /boot is used even for EFI systems).

lsblk output

My partitioning layout can be achieved using:

parted /dev/sda -- mklabel gpt
parted /dev/sdb -- mklabel gpt

# Create the partitions
parted /dev/sda -- mkpart primary 0% 100% # /home

parted /dev/sdb -- mkpart ESP fat32 1MB 1024MB # 512 will do for /boot
parted /dev/sdb -- set 1 esp on 
parted /dev/sdb -- mkpart primary 512MB 100% # / 

# Format to resp filesystems
mkfs.ext4 -L home /dev/sda1
mkfs.ext4 -L nixos /dev/sdb2
mkfs.fat -F 32 -n boot /dev/sdb1
Mount the partitions:

Now, create mount points and mount the partitions to the desired locations. For instance, if you want to mount the root partition to /mnt, use this command:

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

If you - like me - require separate partitions for /home or any other directories, mount them accordingly.

Generate NixOS configuration:

To generate the initial NixOS configuration, run the following command:

nixos-generate-config --root /mnt

This creates the initial configuration file at /mnt/etc/nixos/configuration.nix.

Edit the configuration:

Open up your favorite text editor and customize the configuration file at /mnt/etc/nixos/configuration.nix to your heart’s content. This file allows you to specify partitions, file systems, boot options, network configuration, and other system settings. Make it your playground!

Install NixOS:

After you’re done tweaking the configuration file, save the changes and run the following command to install NixOS:

nixos-install

This will install NixOS using the configuration file you provided. It’s like waving a magic wand!

Reboot into NixOS:

With the installation process completed successfully, remove the USB drive, take a deep breath, and reboot your computer. You should now see the NixOS boot menu, ready to welcome you into your newly installed NixOS system. Enjoy the ride!

Getting Started with NixOS

Congratulations! You’ve made it through the installation process and now have a shiny NixOS installation. Let’s familiarize ourselves with a few key aspects of NixOS.


Want a sample configuration?

My configuration is available here. Feel free to modify and use it. Note that NixOS makes rolling back (to a working config) trivial. Don’t hesitate and play around with the options. The web interface contains the list of packages and configuration options that NixOS supports. Be sure to change the username and other details!

Note that you must symlink the config files to their required places (else the files wouldn’t be read). This can be done easily: (assuming you’ve cloned this to your home directory)

 sudo ln -sf ~/nixos-config/configuration.nix /etc/nixos/ # if you want my config
 mkdir -p ~/.config/home-manager/
 ln -sf ~/nixos-config/home.nix ~/.config/home-manager/

Home-manager is an unofficial tool provided by NixOS that helps take user-level configuration to the next level. It lets you perform declarative management at the user-level, thus enabling the user to manage their progrm configs in a declarative way, like NixOS’ OS-wide declarative approach.


Conclusion

Congratulations once again on installing NixOS! You’ve taken a step into the world of declarative configuration, powerful package management, and system reproducibility. By following the installation process outlined in this guide, you’re well on your way to enjoying the unique features that NixOS brings to the table. Embrace the flexibility, atomic upgrades, and package isolation that NixOS offers, and make your system truly your own. Happy Nix-ing! Wanna try a package before actually installing it onto your system (by adding the package to configuration.nix) ? Use nix-shell -p <package_name> to install the package in an ephemeral shell.