-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e2d34f
commit 8b18c67
Showing
4 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Import From Derivation | ||
|
||
The value of a Nix expression can depend on the contents of a [store object] produced by a [derivation]. | ||
In this case, when that store object is needed, evaluation will be paused, the store object [realised], and then evaluation resumed. | ||
|
||
[store object]: /glossary.md#gloss-store-object | ||
[derivation]: /glossary.md#gloss-derivation | ||
[realised]: /glossary.md#gloss-realise | ||
|
||
This has performance implications: | ||
Since evaluation is sequential, each required store object that is not already in the store will also be realised sequentially. | ||
|
||
Passing a derivation `drv` to any built-in function that reads from the filesystem constitutes Import From Derivation: | ||
|
||
- [`import`](./language/builtins.md#builtins-import)` drv` | ||
- [`builtins.readFile`](./language/builtins.md#builtins-readFile)` drv` | ||
- [`builtins.readDir`](./language/builtins.md#builtins-readDir)` drv` | ||
- [`builtins.pathExists`](./language/builtins.md#builtins-pathExists)` drv` | ||
- [`builtins.filterSource`](./language/builtins.md#builtins-filterSource)` f drv` | ||
- [`builtins.path`](./language/builtins.md#builtins-path)` { path = drv; }` | ||
- [`builtins.hashFile`](./language/builtins.md#builtins-hashFile)` t drv` | ||
- `builtins.scopedImport x drv` | ||
|
||
Building during evaluation can be disabled by setting [`allow-import-from-derivation`](../command-ref/conf-file.md#conf-allow-import-from-derivation) to `false`. | ||
|
||
## Example | ||
|
||
In the following Nix expression, the inner derivation `drv` produces a file containing `"hello"`. | ||
|
||
```nix | ||
# IFD.nix | ||
let | ||
drv = derivation { | ||
name = "hello"; | ||
builder = /bin/sh; | ||
args = [ "-c" ''echo \"hello\" > $out'' ]; | ||
system = builtins.currentSystem; | ||
}; | ||
in "${import drv} world" | ||
``` | ||
|
||
```shellSession | ||
nix-instantiate IFD.nix --eval --read-write-mode | ||
``` | ||
|
||
``` | ||
building '/nix/store/348q1cal6sdgfxs8zqi9v8llrsn4kqkq-hello.drv'... | ||
"hello world" | ||
``` | ||
|
||
Since `"hello"` is a valid Nix expression, it can be [`import`](./builtins.md#builtins-import)ed. | ||
That requires reading from the output [store path](@docroot@/glossary.md#gloss-store-path) of `drv`, which has to be [realised] before its contents can be read and evaluated. | ||
|
||
## Illustration | ||
|
||
The following diagram shows how evaluation is interrupted by a build, if the value of a Nix expression depends on realising a store object. | ||
|
||
``` | ||
+----------------------+ +------------------------+ | ||
| Nix language | | Nix store | | ||
| .----------------. | | | | ||
| | Nix expression | | | | | ||
| '----------------' | | | | ||
| | | | | | ||
| evaluate | | | | ||
| | | | | | ||
| V | | | | ||
| .------------. | | .------------------. | | ||
| | derivation |----|-instantiate-|->| store derivation | | | ||
| '------------' | | '------------------' | | ||
| | | | | | ||
| | | realise | | ||
| | | | | | ||
| | | V | | ||
| .----------------. | | .--------------. | | ||
| | Nix expression |<-|----read-----|----| store object | | | ||
| '----------------' | | '--------------' | | ||
| | | | | | ||
| evaluate | | | | ||
| | | | | | ||
| V | | | | ||
| .------------. | | | | ||
| | value | | | | | ||
| '------------' | | | | ||
+----------------------+ +------------------------+ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters