Kevin De Baerdemaeker

Nix

Modified 2024-04-17

domain-specific

comes with built-in functions to modify the Nix store

declarative

data drives the dependencies between operations

pure

values do not change during computation

functional

functions as values

lazy

compute values when needed

dynamically typed

detecting type errors during evaluation

Language TL;DR

From my experiences working with NixOS, these are the most common concepts to learn about nix. To be fair, I look these up still.

{ x = 1; y = 2; }               # set with attributes named x and y
{ foo.bar = 1; }                # nested set, equivalent to { foo = { bar = 1; }; }
{ x = 1; y = 2; } // { z = 3; } # merge two sets (right-hand precedence)

                                # adds variables to the current attribute set or let
inherit pkgs src;               # becomes: pkgs = pkgs; src = src;
inherit (pkgs) lib stdenv;      # becomes: lib = pkgs.lib; stdenv = pkgs.stdenv;

x: x + 1                        # function which expects an integer
x: y: x + y                     # curried function
(x: x + 1) 100                  # function call, evaluates 101

{ x, y }: x + y                 # a) function which expects a set
{ x, y ? "bar" }: x + y         # like a) but has a default attribute
{ x, y, ... }: x + y            # like a) but ignores all other attributes
{ x, y } @ args: x + y          # like a) but binds the whole set to "args"

Frequent Commands

nix-collect-garbage

Deletes all unreachable store objects in the nix store to clean up the system.

nix-collect-garbage --delete-older-than 30d

nix repl (evaluating snippets)

If you want to load in NixOS packages.

:l <nixpkgs>

You can also load in the current flake configuration.

:lf .

nix shell (run a shell with given packages)

nix shell nixpkgs#hello

nix develop (run a shell with build environment of a derivation)

nix develop nixpkgs#hello

nix run (run a nix application)

nix develop nixpkgs#hello

Infrequent Commands

nix edit (open the nix expression of a package)

nix edit nixpkgs#cat

Retrieving Hashes

nix-prefetch-url

nix shell nixpkgs#nix-prefetch-scripts
nix-prefetch-url https://google.com

nurl

nix shell nixpkgs#nurl
nurl https://github.com/venikx/dotfiles

References


Backlinks