~/.config/nix/nix.conf
/etc/nix/nix.conf
macos
sudo launchctl stop org.nixos.nix-daemon
sudo launchctl start org.nixos.nix-daemon
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
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
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 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 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
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
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 [...]
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
enter a repl
nix repl
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''
nix-repl> [ 2 "foo" true (2+3) ]
[ 2 "foo" true 5 ]
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 = { ... }; }
nix-repl> rec { a = 3; b = a+4; }
{ a = 3; b = 7; }
nix-repl> if true then "foo" else "bar"
"foo"
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
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"]
{ 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 "
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"
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
nix-repl> (x: y: x*x + y*y) 3 7
58
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
nix-repl> :p { age = 2014-1987; }
27
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
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 [].
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
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'
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; }
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