Skip to content
Closed
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
3 changes: 3 additions & 0 deletions pkgs/by-name/ba/bazel_8/bazel-execlog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!@runtimeShell@

exec @binJava@ -jar @out@/share/parser_deploy.jar "$@"
74 changes: 74 additions & 0 deletions pkgs/by-name/ba/bazel_8/build-support/bazelDerivation.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
stdenv,
lndir,
lib,
}:

args@{
bazel,
registry ? null,
bazelRepoCache ? null,
bazelVendorDeps ? null,
startupArgs ? [ ],
commandArgs ? [ ],
bazelPreBuild ? "",
bazelPostBuild ? "",
serverJavabase ? null,
targets,
command,
...
}:

stdenv.mkDerivation (
{
preBuildPhases = [ "preBuildPhase" ];
preBuildPhase =
(lib.optionalString (bazelRepoCache != null) ''
# repo_cache needs to be writeable even in air-gapped builds
mkdir repo_cache
${lndir}/bin/lndir -silent ${bazelRepoCache}/repo_cache repo_cache
'')

+ (lib.optionalString (bazelVendorDeps != null) ''
mkdir vendor_dir
${lndir}/bin/lndir -silent ${bazelVendorDeps}/vendor_dir vendor_dir

# pin all deps to avoid re-fetch attempts by Bazel
rm vendor_dir/VENDOR.bazel
find vendor_dir -mindepth 1 -maxdepth 1 -type d -printf "pin(\"@@%P\")\n" > vendor_dir/VENDOR.bazel
'')
# keep preBuildPhase always defined as it is listed in preBuildPhases
+ ''
true
'';
buildPhase = ''
runHook preBuild

export HOME=$(mktemp -d)

${bazelPreBuild}

${bazel}/bin/bazel ${
lib.escapeShellArgs (
lib.optional (serverJavabase != null) "--server_javabase=${serverJavabase}"
++ [ "--batch" ]
++ startupArgs
)
} ${command} ${
lib.escapeShellArgs (
lib.optional (registry != null) "--registry=file://${registry}"
++ lib.optional (bazelRepoCache != null) "--repository_cache=repo_cache"
++ lib.optional (bazelVendorDeps != null) "--vendor_dir=vendor_dir"
++ commandArgs
++ targets
)
}

${bazelPostBuild}

runHook postBuild
'';

}
// args
)
164 changes: 164 additions & 0 deletions pkgs/by-name/ba/bazel_8/build-support/bazelPackage.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
callPackage,
gnugrep,
lib,
autoPatchelfHook,
stdenv,
}:

{
name,
src,
sourceRoot ? null,
version ? null,
targets,
bazel,
startupArgs ? [ ],
commandArgs ? [ ],
env ? { },
serverJavabase ? null,
registry ? null,
bazelRepoCacheFOD ? {
outputHash = null;
outputHashAlgo = "sha256";
},
bazelVendorDepsFOD ? {
outputHash = null;
outputHashAlgo = "sha256";
},
installPhase,
buildInputs ? [ ],
nativeBuildInputs ? [ ],
autoPatchelfIgnoreMissingDeps ? null,
}:
let
# FOD produced by `bazel fetch`
# Repo cache contains content-addressed external Bazel dependencies without any patching
# Potentially this can be nixified via --experimental_repository_resolved_file
# (Note: file itself isn't reproducible because it has lots of extra info and order
# isn't stable too. Parsing it into nix fetch* commands isn't trivial but might be possible)
bazelRepoCache =
if bazelRepoCacheFOD.outputHash == null then
null
else
(callPackage ./bazelDerivation.nix { } {
name = "bazelRepoCache";
inherit (bazelRepoCacheFOD) outputHash outputHashAlgo;
inherit
src
version
sourceRoot
env
buildInputs
nativeBuildInputs
;
inherit registry;
inherit
bazel
targets
startupArgs
serverJavabase
;
command = "fetch";
outputHashMode = "recursive";
commandArgs = [ "--repository_cache=repo_cache" ] ++ commandArgs;
bazelPreBuild = ''
mkdir repo_cache
'';
installPhase = ''
mkdir -p $out/repo_cache
cp -r --reflink=auto repo_cache/* $out/repo_cache
'';
});
# Stage1: FOD produced by `bazel vendor`, Stage2: eventual patchelf or other tuning
# Vendor deps contains unpacked&patches external dependencies, this may need Nix-specific
# patching to address things like
# - broken symlinks
# - symlinks or other references to absolute nix store paths which isn't allowed for FOD
# - autoPatchelf for externally-fetched binaries
#
# Either repo cache or vendor deps should be enough to build a given package
bazelVendorDeps =
if bazelVendorDepsFOD.outputHash == null then
null
else
(
let
stage1 = callPackage ./bazelDerivation.nix { } {
name = "bazelVendorDepsStage1";
inherit (bazelVendorDepsFOD) outputHash outputHashAlgo;
inherit
src
version
sourceRoot
env
buildInputs
nativeBuildInputs
;
inherit registry;
inherit
bazel
targets
startupArgs
serverJavabase
;
dontFixup = true;
command = "vendor";
outputHashMode = "recursive";
commandArgs = [ "--vendor_dir=vendor_dir" ] ++ commandArgs;
bazelPreBuild = ''
mkdir vendor_dir
'';
bazelPostBuild = ''
# remove symlinks that point to locations under bazel_src/
find vendor_dir -type l -lname "$HOME/*" -exec rm '{}' \;
# remove symlinks to temp build directory on darwin
find vendor_dir -type l -lname "/private/var/tmp/*" -exec rm '{}' \;
# remove broken symlinks
find vendor_dir -xtype l -exec rm '{}' \;

# remove .marker files referencing NIX_STORE as those references aren't allowed in FOD
(${gnugrep}/bin/grep -rI "$NIX_STORE/" vendor_dir --files-with-matches --include="*.marker" --null || true) \
| xargs -0 --no-run-if-empty rm
'';
installPhase = ''
mkdir -p $out/vendor_dir
cp -r --reflink=auto vendor_dir/* $out/vendor_dir
'';

};
in
stdenv.mkDerivation {
name = "bazelVendorDeps";
buildInputs = lib.optional (!stdenv.hostPlatform.isDarwin) autoPatchelfHook ++ buildInputs;
inherit autoPatchelfIgnoreMissingDeps;
src = stage1;
installPhase = ''
cp -r . $out
'';
}
);

package = callPackage ./bazelDerivation.nix { } {
inherit
name
src
version
sourceRoot
env
buildInputs
nativeBuildInputs
;
inherit registry bazelRepoCache bazelVendorDeps;
inherit
bazel
targets
startupArgs
serverJavabase
commandArgs
;
inherit installPhase;
command = "build";
};
in
package // { passthru = { inherit bazelRepoCache bazelVendorDeps; }; }
24 changes: 24 additions & 0 deletions pkgs/by-name/ba/bazel_8/build-support/patching.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
stdenv,
}:
{
# If there's a need to patch external dependencies managed by Bazel
# one option is to configure patches on Bazel level. Bazel doesn't
# allow patches to be in absolute paths so this helper will produce
# sources patch that adds given file to given location
addFilePatch =
{
path,
file,
}:
stdenv.mkDerivation {
name = "add_file.patch";
dontUnpack = true;
buildPhase = ''
mkdir -p $(dirname "${path}")
cp ${file} "${path}"
diff -u /dev/null "${path}" >result.patch || true # diff exit code is non-zero if there's a diff
'';
installPhase = ''cp result.patch $out'';
};
}
41 changes: 41 additions & 0 deletions pkgs/by-name/ba/bazel_8/defaultShell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
lib,
makeBinaryWrapper,
writeShellApplication,
bash,
stdenv,
}:
{ defaultShellUtils }:
let
defaultShellPath = lib.makeBinPath defaultShellUtils;

bashWithDefaultShellUtilsSh = writeShellApplication {
name = "bash";
runtimeInputs = defaultShellUtils;
# Empty PATH in Nixpkgs Bash is translated to /no-such-path
# On other distros empty PATH search fallback is looking in standard
# locations like /bin,/usr/bin
# For Bazel many rules rely on such search finding some common utils,
# so we provide them in case rules or arguments didn't specify a precise PATH
text = ''
if [[ "$PATH" == "/no-such-path" ]]; then
export PATH=${defaultShellPath}
fi
exec ${bash}/bin/bash "$@"
'';
};

in
{
inherit defaultShellUtils defaultShellPath;
# Script-based interpreters in shebangs aren't guaranteed to work,
# especially on MacOS. So let's produce a binary
bashWithDefaultShellUtils = stdenv.mkDerivation {
name = "bash";
src = bashWithDefaultShellUtilsSh;
nativeBuildInputs = [ makeBinaryWrapper ];
buildPhase = ''
makeWrapper ${bashWithDefaultShellUtilsSh}/bin/bash $out/bin/bash
'';
};
}
Loading
Loading