Home Explore Blog Models CI



nixpkgs

7th chunk of `doc/build-helpers/trivial-build-helpers.chapter.md`
0f844e5991e107257da58a66008fc2911339f85e114c79020000000100000ae5
These functions concatenate `files` to the Nix store in a single file. This is useful for configuration files structured in lines of text. `concatTextFile` takes an attribute set and expects two arguments, `name` and `files`. `name` corresponds to the name used in the Nix store path. `files` will be the files to be concatenated. You can also set `executable` to true to make this file have the executable bit set.
`concatText` and`concatScript` are simple wrappers over `concatTextFile`.

Here are a few examples:
```nix
# Writes my-file to /nix/store/<store path>
concatTextFile
  {
    name = "my-file";
    files = [
      drv1
      "${drv2}/path/to/file"
    ];
  }
  # See also the `concatText` helper function below.

  # Writes executable my-file to /nix/store/<store path>/bin/my-file
  concatTextFile
  {
    name = "my-file";
    files = [
      drv1
      "${drv2}/path/to/file"
    ];
    executable = true;
    destination = "/bin/my-file";
  }
  # Writes contents of files to /nix/store/<store path>
  concatText
  "my-file"
  [
    file1
    file2
  ]

  # Writes contents of files to /nix/store/<store path>
  concatScript
  "my-file"
  [
    file1
    file2
  ]
```

## `writeShellApplication` {#trivial-builder-writeShellApplication}

`writeShellApplication` is similar to `writeShellScriptBin` and `writeScriptBin` but supports runtime dependencies with `runtimeInputs`.
Writes an executable shell script to `/nix/store/<store path>/bin/<name>` and checks its syntax with [`shellcheck`](https://github.com/koalaman/shellcheck) and the `bash`'s `-n` option.
Some basic Bash options are set by default (`errexit`, `nounset`, and `pipefail`), but can be overridden with `bashOptions`.

Extra arguments may be passed to `stdenv.mkDerivation` by setting `derivationArgs`; note that variables set in this manner will be set when the shell script is _built,_ not when it's run.
Runtime environment variables can be set with the `runtimeEnv` argument.

For example, the following shell application can refer to `curl` directly, rather than needing to write `${curl}/bin/curl`:

```nix
writeShellApplication {
  name = "show-nixos-org";

  runtimeInputs = [
    curl
    w3m
  ];

  text = ''
    curl -s 'https://nixos.org' | w3m -dump -T text/html
  '';
}
```

## `symlinkJoin` {#trivial-builder-symlinkJoin}

This can be used to put many derivations into the same directory structure. It works by creating a new derivation and adding symlinks to each of the paths listed. It expects two arguments, `name`, and `paths`. `name` (or alternatively `pname` and `version`) is the name used in the Nix store path for the created derivation. `paths` is a list of paths that will be symlinked. These paths can be to Nix store derivations or any other subdirectory contained within.

Title: Nixpkgs Trivial Builders: `concatText` Functions, `writeShellApplication`, and `symlinkJoin`
Summary
This document covers Nixpkgs helper functions for combining files and creating scripts. `concatTextFile`, `concatText`, and `concatScript` merge multiple files into a single Nix store file. `concatTextFile` is the most versatile, allowing customization of name, inputs, executability, and destination. `concatText` and `concatScript` are simpler wrappers; `concatScript` also makes the output executable. `writeShellApplication` creates executable shell scripts in a `bin` directory. It includes syntax checking, sets default Bash options, and notably, allows direct use of `runtimeInputs` binaries. It also supports `runtimeEnv` for runtime variables and `derivationArgs` for build-time configuration. Finally, `symlinkJoin` aggregates multiple derivations or paths into a unified directory structure via symlinks, requiring a `name` and `paths` list.