Skip to content

Commit

Permalink
darwin.stdenv: allow patchShebangs during the bootstrap
Browse files Browse the repository at this point in the history
This fixes pyicu (and any other package that uses `icu-config` instead
of the CMake or some other module to get the build flags).

What happened here is the bootstrap disables `patchShebangs` to avoid
propagating the bootstrap tools to the final stdenv (due to `sh` and
`bash` being on the `PATH` from the bootstrap tools). Because of that,
the `#!/bin/sh` line in `icu-config` was not updated, causing it to
invoke the system bash on Darwin. While that is undesirable in its own
right, when the system bash is invoked as `sh`, `echo -n` will print
`-n`, resulting in the breakage see in #241951 (comment).

The fix is to build bash earlier in the bootstrap while making sure it
is picked up over the one in the bootstrap tools. That allows
`patchShebangs` to be enabled during the bootstrap. Any package with
scripts that is included in the final stdenv should now have its
scripts’ shebang lines properly patched.
  • Loading branch information
reckenrode committed Jul 9, 2023
1 parent a61c7c5 commit 856ebe6
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions pkgs/stdenv/darwin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ let
stdenvNoCC = prevStage.ccWrapperStdenv;
};

bash = prevStage.bash or bootstrapTools;

thisStdenv = import ../generic {
name = "${name}-stdenv-darwin";

Expand All @@ -159,18 +161,19 @@ let

extraBuildInputs = [ prevStage.darwin.CF ];

preHook = ''
preHook = lib.optionalString (!isBuiltByNixpkgsCompiler bash) ''
# Don't patch #!/interpreter because it leads to retained
# dependencies on the bootstrapTools in the final stdenv.
dontPatchShebangs=1
'' + ''
${commonPreHook prevStage}
${extraPreHook}
'' + lib.optionalString (prevStage.darwin ? locale) ''
export PATH_LOCALE=${prevStage.darwin.locale}/share/locale
'';

shell = "${bootstrapTools}/bin/bash";
initialPath = [ bootstrapTools ];
shell = bash + "/bin/bash";
initialPath = [ bash bootstrapTools ];

fetchurlBoot = import ../../build-support/fetchurl {
inherit lib;
Expand Down Expand Up @@ -242,6 +245,8 @@ in
# stage should only access the stage that came before it.
ccWrapperStdenv = self.stdenv;

bash = bootstrapTools;

coreutils = bootstrapTools;
gnugrep = bootstrapTools;

Expand Down Expand Up @@ -445,7 +450,7 @@ in
# making sure both packages are present on x86_64-darwin and aarch64-darwin.
(prevStage:
# previous stage0 stdenv:
assert lib.all isFromBootstrapFiles (with prevStage; [ coreutils cpio gnugrep pbzx ]);
assert lib.all isFromBootstrapFiles (with prevStage; [ bash coreutils cpio gnugrep pbzx ]);

assert lib.all isFromBootstrapFiles (with prevStage.darwin; [
binutils-unwrapped cctools print-reexports rewrite-tbd sigtool system_cmds
Expand Down Expand Up @@ -727,7 +732,8 @@ in
'';
})

# This stage rebuilds Libsystem.
# This stage rebuilds Libsystem. It also rebuilds bash, which will be needed in later stages
# to use in patched shebangs (e.g., to make sure `icu-config` uses bash from nixpkgs).
(prevStage:
# previous stage-xclang stdenv:
assert lib.all isBuiltByBootstrapFilesCompiler (with prevStage; [
Expand Down Expand Up @@ -764,13 +770,19 @@ in

overrides = self: super: {
inherit (prevStage) ccWrapperStdenv
autoconf automake bash binutils-unwrapped bison brotli cmake cmakeMinimal coreutils
autoconf automake binutils-unwrapped bison brotli cmake cmakeMinimal coreutils
cpio curl cyrus_sasl db ed expat flex gettext gmp gnugrep groff icu libedit libffi
libiconv libidn2 libkrb5 libssh2 libtool libunistring libxml2 m4 ncurses nghttp2
ninja openbsm openldap openpam openssh openssl patchutils pbzx perl pkg-config
python3 python3Minimal scons serf sqlite subversion sysctl texinfo unzip which xz
zlib zstd;

# Bash must be linked against the system CoreFoundation instead of the open-source one.
# Otherwise, there will be a dependency cycle: bash -> CF -> icu -> bash (for icu^dev).
bash = super.bash.overrideAttrs (super: {
buildInputs = super.buildInputs ++ [ self.darwin.apple_sdk.frameworks.CoreFoundation ];
});

darwin = super.darwin.overrideScope (selfDarwin: superDarwin: {
inherit (prevStage.darwin)
CF binutils-unwrapped cctools configd darwin-stubs launchd libobjc libtapi locale
Expand Down Expand Up @@ -822,13 +834,15 @@ in
(prevStage:
# previous stage2-Libsystem stdenv:
assert lib.all isBuiltByBootstrapFilesCompiler (with prevStage; [
autoconf automake bash binutils-unwrapped bison brotli cmake cmakeMinimal coreutils
autoconf automake binutils-unwrapped bison brotli cmake cmakeMinimal coreutils
cpio curl cyrus_sasl db ed expat flex gettext gmp gnugrep groff icu libedit libidn2
libkrb5 libssh2 libtool libunistring m4 nghttp2 ninja openbsm openldap openpam openssh
openssl patchutils pbzx perl pkg-config.pkg-config python3 python3Minimal scons serf
sqlite subversion sysctl.provider texinfo unzip which xz zstd
]);

assert lib.all isBuiltByNixpkgsCompiler (with prevStage; [ bash ]);

assert lib.all isBuiltByBootstrapFilesCompiler (with prevStage; [
libffi libiconv libxml2 ncurses zlib zstd
]);
Expand Down Expand Up @@ -946,14 +960,14 @@ in
(prevStage:
# previous stage2-CF stdenv:
assert lib.all isBuiltByBootstrapFilesCompiler (with prevStage; [
autoconf automake bash bison brotli cmake cmakeMinimal coreutils cpio curl cyrus_sasl
autoconf automake bison brotli cmake cmakeMinimal coreutils cpio curl cyrus_sasl
db ed expat flex gettext gmp gnugrep groff libedit libidn2 libkrb5 libssh2 libtool
libunistring m4 ncurses nghttp2 ninja openbsm openldap openpam openssh openssl
patchutils pbzx perl pkg-config.pkg-config python3 python3Minimal scons serf sqlite
subversion sysctl.provider texinfo unzip which xz zstd
]);
assert lib.all isBuiltByNixpkgsCompiler (with prevStage; [
binutils-unwrapped icu libffi libiconv libxml2 zlib
bash binutils-unwrapped icu libffi libiconv libxml2 zlib
]);

assert lib.all isBuiltByBootstrapFilesCompiler (with prevStage.darwin; [
Expand Down Expand Up @@ -1025,15 +1039,15 @@ in
(prevStage:
# previous stage3 stdenv:
assert lib.all isBuiltByBootstrapFilesCompiler (with prevStage; [
autoconf automake bash bison brotli cmake cmakeMinimal coreutils cpio curl cyrus_sasl
autoconf automake bison brotli cmake cmakeMinimal coreutils cpio curl cyrus_sasl
db ed expat flex gettext gmp gnugrep groff libedit libidn2 libkrb5 libssh2 libtool
libunistring m4 nghttp2 ninja openbsm openldap openpam openssh openssl patchutils pbzx
perl pkg-config.pkg-config python3 python3Minimal scons serf sqlite subversion
sysctl.provider texinfo unzip which xz zstd
]);

assert lib.all isBuiltByNixpkgsCompiler (with prevStage; [
binutils-unwrapped icu libffi libiconv libxml2 zlib
bash binutils-unwrapped icu libffi libiconv libxml2 zlib
]);

assert lib.all isBuiltByBootstrapFilesCompiler (with prevStage.darwin; [
Expand All @@ -1060,7 +1074,7 @@ in

overrides = self: super: {
inherit (prevStage) ccWrapperStdenv
autoconf automake bison cmake cmakeMinimal cpio cyrus_sasl db expat flex groff
autoconf automake bash bison cmake cmakeMinimal cpio cyrus_sasl db expat flex groff
libedit libtool m4 ninja openldap openssh patchutils pbzx perl pkg-config python3
python3Minimal scons serf sqlite subversion sysctl texinfo unzip which

Expand Down

0 comments on commit 856ebe6

Please sign in to comment.