Skip to content

Commit

Permalink
incorporate the spirit of change proposed by @DanielSidhion at #27753…
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdonc committed Jan 13, 2024
1 parent 3263868 commit 25b2c3a
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions doc/build-helpers/trivial-build-helpers.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,29 @@ This sets [`allowSubstitutes` to `false`](https://nixos.org/nix/manual/#adv-attr

Nixpkgs provides the following functions for producing derivations which write text into the Nix store: `writeTextFile`, `writeText`, `writeTextDir`, `writeScript`, `writeScriptBin`, `writeShellScript`, and `writeShellScriptBin`.

`writeText`, `writeTextDir`, `writeScript`, and `writeScriptBin` are convenience functions over `writeTextFile`.
`writeText`, `writeTextDir`, `writeScript`, `writeScriptBin`, `writeShellScript`, and `writeShellScriptBin` are convenience functions over `writeTextFile`.

These are useful for creating files from Nix expressions, which may be scripts or non-executable text files.

The result of each of these functions will be a derivation. When you coerce the resulting derivation to text, it will evaluate to the *store path*. Importantly, it will not include the destination subpath produced by the particular function. So, for example, given the following expression:
Each of these functions will cause a derivation to be realized. When you coerce the result of each of these functions to a string, it will evaluate to the *store path* of this derivation.

```nix
:::: {.warning}
Some of these functions will put the resulting files within a directory inside the derivation output. If you need to refer to the resulting files somewhere else in Nix code, remember to append the path to the file For example:

```nix
my-file = writeTextFile {
name = "my-file";
text = ''
Contents of File
'';
destination = "/share/my-file";
}
```

If `my-file` is coerced to text, it will resolve to `/nix/store/<store path>`, like any other derivation. It will *not* evaluate to `/nix/store/<store path>/share/my-file`. So to use it elsewhere, as an example (in this case, within a shell script you're writing in a Nix expression via `writeShellScript`), you might do:
```nix
writeShellScript "evaluate-my-file.sh" ''
cat ${my-file}/share/my-file
'';
```

This will produce the desired result. However, the following will not, it will fail because the store path is a directory, and is not the `my-file` file.

```nix
writeShellScript "evaluate-my-file.sh" ''
cat ${my-file}
'';
```
::::

### `writeTextFile` {#trivial-builder-writeTextFile}

Expand Down Expand Up @@ -180,10 +171,6 @@ writeTextFile {
```
:::

::: {.note}
The commands `writeText`, `writeTextDir`, `writeScript`, `writeScriptBin`, `writeShellScript`, and `writeShellScriptBin` documented below are convenience functions that wrap `writeTextFile`.
:::

### `writeText` {#trivial-builder-writeText}

Writes a text file to the store
Expand Down

0 comments on commit 25b2c3a

Please sign in to comment.