diff --git a/doc/redirects.json b/doc/redirects.json index a2c90c9a4881d1..2c92ab6c93c621 100644 --- a/doc/redirects.json +++ b/doc/redirects.json @@ -812,6 +812,9 @@ "add-bin-to-path.sh": [ "index.html#add-bin-to-path.sh" ], + "writable-tmpdir-as-home.sh": [ + "index.html#writable-tmpdir-as-home.sh" + ], "bintools-wrapper": [ "index.html#bintools-wrapper" ], diff --git a/doc/stdenv/stdenv.chapter.md b/doc/stdenv/stdenv.chapter.md index 87d759b0e6901b..2e3d38fe424e68 100644 --- a/doc/stdenv/stdenv.chapter.md +++ b/doc/stdenv/stdenv.chapter.md @@ -1392,6 +1392,16 @@ in the `./bin/` location are accessible. It is particularly useful during testing, especially for packages that require their executables to be available in the `PATH`. +### `writable-tmpdir-as-home.sh` {#writable-tmpdir-as-home.sh} + +This setup hook ensures that the directory specified by the `HOME` environment +variable is writable. If it is not, the hook assigns `HOME` to a writable +temporary directory. This adjustment is necessary for certain packages that +require write access to the home directory during their check phases. + +By setting `HOME` to a temporary directory, this setup hook prevents failures in +packages that attempt to write to the home directory. + ### Bintools Wrapper and hook {#bintools-wrapper} The Bintools Wrapper wraps the binary utilities for a bunch of miscellaneous purposes. These are GNU Binutils when targeting Linux, and a mix of cctools and GNU binutils for Darwin. \[The “Bintools” name is supposed to be a compromise between “Binutils” and “cctools” not denoting any specific implementation.\] Specifically, the underlying bintools package, and a C standard library (glibc or Darwin’s libSystem, just for the dynamic loader) are all fed in, and dependency finding, hardening (see below), and purity checks for each are handled by the Bintools Wrapper. Packages typically depend on CC Wrapper, which in turn (at run time) depends on the Bintools Wrapper. diff --git a/pkgs/build-support/setup-hooks/writable-tmpdir-as-home.sh b/pkgs/build-support/setup-hooks/writable-tmpdir-as-home.sh new file mode 100644 index 00000000000000..4fcd874b241687 --- /dev/null +++ b/pkgs/build-support/setup-hooks/writable-tmpdir-as-home.sh @@ -0,0 +1,14 @@ +# shellcheck shell=bash +# This setup hook set the HOME environment variable to a temporary directory. + +export HOME + +writableTmpDirAsHome () { + if [[ ! -w "$HOME" ]]; then + HOME=$(mktemp -d) + export HOME + fi +} + +# shellcheck disable=SC2154 +addEnvHooks "$targetOffset" writableTmpDirAsHome diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index f1f651ce65d14c..3afcb3abecea46 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -866,6 +866,13 @@ with pkgs; name = "setup-debug-info-dirs-hook"; } ../build-support/setup-hooks/setup-debug-info-dirs.sh; + writableTmpDirAsHomeHook = callPackage ( + { makeSetupHook }: + makeSetupHook { + name = "writable-tmpdir-as-home-hook"; + } ../build-support/setup-hooks/writable-tmpdir-as-home.sh + ) { }; + useOldCXXAbi = makeSetupHook { name = "use-old-cxx-abi-hook"; } ../build-support/setup-hooks/use-old-cxx-abi.sh;