I tried out NixOS in a VM via Proxmox VE.
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.
Some important facts:
- In Nix, everything is an experssion, there are not statements.
- Values in Nix are immutable.
- Nix is strongly typed, but it's not statically typed.
Launch nix repl
to play with the language.
To interpolate Nix expressions inside strings with the ${...}
syntax:
nix-repl> foo = "strval" nix-repl> "${foo}" "strval"
Lists are a sequence of expressions delimited by space:
nix-repl> [ 2 "foo" true (2+3) ] [ 2 "foo" true 5 ]
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 "baz" nix-repl> s."123" "num"
nix-repl> a = 3 nix-repl> b = 4 nix-repl> if a > b then "yes" else "no" "no"
This expression is to define local variables for inner expressions.
nix-repl> let a = 3; b = 4; in a + b 7
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 7 nix-repl> with longName; a + b 7
Anonymous (lambdas) function:
nix-repl> x: x*2 «lambda»
We can store functions in variables:
nix-repl> double = x: x*2 nix-repl> double «lambda» nix-repl> double 3 6
To take multiple parameters:
nix-repl> mul = a: b: a*b nix-repl> mul «lambda» nix-repl> mul 3 «lambda» nix-repl> mul 3 4 12 nix-repl> mul (6+7) (8+9) 221
To take arguments set as input:
nix-repl> mul = { a, b }: a*b nix-repl> mul { a = 3; b = 4; } 12
To sepcify default values of attributes in the arguments set:
nix-repl> mul = { a, b ? 2 }: a*b nix-repl> mul { a = 3; } 6 nix-repl> mul { a = 3; b = 4; } 12
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 12
The configuration file is located at /etc/nixos/configuration.nix
. Whenever we changed the file, we should do:
nixos-rebuild switch