Skip to Content

Nix Overview

  • Purely Functional Package Manager: Treats packages like values in functional programming, built by side-effect-free functions.
  • Nix Store: Packages are stored in /nix/store/, with each having a unique subdirectory (e.g., /nix/store/b6gvzjyb2pg0kjfwrjmg1vfhh54ad73z-firefox-33.1/).

Key Features

  1. Multiple Versions of Packages:

    • Allows installation of multiple versions/variants of the same package.
    • Prevents conflicts (e.g., “DLL hell”) by storing different versions in separate paths.
  2. Complete Dependencies:

    • Reduces risk of incomplete dependencies by avoiding global locations (e.g., /usr/bin).
    • Ensures that if a package builds successfully, dependencies are explicitly specified.
  3. Atomic Upgrades & Rollbacks:

    • Upgrades are atomic—no partial upgrades that could cause crashes.

    • Old versions are preserved, allowing easy rollback using:

      nix-env --upgrade --attr nixpkgs.some-package nix-env --rollback
  4. Garbage Collection:

    • Unused packages are not deleted immediately to allow rollback.

    • Use nix-collect-garbage to remove unused packages:

      nix-env --uninstall firefox nix-collect-garbage
  5. Functional Package Language:

    • Nix expressions describe package build tasks (derivations).
    • Deterministic builds: Rebuilding an expression yields the same result.
    • Easy support for package variants.
  6. Source/Binary Deployment:

    • Nix expressions usually describe source builds but can use pre-built binaries from a binary cache to save time.

      nix-env --install --attr nixpkgs.firefox
  7. Nix Packages Collection (Nixpkgs):

    • A large set of Nix expressions for hundreds of Unix packages.
  8. Managing Build Environments:

    • Automates setting up build environments with nix-shell, which sets environment variables for compilation.

      nix-shell '<nixpkgs>' --attr pan

NixOS Overview

  • Based on Nix: Uses the purely functional package management system Nix.
  • Package Isolation: Stores all packages in /nix/store/ with unique cryptographic hashes for isolation and version control (e.g., /nix/store/5rnfzla9kcx4mj5zdc7nlnv8na1najvg-firefox-3.5.4/).
  • No Overwriting: Packages are never overwritten; changes result in new packages with different paths in the Nix store.

Key Features

  1. System Configuration Management:

    • Declarative Model: Entire system (kernel, applications, configs) is built from a description in a functional language.

    • Nix Expressions for Configuration: Example of an SSH daemon configuration:

      { boot.loader.grub.device = "/dev/sda"; fileSystems."/".device = "/dev/sda1"; services.sshd.enable = true; }
    • Apply the configuration using:

      nixos-rebuild switch
  2. No Global Directories:

    • NixOS doesn’t use global directories like /bin, /lib, or /usr. All packages are stored in /nix/store/, with /etc containing symlinks to the store for system-wide configurations.
  3. Reliable and Reproducible Upgrades:

    • Reproducibility: The nixos-rebuild switch command produces the same results regardless of the previous system state.
    • Portability: Copying the configuration.nix file to another machine and running nixos-rebuild switch will recreate the same system configuration.
  4. Atomic Upgrades:

    • Transactional Upgrades: Configurations are applied atomically. If interrupted, the system will either boot in the old or new configuration—no partial, inconsistent state.
  5. Rollbacks:

    • Fast Rollbacks: Since new configurations don’t overwrite old ones, you can easily roll back with:

      nixos-rebuild switch --rollback
    • Boot Menu Options: Older system configurations automatically appear in the boot menu for easy rollback if a new configuration fails.

  6. Testing System Changes:

    • Safe Testing: You can test system changes without making them permanent using:

      nixos-rebuild test

      This activates the new configuration without setting it as the default boot configuration.

    • Testing in a Virtual Machine:

      nixos-rebuild build-vm ./result/bin/run-*-vm

      This creates a VM with the new configuration, allowing safe experimentation without affecting the host machine.

Zero to Nix 

Last updated on