#!/usr/bin/env python # coding: utf-8 # # 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`. # In[1]: get_ipython().run_cell_magic('file', 'recipe.nix', 'with import (fetchTarball "channel:nixos-19.09") {};\nhello\n') # In[2]: get_ipython().system(' nix build -f recipe.nix') # To obtain the path we can explicitly create a out-link # In[3]: get_ipython().system(' nix build -f recipe.nix -o result') get_ipython().system(' ls -l result') # We can run the `hello` command that is provided by it # In[4]: get_ipython().system(' result/bin/hello') # ## 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`. # In[5]: get_ipython().system(' nix run --file channel:nixos-20.03 python3 --command python3 -c \'print("hello!")\'')