Getting Started with NixOS and Flakes

2025-01-03

NixOS is a powerful, declarative, and reproducible Linux distribution that enables users to configure their entire system using a single configuration file. Coupled with Flakes, a feature that enhances reproducibility and simplifies dependency management, NixOS becomes an incredibly robust tool for developers and system administrators.

In this post, we’ll guide you through the basics of installing NixOS, setting up Flakes, and using them effectively.


What Is NixOS?

NixOS is a Linux distribution built on the Nix package manager, emphasizing immutability, reproducibility, and simplicity in managing configurations. Instead of modifying configurations directly, you define your system's state in a declarative file.

Key Features of NixOS:

  1. Declarative Configuration: All system settings are described in a single configuration file.
  2. Atomic Upgrades and Rollbacks: Easily revert changes with NixOS’s transactional upgrades.
  3. Reproducibility: System configurations can be precisely replicated on other machines.
  4. Isolated Environments: Perfect for development and testing.

What Are Flakes?

Flakes are an experimental feature in Nix that improves dependency management, caching, and version pinning. Flakes make it easier to create and share reproducible environments across systems.

Advantages of Flakes:


Installing NixOS

To get started with NixOS, follow these steps:

Prerequisites:

Steps:

  1. Download the ISO:
    Visit the NixOS download page and download the latest installation ISO.

  2. Create a Bootable USB:
    Use a tool like dd or Etcher to flash the ISO to a USB drive.

    sudo dd if=nixos-iso-name.iso of=/dev/sdX bs=4M status=progress
  3. Boot the Installer:
    Boot your system from the USB drive and choose the "Install NixOS" option.

  4. Partition Your Disk:
    Partition your disk using tools like fdisk or parted. Create a root (/) and optionally a boot partition.

  5. Mount Partitions:
    Mount the partitions at their respective mount points.

    mount /dev/sdX1 /mnt
  6. Generate Configuration:
    Run:

    nixos-generate-config --root /mnt

    This creates a default configuration.nix file.

  7. Edit Configuration:
    Customize /mnt/etc/nixos/configuration.nix to include your desired packages and settings.

  8. Install NixOS:
    Install the system:

    nixos-install

    Set a root password when prompted, and reboot into your new system.


Enabling and Using Flakes

Step 1: Enable Flakes

To enable Flakes, add the following to your /etc/nixos/configuration.nix file:

nix = {
  package = pkgs.nix;
  extraOptions = ''
    experimental-features = nix-command flakes
  '';
};

Then, apply the changes:

sudo nixos-rebuild switch

Step 2: Create a Flake

Navigate to a directory and initialize a new Flake:

nix flake init -t templates#default

This creates a flake.nix file, which is the core configuration file for the Flake.

Step 3: Use a Flake

To build or run the Flake, use:

nix build .  
nix run .

Managing Your System with NixOS and Flakes

Adding Packages

Add desired packages to configuration.nix under environment.systemPackages:

environment.systemPackages = with pkgs; [
  vim
  firefox
];

Apply the changes with:

sudo nixos-rebuild switch

Using Flakes for Development

You can create isolated development environments using Flakes. For example:

nix develop

Challenges and Tips


Conclusion

NixOS, combined with Flakes, offers unparalleled control over your system and development environments. By mastering these tools, you can create reproducible, reliable, and efficient setups tailored to your needs. Whether you're a developer, system administrator, or Linux enthusiast, NixOS and Flakes are worth exploring.