Nix User's Guide

Draft (Version 0.6.1)

Eelco Dolstra


Table of Contents

1. Introduction
2. Quick Start
3. Installation
3.1. Obtaining Nix
3.2. Prerequisites
3.3. Building Nix from source
3.4. Installing from RPMs
3.5. Permissions
3.5.1. Setuid installation
3.6. Using Nix
4. Package Management
4.1. Basic package management
4.2. Profiles
4.3. Garbage collection
4.3.1. Garbage collector roots
4.4. Channels
5. Writing Nix Expressions
5.1. A simple Nix expression
5.1.1. The Nix expression
5.1.2. The builder
5.1.3. Composition
5.1.4. Testing
5.1.5. The generic builder
5.2. The Nix expression language
5.3. The standard environment
6. Setting up a Build Farm
6.1. Overview
6.2. Setting up distributed builds
A. Command Reference
A.1. nix-env
A.2. nix-store
A.3. nix-instantiate
A.4. nix-collect-garbage
A.5. nix-push
A.6. nix-pull
A.7. nix-prefetch-url
B. Troubleshooting
C. Bugs / To-Do

List of Figures

4.1. User environments

List of Tables

5.1. Operators

List of Examples

5.1. Nix expression for GNU Hello (default.nix)
5.2. Build script for GNU Hello (builder.sh)
5.3. Composing GNU Hello (all-packages-generic.nix)
5.4. Build script using the generic build functions
5.5. Nix expression for Subversion
6.1. Remote machine configuration: remote-systems.conf

Chapter 1. Introduction

Nix is a system for the deployment of software. Software deployment is concerned with the creation, distribution, and management of software components (“packages”). Its main features are:

  • It helps you make sure that dependency specifications are complete. In general in a deployment system you have to specify for each component what its dependencies are, but there are no guarantees that this specification is complete. If you forget a dependency, then the component will build and work correctly on your machine if you have the dependency installed, but not on the end user's machine if it's not there.

  • It is possible to have multiple versions or variants of a component installed at the same time. In contrast, in systems such as RPM different versions of the same package tend to install to the same location in the file system, so installing one version will remove the other. This is especially important if you want to use applications that have conflicting requirements on different versions of a component (e.g., application A requires version 1.0 of library X, while application B requires a non-backwards compatible version 1.1).

  • Users can have different “views” (“profiles” in Nix parlance) on the set of installed applications in a system. For instance, one user can have version 1.0 of some package visible, while another is using version 1.1, and a third doesn't use it at all.

  • It is possible to atomically upgrade software. I.e., there is no time window during an upgrade in which part of the old version and part of the new version are simultaneously visible (which might well cause the component to fail).

  • Likewise, it is possible to atomically roll back after an install, upgrade, or uninstall action. That is, in a fast (O(1)) operation the previous configuration of the system can be restored. This is because upgrade or uninstall actions don't actually remove components from the system.

  • Unused components can be garbage-collected automatically and safely: when you remove an application from a profile, its dependencies will be deleted by the garbage collector only if there are no other active applications using them.

  • Nix supports both source-based deployment models (where you distribute Nix expressions that tell Nix how to build software from source) and binary-based deployment models. The latter is more-or-less transparent: installation of components is always based on Nix expressions, but if the expressions have been built before and Nix knows that the resulting binaries are available somewhere, it will use those instead.

  • Nix is flexible in the deployment policies that it supports. There is a clear separation between the tools that implement basic Nix mechanisms (e.g., building Nix expressions), and the tools that implement various deployment policies. For instance, there is a concept of “Nix channels” that can be used to keep software installations up-to-date automatically from a network source. This is a policy that is implemented by a fairly short Perl script, which can be adapted easily to achieve similar policies.

  • Nix component builds aim to be “pure”; that is, unaffected by anything other than the declared dependencies. This means that if a component was built succesfully once, it can be rebuilt again on another machine and the result will be the same. We cannot guarantee this (e.g., if the build depends on the time-of-day), but Nix (and the tools in the Nix Packages collection) takes special care to help achieve this.

  • Nix expressions (the things that tell Nix how to build components) are self-contained: they describe not just components but complete compositions. In other words, Nix expressions also describe how to build all the dependencies. This is in contrast to component specification languages like RPM spec files, which might say that a component X depends on some other component Y, but since it does not describe exactly what Y is, the result of building or running X might be different on different machines. Combined with purity, self-containedness ensures that a component that “works” on one machine also works on another, when deployed using Nix.

  • The Nix expression language makes it easy to describe variability in components (e.g., optional features or dependencies).

  • Nix is ideal for building build farms that do continuous builds of software from a version management system, since it can take care of building all the dependencies as well. Also, Nix only rebuilds components that have changed, so there are no unnecessary builds. In addition, Nix can transparently distribute build jobs over different machines, including different platforms.

  • Nix can be used not only for software deployment, but also for service deployment, such as the deployment of a complete web server with all its configuration files, static pages, software dependencies, and so on. Nix's advantages for software deployment also apply here: for instance, the ability trivially to have multiple configurations at the same time, or the ability to do rollbacks.

This manual tells you how to install and use Nix and how to write Nix expressions for software not already in the Nix Packages collection. It also discusses some advanced topics, such as setting up a Nix-based build farm, and doing service deployment using Nix.

Warning

This manual is a work in progress. It's quite likely to be incomplete, inconsistent with the current implementation, or simply wrong.

Note

Some background information on Nix can be found in two papers. The ICSE 2004 paper Imposing a Memory Management Discipline on Software Deployment discusses the hashing mechanism used to ensure reliable dependency identification and non-interference between different versions and variants of packages. The LISA 2004 paper Nix: A Safe and Policy-Free System for Software Deployment gives a more general discussion of Nix from a system-administration perspective.

Chapter 2. Quick Start

This chapter is for impatient people who don't like reading documentation. For more in-depth information you are kindly referred to the following chapters.

  1. Download a source tarball or RPM from http://www.cs.uu.nl/groups/ST/Trace/Nix. Build source distributions using the regular sequence:

    $ tar xvfj nix-version.tar.bz2
    $ ./configure
    $ make
    $ make install (as root)

    This will install Nix in /nix. You shouldn't change the prefix if at all possible since that will make it impossible to use our pre-built components. Alternatively, you could grab an RPM if you're on an RPM-based system. You should also add /nix/etc/profile.d/nix.sh to your ~/.bashrc (or some other login file).

  2. Subscribe to the Nix Packages channel.

    $ nix-channel --add http://catamaran.labs.cs.uu.nl/dist/nix/channels/nixpkgs-unstable

  3. Download the latest Nix expressions available in the channel.

    $ nix-channel --update

    Note that this in itself doesn't download any components, it just downloads the Nix expressions that build them and stores them somewhere (under ~/.nix-defexpr, in case you're curious). Also, it registers the fact that pre-built binaries are available remotely.

  4. See what installable components are currently available in the channel:

    $ nix-env -qa
    docbook-xml-4.2
    firefox-1.0pre-PR-0.10.1
    hello-2.1.1
    libxslt-1.1.0
    ...

  5. Install some components from the channel:

    $ nix-env -i hello firefox ... 

    This should download the pre-built components; it should not build them locally (if it does, something went wrong).

  6. Test that they work:

    $ which hello
    /home/eelco/.nix-profile/bin/hello
    $ hello
    Hello, world!
    $ firefox
    (read Slashdot or something)

  7. Uninstall a package:

    $ nix-env -e hello

  8. To keep up-to-date with the channel, do:

    $ nix-channel --update
    $ nix-env -u '*'

    The latter command will upgrade each installed component for which there is a “newer” version (as determined by comparing the version numbers).

  9. If you're unhappy with the result of a nix-env action (e.g., an upgraded component turned out not to work properly), you can go back:

    $ nix-env --rollback

  10. You should periodically run the Nix garbage collector to get rid of unused packages, since uninstalls or upgrades don't actually delete them:

    $ nix-env --delete-generations old
    $ nix-collect-garbage

    The first command deletes old “generations” of your profile (making rollbacks impossible, but also making the components in those old generations available for garbage collection), while the second command actually deletes them.

Chapter 3. Installation

3.1. Obtaining Nix

The easiest way to obtain Nix is to download a source distribution. RPMs for Red Hat 9 are also available. These distributions are generated automatically.

Alternatively, the most recent sources of Nix can be obtained from its Subversion repository. For example, the following command will check out the latest revision into a directory called nix:

$ svn checkout https://svn.cs.uu.nl:12443/repos/trace/nix/trunk nix

Likewise, specific releases can be obtained from the tags directory of the repository. If you don't have Subversion, you can also download an automatically generated compressed tar-file of the head revision of the trunk.

3.2. Prerequisites

The following prerequisites only apply when you build from source. Binary releases (e.g., RPMs) have no prerequisites.

A fairly recent version of GCC/G++ is required. Version 2.95 and higher should work.

To build this manual and the man-pages you need the xmllint and xsltproc programs, which are part of the libxml2 and libxslt packages, respectively. You also need the DocBook XSL stylesheets and optionally the DocBook XML 4.2 DTD. Note that these are only required if you modify the manual sources or when you are building from the Subversion repository.

To build the parser, very recent versions of Bison and Flex are required. (This is because Nix needs GLR support in Bison and reentrancy support in Flex.) For Bison, you need version 1.875c or higher (1.875 does not work), which can be obtained from the GNU FTP server. For Flex, you need version 2.5.31, which is available on SourceForge. Slightly older versions may also work, but ancient versions like the ubiquitous 2.5.4a won't. Note that these are only required if you modify the parser or when you are building from the Subversion repository.

Nix uses Sleepycat's Berkeley DB and CWI's ATerm library. These are included in the Nix source distribution. If you build from the Subversion repository, you must download them yourself and place them in the externals/ directory. See externals/Makefile.am for the precise URLs of these packages. Alternatively, if you already have them installed, you can use configure's --with-bdb and --with-aterm options to point to their respective locations. Note that Berkeley DB must be version 4.2; other versions may not have compatible database formats.

3.3. Building Nix from source

After unpacking or checking out the Nix sources, issue the following commands:

$ ./configure options...
$ make
$ make install

When building from the Subversion repository, these should be preceded by the command:

$ autoreconf -i

The installation path can be specified by passing the --prefix=prefix to configure. The default installation directory is /nix. You can change this to any location you like. You must have write permission to the prefix path.

Warning

It is advisable not to change the installation prefix from its default, since doing so will in all likelihood make it impossible to use derivations built on other systems.

If you want to rebuilt the documentation, pass the full path to the DocBook XML catalog file (docbook.cat) and to the DocBook XSL stylesheets using the --with-docbook-catalog=path and --with-docbook-xsl=path options.

3.4. Installing from RPMs

RPM packages of Nix can be downloaded from http://www.cs.uu.nl/groups/ST/Trace/Nix. These RPMs should work for most fairly recent releases of SuSE and Red Hat Linux. They have been known to work work on SuSE Linux 8.1 and 9.0, and Red Hat 9.0. In fact, it should work on any RPM-based Linux distribution based on glibc 2.3 or later.

Once downloaded, the RPMs can be installed or upgraded using rpm -U. For example,

$ rpm -U nix-0.5pre664-1.i386.rpm

The RPMs install into the directory /nix. Nix can be uninstalled using rpm -e nix. After this it will be necessary to manually remove the Nix store and other auxiliary data:

$ rm -rf /nix/store
$ rm -rf /nix/var

3.5. Permissions

All Nix operations must be performed under the user ID that owns the Nix store and database (prefix/store and prefix/var/nix/db, respectively). When installed from the RPM packages, these directories are owned by root.

3.5.1. Setuid installation

As a somewhat ad hoc hack, you can also install the Nix binaries “setuid” so that a Nix store can be shared among several users. To do this, configure Nix with the --enable-setuid option. Nix will be installed as owned by a user and group specified by the --with-nix-user=user and --with-nix-group=group options. E.g.,

$ ./configure --enable-setuid --with-nix-user=my_nix_user --with-nix-group=my_nix_group

The user and group default to nix. You should make sure that both the user and the group exist. Any “real” users that you want to allow access should be added to the Nix group.

Warning

A setuid installation should only by used if the users in the Nix group are mutually trusted, since any user in that group has the ability to change anything in the Nix store or database. For instance, they could install a trojan horse in executables used by other users.

Warning

On some platforms, the Nix binaries will be installed as setuid root. They drop root privileges immediately after startup and switch to the Nix user. The reason for this is that both the real and effective user must be set to the Nix user, and POSIX has no system call to do this. This is not the case on systems that have the setresuid() system call (such as Linux and FreeBSD), so on those systems the binaries are simply owned by the Nix user.

3.6. Using Nix

To use Nix, some environment variables should be set. In particular, PATH should contain the directories prefix/bin and ~/.nix-profile/bin. The first directory contains the Nix tools themselves, while ~/.nix-profile is a symbolic link to the current user environment (an automatically generated package consisting of symlinks to installed packages). The simplest way to set the required environment variables is to include the file prefix/etc/profile.d/nix.sh in your ~/.bashrc (or similar), like this:

source prefix/etc/profile.d/nix.sh

Chapter 4. Package Management

This chapter discusses how to do package management with Nix, i.e., how to obtain, install, upgrade, and erase components. This is the “user's” perspective of the Nix system — people who want to create components should consult Chapter 5.

4.1. Basic package management

The main command for package management is nix-env. You can use it to install, upgrade, and erase components, and to query what components are installed or are available for installation.

In Nix, different users can have different “views” on the set of installed applications. That is, there might be lots of applications present on the system (possibly in many different versions), but users can have a specific selection of those active — where “active” just means that it appears in a directory in the user's PATH. Such a view on the set of installed applications is called a user environment, which is just a directory tree consisting of symlinks to the files of the active applications.

Components are installed from a set of Nix expressions that tell Nix how to build those components, including, if necessary, their dependencies. There is a collection of Nix expressions called the Nix Package collection that contains components ranging from basic development stuff such as GCC and Glibc, to end-user applications like Mozilla Firefox. (Nix is however not tied to the Nix Package collection; you could write your own Nix expression based on it, or completely new ones.) You can download the latest version from http://catamaran.labs.cs.uu.nl/dist/nix. You probably want the latest unstable release; currently the stable releases tend to lag behind quite a bit.

Assuming that you have downloaded and unpacked a release of Nix Packages, you can view the set of available components in the release:

$ nix-env -qaf nixpkgs-version
ant-blackdown-1.4.2
aterm-2.2
bash-3.0
binutils-2.15
bison-1.875d
blackdown-1.4.2
bzip2-1.0.2
...

where nixpkgs-version is where you've unpacked the release.

It is also possible to see the status of available components, i.e., whether they are installed into the user environment and/or present in the system:

$ nix-env -qasf nixpkgs-version
...
-PS bash-3.0
--S binutils-2.15
IPS bison-1.875d
...

The first character (I) indicates whether the component is installed in your current user environment. The second (P) indicates whether it is present on your system (in which case installing it into your user environment would be a very quick operation). The last one (S) indicates whether there is a so-called substitute for the component, which is Nix's mechanism for doing binary deployment. It just means that Nix know that it can fetch a pre-built component from somewhere (typically a network server) instead of building it locally.

So now that we have a set of Nix expressions we can build the components contained in them. This is done using nix-env -i. For instance,

$ nix-env -f nixpkgs-version -i subversion

will install the component called subversion (which is, of course, the Subversion version management system).

When you do this for the first time, Nix will start building Subversion and all its dependencies. This will take quite a while — typically an hour or two on modern machines. Fortunately, there is a faster way (so do a Ctrl-C on that install operation!): you just need to tell Nix that pre-built binaries of all those components are available somewhere. This is done using the nix-pull command, which must be supplied with a URL containing a manifest describing what binaries are available. This URL should correspond to the Nix Packages release that you're using. For instance, if you obtained a release from http://catamaran.labs.cs.uu.nl/dist/nix/nixpkgs-0.6pre1554/, then you should do:

$ nix-pull http://catamaran.labs.cs.uu.nl/dist/nix/nixpkgs-0.6pre1554/MANIFEST

If you then issue the installation command, it should start downloading binaries from catamaran.labs.cs.uu.nl, instead of building them from source. This might still take a while since all dependencies must be downloaded, but on a reasonably fast connection such as an DSL line it's on the order of a few minutes.

Naturally, packages can also be uninstalled:

$ nix-env -e subversion

Upgrading to a new version is just as easy. If you have a new release of Nix Packages, you can do:

$ nix-env -f nixpkgs-version -u subversion

This will only upgrade Subversion if there is a “newer” version in the new set of Nix expressions, as defined by some pretty arbitrary rules regarding ordering of version numbers (which generally do what you'd expect of them). To just unconditionally replace Subversion with whatever version is in the Nix expressions, use -i instead of -u; -i will remove whatever version is already installed.

You can also upgrade all components for which there are newer versions:

$ nix-env -f nixpkgs-version -u '*'

Sometimes it's useful to be able to ask what nix-env would do, without actually doing it. For instance, to find out what packages would be upgraded by nix-env -u '*', you can do

$ nix-env ... -u '*' --dry-run
(dry run; not doing anything)
upgrading `libxslt-1.1.0' to `libxslt-1.1.10'
upgrading `graphviz-1.10' to `graphviz-1.12'
upgrading `coreutils-5.0' to `coreutils-5.2.1'

If you grow bored of specifying the Nix expressions using -f all the time, you can set a default location:

$ nix-env -I nixpkgs-version

After this you can just say, for instance, nix-env -u '*'.[1]

4.2. Profiles

Profiles and user environments are Nix's mechanism for implementing the ability to allow differens users to have different configurations, and to do atomic upgrades and rollbacks. To understand how they work, it's useful to know a bit about how Nix works. In Nix, components are stored in unique locations in the Nix store (typically, /nix/store). For instance, a particular version of the Subversion component might be stored in a directory /nix/store/eeeeaf42e56b...-subversion-0.32.1/, while another version might be stored in /nix/store/58823d558a6a...-subversion-0.34/. The long hexadecimal numbers prefixed to the directory names are cryptographic hashes[2] of all inputs involved in building the component — sources, dependencies, compiler flags, and so on. So if two components differ in any way, they end up in different locations in the file system, so they don't interfere with each other. Figure 4.1[3] shows a part of a typical Nix store.

Figure 4.1. User environments

User environments

Of course, you wouldn't want to type

$ /nix/store/eeeeaf42e56b...-subversion-0.32.1/bin/svn

every time you want to run Subversion. Of course we could set up the PATH environment variable to include the bin directory of every component we want to use, but this is not very convenient since changing PATH doesn't take effect for already existing processes. The solution Nix uses is to create directory trees of symlinks to activated components. These are called user environments and they are components themselves (though automatically generated by nix-env), so they too reside in the Nix store. For instance, in Figure 4.1 the user environment /nix/store/068150f63831...-user-env contains a symlink to just Subversion 0.32.1 (arrows in the figure indicate symlinks). This would be what we would obtain if we had done

$ nix-env -i subversion

on a set of Nix expressions that contained Subversion 0.32.1.

This doesn't in itself solve the problem, of course; you wouldn't want to type /nix/store/068150f63831...-user-env/bin/svn either. Therefore there are symlinks outside of the store that point to the user environments in the store; for instance, the symlinks 42 and 43 in the example. These are called generations since every time you perform a nix-env operation, a new user environment is generated based on the current one. For instance, generation 43 was created from generation 42 when we did

$ nix-env -i subversion mozilla

on a set of Nix expressions that contained Mozilla and a new version of Subversion.

Generations are grouped together into profiles so that different users don't interfere with each other if they don't want to. For example:

$ ls -l /nix/var/nix/profiles/
...
lrwxrwxrwx  1 eelco ... default-42-link -> /nix/store/068150f63831...-user-env
lrwxrwxrwx  1 eelco ... default-43-link -> /nix/store/84c85f89ddbf...-user-env
lrwxrwxrwx  1 eelco ... default -> default-43-link

This shows a profile called default. The file default itself is actually a symlink that points to the current generation. When we do a nix-env operation, a new user environment and generation link are created based on the current one, and finally the default symlink is made to point at the new generation. This last step is atomic on Unix, which explains how we can do atomic upgrades. (Note that the building/installing of new components doesn't interfere in any way with old components, since they are stored in different locations in the Nix store.)

If you find that you want to undo a nix-env operation, you can just do

$ nix-env --rollback

which will just make the current generation link point at the previous link. E.g., default would be made to point at default-42-link. You can also switch to a specific generation:

$ nix-env --switch-generation 43

which in this example would roll forward to generation 43 again. You can also see all available generations:

$ nix-env --list-generations

Actually, there is another level of indirection not shown in the figure above. You generally wouldn't have /nix/var/nix/profiles/some-profile/bin in your PATH. Rather, there is a symlink ~/.nix-profile that points to your current profile. This means that you should put ~/.nix-profile/bin in your PATH (and indeed, that's what the initialisation script /nix/etc/profile.d/nix.sh does). This makes it easier to switch to a different profile. You can do that using the command nix-env --switch-profile:

$ nix-env --switch-profile /nix/var/nix/profiles/my-profile

$ nix-env --switch-profile /nix/var/nix/profiles/default

These commands switch to the my-profile and default profile, respectively. If the profile doesn't exist, it will be created automatically. You should be careful about storing a profile in another location than the profiles directory, since otherwise it might not be used as a root of the garbage collector (see section Section 4.3).

All nix-env operations work on the profile pointed to by ~/.nix-profile, but you can override this using the --profile option (abbreviation -p):

$ nix-env -p /nix/var/nix/profiles/other-profile -i subversion

This will not change the ~/.nix-profile symlink.

4.3. Garbage collection

nix-env operations such as upgrades (-u) and uninstall (-e) never actually delete components from the system. All they do (as shown above) is to create a new user environment that no longer contains symlinks to the “deleted” components.

Of course, since disk space is not infinite, unused components should be removed at some point. You can do this by running the Nix garbage collector. It will remove from the Nix store any component not used (directly or indirectly) by any generation of any profile.

Note however that as long as old generations reference a component, it will not be deleted. After all, we wouldn't be able to do a rollback otherwise. So in order for garbage collection to be effective, you should also delete (some) old generations. Of course, this should only be done if you are certain that you will not need to roll back.

To delete all old (non-current) generations of your current profile:

$ nix-env --delete-generations old

Instead of old you can also specify a list of generations, e.g.,

$ nix-env --delete-generations 10 11 14

After removing appropriate old generations you can run the garbage collector as follows:

$ nix-collect-garbage

You can alo first view what files would be deleted:

$ nix-collect-garbage --print-dead

Likewise, the option --print-live will show the paths that won't be deleted.

4.3.1. Garbage collector roots

TODO

The garbage collector uses as roots all store expressions mentioned in all files with extension .gcroot in the directory prefix/var/nix/gcroots/, or in any file or directory symlinked to from that directory. E.g., by default, prefix/var/nix/gcroots/ contains a symlink to prefix/var/nix/profiles/, so all generations of all profiles are also roots of the collector.

4.4. Channels

If you want to stay up to date with a set of packages, it's not very convenient to manually download the latest set of Nix expressions for those packages, use nix-pull to register pre-built binaries (if available), and upgrade using nix-env. Fortunately, there's a better way: Nix channels.

A Nix channel is just a URL that points to a place that contains a set of Nix expressions and a manifest. Using the command nix-channel you can automatically stay up to date with whatever is available at that URL.

You can “subscribe” to a channel using nix-channel --add, e.g.,

$ nix-channel --add http://catamaran.labs.cs.uu.nl/dist/nix/channels/nixpkgs-unstable

subscribes you to a channel that always contains that latest version of the Nix Packages collection. (Instead of nixpkgs-unstable you could also subscribe to nixpkgs-stable, which should have a higher level of stability, but right now is just outdated.) Subscribing really just means that the URL is added to the file ~/.nix-channels. Right now there is no command to “unsubscribe”; you should just edit that file manually and delete the offending URL.

To obtain the latest Nix expressions available in a channel, do

$ nix-channel --update

This downloads the Nix expressions in every channel (downloaded from url/nixexprs.tar.bz2) and registers any available pre-built binaries in every channel (by nix-pulling url/MANIFEST). It also makes the union of each channel's Nix expressions the default for nix-env operations. Consequently, you can then say

$ nix-env -u '*'

to upgrade all components in your profile to the latest versions available in the subscribed channels.



[1] Setting a default using -I currently clashes with using Nix channels, since nix-channel --update calls nix-env -I to set the default to the Nix expressions it downloaded from the channel, replacing whatever default you had set.

[2] 128 bit MD5 hashes, to be precise.

[3] TODO: the figure isn't entirely up to date. It should show multiple profiles and ~/.nix-profile.

Chapter 5. Writing Nix Expressions

This chapter shows you how to write Nix expressions, which are the things that tell Nix how to build components. It starts with a simple example (a Nix expression for GNU Hello), and then moves on to a more in-depth look at the Nix expression language.

5.1. A simple Nix expression

This section shows how to add and test the GNU Hello package to the Nix Packages collection. Hello is a program that prints out the text “Hello, world!”.

To add a component to the Nix Packages collection, you generally need to do three things:

  1. Write a Nix expression for the component. This is a file that describes all the inputs involved in building the component, such as dependencies (other components required by the component), sources, and so on.

  2. Write a builder. This is a shell script[4] that actually builds the component from the inputs.

  3. Add the component to the file pkgs/system/all-packages-generic.nix. The Nix expression written in the first step is a function; it requires other components in order to build it. In this step you put it all together, i.e., you call the function with the right arguments to build the actual component.

5.1.1. The Nix expression

Example 5.1. Nix expression for GNU Hello (default.nix)

{stdenv, fetchurl, perl}: 1

stdenv.mkDerivation { 2
  name = "hello-2.1.1"; 3
  builder = ./builder.sh; 4
  src = fetchurl { 5
    url = ftp://ftp.nluug.nl/pub/gnu/hello/hello-2.1.1.tar.gz;
    md5 = "70c9ccf9fac07f762c24f2df2290784d";
  };
  inherit perl; 6
}

Example 5.1 shows a Nix expression for GNU Hello. It's actually already in the Nix Packages collection in pkgs/applications/misc/hello/ex-1/default.nix. It is customary to place each package in a separate directory and call the single Nix expression in that directory default.nix. The file has the following elements (referenced from the figure by number):

1

This states that the expression is a function that expects to be called with three arguments: stdenv, fetchurl, and perl. They are needed to build Hello, but we don't know how to build them here; that's why they are function arguments. stdenv is a component that is used by almost all Nix Packages components; it provides a “standard” environment consisting of the things you would expect in a basic Unix environment: a C/C++ compiler (GCC, to be precise), the Bash shell, fundamental Unix tools such as cp, grep, tar, etc. fetchurl is a function that downloads files. perl is the Perl interpreter.

Nix functions generally have the form {x, y, ..., z}: e where x, y, etc. are the names of the expected arguments, and where e is the body of the function. So here, the entire remainder of the file is the body of the function; when given the required arguments, the body should describe how to build an instance of the Hello component.

2

So we have to build a component. Building something from other stuff is called a derivation in Nix (as opposed to sources, which are built by humans instead of computers). We perform a derivation by calling stdenv.mkDerivation. mkDerivation is a function provided by stdenv that builds a component from a set of attributes. An attribute set is just a list of key/value pairs where each value is an arbitrary Nix expression. They take the general form {name1 = expr1; ... nameN = exprN;}.

3

The attribute name specifies the symbolic name and version of the component. Nix doesn't really care about these things, but they are used by for instance nix-env -q to show a “human-readable” name for components. This attribute is required by mkDerivation.

4

The attribute builder specifies the builder. This attribute can sometimes be omitted, in which case mkDerivation will fill in a default builder (which does a configure; make; make install, in essence). Hello is sufficiently simple that the default builder would suffice, but in this case, we will show an actual builder for educational purposes. The value ./builder.sh refers to the shell script shown in Example 5.2, discussed below.

5

The builder has to know what the sources of the component are. Here, the attribute src is bound to the result of a call to the fetchurl function. Given a URL and an MD5 hash of the expected contents of the file at that URL, this function builds a derivation that downloads the file and checks its hash. So the sources are a dependency that like all other dependencies is built before Hello itself is built.

Instead of src any other name could have been used, and in fact there can be any number of sources (bound to different attributes). However, src is customary, and it's also expected by the default builder (which we don't use in this example).

6

Since the derivation requires Perl, we have to pass the value of the perl function argument to the builder. All attributes in the set are actually passed as environment variables to the builder, so declaring an attribute

perl = perl;

will do the trick: it binds an attribute perl to the function argument which also happens to be called perl. However, it looks a bit silly, so there is a shorter syntax. The inherit keyword causes the specified attributes to be bound to whatever variables with the same name happen to be in scope.

5.1.2. The builder

Example 5.2. Build script for GNU Hello (builder.sh)

. $stdenv/setup 1

PATH=$perl/bin:$PATH 2

tar xvfz $src 3
cd hello-*
./configure --prefix=$out 4
make 5
make install

Example 5.2 shows the builder referenced from Hello's Nix expression (stored in pkgs/applications/misc/hello/ex-1/builder.sh). The builder can actually be made a lot shorter by using the generic builder functions provided by stdenv, but here we write out the build steps to elucidate what a builder does. It performs the following steps:

1

When Nix runs a builder, it initially completely clears the environment (except for the attributes declared in the derivation). For instance, the PATH variable is empty[5]. This is done to prevent undeclared inputs from being used in the build process. If for example the PATH contained /usr/bin, then you might accidentally use /usr/bin/gcc.

So the first step is to set up the environment. This is done by calling the setup script of the standard environment. The environment variable stdenv points to the location of the standard environment being used. (It wasn't specified explicitly as an attribute in Example 5.1, but mkDerivation adds it automatically.)

2

Since Hello needs Perl, we have to make sure that Perl is in the PATH. The perl environment variable points to the location of the Perl component (since it was passed in as an attribute to the derivation), so $perl/bin is the directory containing the Perl interpreter.

3

Now we have to unpack the sources. The src attribute was bound to the result of fetching the Hello source tarball from the network, so the src environment variable points to the location in the Nix store to which the tarball was downloaded. After unpacking, we cd to the resulting source directory.

The whole build is performed in a temporary directory created in /tmp, by the way. This directory is removed after the builder finishes, so there is no need to clean up the sources afterwards. Also, the temporary directory is always newly created, so you don't have to worry about files from previous builds interfering with the current build.

4

GNU Hello is a typical Autoconf-based package, so we first have to run its configure script. In Nix every component is stored in a separate location in the Nix store, for instance /nix/store/9a54ba97fb71b65fda531012d0443ce2-hello-2.1.1. Nix computes this path by cryptographically hashing all attributes of the derivation. The path is passed to the builder through the out environment variable. So here we give configure the parameter --prefix=$out to cause Hello to be installed in the expected location.

5

Finally we build Hello (make) and install it into the location specified by out (make install).

If you are wondering about the absence of error checking on the result of various commands called in the builder: this is because the shell script is evaluated with Bash's -e option, which causes the script to be aborted if any command fails without an error check.

5.1.3. Composition

Example 5.3. Composing GNU Hello (all-packages-generic.nix)

...

rec { 1
  
  hello = (import ../applications/misc/hello/ex-1 2) { 3
    inherit fetchurl stdenv perl;
  };

  perl = (import ../development/interpreters/perl) { 4
    inherit fetchurl stdenv;
  };

  fetchurl = (import ../build-support/fetchurl) { 
    inherit stdenv; ...
  };
  
  stdenv = ...;

}

The Nix expression in Example 5.1 is a function; it is missing some arguments that have to be filled in somewhere. In the Nix Packages collection this is done in the file pkgs/system/all-packages-generic.nix, where all Nix expressions for components are imported and called with the appropriate arguments. Example 5.3 shows some fragments of all-packages-generic.nix.

1

This file defines a set of attributes, all of which are concrete derivations (i.e., not functions). In fact, we define a mutually recursive set of attributes. That is, the attributes can refer to each other. This is precisely what we want since we want to “plug” the various components into each other.

2

Here we import the Nix expression for GNU Hello. The import operation just loads and returns the specified Nix expression. In fact, we could just have put the contents of Example 5.1 in all-packages-generic.nix at this point. That would be completely equivalent, but it would make the file rather bulky.

Note that we refer to ../applications/misc/hello/ex-1, not ../applications/misc/hello/ex-1/default.nix. When you try to import a directory, Nix automatically appends /default.nix to the file name.

3

This is where the actual composition takes place. Here we call the function imported from ../applications/misc/hello/ex-1 with an attribute set containing the things that the function expects, namely fetchurl, stdenv, and perl. We use inherit again to use the attributes defined in the surrounding scope (we could also have written fetchurl = fetchurl;, etc.).

The result of this function call is an actual derivation that can be built by Nix (since when we fill in the arguments of the function, what we get is its body, which is the call to stdenv.mkDerivation in Example 5.1).

4

Likewise, we have to instantiate Perl, fetchurl, and the standard environment.

5.1.4. Testing

You can now try to build Hello. The simplest way to do that is by using nix-env:

$ nix-env -f pkgs/system/i686-linux.nix -i hello
installing `hello-2.1.1'
building path `/nix/store/632d2b22514dcebe704887c3da15448d-hello-2.1.1'
hello-2.1.1/
hello-2.1.1/intl/
hello-2.1.1/intl/ChangeLog
...

This will build Hello and install it into your profile. The file i686-linux is just a simple Nix expression that imports all-packages-generic.nix and instantiates it for Linux on the x86 platform.

Note that the hello argument here refers to the symbolic name given to the Hello derivation (the name attribute in Example 5.1), not the hello attribute in all-packages-generic.nix. nix-env simply walks through all derivations defined in the latter file, looking for one with a name attribute matching the command-line argument.

You can test whether it works:

$ hello
Hello, world!

Generally, however, using nix-env is not the best way to test components, since you may not want to install them into your profile right away (they might not work properly, after all). A better way is to write a short file containing the following:

(import pkgs/system/i686-linux.nix).hello

Call it test.nix. You can then build it without installing it using the command nix-build:

$ nix-build ./test.nix
...
/nix/store/632d2b22514dcebe704887c3da15448d-hello-2.1.1

nix-build will build the derivation and print the output path. It also creates a symlink to the output path called result in the current directory. This is convenient for testing the component:

$ ./result/bin/hello
Hello, world!

Nix has a transactional semantics. Once a build finishes successfully, Nix makes a note of this in its database: it registers that the path denoted by out is now “valid”. If you try to build the derivation again, Nix will see that the path is already valid and finish immediately. If a build fails, either because it returns a non-zero exit code, because Nix or the builder are killed, or because the machine crashes, then the output path will not be registered as valid. If you try to build the derivation again, Nix will remove the output path if it exists (e.g., because the builder died half-way through make install) and try again. Note that there is no “negative caching”: Nix doesn't remember that a build failed, and so a failed build can always be repeated. This is because Nix cannot distinguish between permanent failures (e.g., a compiler error due to a syntax error in the source) and transient failures (e.g., a disk full condition).

Nix also performs locking. If you run multiple Nix builds simultaneously, and they try to build the same derivation, the first Nix instance that gets there will perform the build, while the others block (or perform other derivations if available) until the build finishes. So it is always safe to run multiple instances of Nix in parallel (contrary to, say, make).

If you have a system with multiple CPUs, you may want to have Nix build different derivations in parallel (insofar as possible). Just pass the option -j N, where N is the maximum number of jobs to be run in parallel. Typically this should be the number of CPUs.

5.1.5. The generic builder

Recall from Example 5.2 that the builder looked something like this:

PATH=$perl/bin:$PATH
tar xvfz $src
cd hello-*
./configure --prefix=$out
make
make install

The builders for almost all Unix packages look like this — set up some environment variables, unpack the sources, configure, build, and install. For this reason the standard environment provides some Bash functions that automate the build process. A builder using the generic build facilities in shown in Example 5.4.

Example 5.4. Build script using the generic build functions

buildInputs="$perl" 1

. $stdenv/setup 2

genericBuild 3
1

The buildInputs variable tells setup to use the indicated components as “inputs”. This means that if a component provides a bin subdirectory, it's added to PATH; if it has a include subdirectory, it's added to GCC's header search path; and so on.

2

The function genericBuild is defined in the file $stdenv/setup.

3

The final step calls the shell function genericBuild, which performs the steps that were done explicitly in Example 5.2. The generic builder is smart enough to figure out whether to unpack the sources using gzip, bzip2, etc. It can be customised in many ways; see Section 5.3.

Discerning readers will note that the buildInputs could just as well have been set in the Nix expression, like this:

  buildInputs = [perl];

The perl attribute can then be removed, and the builder becomes even shorter:

. $stdenv/setup
genericBuilder

In fact, mkDerivation provides a default builder that looks exactly like that, so it is actually possible to omit the builder for Hello entirely.

5.2. The Nix expression language

The Nix expression language is a pure, lazy, functional language. Purity means that operations in the language don't have side-effects (for instance, there is no variable assignment). Laziness means that arguments to functions are evaluated only when they are needed. Functional means that functions are “normal” values that can be passed around and manipulated in interesting ways. The language is not a full-featured, general purpose language. It's main job is to describe components, compositions of components, and the variability within components.

This section presents the various features of the language.

Simple values

Nix has the following basic datatypes:

  • Strings, enclosed between double quotes, e.g., "foo bar".

  • Integers, e.g., 123.

  • URIs as defined in appendix B of RFC 2396, e.g., https://svn.cs.uu.nl:12443/dist/trace/trace-nix-trunk.tar.bz2.

  • Paths, e.g., /bin/sh or ./builder.sh. A path must contain at least one slash to be recognised as such; for instance, builder.sh is not a path[6]. If the filename is relative, i.e., if it does not begin with a slash, it is made absolute at parse time relative to the directory of the Nix expression that contained it. For instance, if a Nix expression in /foo/bar/bla.nix refers to ../xyzzy/fnord.nix, the absolutised path is /foo/xyzzy/fnord.nix.

  • Booleans with values true and false.

Lists

Lists are formed by enclosing a whitespace-separated list of values between square bracktes. For example,

[ 123 ./foo.nix "abc" (f {x=y;}) ]

defines a list of four elements, the last being the result of a call to the function f. Note that function calls have to be enclosed in parentheses. If they had been omitted, e.g.,

[ 123 ./foo.nix "abc" f {x=y;} ]

the result would be a list of five elements, the fourth one being a function and the fifth being an attribute set.

Attribute sets

Attribute sets are really the core of the language, since ultimately it's all about creating derivations, which are really just sets of attributes to be passed to build scripts.

Attribute sets are just a list of name/value pairs enclosed in curly brackets, where each value is an arbitrary expression terminated by a semicolon. For example:

{ x = 123;
  text = "Hello";
  y = f { bla = 456; };
}

This defines an attribute set with attributes named x, test, y. The order of the attributes is irrelevant. An attribute name may only occur once.

Attributes can be selected from an attribute set using the . operator. For instance,

{ a = "Foo"; b = "Bar"; }.a

evaluates to "Foo".

Recursive attribute sets

Recursive attribute sets are just normal attribute sets, but the attributes can refer to each other. For example,

rec {
  x = y;
  y = 123;
}.x

evaluates to 123. Note that without rec the binding x = y; would refer to the variable y in the surrounding scope, if one exists, and would be invalid if no such variable exists. That is, in a normal (non-recursive) attribute set, attributes are not added to the lexical scope; in a recursive set, they are.

Recursive attribute sets of course introduce the danger of infinite recursion. For example,

rec {
  x = y;
  y = x;
}.x

does not terminate[7].

Let expressions

A let expression is a simple short-hand for a rec expression followed by an attribute selection: let { attrs } translates to rec { attrs }.body.

For instance,

let {
  x = "foo";
  y = "bar";
  body = x + y;
}

is equivalent to

rec {
  x = "foo";
  y = "bar";
  body = x + y;
}.body

and evaluates to "foobar".

Inheriting attributes

When defining an attribute set it is often convenient to copy variables from the surrounding lexical scope (e.g., when you want to propagate attributes). This can be shortened using the inherit keyword. For instance,

let {
  x = 123;
  body = {
    inherit x;
    y = 456;
  };
}

evaluates to {x = 123; y = 456;}. (Note that this works because x is added to the lexical scope by the let construct.) It is also possible to inherit attributes from another attribute set. For instance, in this fragment from all-packages-generic.nix,

  graphviz = (import ../tools/graphics/graphviz) {
    inherit fetchurl stdenv libpng libjpeg expat x11 yacc;
    inherit (xlibs) libXaw;
  };

  xlibs = {
    libX11 = ...;
    libXaw = ...;
    ...
  }

  libpng = ...;
  libjpg = ...;
  ...

the attribute set used in the function call to the function defined in ../tools/graphics/graphviz inherits a number of variables from the surrounding scope (fetchurl ... yacc), but also inherits libXaw (the X Athena Widgets) from the xlibs (X11 client-side libraries) attribute set.

Functions

Functions have the following form:

{params}: body

This defines a function that must be called with an attribute set containing the attributes listed in params, which is a comma-separated list of attribute names. Optionally, for each parameter a default value may be specified by writing param ? e, where e is an arbitrary expression. If a parameter has a default, the corresponding attribute may be omitted in function calls.

Note that functions do not have names. If you want to give them a name, you can bind them to an attribute, e.g.,

let {
  concat = {x, y}: x + y;
  body = concat {x = "foo"; y = "bar";};
}

It is also possible to define a function that takes a single argument and that does not need to be called with an attribute set as argument. The syntax is

var: body

where var is the name of the argument. It is not possible to define a default. Example:

let {
  negate = x: !x;
  concat = x: y: x + y;
  body = if negate true then concat "foo" "bar" else "";
}

Note that concat is a function that takes one arguments and returns a function that takes another argument. This allows partial parameterisation (i.e., only filling some of the arguments of a function); e.g.,

  map (concat "foo") ["bar", "bla", "abc"]

evaluates to ["foobar" "foobla" "fooabc"].

Conditionals

Conditionals look like this:

if e1 then e2 else e3

where e1 is an expression that should evaluate to a boolean value (true or false).

Assertions

Assertions are generally used to check that certain requirements on or between features and dependencies hold. They look like this:

assert e1; e2

where e1 is an expression that should evaluate to a boolean value. If it evaluates to true, e2 is returned; otherwise expression evaluation is aborted and a backtrace is printed.

Example 5.5. Nix expression for Subversion

{ localServer ? false
, httpServer ? false
, sslSupport ? false
, pythonBindings ? false
, javaSwigBindings ? false
, javahlBindings ? false
, stdenv, fetchurl
, openssl ? null, httpd ? null, db4 ? null, expat, swig ? null, j2sdk ? null
}:

assert localServer -> db4 != null; 1
assert httpServer -> httpd != null && httpd.expat == expat; 2
assert sslSupport -> openssl != null && (httpServer -> httpd.openssl == openssl); 3
assert pythonBindings -> swig != null && swig.pythonSupport;
assert javaSwigBindings -> swig != null && swig.javaSupport;
assert javahlBindings -> j2sdk != null;

stdenv.mkDerivation {
  name = "subversion-1.1.1";
  ...
  openssl = if sslSupport then openssl else null; 4
  ...
}

Example 5.5 show how assertions are used in the Nix expression for Subversion.

1

This assertion states that if Subversion is to have support for local repositories, then Berkeley DB is needed. So if the Subversion function is called with the localServer argument set to true but the db4 argument set to null, then the evaluation fails.

2

This is a more subtle condition: if Subversion is built with Apache (httpServer) support, then the Expat library (an XML library) used by Subversion should be same as the one used by Apache. This is because in this configuration Subversion code ends up being linked with Apache code, and if the Expat libraries do not match, a build- or runtime link error or incompatibility might occur.

2

This assertion says that in order for Subversion to have SSL support (so that it can access https URLs), an OpenSSL library must be passed. Additionally, it says that if Apache support is enabled, then Apache's OpenSSL should match Subversion's. (Note that if Apache support is not enabled, we don't care about Apache's OpenSSL.)

4

The conditional here is not really related to assertions, but is worth pointing out: it ensures that if SSL support is disabled, then the Subversion derivation is not dependent on OpenSSL, even if a non-null value was passed. This prevents an unnecessary rebuild of Subversion if OpenSSL changes.

With expressions

A with expression,

with e1; e2

introduces the attribute set e1 into the lexical scope of the expression e2. For instance,

let {
  as = {x = "foo"; y = "bar";};

  body = with as; x + y;
}

evaluates to "foobar" since the with adds the x and y attributes of as to the lexical scope in the expression x + y. The most common use of with is in conjunction with the import function. E.g.,

with (import ./definitions.nix); ...

makes all attributes defined in the file definitions.nix available as if they were defined locally in a rec-expression.

Operators

Table 5.1 lists the operators in the Nix expression language, in order of precedence (from strongest to weakest binding).

Table 5.1. Operators

SyntaxAssociativityDescription
e1 ~ e2noneConstruct a reference to a subpath of a derivation. E.g., hello ~ "/bin/sh" refers to the /bin/sh path within the Hello derivation. Useful in specifying derivation attributes.
e ? idnoneTest whether attribute set e contains an attribute named id.
e1 + e2leftString or path concatenation.
! eleftBoolean negation.
e1 // e2rightReturn an attribute set consisting of the attributes in e1 and e2 (with the latter taking precedence over the former in case of equally named attributes).
e1 == e2noneEquality.
e1 != e2noneInequality.
e1 && e2leftLogical AND.
e1 || e2leftLogical OR.
e1 -> e2noneLogical implication (equivalent to !e1 || e2).

Derivations

The most important built-in function is derivation, which is used to describe a single derivation (a build action). It takes as input an attribute set, the attributes of which specify the inputs of the build.

  • There must be an attribute named system whose value must be a string specifying a Nix platform identifier, such as "i686-linux" or "powerpc-darwin"[8] The build can only be performed on a machine and operating system matching the platform identifier. (Nix can automatically forward builds for other platforms by forwarding them to other machines; see Section 6.2.)

  • There must be an attribute named name whose value must be a string. This is used as a symbolic name for the component by nix-env, and it is appended to the hash in the output path of the derivation.

  • There must be an attribute named builder that identifies the program that is executed to perform the build. It can be either a derivation or a source (a local file reference, e.g., ./builder.sh).

  • Every attribute is passed as an environment variable to the builder. Attribute values are translated to environment variables as follows:

    • Strings, URIs, and integers are just passed verbatim.

    • A path (e.g., ../foo/sources.tar) causes the referenced file to be copied to the store; its location in the store is put in the environment variable. The idea is that all sources should reside in the Nix store, since all inputs to a derivation should reside in the Nix store.

    • A derivation causes that derivation to be built prior to the present derivation; the output path is put in the environment variable.

    • Lists of the previous types are also allowed. They are simply concatenated, separated by spaces.

  • The optional argument args specifies command-line arguments to be passed to the builder. It should be a list.

(Note that mkDerivation in the standard environment is a wrapper around derivation that adds a default value for system and always uses Bash as the builder, to which the supplied builder is passed as a command-line argument. See Section 5.3.)

The builder is executed as follows:

  • A temporary directory is created under the directory specified by TMPDIR (default /tmp) where the build will take place. The current directory is changed to this directory.

  • The environment is cleared and set to the derivation attributes, as specified above.

  • In addition, the following variables are set:

    • NIX_BUILD_TOP contains the path of the temporary directory for this build.

    • Also, TMPDIR, TEMPDIR, TMP, TEMP are set to point to the temporary directory. This is to prevent the builder from accidentally writing temporary files anywhere else. Doing so might cause interference by other processes.

    • PATH is set to /path-not-set to prevent shells from initialising it to their built-in default value.

    • HOME is set to /homeless-shelter to prevent programs from using /etc/passwd or the like to find the user's home directory, which could cause impurity. Usually, when HOME is set, it is used as the location of the home directory, even if it points to a non-existent path.

    • NIX_STORE is set to the path of the top-level Nix store directory (typically, /nix/store).

    • out is set to point to the output path of the derivation, which is a subdirectory of the Nix store. The output path is a concatenation of the cryptographic hash of all build inputs, and the name attribute.

  • If the output path already exists, it is removed. Also, locks are acquired to prevent multiple Nix instances from performing the same build at the same time.

  • A log of the combined standard output and error is written to /nix/var/log/nix.

  • The builder is executed with the arguments specified by the attribute args. If it exits with exit code 0, it is considered to have succeeded.

  • The temporary directory is removed (unless the -K option was specified).

  • If the build was successful, Nix scans the output for references to the paths of the inputs. These so-called retained dependencies could be used when the output of the derivation is used (e.g., when it's executed or used as input to another derivation), so if we deploy the derivation, we should copy the retained dependencies as well. The scan is performed by looking for the hash parts of file names of the inputs.

Other built-in functions

TODO

Comments

Comments can be single-line, started with a # character, or inline/multi-line, enclosed within /* ... */.

5.3. The standard environment

The standard build environment in the Nix Packages collection provides a basic environment for building Unix packages. It consists of the following components:

  • The GNU C Compiler, configured with C and C++ support. On Linux, the compiler has been patched to provide greater “purity” assurance. For instance, the compiler doesn't search in locations such as /usr/include. In fact, attempts to add such directories through the -I flag are filtered out. Likewise, the linker (from GNU binutils) doesn't search in standard locations such as /usr/lib. Programs built on Linux are linked against a GNU C Library that likewise doesn't search in the default system locations.

  • GNU coreutils (contains a few dozen standard Unix commands).

  • GNU findutils (contains find).

  • GNU diffutils (contains diff, cmp).

  • GNU sed.

  • GNU grep.

  • GNU awk.

  • GNU tar.

  • gzip and bzip2.

  • GNU Make. It has been patched to provide “nested” output that can be fed into the log2xml command and log2html stylesheet to create a structured, readable output of the build steps performed by Make.

  • Bash. This is the shell used for all builders in the Nix Packages collection. Not using /bin/sh removes a large source of portability problems.

  • Patch.

The standard environment is used by passing it as an input called stdenv to the derivation, and then doing

. $stdenv/setup

at the top of the builder.

Apart from adding the aforementioned commands to the PATH, setup also does the following:

  • All input components specified in the buildInputs environment variable have their /bin subdirectory added to PATH, their /include subdirectory added to the C/C++ header file search path, and their /lib subdirectory added to the linker search path. This can be extended. For instance, when the pkgconfig component is used, the subdirectory /lib/pkgconfig of each input is added to the PKG_CONFIG_PATH environment variable.

  • The environment variable NIX_CFLAGS_STRIP is set so that the compiler strips debug information from object files. This can be disabled by setting NIX_STRIP_DEBUG to 0.

The setup script also exports a function called genericBuilder that knows how to build typical Autoconf-style components. It can be customised to perform builds for any type of component. It is advisable to use genericBuild since it provides facilities that are almost always useful such as unpacking of sources, patching of sources, nested logging, etc.

The operation of the generic builder can be modified in many places by setting certain variables. These hook variables are typically set to the name of some shell function defined by you. For instance, to perform some additional steps after make install you would set the postInstall variable:

postInstall=myPostInstall

myPostInstall() {
    mkdir $out/share/extra
    cp extrafiles/* $out/share/extra
}

The generic builder has a number of phases, each of which can be override in its entirety by setting the indicated variable. The phases are:

  • unpackPhase unpacks the source files listed in the src environment variable to the current directory. It supports tar files, optionally compressed with gzip or bzip2; Zip files (but note that the unzip command is not a part of the standard environment; you should add it as a build input yourself); and unpacked source trees (i.e., directories; they are copied verbatim). You can add support for other file types by setting the findUnpacker hook. This hook should set the variable unpackCmd to contain the command to be executed to unpack the file.

    After unpacking all source files, unpackPhase changes the current directory to the directory created by unpacking the sources. If there are multiple source directories, you should set sourceRoot to the name of the intended directory.

    It also calls the hook postUnpack after unpacking.

  • patchPhase calls the patch command with the -p1 option for each patch file listed in the patches variable.

  • configurePhase runs the script called configure in the current directory with a --prefix set to the output path. You can add additional flags through the configureFlags variable. If configure does not exist, nothing happens.

    Before and after running configure, the hooks preConfigure and postConfigure are called, respectively.

  • buildPhase calls make. You can set flags for make through the makeFlags variable.

    Before and after running make, the hooks preBuild and postBuild are called, respectively.

  • checkPhase calls make check, but only if the doCheck variable is set to 1. Additional flags can be set through the checkFlags variable.

  • installPhase calls make install. Additional flags can be set through the installFlags variable. It also strips any static libraries in the output path of debug information unless dontStrip is set to 1.

    Before and after running make install, the hooks preInstall and postInstall are called, respectively.

  • distPhase calls make dist, but only if the doDist variable is set to 1. Additional flags can be set through the distFlags variable. The resulting tarball is copied to the /tarballs subdirectory of the output path.

    Before and after running make dist, the hooks preDist and postDist are called, respectively.

You can change the order in which phases are executed, or add new phases, by setting the phases variable. The default is patchPhase configurePhase buildPhase checkPhase installPhase distPhase.

At the beginning of each phase, the set of all shell variables is written to the file env-vars at the top-level build directory. This is useful for debugging: it allows you to recreate the environment in which a build was performed. For instance, if a build fails, then assuming you used the -K flag, you can go to the output directory and “switch” to the environment of the builder:

$ nix-build -K ./foo.nix
... fails, keeping build directory `/tmp/nix-1234-0'

$ cd /tmp/nix-1234-0

$ source env-vars

(edit some files...)

$ make

(execution continues with the same GCC, make, etc.)

The definitive, up-to-date documentation of the generic builder is the source itself, which resides in pkgs/stdenv/generic/setup.sh.



[4] In fact, it can be written in any language, but typically it's a bash shell script.

[5] Actually, it's initialised to /path-not-set to prevent Bash from setting it to a default value.

[6] It's parsed as an expression that selects the attribute sh from the variable builder.

[7] Actually, Nix detects infinite recursion in this case and aborts (“infinite recursion encountered”).

[8] To figure out your platform identifier, look at the line “Checking for the canonical Nix system name” in the output of Nix's configure script.

Chapter 6. Setting up a Build Farm

This chapter provides some sketchy information on how to set up a Nix-based build farm. Nix is particularly suited as a basis for a build farm, since:

  • Nix supports distributed builds: a local Nix installation can forward Nix builds to other machines over the network. This allows multiple builds to be performed in parallel (thus improving performance), but more in importantly, it allows Nix to perform multi-platform builds in a semi-transparent way. For instance, if you perform a build for a powerpc-darwin on an i686-linux machine, Nix can automatically forward the build to a powerpc-darwin machine, if available.

  • The Nix expression language is ideal for describing build jobs, plus all their dependencies. For instance, if your package has some dependency, you don't have to manually install it on all the machines in the build farm; they will be built automatically.

  • Proper release management requires that builds (if deployed) are traceable: it should be possible to figure out from exactly what sources they were built, in what configuration, etc.; and it should be possible to reproduce the build, if necessary. Nix makes this possible since Nix's hashing scheme uniquely identifies builds, and Nix expressions are self-contained.

  • Nix will only rebuild things that have actually changed. For instance, if the sources of a component haven't changed between runs of the build farm, the component won't be rebuild (unless it was garbage-collected). Also, dependencies typically don't change very often, so they only need to be built once.

  • The results of a Nix build farm can be made available through a channel, so successful builds can be deployed to users immediately.

6.1. Overview

TODO

The sources of the Nix build farm are at https://svn.cs.uu.nl:12443/repos/trace/release/trunk.

6.2. Setting up distributed builds

You can enable distributed builds by setting the environment variable NIX_BUILD_HOOK to point to a program that Nix will call whenever it wants to build a derivation. The build hook (typically a shell or Perl script) can decline the build, in which Nix will perform it in the usual way if possible, or it can accept it, in which case it is responsible for somehow getting the inputs of the build to another machine, doing the build there, and getting the results back.

Example 6.1. Remote machine configuration: remote-systems.conf

nix@mcflurry.labs.cs.uu.nl  powerpc-darwin  /home/nix/.ssh/id_quarterpounder_auto  2
nix@scratchy.labs.cs.uu.nl  i686-linux      /home/nix/.ssh/id_scratchy_auto        1

An example build hook can be found in the Nix build farm sources: https://svn.cs.uu.nl:12443/repos/trace/release/trunk/common/distributed/build-remote.pl. It should be suitable for most purposes, with maybe some minor adjustments. It uses ssh and rsync to copy the build inputs and outputs and perform the remote build. You should define a list of available build machines and set the environment variable REMOTE_SYSTEMS to point to it. An example configuration is shown in Example 6.1. Each line in the file specifies a machine, with the following bits of information:

  1. The name of the remote machine, with optionally the user under which the remote build should be performed. This is actually passed as an argument to ssh, so it can be an alias defined in your ~/.ssh/config.

  2. The Nix platform type identifier, such as powerpc-darwin.

  3. The SSH private key to be used to log in to the remote machine. Since builds should be non-interactive, this key should not have a passphrase!

  4. The maximum “load” of the remote machine. This is just the maximum number of jobs that build-remote.pl will execute in parallel on the machine. Typically this should be equal to the number of CPUs.

You should also set up the environment variable CURRENT_LOAD to point at a file that build-remote.pl uses to remember how many jobs it is currently executing remotely. It doesn't look at the actual load on the remote machine, so if you have multiple instances of Nix running, they should use the same CURRENT_LOAD file[9]. Maybe in the future build-remote.pl will look at the actual remote load. The load file should exist, so you should just create it as an empty file initially.



[9] Although there are probably some race conditions in the script right now.

Appendix A. Command Reference

A.1. nix-env

Name

nix-env — manipulate or query Nix user environments

Synopsis

nix-env [--help] [--version] [--verbose...] [-v...] [--no-build-output] [-Q] [ { --max-jobs | -j } number ] [--keep-going] [-k] [--keep-failed] [-K] [--fallback] [--readonly-mode] [ { --file | -f } path ] [ { --profile | -p } path ] [--preserve-installed] [ --system-filter system ] [--dry-run] operation [options...] [arguments...]

Description

The command nix-env is used to manipulate Nix user environments. User environments are sets of software components available to a user at some point in time. In other words, they are a synthesised view of the programs available in the Nix store. There may be many user environments: different users can have different environments, and individual users can switch between different environments.

nix-env takes exactly one operation flag which indicates the subcommand to be performed. These are documented below.

Common options

This section lists the options that are common to all operations. These options are allowed for every subcommand, though they may not always have an effect.

--help

Prints out a summary of the command syntax and exits.

--version

Prints out the Nix version number on standard output and exits.

--verbose / -v

Increases the level of verbosity of diagnostic messages printed on standard error. For each Nix operation, the information printed on standard output is well-defined; any diagnostic information is printed on standard error, never on standard output.

This option may be specified repeatedly. Currently, the following verbosity levels exist:

0

Errors only”: only print messages explaining why the Nix invocation failed.

1

Informational”: print useful messages about what Nix is doing. This is the default.

2

Talkative”: print more informational messages.

3

Chatty”: print even more informational messages.

4

Debug”: print debug information:

5

Vomit”: print vast amounts of debug information.

--no-build-output / -Q

By default, output written by builders to standard output and standard error is echoed to the Nix command's standard error. This option suppresses this behaviour. Note that the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix.

--max-jobs / -j

Sets the maximum number of build jobs that Nix will perform in parallel to the specified number. The default is 1. A higher value is useful on SMP systems or to exploit I/O latency.

--keep-going / -k

Keep going in case of failed builds, to the greatest extent possible. That is, if building an input of some derivation fails, Nix will still build the other inputs, but not the derivation itself. Without this option, Nix stops if any build fails (except for builds of substitutes), possibly killing builds in progress (in case of parallel or distributed builds).

--keep-failed / -K

Specifies that in case of a build failure, the temporary directory (usually in /tmp) in which the build takes place should not be deleted. The path of the build directory is printed as an informational message.

--fallback

Whenever Nix attempts to realise a derivation for which a closure is already known, but this closure cannot be realised, fall back on normalising the derivation.

The most common scenario in which this is useful is when we have registered substitutes in order to perform binary distribution from, say, a network repository. If the repository is down, the realisation of the derivation will fail. When this option is specified, Nix will build the derivation instead. Thus, binary installation falls back on a source installation. This option is not the default since it is generally not desirable for a transient failure in obtaining the substitutes to lead to a full build from source (with the related consumption of resources).

--readonly-mode

When this option is used, no attempt is made to open the Nix database. Most Nix operations do need database access, so those operations will fail.

--file / -f

Specifies the Nix expression (designated below as the active Nix expression) used by the --install, --upgrade, and --query --available operations to obtain derivations. The default is ~/.nix-defexpr.

--profile / -p

Specifies the profile to be used by those operations that operate on a profile (designated below as the active profile). A profile is sequence of user environments called generations, one of which is the current generation. The default profile is the target of the symbolic link ~/.nix-profile (see below).

--dry-run

For the --install, --upgrade, --uninstall, --switch-generation and --rollback operations, this flag will cause nix-env to print what would be done if this flag had not been specified, without actually doing it.

--preserve-installed

By default, when you install a derivation with the --install operation, it will replace previously installed versions with the same derivation name (regardless of the version number). This option causes those previously installed versions to be kept in the new generation of the profile. Note that this will generally cause conflicts in the creation of the user environment (since multiple versions of a package typically contain the same programs).

--system-filter system

By default, operations such as --query --available only include derivations matching the current platform. This option allows you to use derivations for the specified platform system. The special value * causes derivations for any platform to be included.

Files

~/.nix-defexpr

The default Nix expression used by the --install, --upgrade, and --query --available operations to obtain derivations. It is generally a symbolic link to some other location set using the --import operation. The --file option may be used to override this default.

~/.nix-profile

A symbolic link to the user's current profile. By default, this symlink points to prefix/var/nix/profiles/default. The PATH environment variable should include ~/.nix-profile/bin for the user environment to be visible to the user.

Operation --install

Synopsis

nix-env { --install | -i } [ --preserve-installed | -P ] drvnames...

Description

The install operation creates a new user environment, based on the current generation of the active profile, to which the derivations designated by drvnames in the active Nix expression are added.

Currently installed derivations with a name equal to the name of a derivation being added are removed unless the option --preserve-installed is specified.

Flags

--preserve-installed / -P

Do not remove derivations with a name matching one of the derivations being installed. Usually, trying to have two versions of the same package installed in the same generation of a profile will lead to an error in building the generation, due to file name clashes between the two versions. However, this is not the case for all packages.

Examples

$ nix-env --install gcc-3.3.2 (install specific version)
installing `gcc-3.3.2'
uninstalling `gcc-3.1' (previously installed version is removed)

$ nix-env --install gcc (just pick any version)

$ nix-env -f ~/foo.nix -i '*' (install everything in foo.nix)

Operation --upgrade

Synopsis

nix-env { --upgrade | -u } [ --lt | --leq | --always ] drvnames...

Description

The upgrade operation creates a new user environment, based on the current generation of the active profile, in which all derivations designated by drvnames for which there are newer versions in the active Nix expression are replaced by those newer versions. Matching derivations for which there are no newer versions are left untouched; this is not an error. It is also not an error if an element of drvnames matches no installed derivations.

If multiple derivations in the active Nix expression match an installed derivation, the one with the highest version is selected.

Flags

--lt

Only upgrade a derivation to newer versions. This is the default.

--leq

In addition to upgrading to newer versions, also “upgrade” to derivations that have the same version. Version are not a unique identification of a derivation, so there may be many derivations that have the same version. This flag may be useful to force “synchronisation” between the installed and available derivations.

--always

In addition to upgrading to newer versions, also “upgrade” to derivations that have the same or a lower version. I.e., derivations may actually be downgraded depending on what is available in the active Nix expression.

Examples

$ nix-env --upgrade gcc
upgrading `gcc-3.3.1' to `gcc-3.4'

$ nix-env -u gcc-3.3.2 --always (switch to a specific version)
upgrading `gcc-3.4' to `gcc-3.3.2'

$ nix-env --upgrade pan
(no upgrades available, so nothing happens)

$ nix-env -u '*' (try to upgrade everything)
upgrading `hello-2.1.2' to `hello-2.1.3'
upgrading `mozilla-1.2' to `mozilla-1.4'

Versions

The upgrade operation determines whether a derivation y is an upgrade of a derivation x by looking at their respective name attributes. The names (e.g., gcc-3.3.1 are split into two parts: the package name (gcc), and the version (3.3.1). The version part starts after the first dash not following by a letter. x is considered an upgrade of y if their package names match, and the version of y is higher that that of x.

The versions are compared by splitting them into contiguous components of numbers and letters. E.g., 3.3.1pre5 is split into [3, 3, 1, "pre", 5]. These lists are then compared lexicographically (from left to right). Corresponding components a and b are compared as follows. If they are both numbers, integer comparison is used. If a is an empty string and b is a number, a is considered less than b. The special string component pre (for pre-release) is considered to be less than other components. String components are considered less than number components. Otherwise, they are compared lexicographically (i.e., using case-sensitive string comparison).

This is illustrated by the following examples:

1.0 < 2.3
2.1 < 2.3
2.3 = 2.3
2.5 > 2.3
3.1 > 2.3
2.3.1 > 2.3
2.3.1 > 2.3a
2.3pre1 < 2.3
2.3pre3 < 2.3pre12
2.3a < 2.3c
2.3pre1 < 2.3c
2.3pre1 < 2.3q

Operation --uninstall

Synopsis

nix-env { --uninstall | -e } drvnames...

Description

The uninstall operation creates a new user environment, based on the current generation of the active profile, from which the derivations designated by drvnames are removed.

Examples

$ nix-env --uninstall gcc
$ nix-env -e '*' (remove everything)

Operation --query

Synopsis

nix-env { --query | -q } [ --installed | --available | -a ] { --name | --expr | --status | -s }

Description

The query operation displays information about either the derivations that are installed in the current generation of the active profile (--installed), or the derivations that are available for installation in the active Nix expression (--available).

The derivations are sorted by their name attributes.

Source selection

The following flags specify the set of derivations on which the query operates.

--installed

The query operates on the derivations that are installed in the current generation of the active profile. This is the default

--available / -a

The query operates on the derivations that are available in the active Nix expression.

Queries

The following flags specify what information to display about the selected derivations. Only one type of query may be specified.

--name

Prints the name attribute of each derivation. This is the default.

--expr

Prints the store expression in the Nix store that described the derivation.

--status / -s

Prints the status of each derivation, followed by its name attribute. The status consists of three characters. The first is I or -, indicating whether the derivation is currently installed in the current generation of the active profile. This is by definition the case for --installed, but not for --available. The second is P or -, indicating whether the derivation is present on the system. This indicates whether installation of an available derivation will require the derivation to be built. The third is S or -, indicating whether a substitute is available for the derivation.

Examples

$ nix-env -q (show installed derivations)
MozillaFirebird-0.7
bison-1.875c
docbook-xml-4.2
...

$ nix-env -qa (show available derivations)
GConf-2.4.0.1
MPlayer-1.0pre3
MozillaFirebird-0.7
ORBit2-2.8.3
...

$ nix-env -qas (show status of available derivations)
-P- GConf-2.4.0.1 (not installed but present)
--S MPlayer-1.0pre3 (not present, but there is a substitute for fast installation)
--S MozillaFirebird-0.7 (i.e., this is not the installed Firebird, even though the version is the same!)
IP- bison-1.875c (installed and by definition present)
...

$ nix-env -f ./foo.nix -qa (show available derivations in the Nix expression foo.nix)
foo-1.2.3

Operation --switch-profile

Synopsis

nix-env { --switch-profile | -S } {path}

Description

This operation makes path the current profile for the user. That is, the symlink ~/.nix-profile is made to point to path.

Examples

$ nix-env -S ~/my-profile

Operation --list-generations

Synopsis

nix-env {--list-generations}

Description

This operation print a list of all the currently existing generations for the active profile. These may be switched to using the --switch-generation operation. It also prints the creation date of the generation, and indicates the current generation.

Examples

$ nix-env --list-generations
  95   2004-02-06 11:48:24
  96   2004-02-06 11:49:01
  97   2004-02-06 16:22:45
  98   2004-02-06 16:24:33   (current)

Operation --delete-generations

Synopsis

nix-env {--delete-generations} generations...

Description

This operation deletes the specified generations of the current profile. The generations can be a list of generation numbers, or the special value old to delete all non-current generations. Periodically deleting old generations is important to make garbage collection effective.

Examples

$ nix-env --delete-generations 3 4 8

$ nix-env -p other_profile --delete-generations old

Operation --switch-generation

Synopsis

nix-env { --switch-generation | -G } {generation}

Description

This operation makes generation number generation the current generation of the active profile. That is, if the profile is the path to the active profile, then the symlink profile is made to point to profile-generation-link, which is in turn a symlink to the actual user environment in the Nix store.

Switching will fail if the specified generation does not exist.

Examples

$ nix-env -G 42
switching from generation 50 to 42

Operation --rollback

Synopsis

nix-env {--rollback}

Description

This operation switches to the “previous” generation of the active profile, that is, the highest numbered generation lower than the current generation, if it exists. It is just a convenience wrapper around --list-generations and --switch-generation.

Examples

$ nix-env --rollback
switching from generation 92 to 91

$ nix-env --rolback
error: no generation older than the current (91) exists

Operation --import

Synopsis

nix-env { --import | -I } {path}

Description

This operation makes path the default active Nix expression for the user. That is, the symlink ~/.nix-userenv is made to point to path.

Examples

$ nix-env -I ~/nixpkgs-0.5/

A.2. nix-store

Name

nix-store — manipulate or query the Nix store

Synopsis

nix-store [--help] [--version] [--verbose...] [-v...] [--no-build-output] [-Q] [ { --max-jobs | -j } number ] [--keep-going] [-k] [--keep-failed] [-K] [--fallback] [--readonly-mode] operation [options...] [arguments...]

Description

The command nix-store performs primitive operations on the Nix store. You generally do not need to run this command manually.

nix-store takes exactly one operation flag which indicates the subcommand to be performed. These are documented below.

Common options

This section lists the options that are common to all operations. These options are allowed for every subcommand, though they may not always have an effect.

--help

Prints out a summary of the command syntax and exits.

--version

Prints out the Nix version number on standard output and exits.

--verbose / -v

Increases the level of verbosity of diagnostic messages printed on standard error. For each Nix operation, the information printed on standard output is well-defined; any diagnostic information is printed on standard error, never on standard output.

This option may be specified repeatedly. Currently, the following verbosity levels exist:

0

Errors only”: only print messages explaining why the Nix invocation failed.

1

Informational”: print useful messages about what Nix is doing. This is the default.

2

Talkative”: print more informational messages.

3

Chatty”: print even more informational messages.

4

Debug”: print debug information:

5

Vomit”: print vast amounts of debug information.

--no-build-output / -Q

By default, output written by builders to standard output and standard error is echoed to the Nix command's standard error. This option suppresses this behaviour. Note that the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix.

--max-jobs / -j

Sets the maximum number of build jobs that Nix will perform in parallel to the specified number. The default is 1. A higher value is useful on SMP systems or to exploit I/O latency.

--keep-going / -k

Keep going in case of failed builds, to the greatest extent possible. That is, if building an input of some derivation fails, Nix will still build the other inputs, but not the derivation itself. Without this option, Nix stops if any build fails (except for builds of substitutes), possibly killing builds in progress (in case of parallel or distributed builds).

--keep-failed / -K

Specifies that in case of a build failure, the temporary directory (usually in /tmp) in which the build takes place should not be deleted. The path of the build directory is printed as an informational message.

--fallback

Whenever Nix attempts to realise a derivation for which a closure is already known, but this closure cannot be realised, fall back on normalising the derivation.

The most common scenario in which this is useful is when we have registered substitutes in order to perform binary distribution from, say, a network repository. If the repository is down, the realisation of the derivation will fail. When this option is specified, Nix will build the derivation instead. Thus, binary installation falls back on a source installation. This option is not the default since it is generally not desirable for a transient failure in obtaining the substitutes to lead to a full build from source (with the related consumption of resources).

--readonly-mode

When this option is used, no attempt is made to open the Nix database. Most Nix operations do need database access, so those operations will fail.

Environment variables

The following environment variables affect the behaviour of nix-store.

TMPDIR=path

Use the directory path to store temporary files. In particular, this includes temporary build directories; these can take up substantial amounts of disk space. The default is /tmp.

Operation --realise

Synopsis

nix-store { --realise | -r } paths...

Description

The operation --install realises in the file system the store expressions stored in paths. If these expressions are derivation expressions, they are first normalised into a closure expression. This may happen in two ways. First, the corresponding closure expression (the successor) may already known (either because the build has already been performed, or because a successor was explicitly registered through the --successor operation). Otherwise, the build action described by the derivation is performed, and a closure expression is computed by scanning the result of the build for references to other paths in the store.

The paths of the closure expression corresponding to each expression in paths is printed on standard output.

Operation --gc

Synopsis

nix-store --gc { --print-live | --print-dead | --delete } [--min-age age]

Description

The operation --gc performs a garbage collection on the Nix store. What it does specifically is determined by the sub-operation, which is one of the following:

--print-live

This operation prints on standard output the set of “live” store paths, which are all the store paths reachable from a set of “root” store expressions read from standard input. Live paths should never be deleted, since that would break consistency — it would become possible that applications are installed that reference things that are no longer present in the store.

--print-dead

This operation prints out on standard output the set of “dead” store paths, which is just the opposite of the set of live paths: any path in the store that is not live (with respect to the roots) is dead.

--delete

This operation performs an actual garbage collection. All dead paths are removed from the store.

The set of root store expressions is read from standard input. Each line should contain exactly one store path.

The option --min-age specifies a minimum time in hours that an unreachable store path must not have been used before it is considered dead. The default is 0 (consider all unreachable store paths dead). Whether a store path has been used is determined by looking at its access time (atime), so this does not work if the store is located on a file system that has the noatime option set.

Warning

You generally will want to use the command nix-collect-garbage, which figures out the roots and then calls this command automatically.

Operation --query

Synopsis

nix-store { --query | -q } { --list | -l | --requisites | -R | --predecessors | --graph } [--normalise] [-n] [--force-realise] [-f] args...

Description

The operation --query displays various bits of information about store expressions or store paths. The queries are described below. At most one query can be specified. The default query is --list.

Common query options

--normalise / -n

For those queries that take a Nix store expression, this option causes those expressions to be normalised first.

--force-realise / -f

For those queries that take a Nix store expression, this option causes those expressions to be realised first. This is just a short-cut for the common idiom

nix-store --realise /nix/store/bla.store
x=`nix-store --query --normalise /nix/store/bla.store`
(do something with the path $x

which using this flag can be written as

x=`nix-store --query --normalise --force-realise /nix/store/bla.store`
(do something with the path $x

Queries

--list / -l

Prints out the output paths of the store expressions indicated by the identifiers args. In the case of a derivation expression, these are the paths that will be produced when the derivation is realised. In the case of a closure expression, these are the paths that were produced the derivation expression of which the closure expression is a successor.

--requisites / -R

Prints out the requisite paths of the store expressions indicated by the identifiers args. The requisite paths of a Nix expression are the paths that need to be present in the system to be able to realise the expression. That is, they form the closure of the expression in the file system (i.e., no path in the set of requisite paths points to anything outside the set of requisite paths).

The notion of requisite paths is very useful when one wants to distribute store expressions. Since they form a closure, they are the only paths one needs to distribute to another system to be able to realise the expression on the other system.

This query is generally used to implement various kinds of deployment. A source deployment is obtained by distributing the requisite paths of a derivation expression. A binary deployment is obtained by distributing the requisite paths of a closure expression. A cache deployment is obtained by distributing the requisite paths of a derivation expression and specifying the option --include-successors. This will include not just the paths of a source and binary deployment, but also all expressions and paths of subterms of the source. This is useful if one wants to realise on the target system a Nix expression that is similar but not quite the same as the one being distributed, since any common subterms will be reused.

This query has a number of options:

--exclude-exprs

Excludes the paths of store expressions. This causes the closure property to be lost, that is, the resulting set of paths is not enough to ensure realisibility.

--include-successors

Also include the requisites of successors (normal forms). Only the requisites of known successors are included, i.e., the normal forms of derivation expressions that have never been normalised will not be included.

Note that not just the successor of a derivation expression will be included, but also the successors of all input expressions of that derivation expression. I.e., all normal forms of subterms involved in the normalisation of the top-level term are included.

--predecessors

For each store expression stored at paths args, prints its predecessors. A derivation expression p is a predecessor of a store expression q iff q is a successor of p.

--graph

Prints a graph of the closure of the store expressions identified by args in the format of the dot tool of AT&T's GraphViz package.

Operation --successor

Synopsis

nix-store {--successor} srcpath sucpath...

Description

The operation --successor registers that the closure expression in sucpath is a successor of the derivation expression in srcpath. This is used to implement binary deployment.

Operation --substitute

Synopsis

nix-store {--substitute} srcpath subpath...

Description

The operation --substitute registers that the store path srcpath can be built by realising the derivation expression in subpath. This is used to implement binary deployment.

Operation --verify

Synopsis

nix-store {--verify}

Description

The operation --verify verifies the internal consistency of the Nix database, and the consistency between the Nix database and the Nix store. Any inconsistencies encountered are automatically repaired. Inconsistencies are generally the result of the Nix store or database being modified by non-Nix tools, or of bugs in Nix itself.

A.3. nix-instantiate

Name

nix-instantiate — instantiate store expressions from Nix expressions

Synopsis

nix-instantiate [--help] [--version] [--verbose...] [-v...] [--no-build-output] [-Q] [ { --max-jobs | -j } number ] [--keep-going] [-k] [--keep-failed] [-K] [--fallback] [--readonly-mode] [ --parse-only | --eval-only ] files...

Description

The command nix-instantiate generates (low-level) store expressions from (high-level) Nix expressions. It loads and evaluates the Nix expressions in each of files. Each top-level expression should evaluate to a derivation, a list of derivations, or a set of derivations. The paths of the resulting store expressions are printed on standard output.

This command is generally used for testing Nix expression before they are used with nix-env.

Options

--help

Prints out a summary of the command syntax and exits.

--version

Prints out the Nix version number on standard output and exits.

--verbose / -v

Increases the level of verbosity of diagnostic messages printed on standard error. For each Nix operation, the information printed on standard output is well-defined; any diagnostic information is printed on standard error, never on standard output.

This option may be specified repeatedly. Currently, the following verbosity levels exist:

0

Errors only”: only print messages explaining why the Nix invocation failed.

1

Informational”: print useful messages about what Nix is doing. This is the default.

2

Talkative”: print more informational messages.

3

Chatty”: print even more informational messages.

4

Debug”: print debug information:

5

Vomit”: print vast amounts of debug information.

--no-build-output / -Q

By default, output written by builders to standard output and standard error is echoed to the Nix command's standard error. This option suppresses this behaviour. Note that the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix.

--max-jobs / -j

Sets the maximum number of build jobs that Nix will perform in parallel to the specified number. The default is 1. A higher value is useful on SMP systems or to exploit I/O latency.

--keep-going / -k

Keep going in case of failed builds, to the greatest extent possible. That is, if building an input of some derivation fails, Nix will still build the other inputs, but not the derivation itself. Without this option, Nix stops if any build fails (except for builds of substitutes), possibly killing builds in progress (in case of parallel or distributed builds).

--keep-failed / -K

Specifies that in case of a build failure, the temporary directory (usually in /tmp) in which the build takes place should not be deleted. The path of the build directory is printed as an informational message.

--fallback

Whenever Nix attempts to realise a derivation for which a closure is already known, but this closure cannot be realised, fall back on normalising the derivation.

The most common scenario in which this is useful is when we have registered substitutes in order to perform binary distribution from, say, a network repository. If the repository is down, the realisation of the derivation will fail. When this option is specified, Nix will build the derivation instead. Thus, binary installation falls back on a source installation. This option is not the default since it is generally not desirable for a transient failure in obtaining the substitutes to lead to a full build from source (with the related consumption of resources).

--readonly-mode

When this option is used, no attempt is made to open the Nix database. Most Nix operations do need database access, so those operations will fail.

--parse-only

Just parse the input files, and print their abstract syntax trees on standard output in ATerm format.

--eval-only

Just parse and evaluate the input files, and print the resulting values on standard output. No instantiation of store expressions takes place.

Examples

$ nix-instantiate gcc.nix (instantiate)
/nix/store/468abdcb93aa22bb721142615b97698b-d-gcc-3.3.2.store

$ nix-store -r $(nix-instantiate gcc.nix) (build)

$ nix-store -r $(nix-instantiate gcc.nix) (print output path)
/nix/store/9afa718cddfdfe94b5b9303d0430ceb1-gcc-3.3.2

$ ls -l /nix/store/9afa718cddfdfe94b5b9303d0430ceb1-gcc-3.3.2
dr-xr-xr-x    2 eelco    users         360 2003-12-01 16:12 bin
dr-xr-xr-x    3 eelco    users          72 2003-12-01 16:12 include
...

A.4. nix-collect-garbage

Name

nix-collect-garbage — remove unreachable store paths

Synopsis

nix-collect-garbage [ --print-live | --print-dead ] [--min-age age]

Description

The command nix-collect-garbage performs a garbage collection on the Nix store: any paths in the Nix store that are garbage (not reachable from a set of root store expressions) are deleted.

The roots of the garbage collector are the store expressions mentioned in the files in the directory prefix/var/nix/gcroots. By default, the roots are all user environments in prefix/var/nix/profiles. You can register other store expressions as roots by writing the full path of the store expression to an arbitrary file in the gcroots directory (or a subdirectory thereof).

Options

--print-live / --print-dead

These options cause the set of live or dead paths to be printed, respectively, rather than performing an actual garbage collector. They correspond exactly with the sub-operations in nix-store --gc.

--min-age age

This option corresponds to the --min-age option in nix-store --gc.

Examples

To delete all unreachable paths, just do:

$ nix-collect-garbage

A.5. nix-push

Name

nix-push — push store paths onto a network cache

Synopsis

nix-push archives-put-url archives-get-url manifest-put-url paths...

Description

The command nix-push builds a set of store expressions (if necessary), and then packages and uploads all store paths in the resulting closures to a server. A network cache thus populated can subsequently be used to speed up software deployment on other machines using the nix-pull command.

nix-push performs the following actions.

  1. The store expressions stored in paths are realised (using nix-store --realise).

  2. All paths in the closure of the store expressions stored in paths are determined (using nix-store --query --requisites --include-successors). It should be noted that since the --include-successors flag is used, if you specify a derivation store expression, you get a combined source/binary distribution. If you only want a binary distribution, you should specify the closure store expression that result from realising these (see below).

  3. All store paths determined in the previous step are packaged and compressed into a bzipped NAR archive (extension .nar.bz2).

  4. A manifest is created that contains information on the store paths, their eventual URLs in the cache, and cryptographic hashes of the contents of the NAR archives.

  5. Each store path is uploaded to the remote directory specified by archives-put-url. HTTP PUT requests are used to do this. However, before a file x is uploaded to archives-put-url/x, nix-push first determines whether this upload is unnecessary by issuing a HTTP HEAD request on archives-get-url/x. This allows a cache to be shared between many partially overlapping nix-push invocations. (We use two URLs because the upload URL typically refers to a CGI script, while the download URL just refers to a file system directory on the server.)

  6. The manifest is uploaded using an HTTP PUT request to manifest-put-url. The corresponding URL to download the manifest can then be used by nix-pull.

Examples

To upload files there typically is some CGI script on the server side. This script should be be protected with a password. The following example uploads the store paths resulting from building the Nix expressions in foo.nix, passing appropriate authentication information:

$ nix-push \
    http://foo@bar:server.domain/cgi-bin/upload.pl/cache \
    http://server.domain/cache \
    http://foo@bar:server.domain/cgi-bin/upload.pl/MANIFEST \
    $(nix-instantiate foo.nix)

This will push both sources and binaries (and any build-time dependencies used in the build, such as compilers).

If we just want to push binaries, not sources and build-time dependencies, we can do:

$ nix-push urls $(nix-instantiate $(nix-store -r foo.nix))

A.6. nix-pull

Name

nix-pull — pull substitutes from a network cache

Synopsis

nix-pull url

Description

The command nix-pull obtains a list of pre-built store paths from the URL url, and for each of these store paths, registers a substitute derivation that downloads and unpacks it into the Nix store. This is used to speed up installations: if you attempt to install something that has already been built and stored into the network cache, Nix can transparently re-use the pre-built store paths.

The file at url must be compatible with the files created by nix-push.

Examples

$ nix-pull http://catamaran.labs.cs.uu.nl/dist/nix/nixpkgs-0.5pre753/MANIFEST

A.7. nix-prefetch-url

Name

nix-prefetch-url — copy a file from a URL into the store and print its MD5 hash

Synopsis

nix-prefetch-url url

Description

The command nix-prefetch-url downloads the file referenced by the URL url, prints its MD5 cryptographic hash code, and copies it into the Nix store. The file name in the store is hash-basename, where basename is everything following the final slash in url.

This command is just a convenience to Nix expression writers. Often a Nix expressions fetch some source distribution from the network using the fetchurl expression contained in nixpkgs. However, fetchurl requires an MD5 hash. If you don't know the hash, you would have to download the file first, and then fetchurl would download it again when you build your Nix expression. Since fetchurl uses the same name for the downloaded file as nix-prefetch-url, the redundant download can be avoided.

Examples

$ nix-prefetch-url ftp://ftp.nluug.nl/pub/gnu/make/make-3.80.tar.bz2
...
file has hash 0bbd1df101bc0294d440471e50feca71
...

Appendix B. Troubleshooting

(Nothing.)

Appendix C. Bugs / To-Do

  • The man-pages generated from the DocBook documentation are ugly.

  • Generations properly form a tree. E.g., if after switching to generation 39, we perform an installation action, a generation 43 is created which is a descendant of 39, not 42. So a rollback from 43 ought to go back to 39. This is not currently implemented; generations form a linear sequence.

  • Unify the concepts of successors and substitutes into a general notion of equivalent expressions. Expressions are equivalent if they have the same target paths with the same identifiers. However, even though they are functionally equivalent, they may differ stronly with respect to their performance characteristics. For example, realising a closure expression is more efficient that realising the derivation expression from which it was produced. On the other hand, distributing sources may be more efficient (storage- or bandwidth-wise) than distributing binaries. So we need to be able to attach weigths or priorities or performance annotations to expressions; Nix can then choose the most efficient expression dependent on the context.

  • Build management. In principle it is already possible to do build management using Nix (by writing builders that perform appropriate build steps), but the Nix expression language is not yet powerful enough to make this pleasant (?). The language should be extended with features from the Maak build manager. Another interesting idea is to write a make implementation that uses Nix as a back-end to support legacy build files.

  • There are race conditions between the garbage collector and other Nix tools. For instance, when we run nix-env to build and install a derivation and run the garbage collector at the same time, the garbage collector may kick in exactly between the build and installation steps, i.e., before the newly built derivation has become reachable from a root of the garbage collector.

    One solution would be for these programs to properly register temporary roots for the collector. Another would be to use stop-the-world garbage collection: if any tool is running, the garbage collector blocks, and vice versa. These solutions do not solve the situation where multiple tools are involved, e.g.,

    $ nix-store -r $(nix-instantiate foo.nix)

    since even if nix-instantiate where to register a temporary root, it would be released by the time nix-store is started. A solution would be to write the intermediate value to a file that is used as a root to the collector, e.g.,

    $ nix-instantiate foo.nix > /nix/var/nix/roots/bla
    $ nix-store -r $(cat /nix/var/nix/roots/bla)

  • For security, nix-push manifests should be digitally signed, and nix-pull should verify the signatures. The actual NAR archives in the cache do not need to be signed, since the manifest contains cryptographic hashes of these files (and fetchurl.nix checks them).

  • We should switch away from MD5, since it has been more-or-less cracked. We don't currently depend very much on the collision-resistance of MD5, but we will once we start sharing build results between users.

  • It would be useful to have an option in nix-env --delete-generations to remove non-current generations older than a certain age.