Home Explore Blog Models CI



nixpkgs

doc/hooks/patch-rc-path-hooks.section.md
8b680de0a752172885ffb0e0ffa95ff8b5556e86b0a5578c00000003000008d4

# `patchRcPath` hooks {#sec-patchRcPathHooks}

These hooks provide shell-specific utilities (with the same name as the hook) to patch shell scripts meant to be sourced by software users.

The typical usage is to patch initialisation or [rc](https://unix.stackexchange.com/questions/3467/what-does-rc-in-bashrc-stand-for) scripts inside `$out/bin` or `$out/etc`.
Such scripts, when being sourced, would insert the binary locations of certain commands into `PATH`, modify other environment variables or run a series of start-up commands.
When shipped from the upstream, they sometimes use commands that might not be available in the environment they are getting sourced in.

The compatible shells for each hook are:

 - `patchRcPathBash`: [Bash](https://www.gnu.org/software/bash/), [ksh](http://www.kornshell.org/), [zsh](https://www.zsh.org/) and other shells supporting the Bash-like parameter expansions.
 - `patchRcPathCsh`: Csh scripts, such as those targeting [tcsh](https://www.tcsh.org/).
 - `patchRcPathFish`: [Fish](https://fishshell.com/) scripts.
 - `patchRcPathPosix`: POSIX-conformant shells supporting the limited parameter expansions specified by the POSIX standard. The current implementation uses the parameter expansion `${foo-}` only.

For each supported shell, it modifies the script with a `PATH` prefix that is later removed when the script ends.
It allows nested patching, which guarantees that a patched script may source another patched script.

Syntax to apply the utility to a script:

```sh
patchRcPath<shell> <file> <PATH-prefix>
```

Example usage:

Given a package `foo` containing an init script `this-foo.fish` that depends on `coreutils`, `man` and `which`,
patch the init script for users to source without having the above dependencies in their `PATH`:

```nix
{
  lib,
  stdenv,
  patchRcPathFish,
}:
stdenv.mkDerivation {

  # ...

  nativeBuildInputs = [ patchRcPathFish ];

  postFixup = ''
    patchRcPathFish $out/bin/this-foo.fish ${
      lib.makeBinPath [
        coreutils
        man
        which
      ]
    }
  '';
}
```

::: {.note}
`patchRcPathCsh` and `patchRcPathPosix` implementation depends on `sed` to do the string processing.
The others are in vanilla shell and have no third-party dependencies.
:::

Chunks
a5d490bb (1st chunk of `doc/hooks/patch-rc-path-hooks.section.md`)
Title: Shell Script Patching Utilities (patchRcPath Hooks)
Summary
The `patchRcPath` hooks are shell-specific utilities designed to modify initialization or rc scripts, typically found in `$out/bin` or `$out/etc`. Their primary purpose is to prefix the `PATH` within these scripts with necessary binary locations, ensuring that commands required by the script are available when it's sourced by a user, even if those dependencies aren't in the user's default `PATH`. This allows upstream scripts, which might rely on specific commands, to function correctly in various environments. The utilities support different shells, including `patchRcPathBash`, `patchRcPathCsh`, `patchRcPathFish`, and `patchRcPathPosix`, each tailored to its respective shell's syntax. They enable nested patching and provide a syntax `patchRcPath<shell> <file> <PATH-prefix>` for application, as demonstrated in an example patching a Fish script within a Nix derivation.