native.define_rule. Custom rules are written in the RBS DSL and can be shared across projects.
Defining a rule
Usenative.define_rule to create a new rule type:
Convenience wrappers
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Rule name — exported as a function in BUILD files |
kind | string | No | Classifier (e.g., "binary", "library", "test") |
implementation | function | No | The build logic function receiving ctx |
toolchain | string | No | Toolchain type this rule uses |
attrs | dict | No | Attribute schema for the rule |
fragments | list | No | Required configuration fragments |
Attribute types
Define the schema for rule attributes using theattr module:
| Function | Description |
|---|---|
attr.string() | String attribute |
attr.string_list() | List of strings |
attr.string_dict() | Dictionary of strings |
attr.bool() | Boolean attribute |
attr.int() | Integer attribute |
attr.label() | Reference to another target |
attr.label_list() | List of target references |
attr.dict() | Generic dictionary |
attr.list() | Generic list |
Common parameters
All attribute types support:| Parameter | Type | Default | Description |
|---|---|---|---|
mandatory | bool | False | Whether the attribute is required |
default | any | None | Default value if not specified |
doc | string | "" | Documentation string |
The is_dep parameter
For attr.label() and attr.label_list(), use is_dep = True to mark the attribute as a build dependency. This ensures the referenced target is built before the current rule.
The context object (ctx)
The implementation function receives a ctx object with access to build inputs, outputs, and actions.
Basic information
ctx.actions — Execute commands
ctx.bin — Hermetic output paths (recommended)
Provides per-target, package-isolated output paths for hermetic builds:
ctx.bin.local_dep — Resolve local dependency paths
ctx.bin.external_dep — Resolve external dependency paths
ctx.runfiles — Manage runtime files
ctx.tools — Copy toolchains
ctx.external_deps — Manage external packages
ctx.file — File operations
ctx.dir — Directory operations
ctx.json — JSON operations
ctx.http — HTTP operations
ctx.archive — Archive operations
Launcher scripts — native.create_launcher()
Generate cross-platform launcher scripts for binaries and tests. This is the recommended way to create executable wrappers.
Basic usage
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
output_path | string | Yes | Path to write the launcher script |
executable | string | No | Command to execute |
args | list | No | Arguments for the executable |
env | dict | No | Environment variables |
path_env_vars | dict | No | Path-based env vars with separators |
toolchain_paths | list | No | Glob patterns for toolchain bin dirs |
workdir | string | No | Working directory |
pre_commands | list | No | Shell commands to run before exec |
condition | string | No | Shell condition for conditional execution |
fallback_cmd | string | No | Command if condition fails |
Path-based environment variables
Different languages use different path variables. Thepath_env_vars parameter handles this:
- Python
- Java
- Node.js