···11+###############################################################################
22+# Bazel now uses Bzlmod by default to manage external dependencies.
33+# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
44+#
55+# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
66+###############################################################################
77+
···11+{ stdenv
22+ # nix tooling and utilities
33+, callPackage
44+, lib
55+, fetchurl
66+, makeWrapper
77+, writeTextFile
88+, substituteAll
99+, writeShellApplication
1010+, makeBinaryWrapper
1111+ # this package (through the fixpoint glass)
1212+, bazel_self
1313+ # native build inputs
1414+, runtimeShell
1515+, zip
1616+, unzip
1717+, bash
1818+, coreutils
1919+, which
2020+, gawk
2121+, gnused
2222+, gnutar
2323+, gnugrep
2424+, gzip
2525+, findutils
2626+, diffutils
2727+, gnupatch
2828+, file
2929+, installShellFiles
3030+, lndir
3131+, python3
3232+ # Apple dependencies
3333+, cctools
3434+, libcxx
3535+, sigtool
3636+, CoreFoundation
3737+, CoreServices
3838+, Foundation
3939+, IOKit
4040+ # Allow to independently override the jdks used to build and run respectively
4141+, buildJdk
4242+, runJdk
4343+ # Always assume all markers valid (this is needed because we remove markers; they are non-deterministic).
4444+ # Also, don't clean up environment variables (so that NIX_ environment variables are passed to compilers).
4545+, enableNixHacks ? false
4646+}:
4747+4848+let
4949+ version = "7.0.0";
5050+ sourceRoot = ".";
5151+5252+ src = fetchurl {
5353+ url = "https://github.com/bazelbuild/bazel/releases/download/${version}/bazel-${version}-dist.zip";
5454+ hash = "sha256-R35U9jdAAfQ5qUcbod6deCTa8SnblVEISezF4ZzogXA=";
5555+ };
5656+5757+ # Use builtins.fetchurl to avoid IFD, in particular on hydra
5858+ #lockfile = builtins.fetchurl {
5959+ # url = "https://raw.githubusercontent.com/bazelbuild/bazel/release-${version}/MODULE.bazel.lock";
6060+ # sha256 = "sha256-5xPpCeWVKVp1s4RVce/GoW2+fH8vniz5G1MNI4uezpc=";
6161+ #};
6262+ # Use a local copy of the above lockfile to make ofborg happy.
6363+ lockfile = ./MODULE.bazel.lock;
6464+6565+ # Two-in-one format
6666+ distDir = repoCache;
6767+ repoCache = callPackage ./bazel-repository-cache.nix {
6868+ inherit lockfile;
6969+7070+ # We use the release tarball that already has everything bundled so we
7171+ # should not need any extra external deps. But our nonprebuilt java
7272+ # toolchains hack needs just one non bundled dep.
7373+ requiredDepNamePredicate = name:
7474+ null != builtins.match "rules_java~.*~toolchains~remote_java_tools" name;
7575+ };
7676+7777+ defaultShellUtils =
7878+ # Keep this list conservative. For more exotic tools, prefer to use
7979+ # @rules_nixpkgs to pull in tools from the nix repository. Example:
8080+ #
8181+ # WORKSPACE:
8282+ #
8383+ # nixpkgs_git_repository(
8484+ # name = "nixpkgs",
8585+ # revision = "def5124ec8367efdba95a99523dd06d918cb0ae8",
8686+ # )
8787+ #
8888+ # # This defines an external Bazel workspace.
8989+ # nixpkgs_package(
9090+ # name = "bison",
9191+ # repositories = { "nixpkgs": "@nixpkgs//:default.nix" },
9292+ # )
9393+ #
9494+ # some/BUILD.bazel:
9595+ #
9696+ # genrule(
9797+ # ...
9898+ # cmd = "$(location @bison//:bin/bison) -other -args",
9999+ # tools = [
100100+ # ...
101101+ # "@bison//:bin/bison",
102102+ # ],
103103+ # )
104104+ [
105105+ bash
106106+ coreutils
107107+ diffutils
108108+ file
109109+ findutils
110110+ gawk
111111+ gnugrep
112112+ gnupatch
113113+ gnused
114114+ gnutar
115115+ gzip
116116+ python3
117117+ unzip
118118+ which
119119+ zip
120120+ ];
121121+122122+ defaultShellPath = lib.makeBinPath defaultShellUtils;
123123+124124+ bashWithDefaultShellUtilsSh = writeShellApplication {
125125+ name = "bash";
126126+ runtimeInputs = defaultShellUtils;
127127+ text = ''
128128+ if [[ "$PATH" == "/no-such-path" ]]; then
129129+ export PATH=${defaultShellPath}
130130+ fi
131131+ exec ${bash}/bin/bash "$@"
132132+ '';
133133+ };
134134+135135+ # Script-based interpreters in shebangs aren't guaranteed to work,
136136+ # especially on MacOS. So let's produce a binary
137137+ bashWithDefaultShellUtils = stdenv.mkDerivation {
138138+ name = "bash";
139139+ src = bashWithDefaultShellUtilsSh;
140140+ nativeBuildInputs = [ makeBinaryWrapper ];
141141+ buildPhase = ''
142142+ makeWrapper ${bashWithDefaultShellUtilsSh}/bin/bash $out/bin/bash
143143+ '';
144144+ };
145145+146146+ platforms = lib.platforms.linux ++ lib.platforms.darwin;
147147+148148+ inherit (stdenv.hostPlatform) isDarwin isAarch64;
149149+150150+ system = if isDarwin then "darwin" else "linux";
151151+152152+ # on aarch64 Darwin, `uname -m` returns "arm64"
153153+ arch = with stdenv.hostPlatform; if isDarwin && isAarch64 then "arm64" else parsed.cpu.name;
154154+155155+ bazelRC = writeTextFile {
156156+ name = "bazel-rc";
157157+ text = ''
158158+ startup --server_javabase=${runJdk}
159159+160160+ # Register nix-specific nonprebuilt java toolchains
161161+ build --extra_toolchains=@bazel_tools//tools/jdk:all
162162+ # and set bazel to use them by default
163163+ build --tool_java_runtime_version=local_jdk
164164+ build --java_runtime_version=local_jdk
165165+166166+ # load default location for the system wide configuration
167167+ try-import /etc/bazel.bazelrc
168168+ '';
169169+ };
170170+171171+in
172172+stdenv.mkDerivation rec {
173173+ pname = "bazel";
174174+ inherit version src;
175175+ inherit sourceRoot;
176176+177177+ patches = [
178178+ # Remote java toolchains do not work on NixOS because they download binaries,
179179+ # so we need to use the @local_jdk//:jdk
180180+ # It could in theory be done by registering @local_jdk//:all toolchains,
181181+ # but these java toolchains still bundle binaries for ijar and stuff. So we
182182+ # need a nonprebult java toolchain (where ijar and stuff is built from
183183+ # sources).
184184+ # There is no such java toolchain, so we introduce one here.
185185+ # By providing no version information, the toolchain will set itself to the
186186+ # version of $JAVA_HOME/bin/java, just like the local_jdk does.
187187+ # To ensure this toolchain gets used, we can set
188188+ # --{,tool_}java_runtime_version=local_jdk and rely on the fact no java
189189+ # toolchain registered by default uses the local_jdk, making the selection
190190+ # unambiguous.
191191+ # This toolchain has the advantage that it can use any ambiant java jdk,
192192+ # not only a given, fixed version. It allows bazel to work correctly in any
193193+ # environment where JAVA_HOME is set to the right java version, like inside
194194+ # nix derivations.
195195+ # However, this patch breaks bazel hermeticity, by picking the ambiant java
196196+ # version instead of the more hermetic remote_jdk prebuilt binaries that
197197+ # rules_java provide by default. It also requires the user to have a
198198+ # JAVA_HOME set to the exact version required by the project.
199199+ # With more code, we could define java toolchains for all the java versions
200200+ # supported by the jdk as in rules_java's
201201+ # toolchains/local_java_repository.bzl, but this is not implemented here.
202202+ # To recover vanilla behavior, non NixOS users can set
203203+ # --{,tool_}java_runtime_version=remote_jdk, effectively reverting the
204204+ # effect of this patch and the fake system bazelrc.
205205+ ./java_toolchain.patch
206206+207207+ # Bazel integrates with apple IOKit to inhibit and track system sleep.
208208+ # Inside the darwin sandbox, these API calls are blocked, and bazel
209209+ # crashes. It seems possible to allow these APIs inside the sandbox, but it
210210+ # feels simpler to patch bazel not to use it at all. So our bazel is
211211+ # incapable of preventing system sleep, which is a small price to pay to
212212+ # guarantee that it will always run in any nix context.
213213+ #
214214+ # See also ./bazel_darwin_sandbox.patch in bazel_5. That patch uses
215215+ # NIX_BUILD_TOP env var to conditionnally disable sleep features inside the
216216+ # sandbox. Oddly, bazel_6 does not need that patch :-/.
217217+ #
218218+ # If you want to investigate the sandbox profile path,
219219+ # IORegisterForSystemPower can be allowed with
220220+ #
221221+ # propagatedSandboxProfile = ''
222222+ # (allow iokit-open (iokit-user-client-class "RootDomainUserClient"))
223223+ # '';
224224+ #
225225+ # I do not know yet how to allow IOPMAssertion{CreateWithName,Release}
226226+ ./darwin_sleep.patch
227227+228228+ # Fix DARWIN_XCODE_LOCATOR_COMPILE_COMMAND by removing multi-arch support.
229229+ # Nixpkgs toolcahins do not support that (yet?) and get confused.
230230+ # Also add an explicit /usr/bin prefix that will be patched below.
231231+ ./xcode_locator.patch
232232+233233+ # On Darwin, the last argument to gcc is coming up as an empty string. i.e: ''
234234+ # This is breaking the build of any C target. This patch removes the last
235235+ # argument if it's found to be an empty string.
236236+ ../trim-last-argument-to-gcc-if-empty.patch
237237+238238+ # --experimental_strict_action_env (which may one day become the default
239239+ # see bazelbuild/bazel#2574) hardcodes the default
240240+ # action environment to a non hermetic value (e.g. "/usr/local/bin").
241241+ # This is non hermetic on non-nixos systems. On NixOS, bazel cannot find the required binaries.
242242+ # So we are replacing this bazel paths by defaultShellPath,
243243+ # improving hermeticity and making it work in nixos.
244244+ (substituteAll {
245245+ src = ../strict_action_env.patch;
246246+ strictActionEnvPatch = defaultShellPath;
247247+ })
248248+249249+ # bazel reads its system bazelrc in /etc
250250+ # override this path to a builtin one
251251+ (substituteAll {
252252+ src = ../bazel_rc.patch;
253253+ bazelSystemBazelRCPath = bazelRC;
254254+ })
255255+ ]
256256+ # See enableNixHacks argument above.
257257+ ++ lib.optional enableNixHacks ./nix-hacks.patch;
258258+259259+ postPatch =
260260+ let
261261+ # Workaround for https://github.com/NixOS/nixpkgs/issues/166205
262262+ nixpkgs166205ldflag = lib.optionalString stdenv.cc.isClang "-l${stdenv.cc.libcxx.cxxabi.libName}";
263263+ darwinPatches = ''
264264+ bazelLinkFlags () {
265265+ eval set -- "$NIX_LDFLAGS"
266266+ local flag
267267+ for flag in "$@"; do
268268+ printf ' -Wl,%s' "$flag"
269269+ done
270270+ }
271271+272272+ # Explicitly configure gcov since we don't have it on Darwin, so autodetection fails
273273+ export GCOV=${coreutils}/bin/false
274274+275275+ # Framework search paths aren't added by bintools hook
276276+ # https://github.com/NixOS/nixpkgs/pull/41914
277277+ export NIX_LDFLAGS+=" -F${CoreFoundation}/Library/Frameworks -F${CoreServices}/Library/Frameworks -F${Foundation}/Library/Frameworks -F${IOKit}/Library/Frameworks ${nixpkgs166205ldflag}"
278278+279279+ # libcxx includes aren't added by libcxx hook
280280+ # https://github.com/NixOS/nixpkgs/pull/41589
281281+ export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -isystem ${lib.getDev libcxx}/include/c++/v1"
282282+ # for CLang 16 compatibility in external/upb dependency
283283+ export NIX_CFLAGS_COMPILE+=" -Wno-gnu-offsetof-extensions"
284284+285285+ # This variable is used by bazel to propagate env vars for homebrew,
286286+ # which is exactly what we need too.
287287+ export HOMEBREW_RUBY_PATH="foo"
288288+289289+ # don't use system installed Xcode to run clang, use Nix clang instead
290290+ sed -i -E \
291291+ -e "s;/usr/bin/xcrun (--sdk macosx )?clang;${stdenv.cc}/bin/clang $NIX_CFLAGS_COMPILE $(bazelLinkFlags) -framework CoreFoundation;g" \
292292+ -e "s;/usr/bin/codesign;CODESIGN_ALLOCATE=${cctools}/bin/${cctools.targetPrefix}codesign_allocate ${sigtool}/bin/codesign;" \
293293+ scripts/bootstrap/compile.sh \
294294+ tools/osx/BUILD
295295+296296+ # nixpkgs's libSystem cannot use pthread headers directly, must import GCD headers instead
297297+ sed -i -e "/#include <pthread\/spawn.h>/i #include <dispatch/dispatch.h>" src/main/cpp/blaze_util_darwin.cc
298298+299299+ # XXX: What do these do ?
300300+ sed -i -e 's;"/usr/bin/libtool";_find_generic(repository_ctx, "libtool", "LIBTOOL", overriden_tools);g' tools/cpp/unix_cc_configure.bzl
301301+ wrappers=( tools/cpp/osx_cc_wrapper.sh.tpl )
302302+ for wrapper in "''${wrappers[@]}"; do
303303+ sedVerbose $wrapper \
304304+ -e "s,/usr/bin/xcrun install_name_tool,${cctools}/bin/install_name_tool,g"
305305+ done
306306+ '';
307307+308308+ genericPatches = ''
309309+ # unzip builtins_bzl.zip so the contents get patched
310310+ builtins_bzl=src/main/java/com/google/devtools/build/lib/bazel/rules/builtins_bzl
311311+ unzip ''${builtins_bzl}.zip -d ''${builtins_bzl}_zip >/dev/null
312312+ rm ''${builtins_bzl}.zip
313313+ builtins_bzl=''${builtins_bzl}_zip/builtins_bzl
314314+315315+ # md5sum is part of coreutils
316316+ sed -i 's|/sbin/md5|md5sum|g' src/BUILD third_party/ijar/test/testenv.sh
317317+318318+ echo
319319+ echo "Substituting */bin/* hardcoded paths in src/main/java/com/google/devtools"
320320+ # Prefilter the files with grep for speed
321321+ grep -rlZ /bin/ \
322322+ src/main/java/com/google/devtools \
323323+ src/main/starlark/builtins_bzl/common/python \
324324+ tools \
325325+ | while IFS="" read -r -d "" path; do
326326+ # If you add more replacements here, you must change the grep above!
327327+ # Only files containing /bin are taken into account.
328328+ sedVerbose "$path" \
329329+ -e 's!/usr/local/bin/bash!${bashWithDefaultShellUtils}/bin/bash!g' \
330330+ -e 's!/usr/bin/bash!${bashWithDefaultShellUtils}/bin/bash!g' \
331331+ -e 's!/bin/bash!${bashWithDefaultShellUtils}/bin/bash!g' \
332332+ -e 's!/usr/bin/env bash!${bashWithDefaultShellUtils}/bin/bash!g' \
333333+ -e 's!/usr/bin/env python2!${python3}/bin/python!g' \
334334+ -e 's!/usr/bin/env python!${python3}/bin/python!g' \
335335+ -e 's!/usr/bin/env!${coreutils}/bin/env!g' \
336336+ -e 's!/bin/true!${coreutils}/bin/true!g'
337337+ done
338338+339339+ # Fixup scripts that generate scripts. Not fixed up by patchShebangs below.
340340+ sedVerbose scripts/bootstrap/compile.sh \
341341+ -e 's!/bin/bash!${bashWithDefaultShellUtils}/bin/bash!g' \
342342+ -e 's!shasum -a 256!sha256sum!g'
343343+344344+ # Augment bundled repository_cache with our extra paths
345345+ ${lndir}/bin/lndir ${repoCache}/content_addressable \
346346+ $PWD/derived/repository_cache/content_addressable
347347+348348+ # Add required flags to bazel command line.
349349+ # XXX: It would suit a bazelrc file better, but I found no way to pass it.
350350+ # It seems that bazel bootstrapping ignores it.
351351+ # Passing EXTRA_BAZEL_ARGS is tricky due to quoting.
352352+ sedVerbose compile.sh \
353353+ -e "/bazel_build /a\ --verbose_failures \\\\" \
354354+ -e "/bazel_build /a\ --curses=no \\\\" \
355355+ -e "/bazel_build /a\ --features=-layering_check \\\\" \
356356+ -e "/bazel_build /a\ --experimental_strict_java_deps=off \\\\" \
357357+ -e "/bazel_build /a\ --strict_proto_deps=off \\\\" \
358358+ -e "/bazel_build /a\ --toolchain_resolution_debug='@bazel_tools//tools/jdk:(runtime_)?toolchain_type' \\\\" \
359359+ -e "/bazel_build /a\ --tool_java_runtime_version=local_jdk_17 \\\\" \
360360+ -e "/bazel_build /a\ --java_runtime_version=local_jdk_17 \\\\" \
361361+ -e "/bazel_build /a\ --tool_java_language_version=17 \\\\" \
362362+ -e "/bazel_build /a\ --java_language_version=17 \\\\" \
363363+ -e "/bazel_build /a\ --extra_toolchains=@bazel_tools//tools/jdk:all \\\\" \
364364+365365+ # Also build parser_deploy.jar with bootstrap bazel
366366+ # TODO: Turn into a proper patch
367367+ sedVerbose compile.sh \
368368+ -e 's!bazel_build !bazel_build src/tools/execlog:parser_deploy.jar !' \
369369+ -e 's!clear_log!cp $(get_bazel_bin_path)/src/tools/execlog/parser_deploy.jar output\nclear_log!'
370370+371371+ # append the PATH with defaultShellPath in tools/bash/runfiles/runfiles.bash
372372+ echo "PATH=\$PATH:${defaultShellPath}" >> runfiles.bash.tmp
373373+ cat tools/bash/runfiles/runfiles.bash >> runfiles.bash.tmp
374374+ mv runfiles.bash.tmp tools/bash/runfiles/runfiles.bash
375375+376376+ # reconstruct the now patched builtins_bzl.zip
377377+ pushd src/main/java/com/google/devtools/build/lib/bazel/rules/builtins_bzl_zip &>/dev/null
378378+ zip ../builtins_bzl.zip $(find builtins_bzl -type f) >/dev/null
379379+ rm -rf builtins_bzl
380380+ popd &>/dev/null
381381+ rmdir src/main/java/com/google/devtools/build/lib/bazel/rules/builtins_bzl_zip
382382+383383+ patchShebangs . >/dev/null
384384+ '';
385385+ in
386386+ ''
387387+ function sedVerbose() {
388388+ local path=$1; shift;
389389+ sed -i".bak-nix" "$path" "$@"
390390+ diff -U0 "$path.bak-nix" "$path" | sed "s/^/ /" || true
391391+ rm -f "$path.bak-nix"
392392+ }
393393+ ''
394394+ + lib.optionalString stdenv.hostPlatform.isDarwin darwinPatches
395395+ + genericPatches;
396396+397397+ meta = with lib; {
398398+ homepage = "https://github.com/bazelbuild/bazel/";
399399+ description = "Build tool that builds code quickly and reliably";
400400+ sourceProvenance = with sourceTypes; [
401401+ fromSource
402402+ binaryBytecode # source bundles dependencies as jars
403403+ ];
404404+ license = licenses.asl20;
405405+ maintainers = lib.teams.bazel.members;
406406+ inherit platforms;
407407+ };
408408+409409+ # Bazel starts a local server and needs to bind a local address.
410410+ __darwinAllowLocalNetworking = true;
411411+412412+ buildInputs = [ buildJdk bashWithDefaultShellUtils ] ++ defaultShellUtils;
413413+414414+ # when a command can’t be found in a bazel build, you might also
415415+ # need to add it to `defaultShellPath`.
416416+ nativeBuildInputs = [
417417+ installShellFiles
418418+ makeWrapper
419419+ python3
420420+ unzip
421421+ which
422422+ zip
423423+ python3.pkgs.absl-py # Needed to build fish completion
424424+ ] ++ lib.optionals (stdenv.isDarwin) [
425425+ cctools
426426+ libcxx
427427+ Foundation
428428+ CoreFoundation
429429+ CoreServices
430430+ ];
431431+432432+ # Bazel makes extensive use of symlinks in the WORKSPACE.
433433+ # This causes problems with infinite symlinks if the build output is in the same location as the
434434+ # Bazel WORKSPACE. This is why before executing the build, the source code is moved into a
435435+ # subdirectory.
436436+ # Failing to do this causes "infinite symlink expansion detected"
437437+ preBuildPhases = [ "preBuildPhase" ];
438438+ preBuildPhase = ''
439439+ mkdir bazel_src
440440+ shopt -s dotglob extglob
441441+ mv !(bazel_src) bazel_src
442442+ '';
443443+ buildPhase = ''
444444+ runHook preBuild
445445+446446+ # Increasing memory during compilation might be necessary.
447447+ # export BAZEL_JAVAC_OPTS="-J-Xmx2g -J-Xms200m"
448448+449449+ # If EMBED_LABEL isn't set, it'd be auto-detected from CHANGELOG.md
450450+ # and `git rev-parse --short HEAD` which would result in
451451+ # "3.7.0- (@non-git)" due to non-git build and incomplete changelog.
452452+ # Actual bazel releases use scripts/release/common.sh which is based
453453+ # on branch/tag information which we don't have with tarball releases.
454454+ # Note that .bazelversion is always correct and is based on bazel-*
455455+ # executable name, version checks should work fine
456456+ export EMBED_LABEL="${version}- (@non-git)"
457457+ echo "Stage 1 - Running bazel bootstrap script"
458458+ ${bash}/bin/bash ./bazel_src/compile.sh
459459+460460+ # XXX: get rid of this, or move it to another stage.
461461+ # It is plain annoying when builds fail.
462462+ echo "Stage 2 - Generate bazel completions"
463463+ ${bash}/bin/bash ./bazel_src/scripts/generate_bash_completion.sh \
464464+ --bazel=./bazel_src/output/bazel \
465465+ --output=./bazel_src/output/bazel-complete.bash \
466466+ --prepend=./bazel_src/scripts/bazel-complete-header.bash \
467467+ --prepend=./bazel_src/scripts/bazel-complete-template.bash
468468+ ${python3}/bin/python3 ./bazel_src/scripts/generate_fish_completion.py \
469469+ --bazel=./bazel_src/output/bazel \
470470+ --output=./bazel_src/output/bazel-complete.fish
471471+472472+ runHook postBuild
473473+ '';
474474+475475+ installPhase = ''
476476+ runHook preInstall
477477+478478+ mkdir -p $out/bin
479479+480480+ # official wrapper scripts that searches for $WORKSPACE_ROOT/tools/bazel if
481481+ # it can’t find something in tools, it calls
482482+ # $out/bin/bazel-{version}-{os_arch} The binary _must_ exist with this
483483+ # naming if your project contains a .bazelversion file.
484484+ cp ./bazel_src/scripts/packages/bazel.sh $out/bin/bazel
485485+ versionned_bazel="$out/bin/bazel-${version}-${system}-${arch}"
486486+ mv ./bazel_src/output/bazel "$versionned_bazel"
487487+ wrapProgram "$versionned_bazel" --suffix PATH : ${defaultShellPath}
488488+489489+ mkdir $out/share
490490+ cp ./bazel_src/output/parser_deploy.jar $out/share/parser_deploy.jar
491491+ cat <<EOF > $out/bin/bazel-execlog
492492+ #!${runtimeShell} -e
493493+ ${runJdk}/bin/java -jar $out/share/parser_deploy.jar \$@
494494+ EOF
495495+ chmod +x $out/bin/bazel-execlog
496496+497497+ # shell completion files
498498+ installShellCompletion --bash \
499499+ --name bazel.bash \
500500+ ./bazel_src/output/bazel-complete.bash
501501+ installShellCompletion --zsh \
502502+ --name _bazel \
503503+ ./bazel_src/scripts/zsh_completion/_bazel
504504+ installShellCompletion --fish \
505505+ --name bazel.fish \
506506+ ./bazel_src/output/bazel-complete.fish
507507+ '';
508508+509509+ installCheckPhase = ''
510510+ export TEST_TMPDIR=$(pwd)
511511+512512+ hello_test () {
513513+ $out/bin/bazel test \
514514+ --test_output=errors \
515515+ examples/cpp:hello-success_test \
516516+ examples/java-native/src/test/java/com/example/myproject:hello
517517+ }
518518+519519+ cd ./bazel_src
520520+521521+ # If .bazelversion file is present in dist files and doesn't match `bazel` version
522522+ # running `bazel` command within bazel_src will fail.
523523+ # Let's remove .bazelversion within the test, if present it is meant to indicate bazel version
524524+ # to compile bazel with, not version of bazel to be built and tested.
525525+ rm -f .bazelversion
526526+527527+ # test whether $WORKSPACE_ROOT/tools/bazel works
528528+529529+ mkdir -p tools
530530+ cat > tools/bazel <<"EOF"
531531+ #!${runtimeShell} -e
532532+ exit 1
533533+ EOF
534534+ chmod +x tools/bazel
535535+536536+ # first call should fail if tools/bazel is used
537537+ ! hello_test
538538+539539+ cat > tools/bazel <<"EOF"
540540+ #!${runtimeShell} -e
541541+ exec "$BAZEL_REAL" "$@"
542542+ EOF
543543+544544+ # second call succeeds because it defers to $out/bin/bazel-{version}-{os_arch}
545545+ hello_test
546546+547547+ ## Test that the GSON serialisation files are present
548548+ gson_classes=$(unzip -l $(bazel info install_base)/A-server.jar | grep GsonTypeAdapter.class | wc -l)
549549+ if [ "$gson_classes" -lt 10 ]; then
550550+ echo "Missing GsonTypeAdapter classes in A-server.jar. Lockfile generation will not work"
551551+ exit 1
552552+ fi
553553+554554+ runHook postInstall
555555+ '';
556556+557557+ # Save paths to hardcoded dependencies so Nix can detect them.
558558+ # This is needed because the templates get tar’d up into a .jar.
559559+ postFixup = ''
560560+ mkdir -p $out/nix-support
561561+ echo "${defaultShellPath}" >> $out/nix-support/depends
562562+ # The string literal specifying the path to the bazel-rc file is sometimes
563563+ # stored non-contiguously in the binary due to gcc optimisations, which leads
564564+ # Nix to miss the hash when scanning for dependencies
565565+ echo "${bazelRC}" >> $out/nix-support/depends
566566+ '' + lib.optionalString stdenv.isDarwin ''
567567+ echo "${cctools}" >> $out/nix-support/depends
568568+ '';
569569+570570+ dontStrip = true;
571571+ dontPatchELF = true;
572572+573573+ passthru = {
574574+ # Additional tests that check bazel’s functionality. Execute
575575+ #
576576+ # nix-build . -A bazel_7.tests
577577+ #
578578+ # in the nixpkgs checkout root to exercise them locally.
579579+ tests = callPackage ./tests.nix {
580580+ inherit Foundation bazel_self lockfile repoCache;
581581+ };
582582+583583+ # For ease of debugging
584584+ inherit distDir repoCache lockfile;
585585+ };
586586+}
···11+diff --git a/tools/jdk/BUILD.tools b/tools/jdk/BUILD.tools
22+index a8af76e90c..7f8b030f63 100644
33+--- a/tools/jdk/BUILD.tools
44++++ b/tools/jdk/BUILD.tools
55+@@ -146,6 +146,25 @@ py_test(
66+ ],
77+ )
88+99++##### Nonprebuilt toolchains definitions for NixOS and nix build sandboxes ####
1010++
1111++load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain", "NONPREBUILT_TOOLCHAIN_CONFIGURATION")
1212++
1313++[
1414++ default_java_toolchain(
1515++ name = "nonprebuilt_toolchain_java" + str(version),
1616++ configuration = NONPREBUILT_TOOLCHAIN_CONFIGURATION,
1717++ java_runtime = "@local_jdk//:jdk",
1818++ source_version = str(version),
1919++ target_version = str(version),
2020++ )
2121++ # Ideally we would only define toolchains for the java versions that the
2222++ # local jdk supports. But we cannot access this information in a BUILD
2323++ # file, and this is a hack anyway, so just pick a large enough upper bound.
2424++ # At the current pace, java <= 30 should cover all realeases until 2028.
2525++ for version in range(8, 31)
2626++]
2727++
2828+ #### Aliases to rules_java to keep backward-compatibility (begin) ####
2929+3030+ TARGET_NAMES = [