Bazel: Difference between revisions

From NixOS Wiki
imported>Dmayle
Intial page on developing with Bazel on NixOS
 
imported>Nix
m add Software/Applications subcategory
Line 19: Line 19:


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.
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.
[[Category:Applications]]

Revision as of 05:38, 20 September 2021

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.