Nix
Table of Contents
Nix is the framework of pure dependency chain that does not depends on
system specific things. The dependencies are all stored with hashes in the /nix and the
required packages are loaded on demand.
Nixpkgs is the package management system using Nix.
NixOS is the entire Linux system that is managed by Nix.
1. Installation
1.1. Linux
sudo pacman -S nix
Nix relies on a background daemon for multi-user management. Enable and start `nix-daemon` using `systemctl`:
sudo systemctl enable --now nix-daemon
Add the Nixpkgs channel so you have access to packages:
nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs
nix-channel --update
Verify that Nix is working properly by checking the version and running a test package in an ephemeral shell:
nix --version
nix-shell -p hello --run "hello"
1.2. WSL
In order to install nix as multi-user daemon,systemd must be enabled
in WSL. /etc/wsl.conf must contain:
[boot]
systemd=true
Install nix by running the official script
curl --proto '=https' --tlsv1.2 -L https://nixos.org/nix/install | sh -s -- --daemon
Verify the same way.
1.3. Flake
Nix flake is enabled separately in ~/.config/nix/nix.conf
experimental-features = nix-command flakes
2. CLI
2.1. nix
2.2. nix-shell
nix-shell is a temporary environment that packages can be
tried.
nix-shell options
-p,--packages: add space separated list of packages to shell--run "COMMAND": run shell command in it--pure: discard most environment variables-I VAR=VALUE: determine source.nixpkgs=https://github.com/NixOS/nixpkgs/tarball/2a601aafdc5605a5133a2ca506a34a3a73377247set specific Git revision of nixpkgs
nix-shell can also be run as script.
It interprets non-system shebang lines as well.
#!/usr/bin/env nix-shell # system shebang used by the kernel.
#! nix-shell -i bash --pure
#! nix-shell -p bash cacert curl jq python3Packages.xmljson
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/2a601aafdc5605a5133a2ca506a34a3a73377247.tar.gz
curl https://github.com/NixOS/nixpkgs/releases.atom | xml2json | jq .
nix-shell *.nix can be used to import the derivation setup without building it.
This allows the user to test the building steps manually.
2.3. nix-env
nix-env sets up Nix environment for current user.
The environment is set up in ~/.nix-profile, and
~/.nix-profile/bin is used to incorporate the nix programs
into the main system.
nix-env options
-i PACKAGEinstall. The package can be generic name, store path, or result symlink to store path.
nix-env uses ~/.nix-defexpr to find nixpkgs instead of NIX_PATH environment
variable.
2.4. nix-store
Manipulate the Nix store (/nix/store).
nix-store
-qquery--references FILENAMEdirect runtime dependencies--referrers FILENAMEreverse dependencies
2.5. nix-channel
Manage list of channels (remote repo).
The configuration is stored in ~/.nix-channels.
2.6. nix-instantiate
Run .nix script that defines a .drv (derivation) file
2.7. nix-build
Build derivation generated by .nix script.
If the expression is derivation, build it,
while if it is a function, call it and built the resulting derivation.
3. Nix Language
Stage 1 compiler is the pure compiler with impure libraries, state 2 compiler is with the pure libraries.
Basic libraries and compilers are provided by default.
3.1. Primitive Types
- String
"a" - Number
37 - Boolean
truefalse - Path
./modules/zram.nix - List
[ 1 2 3] - Set
{ name = "Mark"; age = 37; }.to access the property
Spaces are only required when separating identifiers (lexical tokens).
Attribute set
{
a = 1;
b = "hello ${builtins.getEnv "USER"}";
c = {
i = 0.1;
};
d = [ 1 "two" false ];
}
Recursive attribute set is also possible.
Similar to LISP let*
rec {
one = 1;
two = one + 1;
three = two + 1;
}
3.2. Variables and Functions
let
x = "Hello, World!";
f = x: x + 1;
in
{
string = x;
}
Function A lambda expression that supports currying.
The arguments are given by separating them with spaces.
x: x + 1
x: y: x + y
{ a, b }: a + b
{ a, b ? 0 }: a + b
{ a, b, ... }: a + b
{ a, b, ... } @ args: a + b + args.c
@ pattern is there to absorb the excessive arguments into a attribute set (here, args).
Functions can be assigned to a variable (f = x: x + 1). and
be evaluated by appending the arguments (f 1).
The ambiguity in the order of evaluation is resolved by parenthesis.
3.3. Keywords
let VAR1=VAL1; VAR=VAL2; in ...define local variablesinherit (...) ...inherit the definition into the let scope or attribute set.import FILENAMEresolve to the top-level object defined in the file.builtinscollection of standard functionsgetEnvfetchurlfetch from URL and return the saved location in the nix store.fetchTarBalllfetch and extract and return the location
4. Configuration
4.1. shell.nix
pkgs.mkShell, pkgs.mkShellNoCC take attribute set
packages = with pkgs; [ ... ];
4.2. Derivation
A derivation is set of instructions for building a program.
Defined using derivation function, and stored in .drv file.
4.3. nixpkgs
The nix script stack that manages derivations.
The main script takes system and config.
config is imported from ~/.config/nixpkgs/config.nix by default,
or can be defined in NIXPKGS_CONFIG.
5. Nix Flake
The external dependencies like the nix-channel and such are hashed
for reproducibility.
Flake remove the dependency on the environment completely, such as nix channel, and configurations.