Home Explore Blog CI



nixpkgs

1st chunk of `doc/packages/build-support.md`
779bd59488078318db8049b24694735dcb0faf97c5eca17e0000000100000957
# Build Support {#sec-build-support}

## `pkgs.substitute` {#pkgs-substitute}

`pkgs.substitute` is a wrapper around [the `substitute` Bash function](#fun-substitute) in the standard environment.
It replaces strings in `src` as specified by the `substitutions` argument.


:::{.example #ex-pkgs-substitute}
# Usage of `pkgs.substitute`

In a build script, the line:

```bash
substitute $infile $outfile --replace-fail @foo@ ${foopkg}/bin/foo
```

is equivalent to:

```nix
{ substitute, foopkg }:
substitute {
  src = ./sourcefile.txt;
  substitutions = [
    "--replace"
    "@foo@"
    "${foopkg}/bin/foo"
  ];
}
```
:::

## `pkgs.replaceVars` {#pkgs-replacevars}

`pkgs.replaceVars <src> <replacements>` replaces all instances of `@varName@` (`@`s included) in file `src` with the respective value in the attribute set `replacements`.

:::{.example #ex-pkgs-replace-vars}
# Usage of `pkgs.replaceVars`

If `say-goodbye.sh` contains the following:

```bash
#! @bash@/bin/bash

echo @unchanged@
@hello@/bin/hello --greeting @greeting@
```

the following derivation will make substitutions to `@bash@`, `@hello@`, and `@greeting@`:

```nix
{
  replaceVars,
  bash,
  hello,
}:
replaceVars ./say-goodbye.sh {
  inherit bash hello;
  greeting = "goodbye";
  unchanged = null;
}
```

such that `$out` will result in something like the following:

```
#! /nix/store/s30jrpgav677fpc9yvkqsib70xfmx7xi-bash-5.2p26/bin/bash

echo @unchanged@
/nix/store/566f5isbvw014h7knmzmxa5l6hshx43k-hello-2.12.1/bin/hello --greeting goodbye
```

Note that, in contrast to the old `substituteAll`, `unchanged = null` must explicitly be set.
Any unreferenced `@...@` pattern in the source file will throw an error.
:::

## `pkgs.replaceVarsWith` {#pkgs-replacevarswith}

`pkgs.replaceVarsWith` works the same way as [pkgs.replaceVars](#pkgs-replacevars), but additionally allows more options.

:::{.example #ex-pkgs-replace-vars-with}
# Usage of `pkgs.replaceVarsWith`

With the example file `say-goodbye.sh`, consider:

```nix
{ replaceVarsWith }:
replaceVarsWith {
  src = ./say-goodbye.sh;

  replacements = {
    inherit bash hello;
    greeting = "goodbye";
    unchanged = null;
  };

  name = "say-goodbye";
  dir = "bin";
  isExecutable = true;
  meta.mainProgram = "say-goodbye";
}
```

This will make the resulting file executable, put it in `bin/say-goodbye` and set `meta` attributes respectively.
:::

Title: Build Support Functions: `pkgs.substitute`, `pkgs.replaceVars`, and `pkgs.replaceVarsWith`
Summary
This section describes three Nixpkgs functions that assist with text substitution during the build process: `pkgs.substitute`, `pkgs.replaceVars`, and `pkgs.replaceVarsWith`. `pkgs.substitute` is a wrapper around the `substitute` Bash function. `pkgs.replaceVars` replaces all instances of `@varName@` in a file with values from an attribute set. `pkgs.replaceVarsWith` is similar to `pkgs.replaceVars` but offers more options like setting the file's name, directory, executable status, and metadata.