···47474848Many more commands wrap `writeTextFile` including `writeText`, `writeTextDir`, `writeScript`, and `writeScriptBin`. These are convenience functions over `writeTextFile`.
49495050+## `writeShellApplication` {#trivial-builder-writeShellApplication}
5151+5252+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).
5353+5454+For example, look at the following code:
5555+5656+```nix
5757+writeShellApplication {
5858+ name = "show-nixos-org";
5959+6060+ runtimeInputs = [ curl w3m ];
6161+6262+ text = ''
6363+ curl -s 'https://nixos.org' | w3m -dump -T text/html
6464+ '';
6565+}
6666+```
6767+6868+Unlike with normal `writeShellScriptBin`, there is no need to manually write out `${curl}/bin/curl`, setting the PATH
6969+was handled by `writeShellApplication`. Moreover, the script is being checked with `shellcheck` for more strict
7070+validation.
7171+5072## `symlinkJoin` {#trivial-builder-symlinkJoin}
51735274This 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.
+57-2
pkgs/build-support/trivial-builders.nix
···11-{ lib, stdenv, stdenvNoCC, lndir, runtimeShell }:
11+{ lib, stdenv, stdenvNoCC, lndir, runtimeShell, shellcheck }:
2233rec {
44···111111 , executable ? false # run chmod +x ?
112112 , destination ? "" # relative path appended to $out eg "/bin/foo"
113113 , checkPhase ? "" # syntax checks, e.g. for scripts
114114+ , meta ? { }
114115 }:
115116 runCommand name
116116- { inherit text executable checkPhase;
117117+ { inherit text executable checkPhase meta;
117118 passAsFile = [ "text" ];
118119 # Pointless to do this on a remote machine.
119120 preferLocalBuild = true;
···247248 checkPhase = ''
248249 ${stdenv.shell} -n $out/bin/${name}
249250 '';
251251+ };
252252+253253+ /*
254254+ * Similar to writeShellScriptBin and writeScriptBin.
255255+ * Writes an executable Shell script to /nix/store/<store path>/bin/<name> and
256256+ * checks its syntax with shellcheck and the shell's -n option.
257257+ * Automatically includes sane set of shellopts (errexit, nounset, pipefail)
258258+ * and handles creation of PATH based on runtimeInputs
259259+ *
260260+ * Note that the checkPhase uses stdenv.shell for the test run of the script,
261261+ * while the generated shebang uses runtimeShell. If, for whatever reason,
262262+ * those were to mismatch you might lose fidelity in the default checks.
263263+ *
264264+ * Example:
265265+ * # Writes my-file to /nix/store/<store path>/bin/my-file and makes executable.
266266+ * writeShellApplication {
267267+ * name = "my-file";
268268+ * runtimeInputs = [ curl w3m ];
269269+ * text = ''
270270+ * curl -s 'https://nixos.org' | w3m -dump -T text/html
271271+ * '';
272272+ * }
273273+ */
274274+ writeShellApplication =
275275+ { name
276276+ , text
277277+ , runtimeInputs ? [ ]
278278+ , checkPhase ? null
279279+ }:
280280+ writeTextFile {
281281+ inherit name;
282282+ executable = true;
283283+ destination = "/bin/${name}";
284284+ text = ''
285285+ #!${runtimeShell}
286286+ set -o errexit
287287+ set -o nounset
288288+ set -o pipefail
289289+290290+ export PATH="${lib.makeBinPath runtimeInputs}:$PATH"
291291+292292+ ${text}
293293+ '';
294294+295295+ checkPhase =
296296+ if checkPhase == null then ''
297297+ runHook preCheck
298298+ ${stdenv.shell} -n $out/bin/${name}
299299+ ${shellcheck}/bin/shellcheck $out/bin/${name}
300300+ runHook postCheck
301301+ ''
302302+ else checkPhase;
303303+304304+ meta.mainProgram = name;
250305 };
251306252307 # Create a C binary