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
-
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.
-
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.
- Reduces risk of incomplete dependencies by avoiding global locations (e.g.,
-
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
-
-
Garbage Collection:
-
Unused packages are not deleted immediately to allow rollback.
-
Use
nix-collect-garbageto remove unused packages:nix-env --uninstall firefox nix-collect-garbage
-
-
Functional Package Language:
- Nix expressions describe package build tasks (derivations).
- Deterministic builds: Rebuilding an expression yields the same result.
- Easy support for package variants.
-
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
-
-
Nix Packages Collection (Nixpkgs):
- A large set of Nix expressions for hundreds of Unix packages.
-
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
-
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
-
-
No Global Directories:
- NixOS doesn’t use global directories like
/bin,/lib, or/usr. All packages are stored in/nix/store/, with/etccontaining symlinks to the store for system-wide configurations.
- NixOS doesn’t use global directories like
-
Reliable and Reproducible Upgrades:
- Reproducibility: The
nixos-rebuild switchcommand produces the same results regardless of the previous system state. - Portability: Copying the
configuration.nixfile to another machine and runningnixos-rebuild switchwill recreate the same system configuration.
- Reproducibility: The
-
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.
-
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.
-
-
Testing System Changes:
-
Safe Testing: You can test system changes without making them permanent using:
nixos-rebuild testThis activates the new configuration without setting it as the default boot configuration.
-
Testing in a Virtual Machine:
nixos-rebuild build-vm ./result/bin/run-*-vmThis creates a VM with the new configuration, allowing safe experimentation without affecting the host machine.
-