Skip to content

Latest commit

 

History

History
275 lines (198 loc) · 6.21 KB

README.md

File metadata and controls

275 lines (198 loc) · 6.21 KB

Rules for importing a Go toolchain from Nixpkgs.

Rules

Reference documentation

nixpkgs_go_configure

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.

Example

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()

Parameters

sdk_name

optional. default is "go_sdk"

Go sdk name to pass to rules_go

repository

optional. default is None

A repository label identifying which Nixpkgs to use. Equivalent to repositories = { "nixpkgs": ...}.

repositories

optional. default is {}

A dictionary mapping NIX_PATH entries to repository labels.

Setting it to

repositories = { "myrepo" : "//:myrepo" }

for example would replace all instances of <myrepo> in the called nix code by the path to the target "//:myrepo". See the relevant section in the nix manual for more information.

Specify one of path or repositories.

attribute_path

optional. default is "go"

The nixpkgs attribute path for the go to use.

nix_file

optional. 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 package. Takes precedence over attribute_path.

nix_file_deps

optional. default is []

Dependencies of nix_file if any.

nix_file_content

optional. default is None

An expression for a Nix environment derivation. Takes precedence over attribute_path.

nixopts

optional. default is []

fail_not_supported

optional. default is True

See nixpkgs_package.

quiet

optional. default is False

Whether to hide the output of the Nix command.

register

optional. default is True

Automatically register the generated toolchain if set to True.

rules_go_repo_name

optional. default is "io_bazel_rules_go"

The name of the rules_go repository. Defaults to rules_go under bzlmod and io_bazel_rules_go otherwise.",