hatch-mojo
A Hatch build hook plugin that compiles Mojo sources during package builds.
Supports Python extension modules and standalone artifacts (shared libraries, executables, etc.).
Installation
uv add hatch-mojo
Quickstart
Add the hook to your pyproject.toml:
[build-system]
build-backend = "hatchling.build"
requires = ["hatchling", "hatch-mojo"]
[tool.hatch.build.targets.wheel.hooks.mojo]
targets = ["wheel"]
[[tool.hatch.build.targets.wheel.hooks.mojo.jobs]]
name = "core"
input = "src/mo/my_pkg/core.mojo"
emit = "python-extension"
module = "my_pkg._core"
include-dirs = ["src/mo"]
Then build:
hatch build -t wheel
Configuration
All configuration lives under [tool.hatch.build.targets.wheel.hooks.mojo] in pyproject.toml.
Global options
[tool.hatch.build.targets.wheel.hooks.mojo]
mojo-bin = "/opt/mojo/bin/mojo" # path to mojo binary (default: $HATCH_MOJO_BIN or "mojo")
parallel = true # compile jobs in parallel
fail-fast = true # stop on first failure (only with parallel)
clean-before-build = false # remove build-dir before compiling
clean-after-build = false # remove build-dir after compiling
skip-editable = true # skip Mojo compilation for editable installs
build-dir = "build/mojo" # working directory for compiled artifacts
targets = ["wheel"] # hatch build targets that trigger this hook
include = ["src/mo/**/*.mojo"] # git-style globs to include
exclude = ["**/experimental*.mojo"] # git-style globs to exclude
Profiles
Profiles define reusable sets of job options. Jobs reference them by name, and profile values are merged into the job config.
[tool.hatch.build.targets.wheel.hooks.mojo.profiles.default]
include-dirs = ["src/mo"]
flags = ["-I", "vendor/include"]
[[tool.hatch.build.targets.wheel.hooks.mojo.jobs]]
name = "core"
profiles = ["default"]
input = "src/mo/my_pkg/core.mojo"
emit = "python-extension"
module = "my_pkg._core"
Jobs
Each job compiles a single Mojo source into an output artifact.
Python extension:
[[tool.hatch.build.targets.wheel.hooks.mojo.jobs]]
name = "core"
input = "src/mo/my_pkg/core.mojo"
emit = "python-extension"
module = "my_pkg._core"
include-dirs = ["src/mo"]
Non-Python artifact (requires an install mapping):
[[tool.hatch.build.targets.wheel.hooks.mojo.jobs]]
name = "cli"
input = "src/mo/my_pkg/cli.mojo"
emit = "executable"
install = { kind = "scripts", path = "my-cli" }
Job options reference
| Option | Type | Description |
|---|---|---|
name |
string | Unique identifier for the job |
input |
string | Path to the Mojo source file (supports globs) |
emit |
string | python-extension, shared-lib, static-lib, object, executable |
output |
string | Explicit output path (overrides default) |
module |
string | Dotted import path (required for python-extension) |
install |
object | Install mapping for non-Python artifacts: { kind, path } |
profiles |
list | Profile names to inherit settings from |
include-dirs |
list | Additional include directories passed as -I |
defines |
list | Values passed as -D flags to the compiler |
flags |
list | Extra flags passed to mojo build |
env |
object | Environment variables set during compilation |
platforms |
list | Restrict job to specific sys.platform values |
arch |
list | Restrict job to specific platform.machine() values |
marker |
string | PEP 508 marker expression for conditional inclusion |
depends-on |
list | Job names that must complete before this one |
Supported install kinds: package, data, scripts, root, force-include
Runtime library bundling
When building wheels that include Mojo python-extensions, set bundle-libs = true to copy the Mojo runtime libraries into the wheel:
[tool.hatch.build.targets.wheel.hooks.mojo]
bundle-libs = true
This works on both Linux and macOS. On Linux, patchelf sets RPATH on each library. On macOS, install_name_tool rewrites dylib install names and inter-library references to use @rpath.
A NOTICE.mojo-runtime file is automatically included in the wheel alongside the bundled libraries, listing the bundled files and their license terms (Modular Community License). If the SDK directory contains a LICENSE file, it is also copied into the wheel as LICENSE.mojo-runtime.
cibuildwheel
When using cibuildwheel, keep the following in mind:
Linux (manylinux): The Mojo SDK requires manylinux_2_34 or newer due to GLIBCXX requirements. Standard auditwheel repair may reject the wheel — use a retag command instead:
[tool.cibuildwheel.linux]
repair-wheel-command = "python -m wheel tags --remove --platform-tag manylinux_2_34_x86_64 {wheel} && mv {wheel} {dest_dir}"
macOS: With bundle-libs = true, macOS wheels work with standard delocate. No special repair command is needed:
[tool.cibuildwheel.macos]
repair-wheel-command = "delocate-wheel -w {dest_dir} {wheel}"
macOS SIP and DYLD_LIBRARY_PATH: macOS System Integrity Protection (SIP) strips DYLD_LIBRARY_PATH from child processes. Setting it in CIBW_ENVIRONMENT will not propagate to the repair step. If you need it, pass it inline in the repair command:
[tool.cibuildwheel.macos]
repair-wheel-command = "DYLD_LIBRARY_PATH=/path/to/libs delocate-wheel -w {dest_dir} {wheel}"
libstdc++ on manylinux: If the Mojo SDK links against a newer libstdc++ than the manylinux baseline provides, you may need to bundle it from conda-forge. This is a Mojo SDK limitation, not a hatch-mojo issue.
Troubleshooting
| Error | Fix |
|---|---|
mojo executable not found |
Set mojo-bin, HATCH_MOJO_BIN, or add mojo to PATH |
No build jobs resolved |
Check input paths, include/exclude globs, and targets |
unknown dependencies |
Ensure depends-on references valid job names |
| Wheel missing binaries | Set module for extension jobs or install for non-Python jobs |
License
MIT
Release files for hatch-mojo 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hatch_mojo-0.1.8.tar.gz | 125.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hatch_mojo-0.1.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 142.9 kB
Release files / hatch_mojo-0.1.8.tar.gz
| Download URL | hatch_mojo-0.1.8.tar.gz |
|---|---|
| Size | 125.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
291d92715ccf110b5e4d793d90368a36882708a2f01775a758dc9afbf827a867
|
|
BLAKE2b-256 checksum How to use checksums |
f17ee8f0da9d47582aa34802c86d60a67d2cb2937e21cd3424d4e5bb08046f09
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 8, 2026.
Transparency logRelease files / hatch_mojo-0.1.8-py3-none-any.whl
| Download URL | hatch_mojo-0.1.8-py3-none-any.whl |
|---|---|
| Size | 17.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a928ff45fc0856c3fa5d97f3c4c9f374774311318e632319024a50e7a45591ec
|
|
BLAKE2b-256 checksum How to use checksums |
be94a6eb00d4b57c5e362e4b5cdf82331162fea192f70683e27a8e29ca37653e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 8, 2026.
Transparency log