Skip to main content

hatch rs

Hatch plugin for Rust builds

Build Status codecov License PyPI

Overview

A simple, extensible Rust build plugin for hatch.

[tool.hatch.build.hooks.hatch-rs]
verbose = true
path = "."
module = "project"

Rust artifacts and C ABI libraries

Projects can declare multiple Rust artifacts in one hook. Artifact name is the Cargo output stem used for exact file discovery, and crate-type defaults to cdylib. A destination containing {python_extension_name} packages that cdylib as a Python extension module; other Rust artifact destinations package the platform shared library name for standalone C ABI consumers.

[tool.hatch.build.hooks.hatch-rs]
verbose = true
path = "."
module = "project"
target-dir = "isolated"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project"
manifest = "Cargo.toml"
destination = "project/{python_extension_name}"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project_ffi"
manifest = "rust/Cargo.toml"
destination = "project/lib/{shared_library}"

Destination templates support {module}, {target}, {profile}, {name}, {shared_library}, {import_library}, and {python_extension_name}.

Using the Rust debug profile for editable installs

By default rust release profile is used to compile the artifacts and it can be changed using the profile option in tool.hatch.build.hooks.hatch-rs or tool.hatch.build.hooks.hatch-rs.artifacts sections.

When installing the Python project in development mode (aka editable installs) using the command pip install --editable <package>, it can be preferred to use the rust debug profile so the compilation time is faster.

Using the Rust debug profile for editable installs can be enforced by setting the editable-debug option to true either in tool.hatch.build.hooks.hatch-rs or tool.hatch.build.hooks.hatch-rs.artifacts sections.

Generated files and headers

Artifacts with command run an argv-list command and then validate explicit outputs. Outputs can be packaged into the wheel, installed as wheel shared data, or used only as required validation checks. The same outputs table can be used for generated headers, either on a command artifact or on the Rust artifact whose build produced the file.

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "generated-package-files"
command = ["python", "scripts/write_generated_files.py"]
inputs = ["scripts/write_generated_files.py"]

[[tool.hatch.build.hooks.hatch-rs.artifacts.outputs]]
source = "project/generated/package.txt"
destination = "project/generated/package.txt"
install-scheme = "package"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "public-c-header"

[[tool.hatch.build.hooks.hatch-rs.artifacts.outputs]]
source = "project/include/project.h"
destination = "include/project/project.h"
install-scheme = "shared-data"

ABI validation and artifact metadata

cdylib artifacts can validate the copied C ABI library before the wheel is finalized. The hook can check expected exported symbols, verify headers and ABI strings/macros, load the copied library with ctypes.CDLL when validate = true, run project validation commands, include Windows import libraries, and emit a package-local artifact manifest.

[tool.hatch.build.hooks.hatch-rs]
module = "project"
target-dir = "isolated"
artifact-manifest = true

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project_ffi"
manifest = "rust/Cargo.toml"
destination = "project/lib/{shared_library}"
expected-symbols = ["project_ffi_answer"]
expected-headers = ["project/include/project.h"]
expected-abi-strings = ["PROJECT_ABI_VERSION"]
validate = true
include-import-lib = true

[[tool.hatch.build.hooks.hatch-rs.artifacts.validation-commands]]
command = ["python", "scripts/validate_abi.py", "{destination}", "{header}"]

include-import-lib only packages an import library on Windows targets, where Cargo emits .dll.lib or .dll.a files for downstream native linkers.

Platform tags and cibuildwheel

Binary wheel tags are generated with packaging.tags from the resolved Rust target. Linux builds default to linux_<arch> unless AUDITWHEEL_PLAT is set by auditwheel/cibuildwheel or wheel-platform-tag is configured explicitly. Rust targets should use concrete triples such as x86_64-unknown-linux-gnu or x86_64-unknown-linux-musl; manylinux and musllinux are wheel platform tags, not Rust target triples.

For cibuildwheel, keep Cargo outputs isolated so repeated platform builds do not reuse stale artifacts from another target:

[tool.hatch.build.hooks.hatch-rs]
module = "project"
target-dir = "isolated"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project"
manifest = "Cargo.toml"
destination = "project/{python_extension_name}"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project_ffi"
manifest = "rust/Cargo.toml"
destination = "project/lib/{shared_library}"

[tool.cibuildwheel]
build = "cp311-*"
test-command = "python -c \"import project\""

When cross-building outside cibuildwheel, set wheel-platform-tag only if the final wheel platform tag is known, for example manylinux_2_28_x86_64.

Pyodide

Pyodide builds are detected from CARGO_BUILD_TARGET=wasm32-unknown-emscripten. The hook collects Cargo's .wasm cdylib, gives it CPython's Emscripten extension suffix, and uses PYODIDE_ABI_VERSION for the pyemscripten wheel platform tag. No project-specific build hook is required.

[tool.hatch.build.hooks.hatch-rs]
module = "project"
path = "."

[tool.cibuildwheel.pyodide]
xbuild-tools = ["cargo", "rustc", "rustup"]
test-command = "python -c 'import project.project'"

Rust 1.95 or newer is required to emit an Emscripten shared library for a cdylib automatically. wheel-platform-tag remains available as an explicit override when building outside Pyodide's configured environment.

Metadata

Release files for hatch-rs 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hatch-rs 0.4.0
File Size Uploaded
hatch_rs-0.4.0.tar.gz 35.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hatch-rs 0.4.0
File Interpreter ABI Platform
hatch_rs-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 86.6 kB

Release files / hatch_rs-0.4.0.tar.gz

Download URL hatch_rs-0.4.0.tar.gz
Size 35.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2d5c5852bb2e971b2960b146a328cf039ee818231400371da6925c8115c2beef
BLAKE2b-256 checksum
How to use checksums
72381973800b9f09cd8ad803e5c5d142ead523f2c6537d25a22a4f4f386e04a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / hatch_rs-0.4.0-py3-none-any.whl

Download URL hatch_rs-0.4.0-py3-none-any.whl
Size 51.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
014dc906b5d9c09366841648ad3d0d56ddb211eac1615703c97e95a3dd396d24
BLAKE2b-256 checksum
How to use checksums
7c3a026056879a1e86a44fa5c4aba79efcd45b62ed80157e3f80e506b403adc7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

0.4.2

2 release files

0.4.1

2 release files

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page