1 2# `patchRcPath` hooks {#sec-patchRcPathHooks} 3 4These hooks provide shell-specific utilities (with the same name as the hook) to patch shell scripts meant to be sourced by software users. 5 6The 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`. 7Such 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. 8When shipped from the upstream, they sometimes use commands that might not be available in the environment they are getting sourced in. 9 10The compatible shells for each hook are: 11 12 - `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. 13 - `patchRcPathCsh`: Csh scripts, such as those targeting [tcsh](https://www.tcsh.org/). 14 - `patchRcPathFish`: [Fish](https://fishshell.com/) scripts. 15 - `patchRcPathPosix`: POSIX-conformant shells supporting the limited parameter expansions specified by the POSIX standard. Current implementation uses the parameter expansion `${foo-}` only. 16 17For each supported shell, it modifies the script with a `PATH` prefix that is later removed when the script ends. 18It allows nested patching, which guarantees that a patched script may source another patched script. 19 20Syntax to apply the utility to a script: 21 22```sh 23patchRcPath<shell> <file> <PATH-prefix> 24``` 25 26Example usage: 27 28Given a package `foo` containing an init script `this-foo.fish` that depends on `coreutils`, `man` and `which`, 29patch the init script for users to source without having the above dependencies in their `PATH`: 30 31```nix 32{ lib, stdenv, patchRcPathFish}: 33stdenv.mkDerivation { 34 35 # ... 36 37 nativeBuildInputs = [ 38 patchRcPathFish 39 ]; 40 41 postFixup = '' 42 patchRcPathFish $out/bin/this-foo.fish ${lib.makeBinPath [ coreutils man which ]} 43 ''; 44} 45``` 46 47::: {.note} 48`patchRcPathCsh` and `patchRcPathPosix` implementation depends on `sed` to do the string processing. 49The others are in vanilla shell and have no third-party dependencies. 50:::