OS Installation

I tried out NixOS in a VM via Proxmox VE.

Package Installation

To install hello, a simple CLI tool which prints Hello world:

nix-env -i hello

Now we can run hello. Note that everytime we install a new package, it created a new user environment. That's a new generation of our Nix user profile.

We can list generations:

nix-env --list-generations

List installed packages:

nix-env -q


In NixOS, there's a list of channels from which we get packages. The tool to manage channels is nix-channel.

To print all subscribed channels:

nix-channel --list

To update the channel:

nix-channel --update

This is similar to apt-get update in Debian.

The Nix Language

Some important facts:

Launch nix repl to play with the language.


To interpolate Nix expressions inside strings with the ${...} syntax:

nix-repl> foo = "strval"
nix-repl> "${foo}"


Lists are a sequence of expressions delimited by space:

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

Attribute sets

Attribute sets are an associatation between string keys and Nix values.

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

If expressions

nix-repl> a = 3
nix-repl> b = 4
nix-repl> if a > b then "yes" else "no"

Let expressions

This expression is to define local variables for inner expressions.

nix-repl> let a = 3; b = 4; in a + b

With expressions

with takes an attribute set and includes symbols from it in the scope of the inner expression.

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


Anonymous (lambdas) function:

nix-repl> x: x*2

We can store functions in variables:

nix-repl> double = x: x*2
nix-repl> double
nix-repl> double 3

To take multiple parameters:

nix-repl> mul = a: b: a*b
nix-repl> mul
nix-repl> mul 3
nix-repl> mul 3 4
nix-repl> mul (6+7) (8+9)

To take arguments set as input:

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

To sepcify default values of attributes in the arguments set:

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


The import function provides a way to parse a .nix file.

nix-repl> a = import ./a.nix
nix-repl> b = import ./b.nix
nix-repl> mul = import ./mul.nix
nix-repl> mul a b


The configuration file is located at /etc/nixos/configuration.nix. Whenever we changed the file, we should do:

nixos-rebuild switch