Complete OS Guide: NixOS How It Works, Orientation and Curiosities

NixOS: A Declarative Linux Distribution

NixOS is a unique Linux distribution built on the Nix package manager. It emphasizes reproducibility, reliability, and a declarative system configuration. By defining an entire operating system in a single configuration file, NixOS allows users to reproduce exact environments across machines, roll back changes easily, and maintain multiple configurations side by side.

What Is NixOS?

Origins and Philosophy

NixOS emerged from the Nix package manager project, started by Eelco Dolstra in 2003. The core idea was to manage packages in a purely functional way, avoiding side effects and ensuring that building one package does not interfere with others. Over time, this concept evolved into a full-fledged Linux distribution.

Key Characteristics

  • Declarative Configuration: Define the entire system in a single configuration.nix file.
  • Atomic Upgrades and Rollbacks: System upgrades are transactional and can be rolled back instantly.
  • Reproducible Builds: Ensures that building the same configuration yields bit-for-bit identical results.
  • Isolation and Purity: Each package builds in an isolated environment, preventing unintended dependencies.
  • Multi-Version Support: Multiple versions of the same package can coexist without conflicts.

How NixOS Works

The Nix Package Manager

NixOS is powered by the Nix package manager, which treats packages as pure functions: given the same inputs, the build process yields the same output. Packages are stored in the Nix store, located at /nix/store, where each package is identified by a cryptographic hash of its build inputs.

Store Paths and Hashes

  • Each package resides in a directory named after a hash.
  • The hash encodes all inputs: source code, compiler version, dependencies, build scripts, and configuration options.
  • This ensures package builds are reproducible and prevents conflicts.

Declarative System Configuration

In traditional distributions, users install packages imperatively with commands like apt install or dnf install. NixOS instead uses a declarative approach: you edit /etc/nixos/configuration.nix to list desired packages, system services, and settings.

Example configuration.nix excerpt

{ config, pkgs, ... }:
{
  imports = [  ]
  boot.loader.grub.device = /dev/sda
  fileSystems./.device = /dev/disk/by-uuid/...
  networking.hostName = mymachine
  services.openssh.enable = true
  users.users.alice = {
    isNormalUser = true
    extraGroups = [ wheel ]
    password = secret
  }
  environment.systemPackages = with pkgs [
    vim
    git
    firefox
  ]
}

After editing, running nixos-rebuild switch applies the configuration. The system builds a new generation in /nix/var/nix/profiles and updates the symlink /run/current-system to point to it. If something goes wrong, you can reboot and select the previous generation.

Transactional Upgrades and Rollbacks

  • Atomicity: Upgrades either complete fully or leave the system unchanged.
  • Rollback: Users can switch back to any previous generation via GRUB or the nixos-rebuild tool.
  • Profiles: Each generation is a profile the current system is a symlink to one profile, making switching simple.

What NixOS Is Oriented To

NixOS appeals to a variety of use cases and audiences due to its distinctive features.

Developers and DevOps Engineers

  • Reproducible Development Environments: Define project dependencies in shell.nix or default.nix to create isolated shells.
  • Continuous Integration: Build reproducible images for CI pipelines.
  • Infrastructure as Code: Manage entire server fleets declaratively.

System Administrators

  • Configuration Management Alternative: Combines features of Ansible, Puppet, and Docker in one tool.
  • Stable Deployments: Atomic upgrades and easy rollbacks reduce downtime.
  • Multi-tenant Servers: Isolate different services with separate Nix profiles.

Hobbyists and Power Users

  • Customizability: Build your own Linux distribution without complex scripts.
  • Side-by-side Versions: Install multiple versions of programming languages and tools seamlessly.
  • Learning Functional Package Management: Explore Nix as a novel approach to software deployment.

Key Components and Concepts

Nix Expressions

Nix packages and configurations are defined using the Nix expression language, a lazy, functional language specifically designed for describing build processes. Every derivation describes how to build a package, specifying inputs, patches, environment variables, and build commands.

Nixpkgs Repository

The nixpkgs repository contains thousands of package definitions and system modules. It evolves rapidly, and users can easily pull updates or pin to a specific revision for stability.

Nix Channels

Channels are curated snapshots of nixpkgs. The default nixos-XX.YY channel matches a specific NixOS release. Users can subscribe to unstable channels for the latest packages or roll their own.

Curiosities and Advanced Features

Garbage Collection

Because the Nix store accumulates generations and package copies, NixOS includes a garbage collector:

  • nix-collect-garbage removes unreferenced store paths.
  • nix-collect-garbage -d deletes old generations and frees disk space.

Binary Cache and Hydra

NixOS provides official binary caches so users need not build every package from source. Hydra is the continuous build system that compiles packages and publishes them to the binary cache, enabling fast installs.

Docker and Containers

Nix can build Docker images declaratively. Users write Nix expressions to define container contents, then run nix-build –attr dockerImage to produce a Docker image.

Cross-Compilation

NixOS supports cross-compilation out of the box. You can build ARM packages on an x86 machine by setting crossSystem in your configuration.

Immutable Environments

By treating the system as immutable, NixOS reduces “configuration drift.” Each change spawns a new generation, and the old state remains intact until explicitly garbage-collected.

Comparison with Other Distributions

Feature NixOS Debian/Ubuntu Fedora
Package Management Functional, isolated, multiple versions APT, imperative, single version DNF, imperative, single version
System Configuration Declarative (configuration.nix) Imperative, manual edits Imperative, manual edits
Upgrades Atomic rollback Partial, risky Partial, risky
Reproducibility High (bit-for-bit) Low Low
Isolation Builds in clean environments Shared system paths Shared system paths
Multi-Version Support Yes No No

Use Cases and Real-World Deployments

Cloud and Infrastructure

Many organizations use NixOS to manage cloud servers. Declarative definitions ensure that web farms, databases, and microservices are deployed identically across regions.

Scientific Computing

Reproducibility is critical in research. NixOS environments guarantee that computational experiments can be rerun years later with the same software stack.

Continuous Integration

Projects like nixos-hardware and various open source initiatives use Hydra to provide nightly binary builds. Teams can integrate Nix into CI pipelines to build and test software in a hermetic environment.

Getting Started with NixOS

Installation

  1. Download the NixOS ISO from the official website.
  2. Boot the live environment and partition your disk.
  3. Mount file systems to /mnt and generate a default configuration.nix with nixos-generate-config –root /mnt.
  4. Edit /mnt/etc/nixos/configuration.nix to your needs.
  5. Install with nixos-install and reboot.

Post-Installation

  • Enable your favorite desktop environment (GNOME, KDE, etc.) in configuration.nix.
  • Set up users, SSH keys, and network settings declaratively.
  • Explore channels: switch between stable and unstable as needed.
  • Learn the Nix expression language by packaging a small application.

Resources and Community

Conclusion

NixOS represents a paradigm shift in Linux distributions, leveraging functional package management and declarative system configuration to ensure reproducibility, atomic upgrades, and flexibility. Whether you are a developer, system administrator, or hobbyist, NixOS offers powerful tools to manage software and infrastructure in a reliable, maintainable way. By adopting NixOS, you embrace an approach that minimizes configuration drift, simplifies rollbacks, and fosters collaboration through shared, version-controlled system definitions.

Download TXT




Leave a Reply

Your email address will not be published. Required fields are marked *