Bazel: Difference between revisions

From NixOS Wiki
imported>Dmayle
No edit summary
imported>Keegancsmith
replace grep.app link (which had no results) with sourcegraph
 
(2 intermediate revisions by 2 users not shown)
Line 21: Line 21:


== Troubleshooting ==
== Troubleshooting ==
In some versions of bazel, the sandboxing does not inherit the FHS environment, and elements of the toolchain (notably {{ic|protoc}} may fail to run.  In that case, switching to {{ic|pkgs.bazel_4}} should solve the problem.
In some versions of bazel, the sandboxing does not inherit the FHS environment, and elements of the toolchain (notably {{ic|protoc}}) may fail to run.  In that case, switching to {{ic|pkgs.bazel_4}} should solve the problem.
 
== See also ==
 
* [https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/build-bazel-package/default.nix buildBazelPackage implementation]
* [https://sourcegraph.com/search?q=context:global+buildBazelPackage+lang:nix+repo:%5Egithub%5C.com/NixOS/nixpkgs%24+&patternType=standard buildBazelPackage used in nixpkgs]


[[Category:Applications]]
[[Category:Applications]]

Latest revision as of 13:53, 2 November 2022

Bazel is a build system for software development that attempts to create a hermetic build by bootstrapping the toolchain from known sources and binaries. Unfortunately, the desire to be hermetic is more aspirational than exact, and can run into some problems.

Bazel downloads binaries (for example, protoc for compiling protocol buffers, or the go sdk) which are dynamically linked, expecting an FHS environment.

The easiest way to use bazel is with nix-shell by creating a file called shell.nix in the root of the project (alongside the WORKSPACE):

 { pkgs ? import <nixpkgs> {} }:
 
 (pkgs.buildFHSUserEnv {
   name = "bazel-userenv-example";
   targetPkgs = pkgs: [
     pkgs.bazel
     pkgs.glibc
     pkgs.gcc
   ];
 }).env

You can then run `nix-shell` from the root of the project before calling bazel.

While there are solutions for simplifying the use of a shell.nix like direnv, lorri, and devenv, they are not compatible with a shell.nix that creates an FHS environment, since the FHS environment requires a sub-shell to properly setup the chroot, and the three tools above only support making changes to environment variables.

Troubleshooting

In some versions of bazel, the sandboxing does not inherit the FHS environment, and elements of the toolchain (notably protoc) may fail to run. In that case, switching to pkgs.bazel_4 should solve the problem.

See also