Skip to content

Commit 69aa51e

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 10506d1 commit 69aa51e

File tree

9 files changed

+56
-15
lines changed

9 files changed

+56
-15
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
@@ -38,4 +37,5 @@ test --test_output=errors
3837
test --test_summary=detailed
3938
test --test_verbose_timeout_warnings
4039

40+
try-import %workspace%/.nixos-autoconfig/bazelrc
4141
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/

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
@@ -20,7 +20,7 @@ genrule(
2020
name = "gen-clang-format",
2121
outs = ["clang-format.bash"],
2222
cmd = """
23-
echo "#!/bin/bash" > $@
23+
echo "#!/usr/bin/env bash" > $@
2424
echo "cd \\$$BUILD_WORKSPACE_DIRECTORY" >> $@
2525
echo "exec bazelisk build \\$$@ //..." >> $@
2626
""",
@@ -67,7 +67,7 @@ genrule(
6767
name = "gen-clang-tidy",
6868
outs = ["clang-tidy.bash"],
6969
cmd = """
70-
echo "#!/bin/bash" > $@
70+
echo "#!/usr/bin/env bash" > $@
7171
echo "cd \\$$BUILD_WORKSPACE_DIRECTORY" >> $@
7272
echo "exec bazelisk build {options} \\$${{@:-//...}}" >> $@
7373
""".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: 8 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";
@@ -20,14 +20,21 @@
2020
bash
2121
bazelisk
2222
coreutils
23+
diffutils
2324
findutils
2425
gnugrep
26+
gnused
2527
nix
2628
]
2729
++ lib.optionals stdenv.isDarwin [
2830
darwin.cctools
2931
];
3032
in {
33+
nixos-bazelrc = pkgs.writeText
34+
"nixos-${system}.bazelrc"
35+
''
36+
common --shell_executable ${pkgs.lib.getExe pkgs.bash}
37+
'';
3138
default = pkgs.writeShellApplication {
3239
name = "bazel-wrapper";
3340
runtimeInputs = tools;

tools/shell_executable.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
common --shell_executable /nix/store/0nxvi9r5ymdlr2p24rjj9qzyms72zld1-bash-interactive-5.2p37/bin/bash

0 commit comments

Comments
 (0)