{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using the experimental `nix` command\n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Building a package with `nix build`\n", "\n", "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.\n", "\n", "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`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing recipe.nix\n" ] } ], "source": [ "%%file recipe.nix\n", "with import (fetchTarball \"channel:nixos-19.09\") {};\n", "hello" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[K[\u001b[32;1m13.3\u001b[0m MiB DL]\n" ] } ], "source": [ "! nix build -f recipe.nix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To obtain the path we can explicitly create a out-link" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[Klrwxrwxrwx 1 freddy users 54 Mar 28 11:34 result -> /nix/store/4w99qz14nsahk0s798a5rw5l7qk1zwwf-hello-2.10\n" ] } ], "source": [ "! nix build -f recipe.nix -o result\n", "! ls -l result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can run the `hello` command that is provided by it" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "! result/bin/hello" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Shell with specified packages with `nix run`\n", "\n", "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.\n", "\n", "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`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[K[\u001b[32;1m13\u001b[0m copied (\u001b[32;1m99.4\u001b[0m MiB), \u001b[32;1m33.2\u001b[0m MiB DL]\n", "hello!\n" ] } ], "source": [ "! nix run --file channel:nixos-20.03 python3 --command python3 -c 'print(\"hello!\")'" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }