Skip to content

Commit 9be5902

Browse files
Enable Bazel on darwin (TraceMachina#1364)
1 parent ca84219 commit 9be5902

File tree

8 files changed

+147
-26
lines changed

8 files changed

+147
-26
lines changed

.bazelrc

+3
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,8 @@ build --extra_toolchains=@rust_toolchains//:all
8181
# Generated by the LRE flake module.
8282
try-import %workspace%/lre.bazelrc
8383

84+
# Generated by the darwin flake module.
85+
try-import %workspace%/darwin.bazelrc
86+
8487
# Allow user-side customization.
8588
try-import %workspace%/user.bazelrc

.github/workflows/nix.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
os: [ubuntu-22.04]
23+
os: [ubuntu-22.04, macos-13, macos-14]
2424
name: Bazel Dev / ${{ matrix.os }}
2525
runs-on: ${{ matrix.os }}
2626
timeout-minutes: 45

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ trivy-results.sarif
1919
Pulumi.dev.yaml
2020
lre.bazelrc
2121
rust-project.json
22+
darwin.bazelrc

flake.nix

+33-22
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
imports = [
4242
inputs.git-hooks.flakeModule
4343
./local-remote-execution/flake-module.nix
44+
./tools/darwin/flake-module.nix
4445
];
4546
perSystem = {
4647
config,
@@ -69,6 +70,7 @@
6970
stable-rust-native = pkgs.rust-bin.stable.${stable-rust-version};
7071

7172
maybeDarwinDeps = pkgs.lib.optionals pkgs.stdenv.isDarwin [
73+
pkgs.darwin.apple_sdk.frameworks.CoreFoundation
7274
pkgs.darwin.apple_sdk.frameworks.Security
7375
pkgs.libiconv
7476
];
@@ -474,31 +476,40 @@
474476
pkgs.deno
475477
];
476478

477-
shellHook = ''
478-
# Generate the .pre-commit-config.yaml symlink when entering the
479-
# development shell.
480-
${config.pre-commit.installationScript}
481-
482-
# Generate lre.bazelrc which configures LRE toolchains when running
483-
# in the nix environment.
484-
${config.local-remote-execution.installationScript}
485-
486-
# The Bazel and Cargo builds in nix require a Clang toolchain.
487-
# TODO(aaronmondal): The Bazel build currently uses the
488-
# irreproducible host C++ toolchain. Provide
489-
# this toolchain via nix for bitwise identical
490-
# binaries across machines.
491-
export CC=clang
492-
export PULUMI_K8S_AWAIT_ALL=true
493-
export PLAYWRIGHT_BROWSERS_PATH=${pkgs.playwright-driver.browsers}
494-
export PLAYWRIGHT_NODEJS_PATH=${pkgs.nodePackages_latest.nodejs}
495-
export PATH=$HOME/.deno/bin:$PATH
496-
deno types > web/platform/utils/deno.d.ts
497-
'';
479+
shellHook =
480+
''
481+
# Generate the .pre-commit-config.yaml symlink when entering the
482+
# development shell.
483+
${config.pre-commit.installationScript}
484+
485+
# Generate lre.bazelrc which configures LRE toolchains when running
486+
# in the nix environment.
487+
${config.local-remote-execution.installationScript}
488+
489+
# The Bazel and Cargo builds in nix require a Clang toolchain.
490+
# TODO(aaronmondal): The Bazel build currently uses the
491+
# irreproducible host C++ toolchain. Provide
492+
# this toolchain via nix for bitwise identical
493+
# binaries across machines.
494+
export CC=clang
495+
export PULUMI_K8S_AWAIT_ALL=true
496+
export PLAYWRIGHT_BROWSERS_PATH=${pkgs.playwright-driver.browsers}
497+
export PLAYWRIGHT_NODEJS_PATH=${pkgs.nodePackages_latest.nodejs}
498+
export PATH=$HOME/.deno/bin:$PATH
499+
deno types > web/platform/utils/deno.d.ts
500+
''
501+
+ (pkgs.lib.optionalString pkgs.stdenv.isDarwin ''
502+
# On Darwin generate darwin.bazelrc which configures
503+
# darwin libs & frameworks when running in the nix environment.
504+
${config.darwin.installationScript}
505+
'');
498506
};
499507
};
500508
}
501509
// {
502-
flakeModule = ./local-remote-execution/flake-module.nix;
510+
flakeModule = {
511+
default = ./local-remote-execution/flake-module.nix;
512+
darwin = ./tools/darwin/flake-module.nix;
513+
};
503514
};
504515
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
load("@rules_cc//cc:defs.bzl", "cc_binary")
22

3+
# This example matches the config of the lre platform
34
cc_binary(
45
name = "hello_lre",
56
srcs = ["hello.cpp"],
67
copts = ["--verbose"],
8+
target_compatible_with = [
9+
"@platforms//os:linux",
10+
"@platforms//cpu:x86_64",
11+
],
712
)

tools/darwin/flake-module.nix

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
lib,
3+
flake-parts-lib,
4+
...
5+
}: {
6+
options = {
7+
perSystem = flake-parts-lib.mkPerSystemOption (
8+
{
9+
config,
10+
options,
11+
pkgs,
12+
...
13+
}: let
14+
cfg = config.darwin;
15+
in {
16+
options = {
17+
darwin = {
18+
pkgs = lib.mkOption {
19+
type = lib.types.uniq (lib.types.lazyAttrsOf (lib.types.raw or lib.types.unspecified));
20+
description = "Nixpkgs to use.";
21+
default = pkgs;
22+
defaultText = lib.literalMD "`pkgs` (module argument)";
23+
};
24+
settings = lib.mkOption {
25+
type = lib.types.submoduleWith {
26+
modules = [./modules/darwin.nix];
27+
specialArgs = {inherit (cfg) pkgs;};
28+
};
29+
default = {};
30+
description = "Configuration for Bazel on Darwin.";
31+
};
32+
installationScript = lib.mkOption {
33+
type = lib.types.str;
34+
description = "Create darwin.bazelrc.";
35+
default = cfg.settings.installationScript;
36+
defaultText = lib.literalMD "bazelrc content";
37+
readOnly = true;
38+
};
39+
};
40+
};
41+
}
42+
);
43+
};
44+
}

tools/darwin/modules/darwin.nix

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
config,
3+
lib,
4+
pkgs,
5+
...
6+
}: let
7+
# Conditionally set the bazelrc only if the target system is Darwin (macOS)
8+
bazelrc =
9+
if pkgs.stdenv.isDarwin
10+
then
11+
pkgs.writeText "darwin.bazelrc" ''
12+
# These flags are dynamically generated by the Darwin flake module.
13+
#
14+
# Add `try-import %%workspace%%/darwin.bazelrc` to your .bazelrc to
15+
# include these flags when running Bazel in a nix environment.
16+
# These are the libs and frameworks used by darwin.
17+
18+
build --@rules_rust//:extra_rustc_flags=-L${pkgs.libiconv}/lib,-Lframework=${pkgs.darwin.Security}/Library/Frameworks,-Lframework=${pkgs.darwin.CF}/Library/Frameworks
19+
build --@rules_rust//:extra_exec_rustc_flags=-L${pkgs.libiconv}/lib,-Lframework=${pkgs.darwin.Security}/Library/Frameworks,-Lframework=${pkgs.darwin.CF}/Library/Frameworks
20+
''
21+
else "";
22+
in {
23+
options = {
24+
installationScript = lib.mkOption {
25+
type = lib.types.str;
26+
description = "A bash snippet which creates a darwin.bazelrc file in the
27+
repository.";
28+
};
29+
};
30+
config = {
31+
installationScript = ''
32+
if ! type -t git >/dev/null; then
33+
# In pure shells
34+
echo 1>&2 "WARNING: Darwin: git command not found; skipping installation."
35+
elif ! ${pkgs.git}/bin/git rev-parse --git-dir &> /dev/null; then
36+
echo 1>&2 "WARNING: Darwin: .git not found; skipping installation."
37+
else
38+
GIT_WC=`${pkgs.git}/bin/git rev-parse --show-toplevel`
39+
40+
# These update procedures compare before they write, to avoid
41+
# filesystem churn. This improves performance with watch tools like
42+
# lorri and prevents installation loops by lorri.
43+
44+
if ! readlink "''${GIT_WC}/darwin.bazelrc" >/dev/null \
45+
|| [[ $(readlink "''${GIT_WC}/darwin.bazelrc") != ${bazelrc} ]]; then
46+
echo 1>&2 "Darwin: updating $PWD repository"
47+
[ -L darwin.bazelrc ] && unlink darwin.bazelrc
48+
49+
if [ -e "''${GIT_WC}/darwin.bazelrc" ]; then
50+
echo 1>&2 "Darwin: WARNING: Refusing to install because of pre-existing darwin.bazelrc"
51+
echo 1>&2 " Remove the darwin.bazelrc file and add darwin.bazelrc to .gitignore."
52+
else
53+
ln -fs ${bazelrc} "''${GIT_WC}/darwin.bazelrc"
54+
fi
55+
fi
56+
fi
57+
'';
58+
};
59+
}

web/platform/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,4 @@ bun preview
5858

5959
## 🐛 Known issues
6060

61-
- `bun run docs.build` doesn't work on MacOS with the nix version of Bazel.
62-
As a workaround install Bun and Bazel on your host and build the docs outside the flake.
63-
- `bun dev` doesn't reload the changes in the starlight.conf.ts
61+
- `bun dev` doesn't hot reload the changes in the starlight.conf.ts

0 commit comments

Comments
 (0)