Skip to content

Commit

Permalink
Merge pull request #385 from tweag/ylecornec/go_toolchain_version
Browse files Browse the repository at this point in the history
go 1.20 support
  • Loading branch information
mergify[bot] authored May 15, 2023
2 parents e4db4da + b3776b6 commit ad48692
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions toolchains/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ default is <code>None</code>
<p>
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.
</p>
</td>
Expand All @@ -181,7 +181,7 @@ An expression for a Nix environment derivation. The environment should expose th
<td>
optional.
default is <code>None</code>
default is <code>[]</code>
<p>
Expand Down
51 changes: 32 additions & 19 deletions toolchains/go/go.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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/**"],
),
)
Expand All @@ -91,6 +92,7 @@ def go_sdk_for_arch():
srcs = [":srcs"],
tools = [":tools"],
go = "bin/go{exe}",
version = go_version,
)
"""

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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": ...}`.
Expand All @@ -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 <nixpkgs> {{ 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(
Expand All @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions toolchains/go/go.nix
Original file line number Diff line number Diff line change
@@ -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 <nixpkgs>

let
pkgs = import <nixpkgs> {
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 ];
}

0 comments on commit ad48692

Please sign in to comment.