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.
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.
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.
To get started with NixOS, follow these steps:
Download the ISO:
Visit the NixOS download page and download the latest installation ISO.
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=progressBoot the Installer:
Boot your system from the USB drive and choose the "Install NixOS" option.
Partition Your Disk:
Partition your disk using tools like fdisk or parted. Create a root (/) and optionally a boot partition.
Mount Partitions:
Mount the partitions at their respective mount points.
mount /dev/sdX1 /mntGenerate Configuration:
Run:
nixos-generate-config --root /mnt
This creates a default configuration.nix file.
Edit Configuration:
Customize /mnt/etc/nixos/configuration.nix to include your desired packages and settings.
Install NixOS:
Install the system:
nixos-install
Set a root password when prompted, and reboot into your new system.
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 switchNavigate 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.
To build or run the Flake, use:
nix build .
nix run .
Add desired packages to configuration.nix under environment.systemPackages:
environment.systemPackages = with pkgs; [
vim
firefox
];
Apply the changes with:
sudo nixos-rebuild switchYou can create isolated development environments using Flakes. For example:
nix develop
configuration.nix and flake.nix.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.