NixOS Installation Guide

This article explains how to install the command-line version (Minimal) of NixOS. NixOS is a Linux distribution based on the Nix package manager, featuring declarative configuration and other unique characteristics.

Version Used

The NixOS version used in this article is 24.11.

Preparation Before Installation

Prepare a Bootable NixOS USB

Download the ISO

Go to the NixOS official website to download the latest NixOS ISO. Choose the Minimal version. Alternatively, use the following links to download directly:

  • NixOS 24.11 Minimal ISO for amd64
  • SHA-256

Create a Bootable USB

It is recommended to use Ventoy to create a bootable USB. Ventoy supports multiple operating system ISOs and allows you to store multiple ISOs on the same USB. Simply copy the downloaded and verified NixOS ISO to the Ventoy-created USB.

Installation

Boot your computer from the USB and select NixOS to enter the installation interface.

Using SSH for Remote Connection

Since the default passwords for both nixos and root users are empty, set a password using the passwd command before connecting via SSH. You can then connect to the NixOS installation interface from another computer using SSH.

1
2
3
4
5
# Set a password for the current user (default is nixos)
passwd

# Then connect via SSH
ssh nixos@<ip_address>

Check Network Connection

Use the ping command to check if the network connection is working:

1
ping -c 5 ping.ui.com

Partition the Disk

Use the parted command to partition the disk.

Back up your data before partitioning, as partitioning will erase all data on the disk.

Assume the disk for installing NixOS is /dev/nvme0n1, and you want to use the btrfs file system for the root directory. Use the following commands to partition the disk:

1
2
3
4
5
6
7
$ sudo parted /dev/nvme0n1

(parted) mklabel gpt # Create a GPT partition table
(parted) mkpart "EFI system partition" fat32 1MiB 513MiB # Create an EFI partition
(parted) set 1 esp on # Set the EFI partition flag
(parted) mkpart "root partition" btrfs 513MiB 100% # Create a root partition
(parted) quit # Exit parted

After partitioning, format the partitions:

1
2
3
4
5
# Format the EFI partition
sudo mkfs.fat -F 32 /dev/nvme0n1p1

# Format the root partition
sudo mkfs.btrfs /dev/nvme0n1p2

Since we are using the btrfs file system, create subvolumes as needed. Here is an example of creating an @ subvolume:

1
2
3
4
5
6
7
8
# Mount the root partition
sudo mount /dev/nvme0n1p2 /mnt

# Create a subvolume
sudo btrfs subvolume create /mnt/@

# Unmount the root partition
sudo umount /mnt

Mount the Partitions

1
2
3
4
5
6
7
8
# Mount the root partition to /mnt using the @ subvolume and enable zstd:1 compression
sudo mount -o compress=zstd:1,subvol=@ /dev/nvme0n1p2 /mnt

# Create a boot mount point
sudo mkdir -p /mnt/boot

# Mount the EFI partition to /mnt/boot
sudo mount -o umask=077 /dev/nvme0n1p1 /mnt/boot

Generate and Adjust NixOS Configuration Files

Use the sudo nixos-generate-config --root /mnt command to generate NixOS configuration files.

  • /mnt/etc/nixos/hardware-configuration.nix: Hardware configuration file containing system hardware information.
  • /mnt/etc/nixos/configuration.nix: System configuration file containing basic system settings.

Patch hardware-configuration.nix

Add the following content to /mnt/etc/nixos/hardware-configuration.nix:

Modify the following:

1
2
# Adjust the root directory mount options as needed
options = [ "subvol=@" "compress=zstd:1" "lazytime" ];

Patch configuration.nix

Add the following content to /mnt/etc/nixos/configuration.nix:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
networking.hostName = "<YOUR_HOSTNAME>"; # Set the hostname
networking.networkmanager.enable = true; # Enable NetworkManager

time.timeZone = "<YOUR_TIMEZONE>"; # Set the timezone, e.g., Asia/Shanghai, America/Chicago, etc.

# Set up a user account
users.users.<YOUR_USERNAME> = {
isNormalUser = true; # Normal user
extraGroups = [ "wheel" ]; # Add to the wheel group for sudo privileges
packages = with pkgs; [
# Add required packages
git
vim
];
};

# Add a mirror for users with slow network access, using USTC's mirror as an example
nix.settings.substituters = [ "https://mirrors.ustc.edu.cn/nix-channels/store" ];

# Set system-wide installed packages
environment.systemPackages = with pkgs; [
vim
wget
htop
];

# Enable SSH service
services.openssh.enable = true;

# Copy the current configuration to /run/current-system/configuration.nix to prevent accidental deletion
system.copySystemConfiguration = true;

Install NixOS

After adjusting the configuration files, start the installation using the following command:

1
sudo nixos-install # You will be prompted to set the root password; use the --no-root-passwd option to skip

After installation, don’t forget to set the password for your account:

1
nixos-enter --root /mnt -c 'passwd <YOUR_USERNAME>'

Finally, reboot into the newly installed NixOS system:

1
sudo reboot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ nix-shell -p fastfetch

$ fastfetch

▗▄▄▄ ▗▄▄▄▄ ▄▄▄▖ <YOUR_USERNAME>@<YOUR_HOSTNAME>
▜███▙ ▜███▙ ▟███▛ --------------
▜███▙ ▜███▙▟███▛ OS: NixOS 24.11 (Vicuna) x86_64
▜███▙ ▜██████▛ Host: <YOUR_DEVICE_NAME>
▟█████████████████▙ ▜████▛ ▟▙ Kernel: Linux 6.6.84
▟███████████████████▙ ▜███▙ ▟██▙ Uptime: 12 hours, 34 mins
▄▄▄▄▖ ▜███▙ ▟███▛ Packages: 567 (nix-system)
▟███▛ ▜██▛ ▟███▛ Shell: bash 5.2.37
▟███▛ ▜▛ ▟███▛ Terminal: /dev/pts/0
▟███████████▛ ▟██████████▙ CPU: Intel(R) Core(TM) i3-9100T (4) @ 3.70 GHz
▜██████████▛ ▟███████████▛ GPU: Intel UHD Graphics 630 @ 1.10 GHz [Integrated]
▟███▛ ▟▙ ▟███▛ Memory: 409.95 MiB / 7.51 GiB (5%)
▟███▛ ▟██▙ ▟███▛ Swap: Disabled
▟███▛ ▜███▙ ▝▀▀▀▀ Disk (/): 2.41 GiB / 465.26 GiB (1%) - btrfs
▜██▛ ▜███▙ ▜██████████████████▛ Local IP (eno1): 192.168.0.2/24
▜▛ ▟████▙ ▜████████████████▛ Locale: en_US.UTF-8
▟██████▙ ▜███▙
▟███▛▜███▙ ▜███▙
▟███▛ ▜███▙ ▜███▙
▝▀▀▀ ▀▀▀▀▘ ▀▀▀▘