Skip to content

Instantly share code, notes, and snippets.

@ssledz
Last active March 6, 2023 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssledz/cf1abe42592ab3c570922cf29e2adb4c to your computer and use it in GitHub Desktop.
Save ssledz/cf1abe42592ab3c570922cf29e2adb4c to your computer and use it in GitHub Desktop.

Config

  • ~/.config/nix/nix.conf
  • /etc/nix/nix.conf

restart nix deamon

macos

sudo launchctl stop org.nixos.nix-daemon
sudo launchctl start org.nixos.nix-daemon

The unified CLI

nix

install a package from Nixpkgs

nix profile install nixpkgs#hello

list installed packages

nix profile list

show profile history

nix profile history

rollback profile

nix profile rollback

delete non-current versions of a profile

nix profile wipe-history

Profile

current profile

ls -l ~/.nix-profile/

all per suer profiles

ls -la /nix/var/nix/profiles/per-user/ssledz

channel

ls -la /nix/var/nix/profiles/per-user/ssledz/channels

switching profile (If the profile doesn’t exist, it will be created automatically)

nix-env --switch-profile /nix/var/nix/profiles/my-profile

Garbage collection

deletes generation 10 11 14

nix-env --delete-generations 10 11 14

delete all generations older than

nix-env --delete-generations 14d

running garbage collection

nix-store --gc

to check what be deleted

nix-store --gc --print-dead

deletes all old generations of all profiles in /nix/var/nix/profiles

nix-collect-garbage -d

Search

web based search

search the packages in a flake based on a search term

nix search nixpkgs jq

In this command, the nixpkgs flake reference is shorthand for github:NixOS/nixpkgs

output search results as JSON

nix search nixpkgs jq --json

exploring flake

nix flake show github:nix-community/nixpkgs-wayland

it is equivalent to

nix search nixpkgs wayland

Install

install package hello

nix-env -i hello

list installed derivations

nix-env -q

with derivation paths

nix-env -q --out-path
cabal2nix-2.17.0  /nix/store/53931sbbvnzsvq2w4b7lhql6cz9idwd1-cabal2nix-2.17.0
hello-2.10        /nix/store/kzq2f6pqb3ig89278n3c21g6x4y3pghs-hello-2.10
nix-2.3.12        /nix/store/rn9899jk59ckr4fkvxiax8abmb53kf53-nix-2.3.12
nix-prefetch-git  /nix/store/ajf5bg2wxnr465lzjrzqmv4z4n22cchc-nix-prefetch-git

installed binaries

ls -l ~/.nix-profile/bin/
lrwxrwxrwx 1 ssledz ssledz 74 sty  1  1970 cabal2nix -> /nix/store/53931sbbvnzsvq2w4b7lhql6cz9idwd1-cabal2nix-2.17.0/bin/cabal2nix
lrwxrwxrwx 1 ssledz ssledz 76 sty  1  1970 hackage2nix -> /nix/store/53931sbbvnzsvq2w4b7lhql6cz9idwd1-cabal2nix-2.17.0/bin/hackage2nix
lrwxrwxrwx 1 ssledz ssledz 64 sty  1  1970 hello -> /nix/store/kzq2f6pqb3ig89278n3c21g6x4y3pghs-hello-2.10/bin/hello
lrwxrwxrwx 1 ssledz ssledz 62 sty  1  1970 nix -> /nix/store/rn9899jk59ckr4fkvxiax8abmb53kf53-nix-2.3.12/bin/nix

uninstall a package

nix-env -e hello

test package without installing it

nix-shell -p hello

Channels

subscribe to a channel

nix-channel --add https://nixos.org/channels/nixpkgs-unstable

unsubscribe

nix-channel --remove nixpkgs

update a channel (like apt-get update)

nix-channel --update

upgrade all packages in env

nix-env -u

list of channels

nix-channel --list
nixpkgs https://nixos.org/channels/nixpkgs-unstable

all available packages in a channel

nix-env -qa

Other

list generations

nix-env --list-generations

rollback generation

nix-env --rollback

or rollback to generation 3

nix-env -G 3

switch to generation 43

nix-env --switch-generation 43

show runtime dependencies for hello

nix-store -q --references `which hello`

show dependent derivaties on hello

nix-store -q --referrers `which hello`

closure of hello (all deps, recursively obtained)

nix-store -qR `which hello`

a tree view of it

nix-store -q --tree `which hello`
/nix/store/kzq2f6pqb3ig89278n3c21g6x4y3pghs-hello-2.10
+---/nix/store/adxc893j47gxx3xjw403zdf0liiddvw2-glibc-2.32-48
|   +---/nix/store/skzi7q7ip4my4z7vcyvqfli85gnmialv-libidn2-2.3.1
|   |   +---/nix/store/jlz466np275y4myx4vbmab3w7mq6ph8w-libunistring-0.9.10
|   |   |   +---/nix/store/jlz466np275y4myx4vbmab3w7mq6ph8w-libunistring-0.9.10 [...]
|   |   +---/nix/store/skzi7q7ip4my4z7vcyvqfli85gnmialv-libidn2-2.3.1 [...]
|   +---/nix/store/adxc893j47gxx3xjw403zdf0liiddvw2-glibc-2.32-48 [...]
+---/nix/store/kzq2f6pqb3ig89278n3c21g6x4y3pghs-hello-2.10 [...]

Flakes

Reference Description
path:/home/nix-stuff/my-flake The /home/nix-stuff/my-flake directory on the current host
github:DeterminateSystems/zero-to-nix The DeterminateSystems/zero-to-nix GitHub repository
github:DeterminateSystems/zero-to-nix/other The other branch of the DeterminateSystems/zero-to-nix GitHub repository
github:DeterminateSystems/zero-to-nix/d51c83a5d206e882a6f15a282e32b7079f5b6d76 Commit d51c83a5d206e882a6f15a282e32b7079f5b6d76 on the DeterminateSystems/zero-to-nix GitHub repository
nixpkgs The most recent revision of the nixpkgs-unstable branch of Nixpkgs (an alias for github:NixOS/nixpkgs)
nixpkgs/release-22.11 The release-22.11 branch of Nixpkgs

flage registry contains symbolic identifiers like

Symbolic Identifier Flake Reference.
nixpkgs github:NixOS/nixpkgs/nixpkgs-unstable
flake-utils github:numtide/flake-utils
home-manager github:nix-community/home-manager

explore flake

nix flake show github:nix-community/nixpkgs-wayland

Nix Language

enter a repl

nix repl

expressions

nix-repl> builtins.div 6 3
2
nix-repl> 6 / 3
2
nix-repl> 7-4
3
nix-repl> 1+3
4
nix-repl> "foo"
"foo"
nix-repl> ''foo''
"foo"
nix-repl> foo = "strval"
nix-repl> "$foo"
"$foo"
nix-repl> "${foo}"
"strval"
nix-repl> ''${foo}''
"strval"
nix-repl> ''test " test''
"test \" test"
nix-repl> "\${foo}"
"${foo}"
nix-repl> ''test ''${foo} test''

lists delimited by space

nix-repl> [ 2 "foo" true (2+3) ]
[ 2 "foo" true 5 ]

attribute sets (keys can only be strings)

lazy in nature (:p expr - Evaluate and print expression recursively)

nix-repl> {a = 1; b = { c = 1; }; }    
{ a = 1; b = { ... }; }
nix-repl> :p {a = 1; b = { c = 1; }; }
{ a = 1; b = { c = 1; }; }
nix-repl> s = { foo = "bar"; a-b = "baz"; "123" = "num"; }
nix-repl> s
{ "123" = "num"; a-b = "baz"; foo = "bar"; }
nix-repl> s.foo
"bar"
nix-repl> s."123"  
"num"
nix-repl> s.a-b
"baz"

has Attribute

nix-repl>  {a = 1; b = { c = 1; }; } ? a
true
nix-repl>  {a = 1; b = { c = 1; }; } ? b.c
true
nix-repl>  {a = 1; b = { c = 1; }; } ? b.d

update set

{a = 1; b = { c = 1; }; } // { c = 4; }
{ a = 1; b = { ... }; c = 4; }
nix-repl>  {a = 1; b = { c = 1; }; } // { a = 4; } 
{ a = 4; b = { ... }; }

recursive attribute sets (you can refer to keys inside)

nix-repl> rec { a = 3; b = a+4; }
{ a = 3; b = 7; }

if expressions

nix-repl> if true then "foo" else "bar"
"foo"

functions

pattern: body

patterns:

  • single indentifier pattern - matches any argument
  • set pattern - matches a set containing the listed attributes
  • @-pattern - referring to the whole value being matched

single indentifier pattern

nix-repl> concat = a: b: a + b
nix-repl> concat "foo " "bar"
"foo bar"
nix-repl> let concat = a: b: a + b; in map (concat "foo ") ["bar" "mur"]

set pattern

{ x, y, z, ... }: z + y + x

default agruments

nix-repl> concat = { x, y ? "foo" }: x + y
nix-repl> concat { x = "bar "; }
"bar foo"
nix-repl> concat { x = "foo "; y = "bar "; }
"foo bar "

set @-pattern

args@{ x, y, z, ... }: z + y + x + args.a
{ x, y, z, ... } @ args: z + y + x + args.a
nix-repl> concat = args@{ x, y }: x + args.y
nix-repl> concat { x = "foo "; y = "bar"; }
"foo bar"

recursion

nix-repl> let factorial = n: if n <= 1 then 1 else n * factorial (n - 1); in factorial 8
40320
nix-repl> moduleA = let factorial = n: if n <= 1 then 1 else n * factorial (n - 1); 
                    in { inherit factorial; }
nix-repl> moduleA.factorial 5
120

anonymous function

nix-repl> (x: y: x*x + y*y) 3 7
58

inherit

inherit xs ~ xs = xs
nix-repl> xs = { a=1; }
nix-repl> ys = { inherit xs; b=2; }
nix-repl> ys.xs.a
1
nix-repl> zs = { xs = xs; b=2; }
nix-repl> zs.xs.a
1

strict evaluation

nix-repl> :p { age = 2014-1987; }
27

boolean operators

short-circuiting

Operator expression         Equivalent to
! A                         if A then false else true
A || B                      if A then true else B
A && B                      if A then B else false
A -> B                      if A then B else true

builtins

nix-repl> builtins
{ abort = «primop»; add = «primop»; addErrorContext = «primop»; all = «primop»; 
any = «primop»; appendContext = «primop»; attrNames = «primop»; attrValues = «primop»; 
baseNameOf = «primop»; bitAnd = «primop»; bitOr = «primop»; bitXor = «primop»; 
builtins = { ... }; catAttrs = «primop»; ...}
nix-repl> builtins.elemAt [1 (1+1) "three"] 1
2
nix-repl> builtins.attrNames { age = 26; name = "james"; }
[ "age" "name" ]
nix-repl> builtins.tail [1 2 3 4 5]
[ 2 3 4 5 ]
nix-repl> builtins.head [1 2 3 4 5]
1
nix-repl> [1 2 3] ++ [4 5 6]
[ 1 2 3 4 5 6 ]
nix-repl> :doc builtins.head
Synopsis: builtins.head list
    Return the first element of a list; abort evaluation if the argument isn’t a list or is an empty list. 
    You can test whether a list is empty by comparing it with [].

dynamic scope (with)

with - takes a set S, and another expression E, and puts the values of the set S in scope when evaluating the expression E

nix-repl> module = with (import ./lib/streams.nix);
let
  fib = a: b: {
      head = a;
      tail = fib b (a + b);
  }; 
in { fib = n: elemAt (fib 1 1) n; }
nix-repl> module.fib 5 
8

exceptions

cause the entire program to abort.

nix-repl> throw "I'm an exception"
error: I'm an exception

catching exceptions

nix-repl> builtins.tryEval (throw "I'm an exception")   
{ success = false; value = false; }
nix-repl> builtins.tryEval (2 * 4)
{ success = true; value = 8; }

tryEval can't catch errors like

nix-repl> builtins.tryEval (abort "I'm an error")
error: evaluation aborted with the following error message: `I'm an error'

assertions

nix-repl> assert (2 < 1);
error: assertion failed at (string):1:1
nix-repl> assert (1 < 2); 1 + 3
4

assertion is an exception so can be catch with tryEval

nix-repl> builtins.tryEval ( assert (2<1); 1 + 1  )
{ success = false; value = false; }

debuging

builtins.trace : forall a b. a -> b -> b
nix-repl> builtins.trace "sum [1 2]" builtins.foldl' (builtins.add) 0 [1 2]
trace: sum [1 2]
3

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment