Interactive online version Binder badge. Generated from tutorials/01-nix/01-using-nix.ipynb.

Using Nix

This tutorial given an introduction to the basic nix-* commands.

Building with nix-build

The nix-build command is used for building recipes, also known as derivations. The resulting build artifacts are packages that can be installed or copied elsewhere.

In the following example we create a recipe.nix file. In this case, our recipe is simple: we fetch the latest version of NixOS 19.09 and build a package from it, hello.

[1]:
%%file recipe.nix
with import (fetchTarball "channel:nixos-19.09") {};
hello
Writing recipe.nix
[2]:
! nix-build recipe.nix
unpacking 'https://nixos.org/channels/nixos-19.09/nixexprs.tar.xz'...
these paths will be fetched (0.04 MiB download, 0.20 MiB unpacked):
  /nix/store/4w99qz14nsahk0s798a5rw5l7qk1zwwf-hello-2.10
copying path '/nix/store/4w99qz14nsahk0s798a5rw5l7qk1zwwf-hello-2.10' from 'https://cache.nixos.org'...
/nix/store/4w99qz14nsahk0s798a5rw5l7qk1zwwf-hello-2.10

The store path shows where the build is cached. A symbolic link to that path is also created

[3]:
! ls -l result
lrwxrwxrwx 1 runner docker 54 May 10 07:11 result -> /nix/store/4w99qz14nsahk0s798a5rw5l7qk1zwwf-hello-2.10

We can run the hello command that is provided by it

[4]:
! result/bin/hello
Hello, world!

Managing channels with nix-channel

Nix has the concept of channels. A channel is essentially a location providing Nix expressions. The Nixpkgs project offers various channels.

The following lists the currently configured channels

[5]:
! nix-channel --list

We will now install a channel

[6]:
! nix-channel --add https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.09.tar.gz nixos

and can check it is configured

[7]:
! nix-channel --list
nixos https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.09.tar.gz

To use the channel, don’t forget to update it. This will fetch the latest Nix expression on the location the channel points to.

[8]:
! nix-channel --update
these derivations will be built:
  /nix/store/2xyq4hldhcbd2j1lbzbznwakc6fb7mma-nixos-19.09.drv
building '/nix/store/2xyq4hldhcbd2j1lbzbznwakc6fb7mma-nixos-19.09.drv'...
unpacking channels...
created 1 symlinks in user environment

Installing with nix-env

Nix has the concept of profiles where users can install packages in. The nix-env command is used for installing packages. Users have a default profile, let’s see if anything is installed for the current user

[9]:
! nix-env --query

We can now install hello in our profile. That way, the application will remain available. Let’s install it from our recipe.nix file.

[10]:
! nix-env -i -f recipe.nix
installing 'hello-2.10'
building '/nix/store/2dqjaaji1fyfhixacmrfyk0vqm8zkxnz-user-environment.drv'...
created 2 symlinks in user environment

We can now see it is installed

[11]:
! nix-env --query
hello-2.10

and that we can execute it

[12]:
! hello
Hello, world!

We can install the same package but from the channel we configured. Note the package of interest, hello, is an attribute of the Nix expression that the channel nixos corresponds to

[13]:
! nix-env -iA nixos.hello
replacing old 'hello-2.10'
installing 'hello-2.10'
building '/nix/store/gfyg482f00vvpkafszv8hxzy2jamn3js-user-environment.drv'...
created 2 symlinks in user environment

Instead of installing from a local file, we can also fetch an expression. It’s possible to point to any Nix expression or archive containing Nix expressions.

[14]:
! nix-env -iA hello -f https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.09.tar.gz
unpacking 'https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.09.tar.gz'...
replacing old 'hello-2.10'
installing 'hello-2.10'
building '/nix/store/xa305q39lg4ykvdgnmalr1pya6qb1fvf-user-environment.drv'...
created 2 symlinks in user environment

In the example we actually fetched a channel, for which a short-hand notation exists

[15]:
! nix-env -iA hello -f "channel:nixos-19.09"
unpacking 'https://nixos.org/channels/nixos-19.09/nixexprs.tar.xz'...
replacing old 'hello-2.10'
installing 'hello-2.10'
building '/nix/store/xd53ixgrylmxm7l5sncac1rhfxflp0iq-user-environment.drv'...
created 2 symlinks in user environment