Skip to content

Commit 834ebd5

Browse files
committed
Fix NixOS specific build issues
/bin/bash is unavailable on NixOS by default. This breaks many bazel assumptions and causes build failures such as: ``` ... process-wrapper-legacy.cc:80: "execvp(/bin/bash, ...)": No such file or directory ... ``` By unsetting `incompatible_strict_action_env` [0], conditionally (on NixOS) setting `shell_executable` [1] to the bash in the Nix store, and by setting the `bazel-wrapper` PATH with `writeShellApplication.runtimeInputs` we are able to avoid these errors. Finally, tag several targets in //example as "manual" so they are not automatically built with the host toolchain, since this currently fails on NixOS. This should be addressed in a follow-up commit. Testing was performed using nixpkgs supplied bazelisk [3] on Luke Peterson's and Michael Schneider's NixOS machines. This itself presented issues, as this package is documented with `BEWARE: This package does not work on NixOS.` Out of the box this is true, but can be worked around with `programs.nix-ld.enable = true` [4] in `configuration.nix`. Special thanks to Oliver Lee and Michael Schneider during the final day of Nixcademy NixOS training. This was a great collaboration! [0]: https://bazel.build/reference/command-line-reference#flag--incompatible_strict_action_env [1]: https://bazel.build/reference/command-line-reference#flag--shell_executable [2]: https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeShellApplication [3]: https://github.com/NixOS/nixpkgs/blob/nixos-25.05/pkgs/by-name/ba/bazelisk/package.nix [4]: https://github.com/nix-community/nix-ld
1 parent 907742f commit 834ebd5

File tree

9 files changed

+58
-16
lines changed

9 files changed

+58
-16
lines changed

.bazelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ common --action_env="BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1"
88
common --announce_rc
99
common --color=yes
1010
common --curses=yes
11-
common --incompatible_strict_action_env
1211
common --ui_actions_shown=20
1312
common --progress_in_terminal_title
1413
common --enable_platform_specific_config
@@ -40,4 +39,5 @@ test --test_output=errors
4039
test --test_summary=detailed
4140
test --test_verbose_timeout_warnings
4241

42+
try-import %workspace%/.nixos-autoconfig/bazelrc
4343
try-import %workspace%/user.bazelrc

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77

88
# ignore Nix out links to dev shell application
99
/.bazel-wrapper/
10+
11+
# ignore NixOS specific bazelrc
12+
/.nixos-autoconfig/

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,7 @@ transition_config_test(
9494
# developing
9595

9696
Developing requires `bash`, `nix`, and coreutils on the `$PATH` to bootstrap
97-
the Nix shell application used in `tools/bazel`.
97+
the Nix shell application used in `tools/bazel`. On NixOS, [nix-ld][nix-ld] is
98+
required.
99+
100+
[nix-ld]: https://github.com/nix-community/nix-ld

example/minimal/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ load("@rules_cc//cc:defs.bzl", "cc_binary")
33
cc_binary(
44
name = "minimal",
55
srcs = ["main.cpp"],
6+
# This fails since the only registered host toolchain is from
7+
# toolchains_llvm which doesn't currently work on NixOS.
8+
tags = ["manual"],
69
)

example/semihosting/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ load("@rules_cc//cc:defs.bzl", "cc_binary")
44
cc_binary(
55
name = "binary",
66
srcs = ["main.cpp"],
7+
# This fails since the only registered host toolchain is from
8+
# toolchains_llvm which doesn't currently work on NixOS.
9+
tags = ["manual"],
710
)
811

912
# `transition_config_binary` must be run with
@@ -14,6 +17,9 @@ transition_config_binary(
1417
name = "semihosting",
1518
src = ":binary",
1619
semihosting = "enabled",
20+
# This fails since the only registered host toolchain is from
21+
# toolchains_llvm which doesn't currently work on NixOS.
22+
tags = ["manual"],
1723
)
1824

1925
transition_config_binary(

example/unit_test/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ load("@rules_cc//cc:defs.bzl", "cc_test")
44
cc_test(
55
name = "test",
66
srcs = ["main.cpp"],
7+
# This fails since the only registered host toolchain is from
8+
# toolchains_llvm which doesn't currently work on NixOS.
9+
tags = ["manual"],
710
)
811

912
transition_config_test(

tools/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ genrule(
4444
name = "gen-clang-format",
4545
outs = ["clang-format.bash"],
4646
cmd = """
47-
echo "#!/bin/bash" > $@
47+
echo "#!/usr/bin/env bash" > $@
4848
echo "cd \\$$BUILD_WORKSPACE_DIRECTORY" >> $@
4949
echo "exec bazelisk build \\$$@ //..." >> $@
5050
""",
@@ -93,7 +93,7 @@ genrule(
9393
name = "gen-clang-tidy",
9494
outs = ["clang-tidy.bash"],
9595
cmd = """
96-
echo "#!/bin/bash" > $@
96+
echo "#!/usr/bin/env bash" > $@
9797
echo "cd \\$$BUILD_WORKSPACE_DIRECTORY" >> $@
9898
echo "exec bazelisk build {options} \\$${{@:-//...}}" >> $@
9999
""".format(

tools/bazel

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,35 @@ bazel_wrapper_hash=$(cat "${bazel_wrapper}"/* | sha1sum | cut -d' ' -f1)
1818
hashed_out_link="${workspace_root}/.bazel-wrapper/${bazel_wrapper_hash}"
1919

2020
if [[ ! -e "$hashed_out_link" ]]; then
21-
rm -rf "${workspace_root}/.bazel-wrapper"
22-
store_path=$(
23-
nix \
24-
--option warn-dirty false \
25-
build \
26-
--verbose \
27-
--out-link "$hashed_out_link" \
28-
--print-out-paths \
29-
"path:$bazel_wrapper"
30-
)
31-
echo "caching store path $store_path to $hashed_out_link"
21+
rm -rf "${workspace_root}/.bazel-wrapper"
22+
store_path=$(
23+
nix \
24+
--option warn-dirty false \
25+
build \
26+
--verbose \
27+
--out-link "$hashed_out_link" \
28+
--print-out-paths \
29+
"path:$bazel_wrapper"
30+
)
31+
echo "cached store path $store_path to $hashed_out_link"
32+
fi
33+
34+
# NixOS doesn't have /bin/bash so we need to set --shell_executable
35+
nixos_bazelrc_dir="${workspace_root}/.nixos-autoconfig"
36+
if [[ ! -e /bin/bash && ! -e "${nixos_bazelrc_dir}/${bazel_wrapper_hash}" ]]; then
37+
echo "generating/updating nixos specific bazelrc"
38+
39+
rm -rf "$nixos_bazelrc_dir"
40+
nix \
41+
--option warn-dirty false \
42+
build \
43+
--verbose \
44+
--out-link "${nixos_bazelrc_dir}/${bazel_wrapper_hash}" \
45+
"path:${bazel_wrapper}#nixos-bazelrc"
46+
cat > ${nixos_bazelrc_dir}/bazelrc << EOF
47+
import %workspace%/.nixos-autoconfig/${bazel_wrapper_hash}
48+
EOF
49+
3250
fi
3351

3452
exec "$hashed_out_link/bin/bazel-wrapper" "$@"

tools/bazel-wrapper/flake.nix

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
description = "flake defining the repo dev shell";
2+
description = "flake defining files needed to bootstrap bazelisk";
33

44
inputs = {
55
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05";
@@ -28,6 +28,7 @@
2828
bash
2929
bazelisk
3030
coreutils
31+
diffutils
3132
findutils
3233
gnugrep
3334
gnused
@@ -36,8 +37,13 @@
3637
++ lib.optionals stdenv.isDarwin [
3738
darwin.cctools
3839
];
40+
rc_line = ''
41+
common --shell_executable ${pkgs.lib.getExe pkgs.bash}
42+
'';
3943
in
4044
{
45+
nixos-bazelrc = pkgs.writeText "nixos-${system}.bazelrc" "${rc_line}";
46+
4147
default = pkgs.writeShellApplication {
4248
name = "bazel-wrapper";
4349
runtimeInputs = tools;

0 commit comments

Comments
 (0)