Interactive online version . Generated from tutorials/01-nix/02-using-experimental-nix-command.ipynb.
Using the experimental nix
command¶
The previous tutorial showed how to use Nix commands such as nix-build
and nix-env
. In this tutorial we’ll use the new but still experimental nix
command.
Building a package with nix build
¶
Instead of nix-build
it is now possible to build using nix build
. Note that, contrary to nix-build
, no out-link is created by default.
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 -f recipe.nix
[13.3 MiB DL]
To obtain the path we can explicitly create a out-link
[3]:
! nix build -f recipe.nix -o result
! ls -l result
lrwxrwxrwx 1 freddy users 54 Mar 28 11:34 result -> /nix/store/4w99qz14nsahk0s798a5rw5l7qk1zwwf-hello-2.10
We can run the hello
command that is provided by it
[4]:
! result/bin/hello
Hello, world!
Shell with specified packages with nix run
¶
The nix run
command allows you to use a program temporarily; after closing the shell the program is “gone”. Well, not really, it is still in the store but it can be garbage-collected again.
The following one-liner calls nix run
, tells it to use the Nix expressions found at channel-nixos-20.03
, and then open a shell that has the Python 3 package. We also directly invoke python3
using --command
and tell python3
to print hello
.
[5]:
! nix run --file channel:nixos-20.03 python3 --command python3 -c 'print("hello!")'
[13 copied (99.4 MiB), 33.2 MiB DL]
hello!