Bazel on nix = use bazelisk

G’day,

Just a quick note for search engines to find.

Why isn’t there bazel_8 package?

The reason is that you don’t really need one. Use bazelisk instead.

I thought we were stuck on 7.6, but we are not! Yay!

[nix-shell:~/Downloads/go_hello_world_race]$ bazel --version
bazel 7.6.0- (@non-git)
[nix-shell:~/Downloads/go_hello_world_race]$ echo 8.2.1rc1 > .bazelversion

[das@t:~/Downloads/go_hello_world_race]$ bazelisk build --config=local-sysroot //:hello
The program 'bazelisk' is not in your PATH. You can make it available in an
ephemeral shell by typing:
  nix-shell -p bazelisk

[das@t:~/Downloads/go_hello_world_race]$ nix-shell -p bazelisk
these 7 paths will be fetched (2.70 MiB download, 15.96 MiB unpacked):
  /nix/store/9ajjzpf3m5v04cdfgnrj9jw24p2wc4x2-bazelisk-1.22.1
  /nix/store/19djhxh07c8ya6n96ylidaxgi35p8asx-file-5.45
  /nix/store/h42gyp46whkbfsl4c6ayn06db4jc3l8f-gnu-config-2024-01-01
  /nix/store/gvc67j3hwsyz6bd2g9gfr80l1r6n3khm-gnumake-4.4.1
  /nix/store/q3hmrfgjkba7vqyrvr1nlsaiymy1ma5w-patchelf-0.15.0
  /nix/store/8rb50qg3macvs6y7i0fll37nsw2sifyc-stdenv-linux
  /nix/store/hi361xvwkvh3nrqgr6bzrnr39rw2g6yp-update-autotools-gnu-config-scripts-hook
copying path '/nix/store/9ajjzpf3m5v04cdfgnrj9jw24p2wc4x2-bazelisk-1.22.1' from 'https://cache.nixos.org'...
copying path '/nix/store/h42gyp46whkbfsl4c6ayn06db4jc3l8f-gnu-config-2024-01-01' from 'https://cache.nixos.org'...
copying path '/nix/store/19djhxh07c8ya6n96ylidaxgi35p8asx-file-5.45' from 'https://cache.nixos.org'...
copying path '/nix/store/gvc67j3hwsyz6bd2g9gfr80l1r6n3khm-gnumake-4.4.1' from 'https://cache.nixos.org'...
copying path '/nix/store/q3hmrfgjkba7vqyrvr1nlsaiymy1ma5w-patchelf-0.15.0' from 'https://cache.nixos.org'...
copying path '/nix/store/hi361xvwkvh3nrqgr6bzrnr39rw2g6yp-update-autotools-gnu-config-scripts-hook' from 'https://cache.nixos.org'...
copying path '/nix/store/8rb50qg3macvs6y7i0fll37nsw2sifyc-stdenv-linux' from 'https://cache.nixos.org'...

[nix-shell:~/Downloads/go_hello_world_race]$ bazelisk build --config=local-sysroot //:hello
2025/05/21 07:49:42 Downloading https://releases.bazel.build/8.2.1/rc1/bazel-8.2.1rc1-linux-x86_64...
Downloading: 60 MB out of 60 MB (100%) 
Extracting Bazel installation...
Starting local Bazel server (8.2.1rc1) and connecting to it...  <--------- YAY!!
ERROR: Error computing the main repository mapping: error loading package 'external': Both --enable_bzlmod and --enable_workspace are disabled, but one of them must be enabled to fetch external dependencies.
Computing main repo mapping: 

[nix-shell:~/Downloads/go_hello_world_race]$

[nix-shell:~/Downloads/go_hello_world_race]$ bazel --version
ERROR: The project you're trying to build requires Bazel 8.2.1rc1 (specified in /home/das/Downloads/go_hello_world_race/.bazelversion), but it wasn't found in /nix/store/0q7xaqcwffhwxp1w7fzclsd461hhch7f-bazel-7.6.0/bin.

Bazel binaries for all official releases can be downloaded from here:
  https://github.com/bazelbuild/bazel/releases

Please put the downloaded Bazel binary into this location:
  /nix/store/0q7xaqcwffhwxp1w7fzclsd461hhch7f-bazel-7.6.0/bin/bazel-8.2.1rc1-linux-x86_64

[nix-shell:~/Downloads/go_hello_world_race]$ 

Regards,
Dave

This only works on NixOS if you have nix-ld setup.

It’s one of the reasons I ended up going nix-ld – I needed to run bazelisk as well.
Maybe an overlay with all the same versions available would be a good addition, so it would work nicely

Thanks for the reply @fzakaria

Very interesting. I tried this nix-ld thing, but I’m not sure it did anything.

I have just managed to get Bazel to run go race tests on NixOS, and have sent the rules_go a PR

Issue: LD_LIBRARY_PATH cannot be set within the sandbox · Issue #4360 · bazel-contrib/rules_go · GitHub
Pull request: impure_env by randomizedcoder · Pull Request #4361 · bazel-contrib/rules_go · GitHub

… Bazel is a lot less hermetic an Nix :wink:

In this repo, there’s an example of a very simple go program with a race test, and this repo uses Nix to create a sysroot, that gets pulled into the Bazel sandbox, and can then perform the necessary linking using llvm’s ld.lld.

I haven’t used it but people like rules_nixpkg
I want to actually make a “slimmer” bazel that is more geared for Nix or just make Nix more Bazel-like with a simpler DSL like starlark.

:slight_smile: