1# Trivial build helpers {#chap-trivial-builders}
2
3Nixpkgs provides a couple of functions that help with building derivations. The most important one, `stdenv.mkDerivation`, has already been documented above. The following functions wrap `stdenv.mkDerivation`, making it easier to use in certain cases.
4
5## `runCommand` {#trivial-builder-runCommand}
6
7`runCommand :: String -> AttrSet -> String -> Derivation`
8
9`runCommand name drvAttrs buildCommand` returns a derivation that is built by running the specified shell commands.
10
11`name :: String`
12: The name that Nix will append to the store path in the same way that `stdenv.mkDerivation` uses its `name` attribute.
13
14`drvAttr :: AttrSet`
15: Attributes to pass to the underlying call to [`stdenv.mkDerivation`](#chap-stdenv).
16
17`buildCommand :: String`
18: Shell commands to run in the derivation builder.
19
20 ::: {.note}
21 You have to create a file or directory `$out` for Nix to be able to run the builder successfully.
22 :::
23
24::: {.example #ex-runcommand-simple}
25# Invocation of `runCommand`
26
27```nix
28(import <nixpkgs> {}).runCommand "my-example" {} ''
29 echo My example command is running
30
31 mkdir $out
32
33 echo I can write data to the Nix store > $out/message
34
35 echo I can also run basic commands like:
36
37 echo ls
38 ls
39
40 echo whoami
41 whoami
42
43 echo date
44 date
45''
46```
47:::
48
49## `runCommandCC` {#trivial-builder-runCommandCC}
50
51This works just like `runCommand`. The only difference is that it also provides a C compiler in `buildCommand`'s environment. To minimize your dependencies, you should only use this if you are sure you will need a C compiler as part of running your command.
52
53## `runCommandLocal` {#trivial-builder-runCommandLocal}
54
55Variant of `runCommand` that forces the derivation to be built locally, it is not substituted. This is intended for very cheap commands (<1s execution time). It saves on the network round-trip and can speed up a build.
56
57::: {.note}
58This sets [`allowSubstitutes` to `false`](https://nixos.org/nix/manual/#adv-attr-allowSubstitutes), so only use `runCommandLocal` if you are certain the user will always have a builder for the `system` of the derivation. This should be true for most trivial use cases (e.g., just copying some files to a different location or adding symlinks) because there the `system` is usually the same as `builtins.currentSystem`.
59:::
60
61## `writeTextFile`, `writeText`, `writeTextDir`, `writeScript`, `writeScriptBin` {#trivial-builder-writeText}
62
63These functions write `text` to the Nix store. This is useful for creating scripts from Nix expressions. `writeTextFile` takes an attribute set and expects two arguments, `name` and `text`. `name` corresponds to the name used in the Nix store path. `text` will be the contents of the file. You can also set `executable` to true to make this file have the executable bit set.
64
65Many more commands wrap `writeTextFile` including `writeText`, `writeTextDir`, `writeScript`, and `writeScriptBin`. These are convenience functions over `writeTextFile`.
66
67Here are a few examples:
68```nix
69# Writes my-file to /nix/store/<store path>
70writeTextFile {
71 name = "my-file";
72 text = ''
73 Contents of File
74 '';
75}
76# See also the `writeText` helper function below.
77
78# Writes executable my-file to /nix/store/<store path>/bin/my-file
79writeTextFile {
80 name = "my-file";
81 text = ''
82 Contents of File
83 '';
84 executable = true;
85 destination = "/bin/my-file";
86}
87# Writes contents of file to /nix/store/<store path>
88writeText "my-file"
89 ''
90 Contents of File
91 '';
92# Writes contents of file to /nix/store/<store path>/share/my-file
93writeTextDir "share/my-file"
94 ''
95 Contents of File
96 '';
97# Writes my-file to /nix/store/<store path> and makes executable
98writeScript "my-file"
99 ''
100 Contents of File
101 '';
102# Writes my-file to /nix/store/<store path>/bin/my-file and makes executable.
103writeScriptBin "my-file"
104 ''
105 Contents of File
106 '';
107# Writes my-file to /nix/store/<store path> and makes executable.
108writeShellScript "my-file"
109 ''
110 Contents of File
111 '';
112# Writes my-file to /nix/store/<store path>/bin/my-file and makes executable.
113writeShellScriptBin "my-file"
114 ''
115 Contents of File
116 '';
117
118```
119
120## `concatTextFile`, `concatText`, `concatScript` {#trivial-builder-concatText}
121
122These functions concatenate `files` to the Nix store in a single file. This is useful for configuration files structured in lines of text. `concatTextFile` takes an attribute set and expects two arguments, `name` and `files`. `name` corresponds to the name used in the Nix store path. `files` will be the files to be concatenated. You can also set `executable` to true to make this file have the executable bit set.
123`concatText` and`concatScript` are simple wrappers over `concatTextFile`.
124
125Here are a few examples:
126```nix
127
128# Writes my-file to /nix/store/<store path>
129concatTextFile {
130 name = "my-file";
131 files = [ drv1 "${drv2}/path/to/file" ];
132}
133# See also the `concatText` helper function below.
134
135# Writes executable my-file to /nix/store/<store path>/bin/my-file
136concatTextFile {
137 name = "my-file";
138 files = [ drv1 "${drv2}/path/to/file" ];
139 executable = true;
140 destination = "/bin/my-file";
141}
142# Writes contents of files to /nix/store/<store path>
143concatText "my-file" [ file1 file2 ]
144
145# Writes contents of files to /nix/store/<store path>
146concatScript "my-file" [ file1 file2 ]
147```
148
149## `writeShellApplication` {#trivial-builder-writeShellApplication}
150
151This can be used to easily produce a shell script that has some dependencies (`runtimeInputs`). It automatically sets the `PATH` of the script to contain all of the listed inputs, sets some sanity shellopts (`errexit`, `nounset`, `pipefail`), and checks the resulting script with [`shellcheck`](https://github.com/koalaman/shellcheck).
152
153For example, look at the following code:
154
155```nix
156writeShellApplication {
157 name = "show-nixos-org";
158
159 runtimeInputs = [ curl w3m ];
160
161 text = ''
162 curl -s 'https://nixos.org' | w3m -dump -T text/html
163 '';
164}
165```
166
167Unlike with normal `writeShellScriptBin`, there is no need to manually write out `${curl}/bin/curl`, setting the PATH
168was handled by `writeShellApplication`. Moreover, the script is being checked with `shellcheck` for more strict
169validation.
170
171## `symlinkJoin` {#trivial-builder-symlinkJoin}
172
173This can be used to put many derivations into the same directory structure. It works by creating a new derivation and adding symlinks to each of the paths listed. It expects two arguments, `name`, and `paths`. `name` is the name used in the Nix store path for the created derivation. `paths` is a list of paths that will be symlinked. These paths can be to Nix store derivations or any other subdirectory contained within.
174Here is an example:
175```nix
176# adds symlinks of hello and stack to current build and prints "links added"
177symlinkJoin { name = "myexample"; paths = [ pkgs.hello pkgs.stack ]; postBuild = "echo links added"; }
178```
179This creates a derivation with a directory structure like the following:
180```
181/nix/store/sglsr5g079a5235hy29da3mq3hv8sjmm-myexample
182|-- bin
183| |-- hello -> /nix/store/qy93dp4a3rqyn2mz63fbxjg228hffwyw-hello-2.10/bin/hello
184| `-- stack -> /nix/store/6lzdpxshx78281vy056lbk553ijsdr44-stack-2.1.3.1/bin/stack
185`-- share
186 |-- bash-completion
187 | `-- completions
188 | `-- stack -> /nix/store/6lzdpxshx78281vy056lbk553ijsdr44-stack-2.1.3.1/share/bash-completion/completions/stack
189 |-- fish
190 | `-- vendor_completions.d
191 | `-- stack.fish -> /nix/store/6lzdpxshx78281vy056lbk553ijsdr44-stack-2.1.3.1/share/fish/vendor_completions.d/stack.fish
192...
193```
194
195## `writeReferencesToFile` {#trivial-builder-writeReferencesToFile}
196
197Writes the closure of transitive dependencies to a file.
198
199This produces the equivalent of `nix-store -q --requisites`.
200
201For example,
202
203```nix
204writeReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
205```
206
207produces an output path `/nix/store/<hash>-runtime-deps` containing
208
209```nix
210/nix/store/<hash>-hello-2.10
211/nix/store/<hash>-hi
212/nix/store/<hash>-libidn2-2.3.0
213/nix/store/<hash>-libunistring-0.9.10
214/nix/store/<hash>-glibc-2.32-40
215```
216
217You can see that this includes `hi`, the original input path,
218`hello`, which is a direct reference, but also
219the other paths that are indirectly required to run `hello`.
220
221## `writeDirectReferencesToFile` {#trivial-builder-writeDirectReferencesToFile}
222
223Writes the set of references to the output file, that is, their immediate dependencies.
224
225This produces the equivalent of `nix-store -q --references`.
226
227For example,
228
229```nix
230writeDirectReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
231```
232
233produces an output path `/nix/store/<hash>-runtime-references` containing
234
235```nix
236/nix/store/<hash>-hello-2.10
237```
238
239but none of `hello`'s dependencies because those are not referenced directly
240by `hi`'s output.