bazel_7: Filter bzlmod dependencies by name predicate

+26 -31
+11 -16
pkgs/development/tools/build-managers/bazel/bazel_7/bazel-repository-cache.nix
··· 4 4 , fetchurl 5 5 # The path to the right MODULE.bazel.lock 6 6 , lockfile 7 - # The path to a json file containing the list of hashes we should prefetch 8 - , requiredDeps ? null 9 - , extraInputs ? [ ] 7 + # A predicate used to select only some dependencies based on their name 8 + , requiredDepNamePredicate ? _: true 9 + # Extra deps in the form of derivations producing a "single file" output path 10 + , extraDeps ? [ ] 10 11 }: 11 12 let 12 13 modules = builtins.fromJSON (builtins.readFile lockfile); ··· 28 29 in 29 30 # like foldl', force evaluation of intermediate results 30 31 builtins.seq acc' children; 32 + 33 + # remove the "--" prefix, abusing undocumented negative substring length 34 + sanitize = builtins.substring 2 (-1); 31 35 32 36 extract_source = f: acc: value: 33 37 # We take any "attributes" object that has a "sha256" field. Every value ··· 72 76 && f attrs.name 73 77 then accWithNewSource else acc; 74 78 75 - # remove the "--" prefix, abusing undocumented negative substring length 76 - sanitize = builtins.substring 2 (-1); 77 - 78 - inputs = foldlJSON (extract_source (n: builtins.trace (sanitize n) true)) { } modules; 79 - 80 - requiredHashes = builtins.fromJSON (builtins.readFile requiredDeps); 81 - requiredAttrs = lib.genAttrs requiredHashes throw; 82 - 83 - requiredInputs = 84 - if requiredDeps == null 85 - then inputs 86 - else builtins.intersectAttrs requiredAttrs (builtins.trace inputs inputs); 79 + requiredSourcePredicate = n: requiredDepNamePredicate (sanitize n); 80 + requiredDeps = foldlJSON (extract_source requiredSourcePredicate) { } modules; 87 81 88 82 command = '' 89 83 mkdir -p $out/content_addressable/sha256 ··· 92 86 # TODO: Do not re-hash. Use nix-hash to convert hashes 93 87 (drv: '' 94 88 filename=$(basename "${lib.head drv.urls}") 89 + echo Bundling $filename 95 90 hash=$(${rnix-hashes}/bin/rnix-hashes --encoding BASE16 ${drv.sha256} | cut -f 2) 96 91 mkdir -p content_addressable/sha256/$hash 97 92 ln -sfn ${drv} content_addressable/sha256/$hash/file 98 93 # Expect file name conflicts 99 94 ln -sn ${drv} $filename || true 100 95 '') 101 - (builtins.attrValues requiredInputs ++ extraInputs) 96 + (builtins.attrValues requiredDeps ++ extraDeps) 102 97 ; 103 98 104 99 repository_cache = runCommand "bazel-repository-cache" { } command;
+15 -15
pkgs/development/tools/build-managers/bazel/bazel_7/default.nix
··· 35 35 , gnupatch 36 36 , file 37 37 , installShellFiles 38 + , lndir 38 39 # updater 39 40 , python3 40 41 , writeScript ··· 75 76 distDir = repoCache; 76 77 repoCache = callPackage ./bazel-repository-cache.nix { 77 78 inherit lockfile; 78 - # TODO: efficiently filter so that all tests find their dependencies 79 - # without downloading 10 jdk versions 80 - # requiredDeps = ./required-hashes.json; 79 + # We use the release tarball that already has everything bundled, 80 + # But we need one extra dep required by our nonprebuilt java toolchains. 81 + requiredDepNamePredicate = name: 82 + null != builtins.match "rules_java~.*~toolchains~remote_java_tools" name; 81 83 }; 82 84 83 85 defaultShellUtils = ··· 274 276 postPatch = 275 277 let 276 278 darwinPatches = '' 277 - 278 279 bazelLinkFlags () { 279 280 eval set -- "$NIX_LDFLAGS" 280 281 local flag ··· 367 368 -e 's!/bin/bash!${bash}/bin/bash!g' \ 368 369 -e 's!shasum -a 256!sha256sum!g' 369 370 370 - # Add compile options to command line. 371 + # Augment bundled repository_cache with our extra paths 372 + ${lndir}/bin/lndir ${repoCache}/content_addressable \ 373 + $PWD/derived/repository_cache/content_addressable 374 + 375 + # Add required flags to bazel command line. 371 376 # XXX: It would suit a bazelrc file better, but I found no way to pass it. 372 - # It seems it is always ignored. 377 + # It seems that bazel bootstrapping ignores it. 373 378 # Passing EXTRA_BAZEL_ARGS is tricky due to quoting. 374 - 375 379 sedVerbose compile.sh \ 376 380 -e "/bazel_build /a\ --verbose_failures \\\\" \ 377 381 -e "/bazel_build /a\ --curses=no \\\\" \ 378 382 -e "/bazel_build /a\ --features=-layering_check \\\\" \ 379 383 -e "/bazel_build /a\ --experimental_strict_java_deps=off \\\\" \ 380 384 -e "/bazel_build /a\ --strict_proto_deps=off \\\\" \ 381 - -e "/bazel_build /a\ --action_env=NIX_CFLAGS_COMPILE=\"$NIX_CFLAGS_COMPILE\" \\\\" \ 382 - -e "/bazel_build /a\ --action_env=NIX_LDFLAGS=\"$NIX_LDFLAGS\" \\\\" \ 383 - -e "/bazel_build /a\ --host_action_env=NIX_CFLAGS_COMPILE=\"$NIX_CFLAGS_COMPILE\" \\\\" \ 384 - -e "/bazel_build /a\ --host_action_env=NIX_LDFLAGS=\"$NIX_LDFLAGS\" \\\\" \ 385 385 -e "/bazel_build /a\ --toolchain_resolution_debug='@bazel_tools//tools/jdk:(runtime_)?toolchain_type' \\\\" \ 386 386 -e "/bazel_build /a\ --tool_java_runtime_version=local_jdk \\\\" \ 387 387 -e "/bazel_build /a\ --java_runtime_version=local_jdk \\\\" \ 388 388 -e "/bazel_build /a\ --extra_toolchains=@bazel_tools//tools/jdk:all \\\\" \ 389 - -e "/bazel_build /a\ --distdir=${distDir} \\\\" \ 390 389 391 - #-e "/bazel_build /a\ --action_env=NIX_BINTOOLS=\"$NIX_BINTOOLS\" \\\\" \ 392 - #-e "/bazel_build /a\ --action_env=NIX_CC=\"$NIX_CC\" \\\\" \ 393 - #-e "/bazel_build /a\ --action_env=nativeBuildInputs=\"$nativeBuildInputs\" \\\\" \ 390 + #-e "/bazel_build /a\ --action_env=NIX_CFLAGS_COMPILE=\"$NIX_CFLAGS_COMPILE\" \\\\" \ 391 + #-e "/bazel_build /a\ --action_env=NIX_LDFLAGS=\"$NIX_LDFLAGS\" \\\\" \ 392 + #-e "/bazel_build /a\ --host_action_env=NIX_CFLAGS_COMPILE=\"$NIX_CFLAGS_COMPILE\" \\\\" \ 393 + #-e "/bazel_build /a\ --host_action_env=NIX_LDFLAGS=\"$NIX_LDFLAGS\" \\\\" \ 394 394 395 395 # Also build parser_deploy.jar with bootstrap bazel 396 396 # TODO: Turn into a proper patch