Skip to content

Conversation

@oliverlee
Copy link

Thanks for providing these rules for Nixpkgs.

This PR is largely driven by my desire to get a hermetic GCC toolchain on macOS and include the following changes:

  • determine coverage flags based on cc.isClang instead of stdenv.isDarwin
  • allow user override of the C++ standard library to link against
  • allow user override of the toolchain workspace name
  • allow user to avoid use of the support_nix constraint. This can be useful if some toolchains are provided by Nix and other toolchains are not.
  • bake flags into some toolchain config attributes. Some options may only exist in one toolchain flavor or in newer version of the toolchain. Setting flags in a .bazelrc file can be problematic with a project that is building with many different versions of GCC and Clang as e.g. copts will persist after a transition on extra_toolchains.

Since _nixpkgs_cc_toolchain_config uses unix_cc_toolchain_config, I need to disable the macos_default_link_flags feature as mentioned in this issue.

I've only tested this with Bazel 8.2 and I haven't actually looked at any output with bazel coverage. The supported keys for extra_toolchain_flags are ones that I thought made sense but those could be changed.

Here's a usage example which is based on the in-repo example:

load("@rules_nixpkgs_cc//:cc.bzl", "nixpkgs_cc_configure")

def _cc_configure_impl(mctx):
    nixpkgs_cc_configure(
        name = "nixpkgs_config_gcc",
        repository = "@nixpkgs",
        # GCC 15 does not build on darwin-aarch64:
        # https://github.com/NixOS/nixpkgs/issues/425992
        nix_file_content = """
let
  pkgs = import <nixpkgs> {};
  gcc = pkgs.gcc14;
in
  (pkgs.buildEnv {
    name = "gcc-toolchain";
    paths = [
      gcc
    ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
      pkgs.darwin.cctools
    ];
    pathsToLink = [
        "/bin"
        "/lib"
        "/lib64"
        "/libexec"
        "/include"
    ];
  }) // {
    inherit (gcc) targetPrefix isGNU isClang;
  }
        """,
        register = False,
        fail_not_supported = True,
        cc_std = "c++26",
        cc_libstd = "stdc++",
        require_support_nix = False,
        toolchain_workspace = "gcc_toolchain",
        extra_toolchain_flags = {
            "compile_flags": [
                "-D_GLIBCXX_ASSERTIONS",
                "-fdiagnostics-color",
                "-Werror",
                "-Wall",
                "-Wextra",
                "-Wpedantic",
                "-Wconversion",
                "-Wnon-virtual-dtor",
                "-Wold-style-cast",
                "-Wcast-align",
                "-Wunused",
                "-Woverloaded-virtual",
                "-Wmisleading-indentation",
                "-Wnull-dereference",
                "-Wdouble-promotion",
                "-Wformat=2",
                "-Wimplicit-fallthrough",
                "-Wextra-semi",
                "-Wunreachable-code",
                "-Wno-psabi",
                "-Wno-unused-but-set-variable",
            ],
        },
    )

cc_configure = module_extension(
    implementation = _cc_configure_impl,
)

@oliverlee oliverlee requested a review from a team as a code owner December 6, 2025 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant