You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,45 +6,199 @@ To build a local project in a Nix derivation, its source files must be accessibl
6
6
But since the builder runs in an isolated environment (if the [sandbox](https://nixos.org/manual/nix/stable/command-ref/conf-file.html#conf-sandbox) is enabled),
7
7
it won't have access to the local project files by default.
8
8
9
-
To make this work, the Nix language has certain builtin features to copy local paths to the Nix store,
9
+
To make this work regardless, the Nix language has certain builtin features to copy local paths to the Nix store,
10
10
whose paths are then accessible to derivation builders [^1].
11
11
12
12
[^1]: Technically only Nix store paths from the derivations inputs can be accessed,
13
13
but in practice this distinction is not important.
14
14
15
-
These builtin features are very limited in functionality and are not recommended if you need to do anything more advanced.
15
+
These builtin features are very limited in functionality and are not recommended for anything non-trivial. For more advanced use cases, the file set library should be used instead.
16
16
17
-
For advanced uses, use the file set library instead.
17
+
In this tutorial you'll learn both how to use the builtins and the file set library.
18
18
19
-
## Builtins
19
+
## Setting up a local experiment
20
20
21
-
### Path coercion to strings
21
+
To experiment with source file selection, we'll set up a local project.
22
22
23
-
The most transparent builtin feature is coercion of paths to strings, such as:
24
-
- Interpolating paths in strings:
23
+
To start out, create a new directory, enter it, and set up `niv` to manage the Nixpkgs dependency:
building '/nix/store/kmf9sw8fn7ps3ndqs31hvqwsa35b8l3g-hello.drv'...
69
+
hello world
70
+
error: builder for '/nix/store/kmf9sw8fn7ps3ndqs31hvqwsa35b8l3g-hello.drv'
71
+
failed to produce output path for output 'out'
72
+
```
73
+
74
+
We could also add `touch $out` to make the build succeed,
75
+
but we'll omit that for the sake of the tutorial, since we only need the build logs.
76
+
This also makes it easier to build it again, since successful derivation builds would get cached.
77
+
From now on we'll also make build outputs a bit shorter for the sake of brevity.
78
+
79
+
## Builtin coercion of paths to strings
80
+
81
+
The easiest way to use local files in builds is using the built-in coercion of [paths](https://nixos.org/manual/nix/stable/language/values.html#type-path) to strings.
82
+
83
+
Let's create a local `string.txt` file:
84
+
```
85
+
$ echo "This is a string" > string.txt
86
+
```
87
+
88
+
:::{.note}
89
+
Flakes in Git directories requires git add.
90
+
:::
91
+
92
+
The two main ways to coerce paths to strings are:
93
+
- Interpolating paths in strings. To try that, change your `package.nix` file to:
94
+
```nix
95
+
{ runCommand }:
96
+
runCommand "file-interpolation" { } ''
97
+
(
98
+
set -x
99
+
cat ${./string.txt}
100
+
)
101
+
''
102
+
```
103
+
104
+
:::{.note}
105
+
Interpolation into bash scripts generally requires [`lib.escapeShellArg`](https://nixos.org/manual/nixpkgs/stable/#function-library-lib.strings.escapeShellArg) for correct escaping.
106
+
In this case however, the interpolation results in a Nix store path of the form `/nix/store/<hash>-<name>`,
107
+
and all valid characters of such store paths don't need to be escaped in bash.
108
+
:::
109
+
110
+
- Using paths as derivation attributes. To try that, change your `package.nix` file to:
25
111
```nix
26
-
{ runCommandCC }:
27
-
runCommandCC "test" { } ''
28
-
cc ${./main.c} -o $out
112
+
{ runCommand }:
113
+
runCommand "file-interpolation" {
114
+
stringFile = ./string.txt;
115
+
} ''
116
+
(
117
+
set -x
118
+
cat "$stringFile"
119
+
)
29
120
''
30
121
```
31
-
- Using paths as derivation attributes:
122
+
123
+
:::{.note}
124
+
Nowadays using the explicit `env` attribute is recommended to set environment variables.
125
+
`env` doesn't implicitly coerce paths to strings, so it requires using string intepolation instead:
32
126
```nix
33
-
{ stdenv }:
34
-
stdenv.mkDerivation {
35
-
name = "test";
36
-
src = ./.;
37
-
}
127
+
{ runCommand }:
128
+
runCommand "file-interpolation" {
129
+
env.stringFile = "${./string.txt}";
130
+
} ''
131
+
(
132
+
set -x
133
+
cat "$stringFile"
134
+
)
135
+
''
38
136
```
137
+
:::
138
+
139
+
These all do the same when built:
140
+
```shell-session
141
+
$ nix-build
142
+
building '/nix/store/9fi0khrkmqw5srjzjsfa0b05hf8div4c-file-interpolation.drv'...
0 commit comments