Home Explore Blog Models CI



nixpkgs

22th chunk of `doc/stdenv/stdenv.chapter.md`
2d6b5df0efe15c687af09c216569f2c73899889eb83e7f430000000100000fda
For example, `runHook postHook` would run the hook `postHook` and all of the values contained in the `postHooks` array, if it exists.

### `substitute` \<infile\> \<outfile\> \<subs\> {#fun-substitute}

Performs string substitution on the contents of \<infile\>, writing the result to \<outfile\>. The substitutions in \<subs\> are of the following form:

#### `--replace-fail` \<s1\> \<s2\> {#fun-substitute-replace-fail}

Replace every occurrence of the string \<s1\> by \<s2\>.
Will error if no change is made.

#### `--replace-warn` \<s1\> \<s2\> {#fun-substitute-replace-warn}

Replace every occurrence of the string \<s1\> by \<s2\>.
Will print a warning if no change is made.

#### `--replace-quiet` \<s1\> \<s2\> {#fun-substitute-replace-quiet}

Replace every occurrence of the string \<s1\> by \<s2\>.
Will do nothing if no change can be made.

#### `--subst-var` \<varName\> {#fun-substitute-subst-var}

Replace every occurrence of `@varName@` by the contents of the environment variable \<varName\>. This is useful for generating files from templates, using `@...@` in the template as placeholders.

#### `--subst-var-by` \<varName\> \<s\> {#fun-substitute-subst-var-by}

Replace every occurrence of `@varName@` by the string \<s\>.

Example:

```shell
substitute ./foo.in ./foo.out \
    --replace-fail /usr/bin/bar $bar/bin/bar \
    --replace-fail "a string containing spaces" "some other text" \
    --subst-var someVar
```

### `substituteInPlace` \<multiple files\> \<subs\> {#fun-substituteInPlace}

Like `substitute`, but performs the substitutions in place on the files passed.

### `substituteAll` \<infile\> \<outfile\> {#fun-substituteAll}

Replaces every occurrence of `@varName@`, where \<varName\> is any environment variable, in \<infile\>, writing the result to \<outfile\>. For instance, if \<infile\> has the contents

```bash
#! @bash@/bin/sh
PATH=@coreutils@/bin
echo @foo@
```

and the environment contains `bash=/nix/store/bmwp0q28cf21...-bash-3.2-p39` and `coreutils=/nix/store/68afga4khv0w...-coreutils-6.12`, but does not contain the variable `foo`, then the output will be

```bash
#! /nix/store/bmwp0q28cf21...-bash-3.2-p39/bin/sh
PATH=/nix/store/68afga4khv0w...-coreutils-6.12/bin
echo @foo@
```

That is, no substitution is performed for undefined variables.

Environment variables that start with an uppercase letter or an underscore are filtered out, to prevent global variables (like `HOME`) or private variables (like `__ETC_PROFILE_DONE`) from accidentally getting substituted. The variables also have to be valid bash "names", as defined in the bash manpage (alphanumeric or `_`, must not start with a number).

### `substituteAllInPlace` \<file\> {#fun-substituteAllInPlace}

Like `substituteAll`, but performs the substitutions in place on the file \<file\>.

### `stripHash` \<path\> {#fun-stripHash}

Strips the directory and hash part of a store path, outputting the name part to `stdout`. For example:

```bash
# prints coreutils-8.24
stripHash "/nix/store/9s9r019176g7cvn2nvcw41gsp862y6b4-coreutils-8.24"
```

If you wish to store the result in another variable, then the following idiom may be useful:

```bash
name="/nix/store/9s9r019176g7cvn2nvcw41gsp862y6b4-coreutils-8.24"
someVar=$(stripHash $name)
```

### `wrapProgram` \<executable\> \<makeWrapperArgs\> {#fun-wrapProgram}

Convenience function for `makeWrapper` that replaces `<executable>` with a wrapper that executes the original program. It takes all the same arguments as `makeWrapper`, except for `--inherit-argv0` (used by the `makeBinaryWrapper` implementation) and `--argv0` (used by both `makeWrapper` and `makeBinaryWrapper` wrapper implementations).

If you will apply it multiple times, it will overwrite the wrapper file and you will end up with double wrapping, which should be avoided.

### `prependToVar` \<variableName\> \<elements...\> {#fun-prependToVar}

Prepend elements to a variable.

Example:

```shellSession
$ configureFlags="--disable-static"
$ prependToVar configureFlags --disable-dependency-tracking --enable-foo

Title: Nix Build Utilities: String Substitution, Store Path Manipulation, Program Wrapping, and Variable Modification
Summary
This text details Nix build utilities: `substitute` and `substituteInPlace` perform string replacements in files. `substitute` writes to an output, while `substituteInPlace` modifies files directly. Options include `--replace-fail` (errors if no change), `--replace-warn` (warns), `--replace-quiet` (silent), `--subst-var` (replaces `@varName@` with an environment variable), and `--subst-var-by` (replaces with a specified string). `substituteAll` and `substituteAllInPlace` replace all `@varName@` patterns with environment variable values, filtering global/private variables; undefined variables are ignored. `stripHash` extracts the name from a Nix store path (e.g., 'coreutils-8.24'). `wrapProgram` is a `makeWrapper` convenience function for creating program wrappers, which should not be applied multiple times. `prependToVar` prepends elements to a shell variable.