1 # Trivial builders {#chap-trivial-builders}
3 Nixpkgs 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.
5 ## `runCommand` {#trivial-builder-runCommand}
7 This takes three arguments, `name`, `env`, and `buildCommand`. `name` is just the name that Nix will append to the store path in the same way that `stdenv.mkDerivation` uses its `name` attribute. `env` is an attribute set specifying environment variables that will be set for this derivation. These attributes are then passed to the wrapped `stdenv.mkDerivation`. `buildCommand` specifies the commands that will be run to create this derivation. Note that you will need to create `$out` for Nix to register the command as successful.
9 An example of using `runCommand` is provided below.
12 (import <nixpkgs> {}).runCommand "my-example" {} ''
13 echo My example command is running
17 echo I can write data to the Nix store > $out/message
19 echo I can also run basic commands like:
32 ## `runCommandCC` {#trivial-builder-runCommandCC}
34 This 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.
36 ## `runCommandLocal` {#trivial-builder-runCommandLocal}
38 Variant 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.
41 This 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`.
44 ## `writeTextFile`, `writeText`, `writeTextDir`, `writeScript`, `writeScriptBin` {#trivial-builder-writeText}
46 These 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.
48 Many more commands wrap `writeTextFile` including `writeText`, `writeTextDir`, `writeScript`, and `writeScriptBin`. These are convenience functions over `writeTextFile`.
50 Here are a few examples:
52 # Writes my-file to /nix/store/<store path>
59 # See also the `writeText` helper function below.
61 # Writes executable my-file to /nix/store/<store path>/bin/my-file
68 destination = "/bin/my-file";
70 # Writes contents of file to /nix/store/<store path>
75 # Writes contents of file to /nix/store/<store path>/share/my-file
76 writeTextDir "share/my-file"
80 # Writes my-file to /nix/store/<store path> and makes executable
85 # Writes my-file to /nix/store/<store path>/bin/my-file and makes executable.
86 writeScriptBin "my-file"
90 # Writes my-file to /nix/store/<store path> and makes executable.
91 writeShellScript "my-file"
95 # Writes my-file to /nix/store/<store path>/bin/my-file and makes executable.
96 writeShellScriptBin "my-file"
103 ## `concatTextFile`, `concatText`, `concatScript` {#trivial-builder-concatText}
105 These 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.
106 `concatText` and`concatScript` are simple wrappers over `concatTextFile`.
108 Here are a few examples:
111 # Writes my-file to /nix/store/<store path>
114 files = [ drv1 "${drv2}/path/to/file" ];
116 # See also the `concatText` helper function below.
118 # Writes executable my-file to /nix/store/<store path>/bin/my-file
121 files = [ drv1 "${drv2}/path/to/file" ];
123 destination = "/bin/my-file";
125 # Writes contents of files to /nix/store/<store path>
126 concatText "my-file" [ file1 file2 ]
128 # Writes contents of files to /nix/store/<store path>
129 concatScript "my-file" [ file1 file2 ]
132 ## `writeShellApplication` {#trivial-builder-writeShellApplication}
134 This 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).
136 For example, look at the following code:
139 writeShellApplication {
140 name = "show-nixos-org";
142 runtimeInputs = [ curl w3m ];
145 curl -s 'https://nixos.org' | w3m -dump -T text/html
150 Unlike with normal `writeShellScriptBin`, there is no need to manually write out `${curl}/bin/curl`, setting the PATH
151 was handled by `writeShellApplication`. Moreover, the script is being checked with `shellcheck` for more strict
154 ## `symlinkJoin` {#trivial-builder-symlinkJoin}
156 This 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.
159 # adds symlinks of hello and stack to current build and prints "links added"
160 symlinkJoin { name = "myexample"; paths = [ pkgs.hello pkgs.stack ]; postBuild = "echo links added"; }
162 This creates a derivation with a directory structure like the following:
164 /nix/store/sglsr5g079a5235hy29da3mq3hv8sjmm-myexample
166 | |-- hello -> /nix/store/qy93dp4a3rqyn2mz63fbxjg228hffwyw-hello-2.10/bin/hello
167 | `-- stack -> /nix/store/6lzdpxshx78281vy056lbk553ijsdr44-stack-2.1.3.1/bin/stack
171 | `-- stack -> /nix/store/6lzdpxshx78281vy056lbk553ijsdr44-stack-2.1.3.1/share/bash-completion/completions/stack
173 | `-- vendor_completions.d
174 | `-- stack.fish -> /nix/store/6lzdpxshx78281vy056lbk553ijsdr44-stack-2.1.3.1/share/fish/vendor_completions.d/stack.fish
178 ## `writeReferencesToFile` {#trivial-builder-writeReferencesToFile}
180 Writes the closure of transitive dependencies to a file.
182 This produces the equivalent of `nix-store -q --requisites`.
187 writeReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
190 produces an output path `/nix/store/<hash>-runtime-deps` containing
193 /nix/store/<hash>-hello-2.10
195 /nix/store/<hash>-libidn2-2.3.0
196 /nix/store/<hash>-libunistring-0.9.10
197 /nix/store/<hash>-glibc-2.32-40
200 You can see that this includes `hi`, the original input path,
201 `hello`, which is a direct reference, but also
202 the other paths that are indirectly required to run `hello`.
204 ## `writeDirectReferencesToFile` {#trivial-builder-writeDirectReferencesToFile}
206 Writes the set of references to the output file, that is, their immediate dependencies.
208 This produces the equivalent of `nix-store -q --references`.
213 writeDirectReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
216 produces an output path `/nix/store/<hash>-runtime-references` containing
219 /nix/store/<hash>-hello-2.10
222 but none of `hello`'s dependencies because those are not referenced directly