From 9be5902582d1a7cfbe1d20bb7f01e9b85810d848 Mon Sep 17 00:00:00 2001 From: Schahin Date: Thu, 26 Sep 2024 23:27:45 +0200 Subject: [PATCH] Enable Bazel on darwin (#1364) --- .bazelrc | 3 ++ .github/workflows/nix.yaml | 2 +- .gitignore | 1 + flake.nix | 55 +++++++++++-------- local-remote-execution/examples/BUILD.bazel | 5 ++ tools/darwin/flake-module.nix | 44 +++++++++++++++ tools/darwin/modules/darwin.nix | 59 +++++++++++++++++++++ web/platform/README.md | 4 +- 8 files changed, 147 insertions(+), 26 deletions(-) create mode 100644 tools/darwin/flake-module.nix create mode 100644 tools/darwin/modules/darwin.nix diff --git a/.bazelrc b/.bazelrc index c79b1e5e4..64da15b8a 100644 --- a/.bazelrc +++ b/.bazelrc @@ -81,5 +81,8 @@ build --extra_toolchains=@rust_toolchains//:all # Generated by the LRE flake module. try-import %workspace%/lre.bazelrc +# Generated by the darwin flake module. +try-import %workspace%/darwin.bazelrc + # Allow user-side customization. try-import %workspace%/user.bazelrc diff --git a/.github/workflows/nix.yaml b/.github/workflows/nix.yaml index 7317813c4..ed9c8345a 100644 --- a/.github/workflows/nix.yaml +++ b/.github/workflows/nix.yaml @@ -20,7 +20,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-22.04] + os: [ubuntu-22.04, macos-13, macos-14] name: Bazel Dev / ${{ matrix.os }} runs-on: ${{ matrix.os }} timeout-minutes: 45 diff --git a/.gitignore b/.gitignore index 08dc37a8d..316fad008 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ trivy-results.sarif Pulumi.dev.yaml lre.bazelrc rust-project.json +darwin.bazelrc diff --git a/flake.nix b/flake.nix index 710052650..b937e0a74 100644 --- a/flake.nix +++ b/flake.nix @@ -41,6 +41,7 @@ imports = [ inputs.git-hooks.flakeModule ./local-remote-execution/flake-module.nix + ./tools/darwin/flake-module.nix ]; perSystem = { config, @@ -69,6 +70,7 @@ stable-rust-native = pkgs.rust-bin.stable.${stable-rust-version}; maybeDarwinDeps = pkgs.lib.optionals pkgs.stdenv.isDarwin [ + pkgs.darwin.apple_sdk.frameworks.CoreFoundation pkgs.darwin.apple_sdk.frameworks.Security pkgs.libiconv ]; @@ -474,31 +476,40 @@ pkgs.deno ]; - shellHook = '' - # Generate the .pre-commit-config.yaml symlink when entering the - # development shell. - ${config.pre-commit.installationScript} - - # Generate lre.bazelrc which configures LRE toolchains when running - # in the nix environment. - ${config.local-remote-execution.installationScript} - - # The Bazel and Cargo builds in nix require a Clang toolchain. - # TODO(aaronmondal): The Bazel build currently uses the - # irreproducible host C++ toolchain. Provide - # this toolchain via nix for bitwise identical - # binaries across machines. - export CC=clang - export PULUMI_K8S_AWAIT_ALL=true - export PLAYWRIGHT_BROWSERS_PATH=${pkgs.playwright-driver.browsers} - export PLAYWRIGHT_NODEJS_PATH=${pkgs.nodePackages_latest.nodejs} - export PATH=$HOME/.deno/bin:$PATH - deno types > web/platform/utils/deno.d.ts - ''; + shellHook = + '' + # Generate the .pre-commit-config.yaml symlink when entering the + # development shell. + ${config.pre-commit.installationScript} + + # Generate lre.bazelrc which configures LRE toolchains when running + # in the nix environment. + ${config.local-remote-execution.installationScript} + + # The Bazel and Cargo builds in nix require a Clang toolchain. + # TODO(aaronmondal): The Bazel build currently uses the + # irreproducible host C++ toolchain. Provide + # this toolchain via nix for bitwise identical + # binaries across machines. + export CC=clang + export PULUMI_K8S_AWAIT_ALL=true + export PLAYWRIGHT_BROWSERS_PATH=${pkgs.playwright-driver.browsers} + export PLAYWRIGHT_NODEJS_PATH=${pkgs.nodePackages_latest.nodejs} + export PATH=$HOME/.deno/bin:$PATH + deno types > web/platform/utils/deno.d.ts + '' + + (pkgs.lib.optionalString pkgs.stdenv.isDarwin '' + # On Darwin generate darwin.bazelrc which configures + # darwin libs & frameworks when running in the nix environment. + ${config.darwin.installationScript} + ''); }; }; } // { - flakeModule = ./local-remote-execution/flake-module.nix; + flakeModule = { + default = ./local-remote-execution/flake-module.nix; + darwin = ./tools/darwin/flake-module.nix; + }; }; } diff --git a/local-remote-execution/examples/BUILD.bazel b/local-remote-execution/examples/BUILD.bazel index d96d0477a..b7efedad8 100644 --- a/local-remote-execution/examples/BUILD.bazel +++ b/local-remote-execution/examples/BUILD.bazel @@ -1,7 +1,12 @@ load("@rules_cc//cc:defs.bzl", "cc_binary") +# This example matches the config of the lre platform cc_binary( name = "hello_lre", srcs = ["hello.cpp"], copts = ["--verbose"], + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], ) diff --git a/tools/darwin/flake-module.nix b/tools/darwin/flake-module.nix new file mode 100644 index 000000000..0460ae70f --- /dev/null +++ b/tools/darwin/flake-module.nix @@ -0,0 +1,44 @@ +{ + lib, + flake-parts-lib, + ... +}: { + options = { + perSystem = flake-parts-lib.mkPerSystemOption ( + { + config, + options, + pkgs, + ... + }: let + cfg = config.darwin; + in { + options = { + darwin = { + pkgs = lib.mkOption { + type = lib.types.uniq (lib.types.lazyAttrsOf (lib.types.raw or lib.types.unspecified)); + description = "Nixpkgs to use."; + default = pkgs; + defaultText = lib.literalMD "`pkgs` (module argument)"; + }; + settings = lib.mkOption { + type = lib.types.submoduleWith { + modules = [./modules/darwin.nix]; + specialArgs = {inherit (cfg) pkgs;}; + }; + default = {}; + description = "Configuration for Bazel on Darwin."; + }; + installationScript = lib.mkOption { + type = lib.types.str; + description = "Create darwin.bazelrc."; + default = cfg.settings.installationScript; + defaultText = lib.literalMD "bazelrc content"; + readOnly = true; + }; + }; + }; + } + ); + }; +} diff --git a/tools/darwin/modules/darwin.nix b/tools/darwin/modules/darwin.nix new file mode 100644 index 000000000..aaaf18f1f --- /dev/null +++ b/tools/darwin/modules/darwin.nix @@ -0,0 +1,59 @@ +{ + config, + lib, + pkgs, + ... +}: let + # Conditionally set the bazelrc only if the target system is Darwin (macOS) + bazelrc = + if pkgs.stdenv.isDarwin + then + pkgs.writeText "darwin.bazelrc" '' + # These flags are dynamically generated by the Darwin flake module. + # + # Add `try-import %%workspace%%/darwin.bazelrc` to your .bazelrc to + # include these flags when running Bazel in a nix environment. + # These are the libs and frameworks used by darwin. + + build --@rules_rust//:extra_rustc_flags=-L${pkgs.libiconv}/lib,-Lframework=${pkgs.darwin.Security}/Library/Frameworks,-Lframework=${pkgs.darwin.CF}/Library/Frameworks + build --@rules_rust//:extra_exec_rustc_flags=-L${pkgs.libiconv}/lib,-Lframework=${pkgs.darwin.Security}/Library/Frameworks,-Lframework=${pkgs.darwin.CF}/Library/Frameworks + '' + else ""; +in { + options = { + installationScript = lib.mkOption { + type = lib.types.str; + description = "A bash snippet which creates a darwin.bazelrc file in the + repository."; + }; + }; + config = { + installationScript = '' + if ! type -t git >/dev/null; then + # In pure shells + echo 1>&2 "WARNING: Darwin: git command not found; skipping installation." + elif ! ${pkgs.git}/bin/git rev-parse --git-dir &> /dev/null; then + echo 1>&2 "WARNING: Darwin: .git not found; skipping installation." + else + GIT_WC=`${pkgs.git}/bin/git rev-parse --show-toplevel` + + # These update procedures compare before they write, to avoid + # filesystem churn. This improves performance with watch tools like + # lorri and prevents installation loops by lorri. + + if ! readlink "''${GIT_WC}/darwin.bazelrc" >/dev/null \ + || [[ $(readlink "''${GIT_WC}/darwin.bazelrc") != ${bazelrc} ]]; then + echo 1>&2 "Darwin: updating $PWD repository" + [ -L darwin.bazelrc ] && unlink darwin.bazelrc + + if [ -e "''${GIT_WC}/darwin.bazelrc" ]; then + echo 1>&2 "Darwin: WARNING: Refusing to install because of pre-existing darwin.bazelrc" + echo 1>&2 " Remove the darwin.bazelrc file and add darwin.bazelrc to .gitignore." + else + ln -fs ${bazelrc} "''${GIT_WC}/darwin.bazelrc" + fi + fi + fi + ''; + }; +} diff --git a/web/platform/README.md b/web/platform/README.md index 601a7ce57..809726e15 100644 --- a/web/platform/README.md +++ b/web/platform/README.md @@ -58,6 +58,4 @@ bun preview ## 🐛 Known issues -- `bun run docs.build` doesn't work on MacOS with the nix version of Bazel. - As a workaround install Bun and Bazel on your host and build the docs outside the flake. -- `bun dev` doesn't reload the changes in the starlight.conf.ts +- `bun dev` doesn't hot reload the changes in the starlight.conf.ts