tools/bazel: Allow direct invocation of bazel wrapper and add more docs
Change-Id: I5f03225fb3d5c0304e2f3843ac3d81af2d1b504e
Reviewed-on: https://review.monogon.dev/c/monogon/+/2384
Reviewed-by: Leopold Schabel <leo@monogon.tech>
Tested-by: Jenkins CI
diff --git a/tools/bazel b/tools/bazel
index 7711189..4db2421 100755
--- a/tools/bazel
+++ b/tools/bazel
@@ -9,8 +9,24 @@
# Jump into nix-shell if BAZEL_REAL is set to a /nix/store path and we aren't
# inside our shell yet.
if [[ "${BAZEL_REAL:-}" == /nix/store/* && -z "${MONOGON_NIXOS:-}" ]]; then
- echo "BAZEL_REAL is pointing to a /nix/store path, overriding to nix-shell." >&2
+ echo "Detected Nix based bazel installation and we are not in a nix-shell, overriding to nix-shell." >&2
+ USE_NIX_SHELL=yes
+fi
+# If the wrapper is called directly we check if nix-shell is available
+# to automagically switch into the nix-shell. Otherwise complain and
+# exit.
+if [[ -z "${BAZEL_REAL:-}" ]]; then
+ if [[ -x $(command -v nix-shell) ]]; then
+ echo "BAZEL_REAL is not set and nix-shell is available, overriding to nix-shell" >&2
+ USE_NIX_SHELL=yes
+ else
+ echo "BAZEL_REAL is not set and nix-shell not available. Please check the setup guide." >&2
+ exit 1
+ fi
+fi
+
+if [[ -n "${USE_NIX_SHELL:-}" ]]; then
# Jump to project root since bwrap hangs if we aren't there
cd "${DIR}/../"
@@ -28,12 +44,6 @@
BAZEL_ARGS="--noworkspace_rc --bazelrc ${DIR}/../.bazelrc.sandboxroot"
prechecks() {
- # Complain if script invoked directly.
- if [[ -z "${BAZEL_REAL:-}" ]]; then
- echo "BAZEL_REAL is not set - do not run directly, instead, use bazelisk" >&2
- exit 1
- fi
-
# Recommend using Bazelisk instead of Bazel's "bazel.sh" wrapper.
# Skip if we're inside the Nix shell (which uses a customized Bazel build).
if [[ -z "${BAZELISK_SKIP_WRAPPER:-}" && -z "${MONOGON_NIXOS:-}" ]]; then