tools: extend bazel wrapper to execute commands inside nix-shell

Change-Id: I27fd2e2ac863fabdc015437594bc2cc677279af1
Reviewed-on: https://review.monogon.dev/c/monogon/+/2239
Tested-by: Jenkins CI
Reviewed-by: Leopold Schabel <leo@monogon.tech>
diff --git a/tools/bazel b/tools/bazel
index 67de8e4..7711189 100755
--- a/tools/bazel
+++ b/tools/bazel
@@ -4,13 +4,26 @@
 set -euo pipefail
 shopt -s nullglob
 
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
+
+# 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
+
+  # Jump to project root since bwrap hangs if we aren't there
+  cd "${DIR}/../"
+
+  export COMMAND="bazel $*"
+  exec nix-shell
+fi
+
 # Short circuit if we're rebuilding the sandbox via third_party/sandboxroot/regenerate.sh.
 if [[ -n ${MONOGON_SYSROOT_REBUILD:-} ]]; then
   echo "Skipping Bazel wrapper" >&2
   exec -a "$0" "${BAZEL_REAL}" "$@"
 fi
 
-DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
 SANDBOX="${DIR}/../.bazeldnf/sandbox/default"
 BAZEL_ARGS="--noworkspace_rc --bazelrc ${DIR}/../.bazelrc.sandboxroot"
 
diff --git a/tools/run_bazel b/tools/run_bazel
deleted file mode 100755
index c2a9adc..0000000
--- a/tools/run_bazel
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/usr/bin/env bash
-
-export COMMAND="bazel $*"
-nix-shell