1 # Build Support {#sec-build-support}
3 ## `pkgs.substitute` {#pkgs-substitute}
5 `pkgs.substitute` is a wrapper around [the `substitute` Bash function](#fun-substitute) in the standard environment.
6 It replaces strings in `src` as specified by the `substitutions` argument.
9 :::{.example #ex-pkgs-substitute}
10 # Usage of `pkgs.substitute`
12 In a build script, the line:
15 substitute $infile $outfile --replace-fail @foo@ ${foopkg}/bin/foo
21 { substitute, foopkg }:
23 src = ./sourcefile.txt;
33 ## `pkgs.substituteAll` {#pkgs-substituteall}
35 `pkgs.substituteAll` substitutes all instances of `@varName@` (`@`s included) in file `src` with the value of the corresponding environment variable.
36 As this uses the [`substituteAll`] (#fun-substitute) function, its limitations regarding variable names that will or will not be replaced also apply here.
38 :::{.example #ex-pkgs-substituteAll}
39 # Usage of `pkgs.substituteAll`
41 If `say-goodbye.sh` contains the following:
47 @hello@/bin/hello --greeting @greeting@
50 the following derivation will make substitutions to `@bash@`, `@hello@`, and `@greeting@`:
59 src = ./say-goodbye.sh;
67 such that `$out` will result in something like the following:
70 #! /nix/store/s30jrpgav677fpc9yvkqsib70xfmx7xi-bash-5.2p26/bin/bash
73 /nix/store/566f5isbvw014h7knmzmxa5l6hshx43k-hello-2.12.1/bin/hello --greeting goodbye
77 ## `pkgs.substituteAllFiles` {#pkgs-substituteallfiles}
79 `pkgs.substituteAllFiles` replaces `@varName@` with the value of the environment variable `varName`.
80 It expects `src` to be a directory and requires a `files` argument that specifies which files will be subject to replacements; only these files will be placed in `$out`.
82 As it also uses the `substituteAll` function, it is subject to the same limitations on environment variables as discussed in [pkgs.substituteAll](#pkgs-substituteall).
84 :::{.example #ex-pkgs-substitute-all-files}
85 # Usage of `pkgs.substituteAllFiles`
87 If the current directory contains `{foo,bar,baz}.txt` and the following `default.nix`
90 { substituteAllFiles }:
101 in the resulting derivation, every instance of `@hello@` will be replaced with `there` in `$out/foo.txt` and` `$out/bar.txt`; `baz.txt` will not be processed nor will it appear in `$out`.