From 87521c59b636ae9073c58bd549b4cdc334a3dc28 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Wed, 18 Dec 2024 17:55:54 +0100 Subject: [PATCH 1/2] build-support: add `addBinToPathHook` hook --- doc/redirects.json | 3 +++ doc/stdenv/stdenv.chapter.md | 14 ++++++++++++++ pkgs/build-support/setup-hooks/add-bin-to-path.sh | 15 +++++++++++++++ pkgs/top-level/all-packages.nix | 7 +++++++ 4 files changed, 39 insertions(+) create mode 100644 pkgs/build-support/setup-hooks/add-bin-to-path.sh diff --git a/doc/redirects.json b/doc/redirects.json index 4fada4e6cf2f5..5bf6394e422b3 100644 --- a/doc/redirects.json +++ b/doc/redirects.json @@ -827,6 +827,9 @@ "set-source-date-epoch-to-latest.sh": [ "index.html#set-source-date-epoch-to-latest.sh" ], + "add-bin-to-path.sh": [ + "index.html#add-bin-to-path.sh" + ], "bintools-wrapper": [ "index.html#bintools-wrapper" ], diff --git a/doc/stdenv/stdenv.chapter.md b/doc/stdenv/stdenv.chapter.md index 853b56f3c510f..bc32c7ec404fb 100644 --- a/doc/stdenv/stdenv.chapter.md +++ b/doc/stdenv/stdenv.chapter.md @@ -1375,6 +1375,20 @@ This hook only runs when compiling for Linux. This sets `SOURCE_DATE_EPOCH` to the modification time of the most recent file. +### `add-bin-to-path.sh` {#add-bin-to-path.sh} + +This setup hook checks if the `bin/` directory exists in the `$out` output path +and, if so, adds it to the `PATH` environment variable. This ensures that +executables located in `$out/bin` are accessible. + +This hook is particularly useful during testing, as it allows packages to locate their executables without requiring manual modifications to the `PATH`. + +**Note**: This hook is specifically designed for the `$out/bin` directory only +and does not handle and support other paths like `$sourceRoot/bin`. It may not +work as intended in cases with multiple outputs or when binaries are located in +directories like `sbin/`. These caveats should be considered when using this +hook, as they might introduce unexpected behavior in some specific cases. + ### 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/add-bin-to-path.sh b/pkgs/build-support/setup-hooks/add-bin-to-path.sh new file mode 100644 index 0000000000000..308c2706fd059 --- /dev/null +++ b/pkgs/build-support/setup-hooks/add-bin-to-path.sh @@ -0,0 +1,15 @@ +# shellcheck shell=bash +# This setup hook add $out/bin to the PATH environment variable. + +export PATH + +addBinToPath () { + # shellcheck disable=SC2154 + if [ -d "$out/bin" ]; then + PATH="$out/bin:$PATH" + export PATH + fi +} + +# shellcheck disable=SC2154 +addEnvHooks "$targetOffset" addBinToPath diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index e4c00c5bcb56b..8e2bd6b783705 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -163,6 +163,13 @@ with pkgs; __flattenIncludeHackHook = callPackage ../build-support/setup-hooks/flatten-include-hack { }; + addBinToPathHook = callPackage ( + { makeSetupHook }: + makeSetupHook { + name = "add-bin-to-path-hook"; + } ../build-support/setup-hooks/add-bin-to-path.sh + ) { }; + autoreconfHook = callPackage ( { makeSetupHook, autoconf, automake, gettext, libtool }: makeSetupHook { From f8110737aeedf8ba92ea46a0b1d2d298843d7c06 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Wed, 18 Dec 2024 18:02:37 +0100 Subject: [PATCH 2/2] build-support: add `writableTmpDirAsHomeHook` hook --- doc/redirects.json | 3 +++ doc/stdenv/stdenv.chapter.md | 11 +++++++++++ .../setup-hooks/writable-tmpdir-as-home.sh | 15 +++++++++++++++ pkgs/top-level/all-packages.nix | 7 +++++++ 4 files changed, 36 insertions(+) create mode 100644 pkgs/build-support/setup-hooks/writable-tmpdir-as-home.sh diff --git a/doc/redirects.json b/doc/redirects.json index 5bf6394e422b3..5065007b0ae94 100644 --- a/doc/redirects.json +++ b/doc/redirects.json @@ -830,6 +830,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 bc32c7ec404fb..d9264b6e7052d 100644 --- a/doc/stdenv/stdenv.chapter.md +++ b/doc/stdenv/stdenv.chapter.md @@ -1389,6 +1389,17 @@ work as intended in cases with multiple outputs or when binaries are located in directories like `sbin/`. These caveats should be considered when using this hook, as they might introduce unexpected behavior in some specific cases. +### `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 +directory (in `.home` in `$NIX_BUILD_TOP`). This adjustment is necessary for +certain packages that require write access to a home directory. This hook can +be added to any phase. + +By setting `HOME` to a writable 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 0000000000000..f63f3499e7369 --- /dev/null +++ b/pkgs/build-support/setup-hooks/writable-tmpdir-as-home.sh @@ -0,0 +1,15 @@ +# shellcheck shell=bash +# This setup hook set the HOME environment variable to a writable directory. + +export HOME + +writableTmpDirAsHome () { + if [ ! -w "$HOME" ]; then + HOME="$NIX_BUILD_TOP/.home" + mkdir -p "$HOME" + 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 8e2bd6b783705..a78af701c1ee7 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -868,6 +868,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;