Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: improve documentation for writeText* functions #277171

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions doc/build-helpers/trivial-build-helpers.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,57 @@ This sets [`allowSubstitutes` to `false`](https://nixos.org/nix/manual/#adv-attr

## `writeTextFile`, `writeText`, `writeTextDir`, `writeScript`, `writeScriptBin` {#trivial-builder-writeText}

These functions write `text` to the Nix store. This is useful for creating scripts from Nix expressions. `writeTextFile` takes an attribute set and expects two arguments, `name` and `text`. `name` corresponds to the name used in the Nix store path. `text` will be the contents of the file. You can also set `executable` to true to make this file have the executable bit set.
These functions create derivations that write text to a file in the Nix store, which is useful for creating scripts from Nix expressions.

Many more commands wrap `writeTextFile` including `writeText`, `writeTextDir`, `writeScript`, and `writeScriptBin`. These are convenience functions over `writeTextFile`.
`writeTextFile` takes an attribute set of the form:

```nix
{
# The name of the resulting derivation
name = "my-cool-script";

# The contents of the file
text = ''
#!/bin/sh
echo "This is my cool script!"
'';

# (Optional) Whether the resulting file should have the execute permission.
# Defaults to false.
executable = true;

# (Optional) Path of the resulting file relative to the derivation output.
asymmetric marked this conversation as resolved.
Show resolved Hide resolved
# Defaults to "", which will write the file directly to the derivation's output path.
# Any parent directories will be created automatically.
destination = "some/subpath/my-cool-script";

# (Optional) Commands to run after generating the file, e.g. lints.
# Defaults to "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Defaults to "";
# Defaults to "".

checkPhase = ''
${pkgs.shellcheck}/bin/shellcheck $out/some/subpath/my-cool-script
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use destination here to avoid duplication?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to test it if you do.

'';

# (Optional) Additional metadata for the derivation.
# Defaults to {}.
meta = {
license = pkgs.lib.licenses.cc0;
};

# (Optional) Whether to allow substituting from a binary cache.
# Defaults to false, as the operation is assumed to be faster performed locally.
# You may want to set this to true if the checking step is expensive.
allowSubstitutes = true;

# (Optional) Whether to prefer building locally, even if faster remote builders are available.
# Defaults to true for similar reasons.
preferLocalBuild = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does this take precedence on the above, so that if e.g. allowSubstitutes = true && preferLocalBuild = true, the build is kept local?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should refer to the Nix manual and not copy the explanation.
It may be subject to change and documenting it here would suggest that it's unique.

}
```

Note that the output of `writeText` is a derivation, which will coerce to the store path of its output,
which will not match the path of the actual file if `destination` is set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example could be nice here IMO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Many more functions wrap `writeTextFile`, including `writeText`, `writeTextDir`, `writeScript`, and `writeScriptBin`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is already like this in the manual, but instead of including, could we list them all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no! This documentation has been duplicated across

  • doc/build-helpers/trivial-build-helpers.chapter.md, and
  • pkgs/build-support/trivial-builders/default.nix

We should either use nixdoc (like lib/), or replace all the doc comments by # See doc/build-helpers/trivial-build-helpers.chapter.md.

There's no point doing anything without addressing this duplication first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no point doing anything without addressing this duplication first.

Maybe I should tone that down, but it is a very very serious problem nonetheless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As much as I don't want to throw this over the fence, I'm not sure I have it in me right now to handle a large scale cleanup like this. If anyone's interested, feel free to take over.


Here are a few examples:
```nix
Expand Down