aboutsummaryrefslogtreecommitdiff
path: root/doc/manual/src/expressions/arguments-variables.md
blob: 12198c879d389865b17841b07f9d0f2a7999f36a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Arguments and Variables

The [Nix expression for GNU Hello](expression-syntax.md) 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:

```nix
...

rec { 

  hello = import ../applications/misc/hello/ex-1  { 
    inherit fetchurl stdenv perl;
  };

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

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

  stdenv = ...;

}
```

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.

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 the Nix expression
    for GNU Hello in `all-packages.nix` at this point. That would be
    completely equivalent, but it would make `all-packages.nix` 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 a 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 the [Nix expression for GNU Hello](expression-syntax.md)).
    
    > **Note**
    > 
    > Nixpkgs has a convenience function `callPackage` that imports and
    > calls a function, filling in any missing arguments by passing the
    > corresponding attribute from the Nixpkgs set, like this:
    > 
    > ```nix
    > hello = callPackage ../applications/misc/hello/ex-1 { };
    > ```
    > 
    > If necessary, you can set or override arguments:
    > 
    > ```nix
    > hello = callPackage ../applications/misc/hello/ex-1 { stdenv = myStdenv; };
    > ```

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