Nix

Table of Contents

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

nix subcommands

  • repl start REPL
  • profile profile management that replaces nix-env. It can continue on nix-env but not vice versa.
  • build same as nix-build
  • develop run devShell

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/2a601aafdc5605a5133a2ca506a34a3a73377247 set 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 PACKAGE install. 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

  • -q query
    • --references FILENAME direct runtime dependencies
    • --referrers FILENAME reverse 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

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;
}

Function A lambda expression that supports currying.

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.

Keywords

  • let VAR1=VAL1; VAR=VAL2; in ... define local variables
  • inherit (...) ... inherit the definition into the let scope or attribute set.
  • import FILENAME resolve to the top-level object defined in the file.
  • builtins collection of standard functions
    • getEnv
    • fetchurl fetch from URL and return the saved location in the nix store.
    • fetchTarBalll fetch 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.

4.4. Flake

Flake remove the dependency on the environment completely, such as nix channel, and configurations.

5. References

Author: Jeemin Kim

Created: 2026-10-27 Tue 05:56