{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Nix\n", "\n", "This tutorial given an introduction to the basic `nix-*` commands." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Building with `nix-build`\n", "\n", "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.\n", "\n", "In the following example we create a `recipe.nix` file. In this case,\n", "our recipe is simple: we fetch the latest version of NixOS 19.09 and build\n", "a package from it, `hello`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%file recipe.nix\n", "with import (fetchTarball \"channel:nixos-19.09\") {};\n", "hello" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-build recipe.nix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The store path shows where the build is cached. A symbolic link to that path is also created" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! ls -l result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can run the `hello` command that is provided by it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! result/bin/hello" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Managing channels with `nix-channel`\n", "\n", "Nix has the concept of channels. A channel is essentially a location providing Nix expressions. The Nixpkgs project offers various channels.\n", "\n", "The following lists the currently configured channels" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-channel --list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will now install a channel" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-channel --add https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.09.tar.gz nixos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and can check it is configured" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-channel --list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use the channel, don't forget to update it. This will fetch the latest Nix expression on the location the channel points to." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-channel --update" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installing with `nix-env`\n", "\n", "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-env --query" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now install `hello` in our profile. That way, the application will remain available. Let's install it from our `recipe.nix` file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-env -i -f recipe.nix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now see it is installed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-env --query" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and that we can execute it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! hello" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-env -iA nixos.hello" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-env -iA hello -f https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.09.tar.gz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the example we actually fetched a channel, for which a short-hand notation exists" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-env -iA hello -f \"channel:nixos-19.09\"" ] } ], "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 }