summaryrefslogtreecommitdiffstats
path: root/doc/build-helpers/fixed-point-arguments.chapter.md
blob: 38601a64eae46ad3119518872adea0927613099d (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Fixed-point arguments of build helpers {#chap-build-helpers-finalAttrs}

`stdenv.mkDerivation` also accepts a [fixed-point function](#function-library-lib.fixedPoints.fix) instead of a plain attribute set:

```nix
{
  stdenv,
  fetchurl,
}:
stdenv.mkDerivation (finalAttrs: {
  pname = "hello";
  version = "2.12";

  src = fetchurl {
    url = "mirror://gnu/hello/hello-${finalAttrs.version}.tar.gz";
    hash = "sha256-...";
  };
})
```

The function's input, conventionally named `finalAttrs`, is the final state of the attribute set. Here `src` reads `finalAttrs.version` instead of repeating the version string. A build helper like this is said to accept **fixed-point arguments**.

Attributes that reference each other through `finalAttrs` stay correct when changing any of them with [`overrideAttrs`](#sec-pkg-overrideAttrs), because they all access the final values of the fixed-point computation.

`rec` cannot do this: its self-references are fixed when the set is defined and ignore later overrides.
See [recursive-sets](https://nix.dev/manual/nix/stable/language/syntax#recursive-sets) for the underlying mechanism.

## Define a build helper with `lib.extendMkDerivation` {#sec-build-helper-extendMkDerivation}

Use [`lib.customisation.extendMkDerivation`](#function-library-lib.customisation.extendMkDerivation) to define a build helper with fixed-point support from an existing one.
Its argument `extendDrvArgs` takes an attribute overlay similar to [`<pkg>.overrideAttrs`](#sec-pkg-overrideAttrs).

Besides overriding, `lib.extendMkDerivation` also supports `excludeDrvArgNames` to optionally exclude some arguments in the input fixed-point arguments from passing down to the base build helper (specified as `constructDrv`).

:::{.example #ex-build-helpers-extendMkDerivation}

# Example `mkLocalDerivation` - a build helper over `mkDerivation`

Define a build helper named `mkLocalDerivation` that builds locally without using substitutes by default.

Use `lib.extendMkDerivation`:

```nix
{
  lib,
  stdenv,
}:
lib.extendMkDerivation {
  constructDrv = stdenv.mkDerivation;
  excludeDrvArgNames = [
    # Don't pass specialArg into mkDerivation.
    "specialArg"
  ];
  extendDrvArgs =
    finalAttrs:
    {
      preferLocalBuild ? true,
      allowSubstitute ? false,
      specialArg ? (_: false),
      ...
    }@args:
    {
      # Arguments to pass
      inherit preferLocalBuild allowSubstitute;
      # Some expressions involving specialArg
      greeting = if specialArg "hi" then "hi" else "hello";
    };
}
```
:::

To apply extra changes to the result derivation, pass `transformDrv` to `lib.extendMkDerivation`:

```nix
lib.customisation.extendMkDerivation { transformDrv = drv: /...; }
```

Construct a wrapper derivation around another derivation using `transformDrv`

The wrapper has access to the original arguments

:::{.example #ex-build-helpers-extendMkDerivation-transformDrv-wrapper}

# Define a custom build helper that downloads and builds

```nix
{
  lib,
  stdenvNoCC,
  cacert,
  configure-example,
  download-example,
}:

lib.extendMkDerivation {
  constructDrv = stdenvNoCC.mkDerivation;

  excludeDrvArgNames = [
    "bar"
  ];

  extendDrvArgs =
    finalAttrs:
    {
      bar,
      foo,
      hash ? "",
      ...
    }@args:
    {
      inherit hash;
      nativeBuildInputs = args.nativeBuildInputs or [ ] ++ [
        cacert
        download-example
      ];
      buildPhase = ''
        runHook preBuild
        download-example --foo="$foo" --out="$out"
        runHook postBuild
      '';
      impureEnvVars = lib.fetchers.proxyImpureEnvVars;
      outputHash = if finalAttrs.hash != "" then finalAttrs.hash else lib.fakeHash;
      outputHashFormat = "recursive";
      passthru = args.passthru or { } // {
        inherit bar;
      };
    };

  transformDrv =
    unwrapped:
    stdenvNoCC.mkDerivation (finalAttrs: {
      name = finalAttrs.src.name + "-wrapped";
      src = unwrapped;
      nativeBuildInputs = [
        configure-example
      ];
      inherit (unwrapped) bar;
      buildPhase = ''
        runHook preBuild
        configure-example --bar="$bar"
        runHook postBuild
      '';
    });
}
```
:::