Everything that’s on this site was taken from [[https://nixos.org/guides/nix-pills]]

Beginning

To install just go to official website it’s pretty straightforward

Nix-env

Install / remove /upgrade a package

nix-env -iA nixpkgs.kubectl
nix-env -e 'kubectl'
nix-env -u kubectl

List installed packages

nix-env -q --installed

Check your env history

nix-env --list-generations

Rollback / switch

nix-env --rollback
nix-env -G <id>

Expressions

Nix is only expressions not statements this means you always have to return a output (cannot do a if without a else for example).

if :

if a > b then "yes" else "no"

let :

let a = 5; b = 4 ; in a + b
9

let a = 3 ; in let b = 4; in a+b
7

let a = 4 ; in let a = 8; in a
8

with :

nix-repl> longName = { a = 3; b = 4; }
nix-repl> longName.a + longName.b
7
nix-repl> with longName; a+b
7

Functions

Single param

nix-repl> double = x: x*2
nix-repl> double
«lambda»
nix-repl> double 3
6

Mutli params

nix-repl> mul = a: (b: a*b)
nix-repl> mul
«lambda»
nix-repl> mul 3
«lambda»
nix-repl> (mul 3) 4
12

But you can simplify by removing parenthesis

nix-repl> mul = a: b: a*b
nix-repl> mul
«lambda»
nix-repl> mul 3
«lambda»
nix-repl> mul 3 4
12

And you can even do function of function

nix-repl> foo = mul 3
nix-repl> foo 4
12
nix-repl> foo 5
15

Default and optional on set function

nix-repl> mul = { a, b ? 2 }: a*b
nix-repl> mul { a = 3; }
6
nix-repl> mul { a = 3; b = 4; }
12

nix-repl> mul = { a, b, ... }: a*b
nix-repl> mul { a = 3; b = 4; c = 2; }
12

nix-repl> mul = s@{ a, b, ... }: a*b*s.c
nix-repl> mul { a = 3; b = 4; c = 2; }
24

Upgrade and cleanup :

$ nix-channel --update
$ nix-env -u --always
$ rm /nix/var/nix/gcroots/auto/*
$ nix-collect-garbage -d    <-------- Delete all previous generations be careful