1 # Trivial build helpers {#chap-trivial-builders}
3 Nixpkgs provides a variety of wrapper functions that help build commonly useful derivations.
4 Like [`stdenv.mkDerivation`](#sec-using-stdenv), each of these build helpers creates a derivation, but the arguments passed are different (usually simpler) from those required by `stdenv.mkDerivation`.
7 ## `runCommandWith` {#trivial-builder-runCommandWith}
9 The function `runCommandWith` returns a derivation built using the specified command(s), in a specified environment.
11 It is the underlying base function of all [`runCommand*` variants].
12 The general behavior is controlled via a single attribute set passed
13 as the first argument, and allows specifying `stdenv` freely.
15 The following [`runCommand*` variants] exist: `runCommand`, `runCommandCC`, and `runCommandLocal`.
17 [`runCommand*` variants]: #trivial-builder-runCommand
19 ### Type {#trivial-builder-runCommandWith-Type}
24 stdenv? :: Derivation;
26 derivationArgs? :: { ... };
27 } -> String -> Derivation
30 ### Inputs {#trivial-builder-runCommandWith-Inputs}
33 : The derivation's name, which Nix will append to the store path; see [`mkDerivation`](#sec-using-stdenv).
36 : If set to `true` this forces the derivation to be built locally, not using [substitutes] nor remote builds.
37 This is intended for very cheap commands (<1s execution time) which can be sped up by avoiding the network round-trip(s).
38 Its effect is to set [`preferLocalBuild = true`][preferLocalBuild] and [`allowSubstitutes = false`][allowSubstitutes].
41 This prevents the use of [substituters][substituter], so only set `runLocal` (or use `runCommandLocal`) when certain the user will
42 always have a builder for the `system` of the derivation. This should be true for most trivial use cases
43 (e.g., just copying some files to a different location or adding symlinks) because there the `system`
44 is usually the same as `builtins.currentSystem`.
48 : The [standard environment](#chap-stdenv) to use, defaulting to `pkgs.stdenv`
50 `derivationArgs` (Attribute set)
51 : Additional arguments for [`mkDerivation`](#sec-using-stdenv).
53 `buildCommand` (String)
54 : Shell commands to run in the derivation builder.
57 You have to create a file or directory `$out` for Nix to be able to run the builder successfully.
60 [allowSubstitutes]: https://nixos.org/nix/manual/#adv-attr-allowSubstitutes
61 [preferLocalBuild]: https://nixos.org/nix/manual/#adv-attr-preferLocalBuild
62 [substituter]: https://nix.dev/manual/nix/latest/glossary#gloss-substituter
63 [substitutes]: https://nix.dev/manual/nix/2.23/glossary#gloss-substitute
65 ::: {.example #ex-runcommandwith}
66 # Invocation of `runCommandWith`
71 derivationArgs.nativeBuildInputs = [ cowsay ];
74 'runCommandWith' is a bit cumbersome,
75 so we have more ergonomic wrappers.
83 ## `runCommand` and `runCommandCC` {#trivial-builder-runCommand}
85 The function `runCommand` returns a derivation built using the specified command(s), in the `stdenvNoCC` environment.
87 `runCommandCC` is similar but uses the default compiler environment. To minimize dependencies, `runCommandCC`
88 should only be used when the build command needs a C compiler.
90 `runCommandLocal` is also similar to `runCommand`, but forces the derivation to be built locally.
91 See the note on [`runCommandWith`] about `runLocal`.
93 [`runCommandWith`]: #trivial-builder-runCommandWith
95 ### Type {#trivial-builder-runCommand-Type}
98 runCommand :: String -> AttrSet -> String -> Derivation
99 runCommandCC :: String -> AttrSet -> String -> Derivation
100 runCommandLocal :: String -> AttrSet -> String -> Derivation
103 ### Input {#trivial-builder-runCommand-Input}
105 While the type signature(s) differ from [`runCommandWith`], individual arguments with the same name will have the same type and meaning:
108 : The derivation's name
110 `derivationArgs` (Attribute set)
111 : Additional parameters passed to [`mkDerivation`]
113 `buildCommand` (String)
114 : The command(s) run to build the derivation.
117 ::: {.example #ex-runcommand-simple}
118 # Invocation of `runCommand`
121 runCommand "my-example" {} ''
122 echo My example command is running
126 echo I can write data to the Nix store > $out/message
128 echo I can also run basic commands like:
143 `runCommand name derivationArgs buildCommand` is equivalent to
146 inherit name derivationArgs;
151 Likewise, `runCommandCC name derivationArgs buildCommand` is equivalent to
154 inherit name derivationArgs;
160 ## Writing text files {#trivial-builder-text-writing}
162 Nixpkgs provides the following functions for producing derivations which write text files or executable scripts into the Nix store.
163 They are useful for creating files from Nix expression, and are all implemented as convenience wrappers around `writeTextFile`.
165 Each of these functions will cause a derivation to be produced.
166 When you coerce the result of each of these functions to a string with [string interpolation](https://nixos.org/manual/nix/stable/language/string-interpolation) or [`builtins.toString`](https://nixos.org/manual/nix/stable/language/builtins#builtins-toString), it will evaluate to the [store path](https://nixos.org/manual/nix/stable/store/store-path) of this derivation.
169 Some of these functions will put the resulting files within a directory inside the [derivation output](https://nixos.org/manual/nix/stable/language/derivations#attr-outputs).
170 If you need to refer to the resulting files somewhere else in a Nix expression, append their path to the derivation's store path.
172 For example, if the file destination is a directory:
176 my-file = writeTextFile {
181 destination = "/share/my-file";
186 Remember to append "/share/my-file" to the resulting store path when using it elsewhere:
189 writeShellScript "evaluate-my-file.sh" ''
190 cat ${my-file}/share/my-file
195 ### `makeDesktopItem` {#trivial-builder-makeDesktopItem}
197 Write an [XDG desktop file](https://specifications.freedesktop.org/desktop-entry-spec/1.4/) to the Nix store.
199 This function is usually used to add desktop items to a package through the `copyDesktopItems` hook.
201 `makeDesktopItem` adheres to version 1.4 of the specification.
203 #### Inputs {#trivial-builder-makeDesktopItem-inputs}
205 `makeDesktopItem` takes an attribute set that accepts most values from the [XDG specification](https://specifications.freedesktop.org/desktop-entry-spec/1.4/ar01s06.html).
207 All recognised keys from the specification are supported with the exception of the "Hidden" field. The keys are converted into camelCase format, but correspond 1:1 to their equivalent in the specification: `genericName`, `noDisplay`, `comment`, `icon`, `onlyShowIn`, `notShowIn`, `dbusActivatable`, `tryExec`, `exec`, `path`, `terminal`, `mimeTypes`, `categories`, `implements`, `keywords`, `startupNotify`, `startupWMClass`, `url`, `prefersNonDefaultGPU`.
209 The "Version" field is hardcoded to the version `makeDesktopItem` currently adheres to.
211 The following fields are either required, are of a different type than in the specification, carry specific default values, or are additional fields supported by `makeDesktopItem`:
215 : The name of the desktop file in the Nix store.
217 `type` (String; _optional_)
219 : Default value: `"Application"`
221 `desktopName` (String)
223 : Corresponds to the "Name" field of the specification.
225 `actions` (List of Attribute set; _optional_)
227 : A list of attribute sets {name, exec?, icon?}
229 `extraConfig` (Attribute set; _optional_)
231 : Additional key/value pairs to be added verbatim to the desktop file. Attributes need to be prefixed with 'X-'.
233 #### Examples {#trivial-builder-makeDesktopItem-examples}
235 ::: {.example #ex-makeDesktopItem}
236 # Usage 1 of `makeDesktopItem`
238 Write a desktop file `/nix/store/<store path>/my-program.desktop` to the Nix store.
244 desktopName = "My Program";
245 genericName = "Video Player";
247 comment = "Cool video player";
248 icon = "/path/to/icon";
249 onlyShowIn = [ "KDE" ];
250 dbusActivatable = true;
251 tryExec = "my-program";
252 exec = "my-program --someflag";
253 path = "/some/working/path";
257 exec = "my-program --new-window";
260 mimeTypes = [ "video/mp4" ];
261 categories = [ "Utility" ];
262 implements = [ "org.my-program" ];
263 keywords = [ "Video" "Player" ];
264 startupNotify = false;
265 startupWMClass = "MyProgram";
266 prefersNonDefaultGPU = false;
267 extraConfig.X-SomeExtension = "somevalue";
273 ::: {.example #ex2-makeDesktopItem}
274 # Usage 2 of `makeDesktopItem`
276 Override the `hello` package to add a desktop item.
283 hello.overrideAttrs {
284 nativeBuildInputs = [ copyDesktopItems ];
286 desktopItems = [(makeDesktopItem {
288 desktopName = "Hello";
296 ### `writeTextFile` {#trivial-builder-writeTextFile}
298 Write a text file to the Nix store.
300 `writeTextFile` takes an attribute set with the following possible attributes:
304 : Corresponds to the name used in the Nix store path identifier.
308 : The contents of the file.
310 `executable` (Bool, _optional_)
312 : Make this file have the executable bit set.
316 `destination` (String, _optional_)
318 : A subpath under the derivation's output path into which to put the file.
319 Subdirectories are created automatically when the derivation is realised.
321 By default, the store path itself will be a file containing the text contents.
325 `checkPhase` (String, _optional_)
327 : Commands to run after generating the file.
331 `meta` (Attribute set, _optional_)
333 : Additional metadata for the derivation.
337 `allowSubstitutes` (Bool, _optional_)
339 : Whether to allow substituting from a binary cache.
340 Passed through to [`allowSubstitutes`](https://nixos.org/manual/nix/stable/language/advanced-attributes#adv-attr-allowSubstitutes) of the underlying call to `builtins.derivation`.
342 It defaults to `false`, as running the derivation's simple `builder` executable locally is assumed to be faster than network operations.
343 Set it to true if the `checkPhase` step is expensive.
347 `preferLocalBuild` (Bool, _optional_)
349 : Whether to prefer building locally, even if faster [remote build machines](https://nixos.org/manual/nix/stable/command-ref/conf-file#conf-substituters) are available.
351 Passed through to [`preferLocalBuild`](https://nixos.org/manual/nix/stable/language/advanced-attributes#adv-attr-preferLocalBuild) of the underlying call to `builtins.derivation`.
353 It defaults to `true` for the same reason `allowSubstitutes` defaults to `false`.
357 `derivationArgs` (Attribute set, _optional_)
359 : Extra arguments to pass to the underlying call to `stdenv.mkDerivation`.
363 The resulting store path will include some variation of the name, and it will be a file unless `destination` is used, in which case it will be a directory.
365 ::: {.example #ex-writeTextFile}
366 # Usage 1 of `writeTextFile`
368 Write `my-file` to `/nix/store/<store path>/some/subpath/my-cool-script`, making it executable.
369 Also run a check on the resulting file in a `checkPhase`, and supply values for the less-used options.
373 name = "my-cool-script";
376 echo "This is my cool script!"
379 destination = "/some/subpath/my-cool-script";
381 ${pkgs.shellcheck}/bin/shellcheck $out/some/subpath/my-cool-script
384 license = pkgs.lib.licenses.cc0;
386 allowSubstitutes = true;
387 preferLocalBuild = false;
392 ::: {.example #ex2-writeTextFile}
393 # Usage 2 of `writeTextFile`
395 Write the string `Contents of File` to `/nix/store/<store path>`.
396 See also the [](#trivial-builder-writeText) helper function.
408 ::: {.example #ex3-writeTextFile}
409 # Usage 3 of `writeTextFile`
411 Write an executable script `my-script` to `/nix/store/<store path>/bin/my-script`.
412 See also the [](#trivial-builder-writeScriptBin) helper function.
421 destination = "/bin/my-script";
426 ### `writeText` {#trivial-builder-writeText}
428 Write a text file to the Nix store
430 `writeText` takes the following arguments:
435 : The name used in the Nix store path.
439 : The contents of the file.
441 The store path will include the name, and it will be a file.
443 ::: {.example #ex-writeText}
444 # Usage of `writeText`
446 Write the string `Contents of File` to `/nix/store/<store path>`:
456 This is equivalent to:
467 ### `writeTextDir` {#trivial-builder-writeTextDir}
469 Write a text file within a subdirectory of the Nix store.
471 `writeTextDir` takes the following arguments:
475 : The destination within the Nix store path under which to create the file.
479 : The contents of the file.
481 The store path will be a directory.
483 ::: {.example #ex-writeTextDir}
484 # Usage of `writeTextDir`
486 Write the string `Contents of File` to `/nix/store/<store path>/share/my-file`:
489 writeTextDir "share/my-file"
496 This is equivalent to:
504 destination = "/share/my-file";
508 ### `writeScript` {#trivial-builder-writeScript}
510 Write an executable script file to the Nix store.
512 `writeScript` takes the following arguments:
516 : The name used in the Nix store path.
520 : The contents of the file.
522 The created file is marked as executable.
523 The store path will include the name, and it will be a file.
525 ::: {.example #ex-writeScript}
526 # Usage of `writeScript`
528 Write the string `Contents of File` to `/nix/store/<store path>` and make the file executable.
531 writeScript "my-file"
537 This is equivalent to:
550 ### `writeScriptBin` {#trivial-builder-writeScriptBin}
552 Write a script within a `bin` subdirectory of a directory in the Nix store.
553 This is for consistency with the convention of software packages placing executables under `bin`.
555 `writeScriptBin` takes the following arguments:
559 : The name used in the Nix store path and within the file created under the store path.
563 : The contents of the file.
565 The created file is marked as executable.
566 The file's contents will be put into `/nix/store/<store path>/bin/<name>`.
567 The store path will include the name, and it will be a directory.
569 ::: {.example #ex-writeScriptBin}
570 # Usage of `writeScriptBin`
573 writeScriptBin "my-script"
580 This is equivalent to:
589 destination = "/bin/my-script";
593 ### `writeShellScript` {#trivial-builder-writeShellScript}
595 Write a Bash script to the store.
597 `writeShellScript` takes the following arguments:
601 : The name used in the Nix store path.
605 : The contents of the file.
607 The created file is marked as executable.
608 The store path will include the name, and it will be a file.
610 This function is almost exactly like [](#trivial-builder-writeScript), except that it prepends to the file a [shebang](https://en.wikipedia.org/wiki/Shebang_%28Unix%29) line that points to the version of Bash used in Nixpkgs.
611 <!-- this cannot be changed in practice, so there is no point pretending it's somehow generic -->
613 ::: {.example #ex-writeShellScript}
614 # Usage of `writeShellScript`
617 writeShellScript "my-script"
624 This is equivalent to:
630 #! ${pkgs.runtimeShell}
637 ### `writeShellScriptBin` {#trivial-builder-writeShellScriptBin}
639 Write a Bash script to a "bin" subdirectory of a directory in the Nix store.
641 `writeShellScriptBin` takes the following arguments:
645 : The name used in the Nix store path and within the file generated under the store path.
649 : The contents of the file.
651 The file's contents will be put into `/nix/store/<store path>/bin/<name>`.
652 The store path will include the the name, and it will be a directory.
654 This function is a combination of [](#trivial-builder-writeShellScript) and [](#trivial-builder-writeScriptBin).
656 ::: {.example #ex-writeShellScriptBin}
657 # Usage of `writeShellScriptBin`
660 writeShellScriptBin "my-script"
667 This is equivalent to:
673 #! ${pkgs.runtimeShell}
677 destination = "/bin/my-script";
681 ## `concatTextFile`, `concatText`, `concatScript` {#trivial-builder-concatText}
683 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.
684 `concatText` and`concatScript` are simple wrappers over `concatTextFile`.
686 Here are a few examples:
689 # Writes my-file to /nix/store/<store path>
692 files = [ drv1 "${drv2}/path/to/file" ];
694 # See also the `concatText` helper function below.
696 # Writes executable my-file to /nix/store/<store path>/bin/my-file
699 files = [ drv1 "${drv2}/path/to/file" ];
701 destination = "/bin/my-file";
703 # Writes contents of files to /nix/store/<store path>
704 concatText "my-file" [ file1 file2 ]
706 # Writes contents of files to /nix/store/<store path>
707 concatScript "my-file" [ file1 file2 ]
710 ## `writeShellApplication` {#trivial-builder-writeShellApplication}
712 `writeShellApplication` is similar to `writeShellScriptBin` and `writeScriptBin` but supports runtime dependencies with `runtimeInputs`.
713 Writes an executable shell script to `/nix/store/<store path>/bin/<name>` and checks its syntax with [`shellcheck`](https://github.com/koalaman/shellcheck) and the `bash`'s `-n` option.
714 Some basic Bash options are set by default (`errexit`, `nounset`, and `pipefail`), but can be overridden with `bashOptions`.
716 Extra arguments may be passed to `stdenv.mkDerivation` by setting `derivationArgs`; note that variables set in this manner will be set when the shell script is _built,_ not when it's run.
717 Runtime environment variables can be set with the `runtimeEnv` argument.
719 For example, the following shell application can refer to `curl` directly, rather than needing to write `${curl}/bin/curl`:
722 writeShellApplication {
723 name = "show-nixos-org";
725 runtimeInputs = [ curl w3m ];
728 curl -s 'https://nixos.org' | w3m -dump -T text/html
733 ## `symlinkJoin` {#trivial-builder-symlinkJoin}
735 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.
738 # adds symlinks of hello and stack to current build and prints "links added"
739 symlinkJoin { name = "myexample"; paths = [ pkgs.hello pkgs.stack ]; postBuild = "echo links added"; }
741 This creates a derivation with a directory structure like the following:
743 /nix/store/sglsr5g079a5235hy29da3mq3hv8sjmm-myexample
745 | |-- hello -> /nix/store/qy93dp4a3rqyn2mz63fbxjg228hffwyw-hello-2.10/bin/hello
746 | `-- stack -> /nix/store/6lzdpxshx78281vy056lbk553ijsdr44-stack-2.1.3.1/bin/stack
750 | `-- stack -> /nix/store/6lzdpxshx78281vy056lbk553ijsdr44-stack-2.1.3.1/share/bash-completion/completions/stack
752 | `-- vendor_completions.d
753 | `-- stack.fish -> /nix/store/6lzdpxshx78281vy056lbk553ijsdr44-stack-2.1.3.1/share/fish/vendor_completions.d/stack.fish
757 ## `writeReferencesToFile` {#trivial-builder-writeReferencesToFile}
759 Deprecated. Use [`writeClosure`](#trivial-builder-writeClosure) instead.
761 ## `writeClosure` {#trivial-builder-writeClosure}
763 Given a list of [store paths](https://nixos.org/manual/nix/stable/glossary#gloss-store-path) (or string-like expressions coercible to store paths), write their collective [closure](https://nixos.org/manual/nix/stable/glossary#gloss-closure) to a text file.
765 The result is equivalent to the output of `nix-store -q --requisites`.
770 writeClosure [ (writeScriptBin "hi" ''${hello}/bin/hello'') ]
773 produces an output path `/nix/store/<hash>-runtime-deps` containing
776 /nix/store/<hash>-hello-2.10
778 /nix/store/<hash>-libidn2-2.3.0
779 /nix/store/<hash>-libunistring-0.9.10
780 /nix/store/<hash>-glibc-2.32-40
783 You can see that this includes `hi`, the original input path,
784 `hello`, which is a direct reference, but also
785 the other paths that are indirectly required to run `hello`.
787 ## `writeDirectReferencesToFile` {#trivial-builder-writeDirectReferencesToFile}
789 Writes the set of references to the output file, that is, their immediate dependencies.
791 This produces the equivalent of `nix-store -q --references`.
796 writeDirectReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
799 produces an output path `/nix/store/<hash>-runtime-references` containing
802 /nix/store/<hash>-hello-2.10
805 but none of `hello`'s dependencies because those are not referenced directly