toolchains.md

May 9, 2026 ยท View on GitHub

Rules to declare Zig toolchains.

zig_path_toolchain

load("@rules_zig//zig:toolchain.bzl", "zig_path_toolchain")

zig_path_toolchain(name, zig_cache, zig_exe_path, zig_lib_path, zig_version)

Defines a non-hermetic Zig compiler toolchain from absolute paths.

Use this rule when Zig is installed outside Bazel and cannot be exposed as Bazel files. The executable and library directory paths must be absolute and available on every execution machine that can run actions using this toolchain.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
zig_cacheThe Zig cache directory prefix. Used for both the global and local cache.Stringrequired
zig_exe_pathAbsolute path to an existing Zig executable for the target platform.Stringrequired
zig_lib_pathAbsolute path to an existing Zig library directory for the target platform.Stringrequired
zig_versionThe Zig toolchain's version.Stringrequired

zig_target_toolchain

load("@rules_zig//zig:toolchain.bzl", "zig_target_toolchain")

zig_target_toolchain(name, dynamic_linker, target)

Defines a Zig target configuration toolchain.

The Zig compiler toolchain, defined by the zig_toolchain rule, has builtin cross-compilation support. Meaning, most Zig toolchains can target any platform supported by Zig independent of the execution platform.

Therefore, there is no need to couple the execution platform with the target platform, at least not by default.

Use this rule to configure a Zig target platform and declare the corresponding Bazel target platform constraints using the builtin toolchain rule.

Use the target @rules_zig//zig/target:resolved_toolchain to access the resolved toolchain for the current target platform. You can build this target to obtain a JSON file capturing the relevant Zig compiler flags.

See https://bazel.build/extending/toolchains#defining-toolchains.

EXAMPLE

zig_target_toolchain(
    name = "x86_64-linux",
    target = "x86_64-linux",
)

toolchain(
    name = "x86_64-linux_toolchain",
    target_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
    toolchain = ":x86_64-linux",
    toolchain_type = "@rules_zig//zig/target:toolchain_type",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
dynamic_linkerThe value of the --dynamic-linker flag.Stringoptional""
targetThe value of the -target flag.Stringrequired

zig_toolchain

load("@rules_zig//zig:toolchain.bzl", "zig_toolchain")

zig_toolchain(name, zig_cache, zig_exe, zig_h, zig_lib, zig_version)

Defines a Zig compiler toolchain.

The Zig compiler toolchain, defined by the zig_toolchain rule, has builtin cross-compilation support. Meaning, most Zig toolchains can target any platform supported by Zig independent of the execution platform.

Therefore, there is no need to couple the execution platform with the target platform, at least not by default.

This rule configures a Zig compiler toolchain and the corresponding Bazel execution platform constraints can be declared using the builtin toolchain rule.

You will rarely need to invoke this rule directly. Instead, use the zig module extension provided by @rules_zig//zig:extensions.bzl.

Use the target @rules_zig//zig:resolved_toolchain to access the resolved toolchain for the current execution platform.

See https://bazel.build/extending/toolchains#defining-toolchains.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
zig_cacheThe Zig cache directory prefix. Used for both the global and local cache.Stringrequired
zig_exeA hermetically downloaded Zig executable for the target platform.Labelrequired
zig_hThe Zig header at the root of the Zig library directory.Labelrequired
zig_libA source directory containing the hermetic Zig library for the target platform.Labelrequired
zig_versionThe Zig toolchain's version.Stringrequired