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
Attribute types
Define the schema for rule attributes using theattr module:
Common parameters
All attribute types support: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
Path-based environment variables
Different languages use different path variables. Thepath_env_vars parameter handles this:
- Python
- Java
- Node.js
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.