Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build-support: add new hooks #370869

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/redirects.json
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,12 @@
"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"
],
"writable-tmpdir-as-home.sh": [
"index.html#writable-tmpdir-as-home.sh"
],
"bintools-wrapper": [
"index.html#bintools-wrapper"
],
Expand Down
25 changes: 25 additions & 0 deletions doc/stdenv/stdenv.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,31 @@ 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.

### `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.
Expand Down
15 changes: 15 additions & 0 deletions pkgs/build-support/setup-hooks/add-bin-to-path.sh
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we log something if the directory doesn't exist?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It hasn't been suggested during the month this PR has been opened, so no.
That said, while implementing that hook this morning, I had an issue because of that if condition. I removed it in 62d4ca6

PATH="$out/bin:$PATH"
export PATH
fi
}

# shellcheck disable=SC2154
addEnvHooks "$targetOffset" addBinToPath
15 changes: 15 additions & 0 deletions pkgs/build-support/setup-hooks/writable-tmpdir-as-home.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# shellcheck shell=bash
# This setup hook set the HOME environment variable to a writable directory.

export HOME

writableTmpDirAsHome () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we name the hook "writable" shouldn't it also call chmod +w or so to make sure it is writable? Right now with the wrong umask it isn't

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't needed so far, but it could be done in a future PR.

if [ ! -w "$HOME" ]; then
HOME="$NIX_BUILD_TOP/.home"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that a hidden directory and not just home?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been suggested to name it as such, I don't really have any preference.

mkdir -p "$HOME"
export HOME
fi
}

# shellcheck disable=SC2154
addEnvHooks "$targetOffset" writableTmpDirAsHome
14 changes: 14 additions & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -861,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;
Expand Down
Loading