Introduction to Nix

Anton Christoffersson

Created: 2024-03-26 tis 11:07

1. What is nix?

  • Package manager: A tool designed for software package management emphasizing reproducibility, isolation and reliability.
  • NixOS: A Linux distribution that incorporates the Nix package manager for system and software management.
  • Nix-env: Manage environment packages. Similarly to other package managers.
  • Nix-shell: A tool for creating development environments that are consistent across different systems.

2. Nix package manager

  • Purely functional package manager
    • Guarantees identical outputs for identical inputs, eliminating “works on my machine” issues.
  • Immutable and isolated packages
    • Ensures consistency and prevents dependency conflicts.
  • Nix store
    • Centralized storage for all packages and dependencies (usually /nix/store). Unique package directories based on package hash.

      zn14alw8acfjgfjc6k9l5wy3nfgsh3yy-git-2.42.0-doc
      zn44101nf002b6y7nmhr27mg4ln8a6b7-streaming-commons-0.2.2.6.tar.gz.drv
      znb0d764af4ra71dp0fgky5javaxacb0-setuptools-check-hook.drv
      
    • Isolates each package in its own directory ensuring they don’t interfere with each other.

2.1. Nix Package manager

  • Profiles
    • Links everything together (literally) with symlinks to /nix/store packages.

      .nix-profile/
      ├── bin
      │   ├── emacs-29.2 -> /nix/store/4wxg7i1bqwx5ki1hp4m87xsaa8b8kr4v-home-manager-path/bin/emacs-29.2
      │   ├── fish -> /nix/store/4wxg7i1bqwx5ki1hp4m87xsaa8b8kr4v-home-manager-path/bin/fish
      │   ├── git -> /nix/store/4wxg7i1bqwx5ki1hp4m87xsaa8b8kr4v-home-manager-path/bin/git
      ├── etc -> /nix/store/4wxg7i1bqwx5ki1hp4m87xsaa8b8kr4v-home-manager-path/etc
      ├── include -> /nix/store/4wxg7i1bqwx5ki1hp4m87xsaa8b8kr4v-home-manager-path/include
      ├── lib -> /nix/store/4wxg7i1bqwx5ki1hp4m87xsaa8b8kr4v-home-manager-path/lib
      ├── libexec -> /nix/store/4wxg7i1bqwx5ki1hp4m87xsaa8b8kr4v-home-manager-path/libexec
      ├── manifest.nix -> /nix/store/s8i50swmc5cfdgb6dcaqahgcy90fkxli-env-manifest.nix
      ├── sbin -> /nix/store/4wxg7i1bqwx5ki1hp4m87xsaa8b8kr4v-home-manager-path/sbin
      └── share -> /nix/store/4wxg7i1bqwx5ki1hp4m87xsaa8b8kr4v-home-manager-path/share
      
    • Allows garbage collection
      • Safely remove all unused packages

        $ sudo nix-collect-garbage -d
        

2.2. Nix Package manager

  • Declarative
    • Everything is specified in configuration files

      with import <nixpkgs> {};
        gcc11Stdenv.mkDerivation rec {
          name = "dev-env";
          buildInputs = with pkgs; [
            cmake
          ];
      }
      
  • Almost 90 000 packages available
  • Available on Linux and Darwin(macOS)

3. Packages common distros

map_repo_size_fresh.svg

4. Packages all distros

map_repo_size_fresh2.svg

5. NixOs

  • Operating system Uses Nix package manager at its core for managing system configuration and packages.
  • Atomic upgrades and rollbacks
    • Each rebuild creates a new generation (profile)
    • Atomically jump between generations
  • Erase/kill your darlings
    • Recreate system on boot from /nix/store
  • Deploy with NixOps
    • Atomically deploy to local and cloud servers
    • Secret management
  • Modularize and reuse configurations (Example)

6. Nix-env

  • Lets users install and remove packages similarly to other package managers, without adding them to a config file.
  • Not recommended in general but useful for testing.

7. Nix-shell

  • Flexible environment
    • Supports both pure (isolated) and impure (system-access) environments.
  • Pure or impure environment with specified packages
  • Home-manager (Example)
    • Manages environments on user level, manage shell and dotfiles
  • Direnv (Example)
    • Project specific automatic development environment

8. Questions and links