diff options
Diffstat (limited to 'doc/manual/src')
-rw-r--r-- | doc/manual/src/expressions/arguments-variables.md | 28 | ||||
-rw-r--r-- | doc/manual/src/expressions/build-script.md | 32 | ||||
-rw-r--r-- | doc/manual/src/expressions/builtins.md | 54 | ||||
-rw-r--r-- | doc/manual/src/expressions/expression-syntax.md | 29 | ||||
-rw-r--r-- | doc/manual/src/expressions/generic-builder.md | 18 | ||||
-rw-r--r-- | doc/manual/src/expressions/language-constructs.md | 22 |
6 files changed, 93 insertions, 90 deletions
diff --git a/doc/manual/src/expressions/arguments-variables.md b/doc/manual/src/expressions/arguments-variables.md index 9a373d94d..436ae559d 100644 --- a/doc/manual/src/expressions/arguments-variables.md +++ b/doc/manual/src/expressions/arguments-variables.md @@ -1,14 +1,21 @@ # Arguments and Variables +The Nix expression in [???](#ex-hello-nix) 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/top-level/all-packages.nix`, +where all Nix expressions for packages are imported and called with the +appropriate arguments. Here are some fragments of `all-packages.nix`, +with annotations of what they mean: + ... - rec { + rec { ① - hello = import ../applications/misc/hello/ex-1 { + hello = import ../applications/misc/hello/ex-1 ② { ③ inherit fetchurl stdenv perl; }; - perl = import ../development/interpreters/perl { + perl = import ../development/interpreters/perl { ④ inherit fetchurl stdenv; }; @@ -20,20 +27,13 @@ } -The Nix expression in [???](#ex-hello-nix) 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/top-level/all-packages.nix`, -where all Nix expressions for packages are imported and called with the -appropriate arguments. [example\_title](#ex-hello-composition) shows -some fragments of `all-packages.nix`. - - - This file defines a set of attributes, all of which are concrete +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 packages into each other. - - Here we *import* the Nix expression for GNU Hello. The import +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 [???](#ex-hello-nix) in `all-packages.nix` at this point. That would be completely @@ -44,7 +44,7 @@ some fragments of `all-packages.nix`. import a directory, Nix automatically appends `/default.nix` to the file name. - - This is where the actual composition takes place. Here we *call* the +3. This is where the actual composition takes place. Here we *call* the function imported from `../applications/misc/hello/ex-1` with a set containing the things that the function expects, namely `fetchurl`, `stdenv`, and `perl`. We use inherit again to use the attributes @@ -68,5 +68,5 @@ some fragments of `all-packages.nix`. > > hello = callPackage ../applications/misc/hello/ex-1 { stdenv = myStdenv; }; - - Likewise, we have to instantiate Perl, `fetchurl`, and the standard +4. Likewise, we have to instantiate Perl, `fetchurl`, and the standard environment. diff --git a/doc/manual/src/expressions/build-script.md b/doc/manual/src/expressions/build-script.md index a359ebc46..f922182c2 100644 --- a/doc/manual/src/expressions/build-script.md +++ b/doc/manual/src/expressions/build-script.md @@ -1,23 +1,23 @@ # Build Script - source $stdenv/setup +Here is the builder referenced from Hello's Nix expression (stored in +`pkgs/applications/misc/hello/ex-1/builder.sh`): + + source $stdenv/setup ① - PATH=$perl/bin:$PATH + PATH=$perl/bin:$PATH ② - tar xvfz $src + tar xvfz $src ③ cd hello-* - ./configure --prefix=$out - make + ./configure --prefix=$out ④ + make ⑤ make install -[example\_title](#ex-hello-builder) 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: +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: - - When Nix runs a builder, it initially completely clears the +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\[1\]. This is done to prevent undeclared inputs from being used in the build process. If @@ -31,13 +31,13 @@ what a builder does. It performs the following steps: attribute in [???](#ex-hello-nix), but `mkDerivation` adds it automatically.) - - Since Hello needs Perl, we have to make sure that Perl is in the +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 package (since it was passed in as an attribute to the derivation), so `$perl/bin` is the directory containing the Perl interpreter. - - Now we have to unpack the sources. The `src` attribute was bound to +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` @@ -50,7 +50,7 @@ what a builder does. It performs the following steps: have to worry about files from previous builds interfering with the current build. - - GNU Hello is a typical Autoconf-based package, so we first have to +4. GNU Hello is a typical Autoconf-based package, so we first have to run its `configure` script. In Nix every package is stored in a separate location in the Nix store, for instance `/nix/store/9a54ba97fb71b65fda531012d0443ce2-hello-2.1.1`. Nix @@ -60,7 +60,7 @@ what a builder does. It performs the following steps: `--prefix=$out` to cause Hello to be installed in the expected location. - - Finally we build Hello (`make`) and install it into the 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 diff --git a/doc/manual/src/expressions/builtins.md b/doc/manual/src/expressions/builtins.md index a378fe8e4..f60af9d24 100644 --- a/doc/manual/src/expressions/builtins.md +++ b/doc/manual/src/expressions/builtins.md @@ -735,31 +735,7 @@ For instance, `derivation` is also available as `builtins.derivation`. builder in a more structured format than plain environment variables. - [example\_title](#ex-toxml) shows an example where this is the case. - The builder is supposed to generate the configuration file for a - [Jetty servlet container](http://jetty.mortbay.org/). A servlet - container contains a number of servlets (`*.war` files) each - exported under a specific URI prefix. So the servlet configuration - is a list of sets containing the `path` and `war` of the servlet - ([co\_title](#ex-toxml-co-servlets)). This kind of information is - difficult to communicate with the normal method of passing - information through an environment variable, which just concatenates - everything together into a string (which might just work in this - case, but wouldn’t work if fields are optional or contain lists - themselves). Instead the Nix expression is converted to an XML - representation with `toXML`, which is unambiguous and can easily be - processed with the appropriate tools. For instance, in the example - an XSLT stylesheet ([co\_title](#ex-toxml-co-stylesheet)) is applied - to it ([co\_title](#ex-toxml-co-apply)) to generate the XML - configuration file for the Jetty server. The XML representation - produced from [co\_title](#ex-toxml-co-servlets) by `toXML` is shown - in [example\_title](#ex-toxml-result). - - Note that [example\_title](#ex-toxml) uses the `toFile` built-in to - write the builder and the stylesheet “inline” in the Nix expression. - The path of the stylesheet is spliced into the builder at `xsltproc - ${stylesheet} - ...`. + Here is an example where this is the case: { stdenv, fetchurl, libxslt, jira, uberwiki }: @@ -771,10 +747,10 @@ For instance, `derivation` is also available as `builtins.derivation`. builder = builtins.toFile "builder.sh" " source $stdenv/setup mkdir $out - echo "$servlets" | xsltproc ${stylesheet} - > $out/server-conf.xml + echo "$servlets" | xsltproc ${stylesheet} - > $out/server-conf.xml ① "; - stylesheet = builtins.toFile "stylesheet.xsl" + stylesheet = builtins.toFile "stylesheet.xsl" ② "<?xml version='1.0' encoding='UTF-8'?> <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'> <xsl:template match='/'> @@ -790,12 +766,29 @@ For instance, `derivation` is also available as `builtins.derivation`. </xsl:stylesheet> "; - servlets = builtins.toXML [ + servlets = builtins.toXML [ ③ { path = "/bugtracker"; war = jira + "/lib/atlassian-jira.war"; } { path = "/wiki"; war = uberwiki + "/uberwiki.war"; } ]; }) + The builder is supposed to generate the configuration file for a + [Jetty servlet container](http://jetty.mortbay.org/). A servlet + container contains a number of servlets (`*.war` files) each + exported under a specific URI prefix. So the servlet configuration + is a list of sets containing the `path` and `war` of the servlet + ([???](#ex-toxml-co-servlets)). This kind of information is + difficult to communicate with the normal method of passing + information through an environment variable, which just concatenates + everything together into a string (which might just work in this + case, but wouldn’t work if fields are optional or contain lists + themselves). Instead the Nix expression is converted to an XML + representation with `toXML`, which is unambiguous and can easily be + processed with the appropriate tools. For instance, in the example + an XSLT stylesheet (at point ②) is applied to it (at point ①) to + generate the XML configuration file for the Jetty server. The XML + representation produced at point ③ by `toXML` is as follows: + <?xml version='1.0' encoding='utf-8'?> <expr> <list> @@ -817,6 +810,11 @@ For instance, `derivation` is also available as `builtins.derivation`. </attrs> </list> </expr> + + Note that [???](#ex-toxml) uses the `toFile` built-in to write the + builder and the stylesheet “inline” in the Nix expression. The path + of the stylesheet is spliced into the builder using the syntax + `xsltproc ${stylesheet}`. - `builtins.trace` e1 e2 Evaluate e1 and print its abstract syntax representation on standard diff --git a/doc/manual/src/expressions/expression-syntax.md b/doc/manual/src/expressions/expression-syntax.md index 935484c33..9e99a1f60 100644 --- a/doc/manual/src/expressions/expression-syntax.md +++ b/doc/manual/src/expressions/expression-syntax.md @@ -1,25 +1,26 @@ # Expression Syntax - { stdenv, fetchurl, perl }: +Here is a Nix expression for GNU Hello: + + { stdenv, fetchurl, perl }: ① - stdenv.mkDerivation { - name = "hello-2.1.1"; - builder = ./builder.sh; - src = fetchurl { + stdenv.mkDerivation { ② + name = "hello-2.1.1"; ③ + builder = ./builder.sh; ④ + src = fetchurl { ⑤ url = "ftp://ftp.nluug.nl/pub/gnu/hello/hello-2.1.1.tar.gz"; sha256 = "1md7jsfd8pa45z73bz1kszpp01yw6x5ljkjk2hx7wl800any6465"; }; - inherit perl; + inherit perl; ⑥ } -[example\_title](#ex-hello-nix) shows a Nix expression for GNU Hello. -It's actually already in the Nix Packages collection in +This file is 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): - - This states that the expression is a *function* that expects to be +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 package that @@ -37,7 +38,7 @@ elements (referenced from the figure by number): the required arguments, the body should describe how to build an instance of the Hello package. - - So we have to build a package. Building something from other stuff +2. So we have to build a package. 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 @@ -50,13 +51,13 @@ elements (referenced from the figure by number): nameN = exprN; }`. - - The attribute `name` specifies the symbolic name and version of the +3. The attribute `name` specifies the symbolic name and version of the package. 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 packages. This attribute is required by `mkDerivation`. - - The attribute `builder` specifies the builder. This attribute can +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 @@ -64,7 +65,7 @@ elements (referenced from the figure by number): educational purposes. The value `./builder.sh` refers to the shell script shown in [???](#ex-hello-builder), discussed below. - - The builder has to know what the sources of the package are. Here, +5. The builder has to know what the sources of the package are. Here, the attribute `src` is bound to the result of a call to the `fetchurl` function. Given a URL and a SHA-256 hash of the expected contents of the file at that URL, this function builds a derivation @@ -77,7 +78,7 @@ elements (referenced from the figure by number): However, `src` is customary, and it's also expected by the default builder (which we don't use in this example). - - Since the derivation requires Perl, we have to pass the value of the +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 diff --git a/doc/manual/src/expressions/generic-builder.md b/doc/manual/src/expressions/generic-builder.md index a00b08b55..90bdc556b 100644 --- a/doc/manual/src/expressions/generic-builder.md +++ b/doc/manual/src/expressions/generic-builder.md @@ -13,24 +13,26 @@ like this: 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\_title](#ex-hello-builder2). +functions that automate the build process. Here is what a builder using +the generic build facilities looks like: - buildInputs="$perl" + buildInputs="$perl" ① - source $stdenv/setup + source $stdenv/setup ② - genericBuild + genericBuild ③ - - The `buildInputs` variable tells `setup` to use the indicated +Here is what each line means: + +1. The `buildInputs` variable tells `setup` to use the indicated packages as “inputs”. This means that if a package 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.\[1\] - - The function `genericBuild` is defined in the file `$stdenv/setup`. +2. The function `genericBuild` is defined in the file `$stdenv/setup`. - - The final step calls the shell function `genericBuild`, which +3. The final step calls the shell function `genericBuild`, which performs the steps that were done explicitly in [???](#ex-hello-builder). The generic builder is smart enough to figure out whether to unpack the sources using `gzip`, `bzip2`, etc. diff --git a/doc/manual/src/expressions/language-constructs.md b/doc/manual/src/expressions/language-constructs.md index a773c239b..121e4d998 100644 --- a/doc/manual/src/expressions/language-constructs.md +++ b/doc/manual/src/expressions/language-constructs.md @@ -203,6 +203,9 @@ 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. +Here is a Nix expression for the Subversion package that shows how +assertions can be used:. + { localServer ? false , httpServer ? false , sslSupport ? false @@ -213,9 +216,9 @@ aborted and a backtrace is printed. , openssl ? null, httpd ? null, db4 ? null, expat, swig ? null, j2sdk ? null }: - assert localServer -> db4 != null; - assert httpServer -> httpd != null && httpd.expat == expat; - assert sslSupport -> openssl != null && (httpServer -> httpd.openssl == openssl); + assert localServer -> db4 != null; ① + assert httpServer -> httpd != null && httpd.expat == expat; ② + assert sslSupport -> openssl != null && (httpServer -> httpd.openssl == openssl); ③ assert pythonBindings -> swig != null && swig.pythonSupport; assert javaSwigBindings -> swig != null && swig.javaSupport; assert javahlBindings -> j2sdk != null; @@ -223,33 +226,32 @@ aborted and a backtrace is printed. stdenv.mkDerivation { name = "subversion-1.1.1"; ... - openssl = if sslSupport then openssl else null; + openssl = if sslSupport then openssl else null; ④ ... } -[example\_title](#ex-subversion-nix) show how assertions are used in the -Nix expression for Subversion. +The points of interest are: - - This assertion states that if Subversion is to have support for +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. - - This is a more subtle condition: if Subversion is built with Apache +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. - - This assertion says that in order for Subversion to have SSL support +3. 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.) - - The conditional here is not really related to assertions, but is +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 |