Nix: Chapter Zero
1. What is Nix?
Discussions of nix
are made more confusion by the fact that the name
refers to multiple separate, but interrelated topics.
- The nix Language
- A language for writing packages
- The nix Command
- A utility for evaluating
nix-lang
- The nixpkgs Collection
- A suite of packages written in
nix-lang
- The Nixos Distribution
- An operating system using the
nix
tool as its primary package manager.
2. The nix language
- Purely Functional
- A derivation can only depend on its explicit inputs
- Lazy
- Package inputs that aren't used aren't build
- Dedicated
- Package derivations are a first class type
- Dynamic
- Allow arbitrary sets to be passed around and allow their usage site to decide on the parts that matter.
For individuals with a static typing bent, there is a Dhall language
that compiles to nix
and is very strongly typed.
3. The nix language example
{ pkgs ? import <nixpkgs> { }, ... }: pkgs.stdenv.mkDerivation { name = "NeXus"; buildInputs = with pkgs; [cmake hdf5 hdf5-cpp hdf5.dev hdf5-cpp.dev libjpeg pkg-config hdf4 hdf4.dev ]; src = pkgs.fetchFromGitHub { owner = "nexusformat"; repo = "code"; rev = "v4.4.3"; sha256 = "16h3ag7rm6gvg7dfmgk3qgalnmwc8rvk3dzd5095pybch73rvdq6"; }; cmakeFlags = [ "-DENABLE_CXX=ON" "-DENABLE_JAVA=ON" "-DENABLE_HDF4=ON" ]; }
4. The nix command
The nix
command evaluates nix expressions. Packages are cached, so
they only need to be built once, even when installed from multiple,
separate packages.
- nix build
- Evaluate the expression and put the resulting package
into the local folder in
result
- nix develop
- Create a local shell with all of the necessary development tools and libraries available.
- nix-collect-garbage
- Uninstall unused packages
- nix run
- Run the package
- nix bundle
- Create a portable "static-ish" executable
- nix copy
- Install a package onto another machine
5. The nixpkgs collection
- A collection of community maintained packages.
- All contained in a single GitHub repo
- Largest package collection of any Linux Distribution
- Also contains a vast library of utilities for writing packages
- Build tools for Python / Rust / Go / JavaScript /etc.
- Qt wrappers
- JSON / TOML / XML / Text builders
- Singularity image builders
6. Varying levels of support for
- Tiers
- Tier 1
- Continuously tested support
- Tier 2
- Endorsed support, but may lack pre-built packages and require special configuration.
- Tier 3
- No pre-built packages, many packages won't run
- Tier 4
- Only packages specifically designed for this environment will run
- Tier 5
- Tooling exists, but no other effort provided.
- Platforms
- Tier 1
- Linux x86-64
- Tier 2
- Darwin/Mac OS, Linux ARM
- Tier 3
- BSD, Windows
- Tier 4
- Android, iOS
- Tier 5
- The browser
There is a current European Commission grant funding the move of Android devices from Tier 4 to Tier 1.
7. The Nixos Distribution
- A linux distribution using
nix
as the primary package manager - All system aspects are controlled by nix
- Management of system services
- Apache / Nginx / Let's Encrypt
- User accounts
- Disk mounting
- Can rollback an OS upgrade in under a minute
- Is not required for using nix
8. Flakes
Just to make things extra confusing, the nix community is transitioning to a new architure: flake.nix
- Channel style
- Maintained local "channel" that points to a version of nixpkgs
- Very hard to keep multiple systems on the same channel
- Could only share software by adding to
nixpkgs
repository
- Flake style
- ALL dependency relationships are maintained in a single
flake.nix
file. - ALL dependencies are pinned to an exact version.
- Flakes are composable
- Install flakes from multiple sources that may depend on different version of
nixpkgs
- Use other people's flaeks as the dependencies for your flakes.
- Install flakes from multiple sources that may depend on different version of
- ALL dependency relationships are maintained in a single
9. Resources
- nixos.org The official homepage of the nix project
- The nix manual The manual to the
nix
tool and language - The nixpkgs manual The manual for the
nixpkg
- The Nixos Manual The manual for
Nixos
- Nix Package Search Find a package in the
nixpkgs
collection - nix.dev An opinionated guide to the nix eco-system
- nixpkgs repo The actual code behind the
nixpkgs
collection - Nix pills A very low level explanation of the nix ecosystem
- Home Manager A utility for controlling the user environment through
nix