Rules for importing a Go toolchain from Nixpkgs.
nixpkgs_go_configure(sdk_name, repository, repositories, attribute_path, nix_file, nix_file_deps, nix_file_content, nixopts, fail_not_supported, quiet, register, rules_go_repo_name)
Use go toolchain from Nixpkgs.
By default rules_go configures the go toolchain to be downloaded as binaries (which doesn't work on NixOS).
There is a way to tell rules_go to look into environment and find local go binary which is not hermetic.
This command allows to setup a hermetic go sdk from Nixpkgs, which should be considered as best practice.
Cross toolchains are declared and registered for each entry in the PLATFORMS
constant in rules_go
.
Note that the nix package must provide a full go sdk at the root of the package instead of in $out/share/go
,
and also provide an empty normal file named ROOT
at the root of package.
nixpkgs_go_configure(repository = "@nixpkgs//:default.nix")
Example (optional nix support when go is a transitive dependency):
# .bazel-lib/nixos-support.bzl
def _has_nix(ctx):
return ctx.which("nix-build") != None
def _gen_imports_impl(ctx):
ctx.file("BUILD", "")
imports_for_nix = """
load("@io_tweag_rules_nixpkgs//nixpkgs:toolchains/go.bzl", "nixpkgs_go_configure")
def fix_go():
nixpkgs_go_configure(repository = "@nixpkgs")
"""
imports_for_non_nix = """
def fix_go():
# if go isn't transitive you'll need to add call to go_register_toolchains here
pass
"""
if _has_nix(ctx):
ctx.file("imports.bzl", imports_for_nix)
else:
ctx.file("imports.bzl", imports_for_non_nix)
_gen_imports = repository_rule(
implementation = _gen_imports_impl,
attrs = dict(),
)
def gen_imports():
_gen_imports(
name = "nixos_support",
)
# WORKSPACE
// ...
http_archive(name = "io_tweag_rules_nixpkgs", ...)
// ...
local_repository(
name = "bazel_lib",
path = ".bazel-lib",
)
load("@bazel_lib//:nixos-support.bzl", "gen_imports")
gen_imports()
load("@nixos_support//:imports.bzl", "fix_go")
fix_go()
sdk_name |
optional.
default is
Go sdk name to pass to rules_go |
repository |
optional.
default is
A repository label identifying which Nixpkgs to use. Equivalent to |
repositories |
optional.
default is
A dictionary mapping Setting it to
for example would replace all instances of Specify one of |
attribute_path |
optional.
default is
The nixpkgs attribute path for the |
nix_file |
optional.
default is
An expression for a Nix environment derivation. The environment should expose the whole go SDK ( |
nix_file_deps |
optional.
default is
Dependencies of |
nix_file_content |
optional.
default is
An expression for a Nix environment derivation. Takes precedence over attribute_path. |
nixopts |
optional.
default is |
fail_not_supported |
optional.
default is
See |
quiet |
optional.
default is
Whether to hide the output of the Nix command. |
register |
optional.
default is
Automatically register the generated toolchain if set to True. |
rules_go_repo_name |
optional.
default is
The name of the rules_go repository. Defaults to rules_go under bzlmod and io_bazel_rules_go otherwise.", |