Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ common --action_env="BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1"
common --announce_rc
common --color=yes
common --curses=yes
common --incompatible_strict_action_env
common --ui_actions_shown=20
common --progress_in_terminal_title
common --enable_platform_specific_config
Expand Down Expand Up @@ -40,4 +39,5 @@ test --test_output=errors
test --test_summary=detailed
test --test_verbose_timeout_warnings

try-import %workspace%/.nixos-autoconfig/bazelrc
try-import %workspace%/user.bazelrc
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@

# ignore Nix out links to dev shell application
/.bazel-wrapper/

# ignore NixOS specific bazelrc
/.nixos-autoconfig/
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ transition_config_test(
# developing

Developing requires `bash`, `nix`, and coreutils on the `$PATH` to bootstrap
the Nix shell application used in `tools/bazel`.
the Nix shell application used in `tools/bazel`. On NixOS, [nix-ld][nix-ld] is
required.

[nix-ld]: https://github.com/nix-community/nix-ld
3 changes: 3 additions & 0 deletions example/minimal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "minimal",
srcs = ["main.cpp"],
# This fails since the only registered host toolchain is from
# toolchains_llvm which doesn't currently work on NixOS.
tags = ["manual"],
)
6 changes: 6 additions & 0 deletions example/semihosting/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "binary",
srcs = ["main.cpp"],
# This fails since the only registered host toolchain is from
# toolchains_llvm which doesn't currently work on NixOS.
tags = ["manual"],
)

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

transition_config_binary(
Expand Down
3 changes: 3 additions & 0 deletions example/unit_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ load("@rules_cc//cc:defs.bzl", "cc_test")
cc_test(
name = "test",
srcs = ["main.cpp"],
# This fails since the only registered host toolchain is from
# toolchains_llvm which doesn't currently work on NixOS.
tags = ["manual"],
)

transition_config_test(
Expand Down
4 changes: 2 additions & 2 deletions tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ genrule(
name = "gen-clang-format",
outs = ["clang-format.bash"],
cmd = """
echo "#!/bin/bash" > $@
echo "#!/usr/bin/env bash" > $@
echo "cd \\$$BUILD_WORKSPACE_DIRECTORY" >> $@
echo "exec bazelisk build \\$$@ //..." >> $@
""",
Expand Down Expand Up @@ -93,7 +93,7 @@ genrule(
name = "gen-clang-tidy",
outs = ["clang-tidy.bash"],
cmd = """
echo "#!/bin/bash" > $@
echo "#!/usr/bin/env bash" > $@
echo "cd \\$$BUILD_WORKSPACE_DIRECTORY" >> $@
echo "exec bazelisk build {options} \\$${{@:-//...}}" >> $@
""".format(
Expand Down
40 changes: 29 additions & 11 deletions tools/bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,35 @@ bazel_wrapper_hash=$(cat "${bazel_wrapper}"/* | sha1sum | cut -d' ' -f1)
hashed_out_link="${workspace_root}/.bazel-wrapper/${bazel_wrapper_hash}"

if [[ ! -e "$hashed_out_link" ]]; then
rm -rf "${workspace_root}/.bazel-wrapper"
store_path=$(
nix \
--option warn-dirty false \
build \
--verbose \
--out-link "$hashed_out_link" \
--print-out-paths \
"path:$bazel_wrapper"
)
echo "caching store path $store_path to $hashed_out_link"
rm -rf "${workspace_root}/.bazel-wrapper"
store_path=$(
nix \
--option warn-dirty false \
build \
--verbose \
--out-link "$hashed_out_link" \
--print-out-paths \
"path:$bazel_wrapper"
)
echo "cached store path $store_path to $hashed_out_link"
fi

# NixOS doesn't have /bin/bash so we need to set --shell_executable
nixos_bazelrc_dir="${workspace_root}/.nixos-autoconfig"
if [[ ! -e /bin/bash && ! -e "${nixos_bazelrc_dir}/${bazel_wrapper_hash}" ]]; then
echo "generating/updating nixos specific bazelrc"

rm -rf "$nixos_bazelrc_dir"
nix \
--option warn-dirty false \
build \
--verbose \
--out-link "${nixos_bazelrc_dir}/${bazel_wrapper_hash}" \
"path:${bazel_wrapper}#nixos-bazelrc"
cat > ${nixos_bazelrc_dir}/bazelrc << EOF
import %workspace%/.nixos-autoconfig/${bazel_wrapper_hash}
EOF

fi

exec "$hashed_out_link/bin/bazel-wrapper" "$@"
8 changes: 7 additions & 1 deletion tools/bazel-wrapper/flake.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
description = "flake defining the repo dev shell";
description = "flake defining files needed to bootstrap bazelisk";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05";
Expand Down Expand Up @@ -28,6 +28,7 @@
bash
bazelisk
coreutils
diffutils
findutils
gnugrep
gnused
Expand All @@ -36,8 +37,13 @@
++ lib.optionals stdenv.isDarwin [
darwin.cctools
];
rc_line = ''
common --shell_executable ${pkgs.lib.getExe pkgs.bash}
'';
in
{
nixos-bazelrc = pkgs.writeText "nixos-${system}.bazelrc" "${rc_line}";

default = pkgs.writeShellApplication {
name = "bazel-wrapper";
runtimeInputs = tools;
Expand Down
Loading