Skip to content

Commit e80d333

Browse files
Ericson2314roberth
andauthored
Document Store Derivations and Deriving Paths (#12290)
This is a big step documenting the store layer on its own, separately from the evaluator (and `builtins.derivation`). Co-authored-by: Robert Hensing <[email protected]>
1 parent aa383a0 commit e80d333

File tree

21 files changed

+611
-241
lines changed

21 files changed

+611
-241
lines changed

doc/manual/redirects.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ const redirects = {
346346
"scoping-rules": "scoping.html",
347347
"string-literal": "string-literals.html",
348348
},
349+
"language/derivations.md": {
350+
"builder-execution": "store/drv/building.md#builder-execution",
351+
},
349352
"installation/installing-binary.html": {
350353
"linux": "uninstall.html#linux",
351354
"macos": "uninstall.html#macos",

doc/manual/source/SUMMARY.md.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
- [Store Object](store/store-object.md)
2323
- [Content-Addressing Store Objects](store/store-object/content-address.md)
2424
- [Store Path](store/store-path.md)
25+
- [Store Derivation and Deriving Path](store/drv.md)
26+
- [Building](store/building.md)
2527
- [Store Types](store/types/index.md)
2628
{{#include ./store/types/SUMMARY.md}}
2729
- [Nix Language](language/index.md)

doc/manual/source/architecture/architecture.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ It can also execute build plans to produce new data, which are made available to
6969
A build plan itself is a series of *build tasks*, together with their build inputs.
7070

7171
> **Important**
72-
> A build task in Nix is called [derivation](@docroot@/glossary.md#gloss-derivation).
72+
> A build task in Nix is called [store derivation](@docroot@/glossary.md#gloss-store-derivation).
7373
7474
Each build task has a special build input executed as *build instructions* in order to perform the build.
7575
The result of a build task can be input to another build task.

doc/manual/source/command-ref/nix-env/install.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ It is based on the current generation of the active [profile](@docroot@/command-
2222

2323
The arguments *args* map to store paths in a number of possible ways:
2424

25-
- By default, *args* is a set of [derivation] names denoting derivations in the [default Nix expression].
25+
- By default, *args* is a set of names denoting derivations in the [default Nix expression].
2626
These are [realised], and the resulting output paths are installed.
2727
Currently installed derivations with a name equal to the name of a derivation being added are removed unless the option `--preserve-installed` is specified.
2828

29-
[derivation]: @docroot@/glossary.md#gloss-derivation
29+
[derivation expression]: @docroot@/glossary.md#gloss-derivation-expression
3030
[default Nix expression]: @docroot@/command-ref/files/default-nix-expression.md
3131
[realised]: @docroot@/glossary.md#gloss-realise
3232

@@ -66,11 +66,11 @@ The arguments *args* map to store paths in a number of possible ways:
6666
This can be used to override the priority of the derivations being installed.
6767
This is useful if *args* are [store paths], which don't have any priority information.
6868

69-
- If *args* are [store derivations](@docroot@/glossary.md#gloss-store-derivation), then these are [realised], and the resulting output paths are installed.
69+
- If *args* are [store paths] that point to [store derivations][store derivation], then those store derivations are [realised], and the resulting output paths are installed.
7070

71-
- If *args* are [store paths] that are not store derivations, then these are [realised] and installed.
71+
- If *args* are [store paths] that do not point to store derivations, then these are [realised] and installed.
7272

73-
- By default all [outputs](@docroot@/language/derivations.md#attr-outputs) are installed for each [derivation].
73+
- By default all [outputs](@docroot@/language/derivations.md#attr-outputs) are installed for each [store derivation].
7474
This can be overridden by adding a `meta.outputsToInstall` attribute on the derivation listing a subset of the output names.
7575

7676
Example:
@@ -122,6 +122,8 @@ The arguments *args* map to store paths in a number of possible ways:
122122
manifest.nix
123123
```
124124

125+
[store derivation]: @docroot@/glossary.md#gloss-store-derivation
126+
125127
# Options
126128

127129
- `--prebuilt-only` / `-b`

doc/manual/source/command-ref/nix-env/query.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ derivation is shown unless `--no-name` is specified.
125125

126126
- `--drv-path`
127127

128-
Print the path of the [store derivation](@docroot@/glossary.md#gloss-store-derivation).
128+
Print the [store path] to the [store derivation].
129+
130+
[store path]: @docroot@/glossary.md#gloss-store-path
131+
[store derivation]: @docroot@/glossary.md#gloss-derivation
129132

130133
- `--out-path`
131134

doc/manual/source/command-ref/nix-instantiate.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ standard input.
4242
- `--eval`
4343

4444
Just parse and evaluate the input files, and print the resulting
45-
values on standard output. No instantiation of store derivations
46-
takes place.
45+
values on standard output.
46+
Store derivations are not serialized and written to the store, but instead just hashed and discarded.
4747

4848
> **Warning**
4949
>

doc/manual/source/glossary.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,35 @@
1919

2020
Besides content addressing, the Nix store also uses [input addressing](#gloss-input-addressed-store-object).
2121

22-
- [derivation]{#gloss-derivation}
22+
- [store derivation]{#gloss-store-derivation}
2323

24-
A description of a build task. The result of a derivation is a
25-
store object. Derivations declared in Nix expressions are specified
26-
using the [`derivation` primitive](./language/derivations.md). These are
27-
translated into low-level *store derivations* (implicitly by
28-
`nix-build`, or explicitly by `nix-instantiate`).
24+
A single build task.
25+
See [Store Derivation](@docroot@/store/drv.md#store-derivation) for details.
2926

30-
[derivation]: #gloss-derivation
27+
[store derivation]: #gloss-store-derivation
3128

32-
- [store derivation]{#gloss-store-derivation}
29+
- [derivation path]{#gloss-derivation-path}
3330

34-
A [derivation] represented as a `.drv` file in the [store].
35-
It has a [store path], like any [store object].
36-
It is the [instantiated][instantiate] form of a derivation.
31+
A [store path] which uniquely identifies a [store derivation].
3732

38-
Example: `/nix/store/g946hcz4c8mdvq2g8vxx42z51qb71rvp-git-2.38.1.drv`
33+
See [Referencing Store Derivations](@docroot@/store/drv.md#derivation-path) for details.
3934

40-
See [`nix derivation show`](./command-ref/new-cli/nix3-derivation-show.md) (experimental) for displaying the contents of store derivations.
35+
Not to be confused with [deriving path].
4136

42-
[store derivation]: #gloss-store-derivation
37+
[derivation path]: #gloss-derivation-path
38+
39+
- [derivation expression]{#gloss-derivation-expression}
40+
41+
A description of a [store derivation] in the Nix language.
42+
The output(s) of a derivation are store objects.
43+
Derivations are typically specified in Nix expressions using the [`derivation` primitive](./language/derivations.md).
44+
These are translated into store layer *derivations* (implicitly by `nix-env` and `nix-build`, or explicitly by `nix-instantiate`).
45+
46+
[derivation expression]: #gloss-derivation-expression
4347

4448
- [instantiate]{#gloss-instantiate}, instantiation
4549

46-
Save an evaluated [derivation] as a [store derivation] in the Nix [store].
50+
Translate a [derivation expression] into a [store derivation].
4751

4852
See [`nix-instantiate`](./command-ref/nix-instantiate.md), which produces a store derivation from a Nix expression that evaluates to a derivation.
4953

@@ -55,7 +59,7 @@
5559

5660
This can be achieved by:
5761
- Fetching a pre-built [store object] from a [substituter]
58-
- Running the [`builder`](@docroot@/language/derivations.md#attr-builder) executable as specified in the corresponding [derivation]
62+
- Running the [`builder`](@docroot@/language/derivations.md#attr-builder) executable as specified in the corresponding [store derivation]
5963
- Delegating to a [remote machine](@docroot@/command-ref/conf-file.md#conf-builders) and retrieving the outputs
6064
<!-- TODO: link [running] to build process page, #8888 -->
6165

@@ -73,7 +77,7 @@
7377

7478
- [fixed-output derivation]{#gloss-fixed-output-derivation} (FOD)
7579

76-
A [derivation] where a cryptographic hash of the [output] is determined in advance using the [`outputHash`](./language/advanced-attributes.md#adv-attr-outputHash) attribute, and where the [`builder`](@docroot@/language/derivations.md#attr-builder) executable has access to the network.
80+
A [store derivation] where a cryptographic hash of the [output] is determined in advance using the [`outputHash`](./language/advanced-attributes.md#adv-attr-outputHash) attribute, and where the [`builder`](@docroot@/language/derivations.md#attr-builder) executable has access to the network.
7781

7882
- [store]{#gloss-store}
7983

@@ -188,7 +192,7 @@
188192
>
189193
> The contents of a `.nix` file form a Nix expression.
190194
191-
Nix expressions specify [derivations][derivation], which are [instantiated][instantiate] into the Nix store as [store derivations][store derivation].
195+
Nix expressions specify [derivation expressions][derivation expression], which are [instantiated][instantiate] into the Nix store as [store derivations][store derivation].
192196
These derivations can then be [realised][realise] to produce [outputs][output].
193197

194198
> **Example**
@@ -230,14 +234,14 @@
230234

231235
- [output]{#gloss-output}
232236

233-
A [store object] produced by a [derivation].
237+
A [store object] produced by a [store derivation].
234238
See [the `outputs` argument to the `derivation` function](@docroot@/language/derivations.md#attr-outputs) for details.
235239

236240
[output]: #gloss-output
237241

238242
- [output path]{#gloss-output-path}
239243

240-
The [store path] to the [output] of a [derivation].
244+
The [store path] to the [output] of a [store derivation].
241245

242246
[output path]: #gloss-output-path
243247

@@ -246,14 +250,11 @@
246250

247251
- [deriving path]{#gloss-deriving-path}
248252

249-
Deriving paths are a way to refer to [store objects][store object] that ar not yet [realised][realise].
250-
This is necessary because, in general and particularly for [content-addressed derivations][content-addressed derivation], the [output path] of an [output] is not known in advance.
251-
There are two forms:
253+
Deriving paths are a way to refer to [store objects][store object] that might not yet be [realised][realise].
252254

253-
- *constant*: just a [store path]
254-
It can be made [valid][validity] by copying it into the store: from the evaluator, command line interface or another store.
255+
See [Deriving Path](./store/drv.md#deriving-path) for details.
255256

256-
- *output*: a pair of a [store path] to a [derivation] and an [output] name.
257+
Not to be confused with [derivation path].
257258

258259
- [deriver]{#gloss-deriver}
259260

doc/manual/source/language/derivations.md

Lines changed: 9 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Derivations
22

3-
The most important built-in function is `derivation`, which is used to describe a single derivation:
4-
a specification for running an executable on precisely defined input files to repeatably produce output files at uniquely determined file system paths.
3+
The most important built-in function is `derivation`, which is used to describe a single store-layer [store derivation].
4+
Consult the [store chapter](@docroot@/store/drv.md) for what a store derivation is;
5+
this section just concerns how to create one from the Nix language.
56

6-
It takes as input an attribute set, the attributes of which specify the inputs to the process.
7+
This builtin function takes as input an attribute set, the attributes of which specify the inputs to the process.
78
It outputs an attribute set, and produces a [store derivation] as a side effect of evaluation.
89

910
[store derivation]: @docroot@/glossary.md#gloss-store-derivation
@@ -15,7 +16,7 @@ It outputs an attribute set, and produces a [store derivation] as a side effect
1516
- [`name`]{#attr-name} ([String](@docroot@/language/types.md#type-string))
1617

1718
A symbolic name for the derivation.
18-
It is added to the [store path] of the corresponding [store derivation] as well as to its [output paths](@docroot@/glossary.md#gloss-output-path).
19+
See [derivation outputs](@docroot@/store/drv.md#outputs) for what this is affects.
1920

2021
[store path]: @docroot@/store/store-path.md
2122

@@ -28,17 +29,12 @@ It outputs an attribute set, and produces a [store derivation] as a side effect
2829
> }
2930
> ```
3031
>
31-
> The store derivation's path will be `/nix/store/<hash>-hello.drv`.
32+
> The derivation's path will be `/nix/store/<hash>-hello.drv`.
3233
> The [output](#attr-outputs) paths will be of the form `/nix/store/<hash>-hello[-<output>]`
3334
3435
- [`system`]{#attr-system} ([String](@docroot@/language/types.md#type-string))
3536
36-
The system type on which the [`builder`](#attr-builder) executable is meant to be run.
37-
38-
A necessary condition for Nix to build derivations locally is that the `system` attribute matches the current [`system` configuration option].
39-
It can automatically [build on other platforms](@docroot@/language/derivations.md#attr-builder) by forwarding build requests to other machines.
40-
41-
[`system` configuration option]: @docroot@/command-ref/conf-file.md#conf-system
37+
See [system](@docroot@/store/drv.md#system).
4238
4339
> **Example**
4440
>
@@ -68,7 +64,7 @@ It outputs an attribute set, and produces a [store derivation] as a side effect
6864
6965
- [`builder`]{#attr-builder} ([Path](@docroot@/language/types.md#type-path) | [String](@docroot@/language/types.md#type-string))
7066
71-
Path to an executable that will perform the build.
67+
See [builder](@docroot@/store/drv.md#builder).
7268
7369
> **Example**
7470
>
@@ -117,7 +113,7 @@ It outputs an attribute set, and produces a [store derivation] as a side effect
117113
118114
Default: `[ ]`
119115
120-
Command-line arguments to be passed to the [`builder`](#attr-builder) executable.
116+
See [args](@docroot@/store/drv.md#args).
121117
122118
> **Example**
123119
>
@@ -239,77 +235,3 @@ It outputs an attribute set, and produces a [store derivation] as a side effect
239235
passed as an empty string.
240236
241237
<!-- FIXME: add a section on output attributes -->
242-
243-
## Builder execution
244-
245-
The [`builder`](#attr-builder) is executed as follows:
246-
247-
- A temporary directory is created under the directory specified by
248-
`TMPDIR` (default `/tmp`) where the build will take place. The
249-
current directory is changed to this directory.
250-
251-
- The environment is cleared and set to the derivation attributes, as
252-
specified above.
253-
254-
- In addition, the following variables are set:
255-
256-
- `NIX_BUILD_TOP` contains the path of the temporary directory for
257-
this build.
258-
259-
- Also, `TMPDIR`, `TEMPDIR`, `TMP`, `TEMP` are set to point to the
260-
temporary directory. This is to prevent the builder from
261-
accidentally writing temporary files anywhere else. Doing so
262-
might cause interference by other processes.
263-
264-
- `PATH` is set to `/path-not-set` to prevent shells from
265-
initialising it to their built-in default value.
266-
267-
- `HOME` is set to `/homeless-shelter` to prevent programs from
268-
using `/etc/passwd` or the like to find the user's home
269-
directory, which could cause impurity. Usually, when `HOME` is
270-
set, it is used as the location of the home directory, even if
271-
it points to a non-existent path.
272-
273-
- `NIX_STORE` is set to the path of the top-level Nix store
274-
directory (typically, `/nix/store`).
275-
276-
- `NIX_ATTRS_JSON_FILE` & `NIX_ATTRS_SH_FILE` if `__structuredAttrs`
277-
is set to `true` for the derivation. A detailed explanation of this
278-
behavior can be found in the
279-
[section about structured attrs](./advanced-attributes.md#adv-attr-structuredAttrs).
280-
281-
- For each output declared in `outputs`, the corresponding
282-
environment variable is set to point to the intended path in the
283-
Nix store for that output. Each output path is a concatenation
284-
of the cryptographic hash of all build inputs, the `name`
285-
attribute and the output name. (The output name is omitted if
286-
it’s `out`.)
287-
288-
- If an output path already exists, it is removed. Also, locks are
289-
acquired to prevent multiple Nix instances from performing the same
290-
build at the same time.
291-
292-
- A log of the combined standard output and error is written to
293-
`/nix/var/log/nix`.
294-
295-
- The builder is executed with the arguments specified by the
296-
attribute `args`. If it exits with exit code 0, it is considered to
297-
have succeeded.
298-
299-
- The temporary directory is removed (unless the `-K` option was
300-
specified).
301-
302-
- If the build was successful, Nix scans each output path for
303-
references to input paths by looking for the hash parts of the input
304-
paths. Since these are potential runtime dependencies, Nix registers
305-
them as dependencies of the output paths.
306-
307-
- After the build, Nix sets the last-modified timestamp on all files
308-
in the build result to 1 (00:00:01 1/1/1970 UTC), sets the group to
309-
the default group, and sets the mode of the file to 0444 or 0555
310-
(i.e., read-only, with execute permission enabled if the file was
311-
originally executable). Note that possible `setuid` and `setgid`
312-
bits are cleared. Setuid and setgid programs are not currently
313-
supported by Nix. This is because the Nix archives used in
314-
deployment have no concept of ownership information, and because it
315-
makes the build result dependent on the user performing the build.

doc/manual/source/language/import-from-derivation.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ Boxes are data structures, arrow labels are transformations.
7171
| evaluate | | |
7272
| | | | |
7373
| V | | |
74-
| .------------. | | .------------------. |
75-
| | derivation |----|-instantiate-|->| store derivation | |
74+
| .------------. | | |
75+
| | derivation | | | .------------------. |
76+
| | expression |----|-instantiate-|->| store derivation | |
7677
| '------------' | | '------------------' |
7778
| | | | |
7879
| | | realise |

doc/manual/source/language/string-interpolation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Rather than writing
2222
"--with-freetype2-library=" + freetype + "/lib"
2323
```
2424

25-
(where `freetype` is a [derivation]), you can instead write
25+
(where `freetype` is a [derivation expression]), you can instead write
2626

27-
[derivation]: @docroot@/glossary.md#gloss-derivation
27+
[derivation expression]: @docroot@/glossary.md#gloss-derivation-expression
2828

2929
```nix
3030
"--with-freetype2-library=${freetype}/lib"
@@ -148,7 +148,7 @@ An expression that is interpolated must evaluate to one of the following:
148148
- `__toString` must be a function that takes the attribute set itself and returns a string
149149
- `outPath` must be a string
150150
151-
This includes [derivations](./derivations.md) or [flake inputs](@docroot@/command-ref/new-cli/nix3-flake.md#flake-inputs) (experimental).
151+
This includes [derivation expressions](./derivations.md) or [flake inputs](@docroot@/command-ref/new-cli/nix3-flake.md#flake-inputs) (experimental).
152152
153153
A string interpolates to itself.
154154

0 commit comments

Comments
 (0)