Skip to main content
RBS is fully extensible. You can define custom build rules for any language, tool, or workflow using native.define_rule. Custom rules are written in the RBS DSL and can be shared across projects.

Defining a rule

Use native.define_rule to create a new rule type:

Convenience wrappers

Parameters

ParameterTypeRequiredDescription
namestringYesRule name — exported as a function in BUILD files
kindstringNoClassifier (e.g., "binary", "library", "test")
implementationfunctionNoThe build logic function receiving ctx
toolchainstringNoToolchain type this rule uses
attrsdictNoAttribute schema for the rule
fragmentslistNoRequired configuration fragments

Attribute types

Define the schema for rule attributes using the attr module:
FunctionDescription
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:
ParameterTypeDefaultDescription
mandatoryboolFalseWhether the attribute is required
defaultanyNoneDefault value if not specified
docstring""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

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

ParameterTypeRequiredDescription
output_pathstringYesPath to write the launcher script
executablestringNoCommand to execute
argslistNoArguments for the executable
envdictNoEnvironment variables
path_env_varsdictNoPath-based env vars with separators
toolchain_pathslistNoGlob patterns for toolchain bin dirs
workdirstringNoWorking directory
pre_commandslistNoShell commands to run before exec
conditionstringNoShell condition for conditional execution
fallback_cmdstringNoCommand if condition fails

Path-based environment variables

Different languages use different path variables. The path_env_vars parameter handles this:

Conditional execution

Complete example — Python binary rule

External dependency resolvers

Define custom resolvers to manage how packages are downloaded and cached:

Next steps

Language SDKs

See built-in rules for Python, Java, Node.js, and more.

Container Building

Learn to build OCI container images with custom rules.