diff --git a/CHANGELOG.md b/CHANGELOG.md index 62f0f58a5..b194c5f85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ### Added - nixpkgs_nodejs_configure_platforms for platform transparent npm_install See [#309] + +### Breaking changes +- rules_nixpkgs_go: Custom derivations passed to `nixpkgs_go_configure` (via `nix-file`, `nix-file-content` or `attribute_path`) must now contain a `version` attribute. ## [0.9.0] - 2022-07-19 diff --git a/toolchains/go/README.md b/toolchains/go/README.md index be254f1f4..2d49ebb66 100644 --- a/toolchains/go/README.md +++ b/toolchains/go/README.md @@ -171,7 +171,7 @@ default is None

-An expression for a Nix environment derivation. The environment should expose the whole go SDK (`bin`, `src`, ...) at the root of package. It also must contain a `ROOT` file in the root of pacakge. Takes precedence over attribute_path. +An expression for a Nix environment derivation. The environment should expose the whole go SDK (`bin`, `src`, ...) at the root of package. It also must contain a `ROOT` file in the root of package. Takes precedence over attribute_path.

@@ -181,7 +181,7 @@ An expression for a Nix environment derivation. The environment should expose th optional. -default is None +default is []

diff --git a/toolchains/go/go.bzl b/toolchains/go/go.bzl index 96c5d8605..cb0f26ad8 100644 --- a/toolchains/go/go.bzl +++ b/toolchains/go/go.bzl @@ -71,11 +71,12 @@ def _detect_host_platform(ctx): go_helpers_build = """ load("@{rules_go}//go:def.bzl", "go_sdk") -def go_sdk_for_arch(): +def go_sdk_for_arch(go_version): native.filegroup( name = "libs", srcs = native.glob( ["pkg/{goos}_{goarch}/**/*.a"], + allow_empty = True, exclude = ["pkg/{goos}_{goarch}/**/cmd/**"], ), ) @@ -91,6 +92,7 @@ def go_sdk_for_arch(): srcs = [":srcs"], tools = [":tools"], go = "bin/go{exe}", + version = go_version, ) """ @@ -211,10 +213,11 @@ load("@{rules_go}//go/private/rules:binary.bzl", "go_tool_binary") load("@{rules_go}//go/private/rules:sdk.bzl", "package_list") load("@{rules_go}//go:def.bzl", "go_sdk") load("@{helpers}//:go_sdk.bzl", "go_sdk_for_arch") +load(":go_version.bzl", "go_version") package(default_visibility = ["//visibility:public"]) -go_sdk_for_arch() +go_sdk_for_arch(go_version) filegroup( name = "headers", @@ -265,7 +268,7 @@ def nixpkgs_go_configure( repositories = {}, attribute_path = "go", nix_file = None, - nix_file_deps = None, + nix_file_deps = [], nix_file_content = None, nixopts = [], fail_not_supported = True, @@ -343,7 +346,7 @@ def nixpkgs_go_configure( Args: sdk_name: Go sdk name to pass to rules_go attribute_path: The nixpkgs attribute path for the `go` to use. - nix_file: An expression for a Nix environment derivation. The environment should expose the whole go SDK (`bin`, `src`, ...) at the root of package. It also must contain a `ROOT` file in the root of pacakge. Takes precedence over attribute_path. + nix_file: An expression for a Nix environment derivation. The environment should expose the whole go SDK (`bin`, `src`, ...) at the root of package. It also must contain a `ROOT` file in the root of package. Takes precedence over attribute_path. nix_file_deps: Dependencies of `nix_file` if any. nix_file_content: An expression for a Nix environment derivation. Takes precedence over attribute_path. repository: A repository label identifying which Nixpkgs to use. Equivalent to `repositories = { "nixpkgs": ...}`. @@ -362,19 +365,30 @@ def nixpkgs_go_configure( rules_go_repo_name: The name of the rules_go repository. Defaults to rules_go under bzlmod and io_bazel_rules_go otherwise.", """ - if not nix_file and not nix_file_content: - nix_file_content = """ - with import {{ config = {{}}; overlays = []; }}; buildEnv {{ - name = "bazel-go-toolchain"; - paths = [ - {attribute_path} - ]; - postBuild = '' - touch $out/ROOT - ln -s $out/share/go/{{api,doc,lib,misc,pkg,src}} $out/ - ''; - }} - """.format(attribute_path = attribute_path) + nixopts = list(nixopts) + nix_file_deps = list(nix_file_deps) + + custom_nix_expr = None + if nix_file and nix_file_content: + fail("Cannot specify both 'nix_file' and 'nix_file_content'.") + elif nix_file: + custom_nix_expr = "import $(location {})".format(nix_file) + nix_file_deps.append(nix_file) + elif nix_file_content: + custom_nix_expr = nix_file_content + + if custom_nix_expr: + nixopts.extend([ + "--arg", + "goExpr", + custom_nix_expr, + ]) + else: + nixopts.extend([ + "--argstr", + "goAttrPath", + attribute_path, + ]) helpers_repo = sdk_name + "_helpers" nixpkgs_go_helpers( @@ -385,9 +399,8 @@ def nixpkgs_go_configure( name = sdk_name, repository = repository, repositories = repositories, - nix_file = nix_file, + nix_file = "@rules_nixpkgs_go//:go.nix", nix_file_deps = nix_file_deps, - nix_file_content = nix_file_content, build_file_content = go_sdk_build.format( rules_go = rules_go_repo_name, helpers = helpers_repo, diff --git a/toolchains/go/go.nix b/toolchains/go/go.nix new file mode 100644 index 000000000..f559c356a --- /dev/null +++ b/toolchains/go/go.nix @@ -0,0 +1,38 @@ +# Exactly one of goAttrPath or goExpr must be set. +# If a custom `goExpr` derivation is provided by the user, we add the `go_version.bzl` file to it. +# Otherwise we create a toolchain based on the `goAttrPath` attribute path from + +let + pkgs = import { + config = { }; + overlays = [ ]; + }; +in args@{ goAttrPath ? null, goExpr ? null }: +let + getVersion = s: + s.version or (throw + "nix_pkgs_go_configure: the provided go derivation should contain a `version` attribute."); + + bazelGoToolchain = args.goExpr or (let + goAttr = + pkgs.lib.attrByPath (pkgs.lib.splitString "." goAttrPath) null pkgs; + in pkgs.buildEnv { + name = "bazel-go-toolchain"; + paths = [ goAttr ]; + postBuild = '' + touch $out/ROOT + ln -s $out/share/go/{api,doc,lib,misc,pkg,src} $out/ + ''; + } // { + version = getVersion goAttr; + }); + + goVersionFile = pkgs.runCommand "bazel-go-toolchain-go-version" { } '' + mkdir $out + echo 'go_version = "${getVersion bazelGoToolchain}"' >> $out/go_version.bzl + ''; + +in pkgs.symlinkJoin { + name = "bazel-go-toolchain"; + paths = [ bazelGoToolchain goVersionFile ]; +}