Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pre-compiling Runtimes

The eryx-precompile tool generates platform-specific pre-compiled runtimes that dramatically speed up sandbox creation. Instead of compiling WebAssembly at startup, sandboxes load pre-compiled native code directly.

Installation

Install from GitHub Releases using cargo binstall:

cargo binstall eryx-precompile

Or download a binary directly from the GitHub Releases page.

Quick Setup

For most users, the setup subcommand handles everything:

eryx-precompile setup

This will:

  1. Download runtime-python.wasm from the matching GitHub Release and check it against the release’s runtime-python.wasm.sha256
  2. Pre-compile it to native code for your platform
  3. Cache the result in ~/.cache/eryx/, with a checksum file next to it

Running setup again reuses the cached runtime only if it still matches its checksum, so an interrupted run is repaired by running it again.

After this one-time setup, cargo build with features = ["embedded"] finds the cached runtime automatically. The build uses it only if it still matches its checksum file and, on Unix, the file, its checksum file and the cache directory are owned by you and not writable by your group or others. Otherwise it prints a warning, falls back as if no cache existed, and running setup again fixes it. Inside an eryx checkout, a runtime built in the workspace (crates/eryx-runtime/runtime-<language>.cwasm, and likewise coreutils.cwasm and eryx-bundler.cwasm) comes before the cache, so the build embeds the runtime built from those sources rather than the released one.

You can also point to a specific file:

export ERYX_RUNTIME_CWASM=/path/to/runtime-python.cwasm

A file given through ERYX_RUNTIME_CWASM is embedded as is, without those checks, so only point it at a runtime you trust.

Setup Options

# Set up the JavaScript and shell runtimes as well as Python's
eryx-precompile setup --language python --language javascript --language shell

# Check the download against a known hash instead of the release's checksum file
eryx-precompile setup --sha256 <hex>

# Download from a mirror, or a local directory served over HTTP, instead of GitHub
eryx-precompile setup --release-url http://localhost:8000

# Verbose output for debugging
eryx-precompile setup --verbose

With --release-url, the URL must serve this eryx version’s assets: setup stamps what it downloads with its own version, so an older release’s assets would be cached as current. The .sha256 files come from the same URL as the downloads, so they catch corruption but not a hostile mirror; use one you trust. --sha256 pins only the language runtime: with --language shell, coreutils is always checked against the mirror’s checksum, so it is only as trustworthy as the mirror.

--language shell also sets up the shell’s embedded commands: it downloads coreutils.wasm, checks it against coreutils.wasm.sha256, and caches it as coreutils-v<version>-<arch>-<os>.cwasm. The shell feature turns on coreutils, which embeds them, so a build with features = ["embedded", "shell"] needs both. --sha256 only applies to the runtime; coreutils is always checked against its own checksum file.

--bundler sets up the JavaScript bundler that eryx’s env-pack feature embeds to pack environments: it downloads eryx-bundler.wasm, checks it against eryx-bundler.wasm.sha256, and caches it as eryx-bundler-v<version>-<arch>-<os>.cwasm, so a crates.io build with env-pack doesn’t need the bundler built from source. Alone (eryx-precompile setup --bundler) it sets up only the bundler; with --language, both.

A precompiled runtime only loads in the eryx version that built it, so setup always sets up its own version. To set up another, install the matching binary, for example cargo binstall [email protected].

Advanced: The compile Subcommand

For cross-compilation, custom CPU targets, or bundling packages into the runtime, use compile:

eryx-precompile compile [input.wasm] [OPTIONS]

The input is the component to compile. With --preinit for Python, compile builds from the Python runtime built into eryx-precompile instead, so it takes no input (one given is ignored) and needs -o.

Pre-initialization

Pre-initialization runs Python’s interpreter startup and captures the memory state, reducing session creation time from ~450ms to ~1-5ms:

eryx-precompile compile -o runtime-python.cwasm \
  --preinit --stdlib ./python-stdlib

--stdlib is optional: it defaults to the Python stdlib embedded in eryx-precompile, which matches its runtime.

To produce an architecture-independent .wasm (for later compilation on different targets):

eryx-precompile compile -o runtime-preinit.wasm \
  --preinit --stdlib ./python-stdlib --wasm-only

The JavaScript runtime pre-initializes too: it creates the QuickJS runtime and context and evaluates the eryx prelude, cutting a stateless execution from ~0.5ms to ~0.1ms. The embedded runtime-javascript.cwasm is built this way. It takes no stdlib, packages, imports, setup code or callbacks:

eryx-precompile compile runtime-javascript.wasm -o runtime-javascript.cwasm \
  --language javascript --preinit

CPU Target Selection

Control which CPU features the compiled runtime uses:

# Use host CPU features (default, fastest but not portable)
eryx-precompile compile runtime-python.wasm -o runtime-python.cwasm --target native

# AVX2/FMA — recommended for cloud VMs (Fly.io, AWS, GCP)
eryx-precompile compile runtime-python.wasm -o runtime-python.cwasm --target x86-64-v3

# Broad compatibility (~2008+ x86 CPUs)
eryx-precompile compile runtime-python.wasm -o runtime-python.cwasm --target x86-64-v2

# Baseline x86 (maximum compatibility)
eryx-precompile compile runtime-python.wasm -o runtime-python.cwasm --target x86-64

# Full target triple for cross-compilation
eryx-precompile compile runtime-python.wasm -o runtime-python.cwasm \
  --target aarch64-unknown-linux-gnu
TargetCPU FeaturesUse Case
nativeHost CPU (default)Local development
x86-64-v3AVX2, FMA, BMI1/2Cloud VMs (no AVX-512)
x86-64-v2SSE4.2, POPCNTOlder servers
x86-64Baseline SSE2Maximum compatibility
<triple>Native for target archCross-compilation

Bundling Packages

Bundle Python packages into the pre-compiled runtime so they’re instantly available without installation at runtime:

# Bundle a single package
eryx-precompile compile -o runtime-python.cwasm \
  --preinit --stdlib ./python-stdlib \
  --package numpy-2.2.3-wasi.tar.gz --import numpy

# Bundle multiple packages
eryx-precompile compile -o runtime-python.cwasm \
  --preinit --stdlib ./python-stdlib \
  --package jinja2-3.1.2-py3-none-any.whl \
  --package markupsafe-2.1.3-py3-none-any.whl \
  --import jinja2

# Use a site-packages directory
eryx-precompile compile -o runtime-python.cwasm \
  --preinit --stdlib ./python-stdlib \
  --site-packages ./my-site-packages --import jinja2

Supported package formats: .whl, .tar.gz, and directories. Native extensions (.so files) are detected and linked automatically.

The snapshot holds only the modules imported during pre-initialization. A sandbox running the runtime still needs the packages mounted at /site-packages for anything they import later (pandas’ describe() imports numpy.rec, for example): SandboxBuilder::with_site_packages in Rust, or --site-packages for the gRPC server.

Setup Code

Beyond importing modules, you can execute arbitrary Python code during pre-initialization. The resulting state (variables, objects, environments) is captured in the memory snapshot — each sandbox starts with that state in copy-on-write memory, preserving full isolation.

This is useful when your sandboxes all need the same expensive setup that doesn’t change between requests:

# Bake a Jinja2 SandboxedEnvironment with custom filters into the snapshot
eryx-precompile compile -o jinja2.cwasm \
  --preinit --stdlib ./python-stdlib \
  --package jinja2.whl --package markupsafe.whl \
  --import jinja2 --import jinja2.sandbox \
  --setup-code "
from jinja2.sandbox import SandboxedEnvironment
env = SandboxedEnvironment()
"

# For longer setup scripts, use --setup-file
eryx-precompile compile -o jinja2.cwasm \
  --preinit --stdlib ./python-stdlib \
  --package jinja2.whl --package markupsafe.whl \
  --import jinja2 \
  --setup-file setup.py

Each sandbox created from the resulting artifact starts with env already defined. Per-request code only needs to handle the request-specific work (deserializing data, compiling the template, rendering).

Callbacks

Registering callbacks on a sandbox installs a Python wrapper for each of them inside the guest. A persistent session pays for that once, but a fresh sandbox per request starts from the snapshot, and the snapshot only knows the callbacks it was taken with — none, by default — so every request re-runs the installation (a few milliseconds of Python for a handful of callbacks, more than the instantiation itself).

If the set of callbacks is fixed per deployment, declare it at pre-initialization and the wrappers are part of the snapshot. A sandbox that registers callbacks with the same names, descriptions and parameter schemas (in any order) then skips the installation; one that registers a different set still works, it just installs as before.

# callbacks.json: what the sandboxes will register at runtime
# [
#   {"name": "get_time", "description": "Current time", "parameters": {"type": "object", "properties": {}}},
#   {"name": "fetch", "description": "HTTP GET", "parameters": {"type": "object", "properties": {"url": {"type": "string"}}}}
# ]
eryx-precompile compile -o runtime-python.cwasm \
  --preinit --stdlib ./python-stdlib \
  --callbacks callbacks.json

parameters defaults to {} when omitted. The schemas are compared as text, so list each schema’s keys (properties included) in the same order as the schema the host registers: that order is also the order positional arguments bind in. Setup code cannot invoke the callbacks: there is no host to answer them during pre-initialization. In Rust, pass PreInitOptions::callbacks (build the declarations with eryx::preinit::callback_declaration); in Python, pass callbacks= to SandboxFactory.

Verification

By default, compile verifies the output: a runtime by creating a test sandbox (with the same --site-packages or --package contents mounted, as a sandbox using it would have, so --verify-code can reach modules the snapshot didn’t import), and a command (no --language, a component exporting wasi:cli/run) by loading it as a shell sandbox would, which checks its version stamp and that it links. Verification is skipped for a target this machine can’t run. For a runtime you can add custom verification (--verify-code is Python, so a command refuses it):

# Verify that numpy actually works, not just imports
eryx-precompile compile -o numpy.cwasm \
  --preinit --stdlib ./python-stdlib \
  --package numpy-wasi.tar.gz --import numpy \
  --verify-code "import numpy; print(numpy.array([1,2,3]).sum())"

# Skip verification (faster, use when you know the input is good)
eryx-precompile compile runtime-python.wasm -o runtime-python.cwasm --no-verify

Checking Shell Commands

commands inspect reads a .wasm command (it doesn’t compile it) and prints its WASI version, each interface it imports with whether shell children get it, its exports, and whether eryx can run it. It exits 1 if eryx can’t run the command, and 2 if the file can’t be inspected at all (unreadable, a .cwasm, or neither a component nor a WASI preview 1 module); with --json, that prints {"error": "..."}:

eryx-precompile commands inspect rg.wasm
eryx-precompile commands inspect --json rg.wasm

compile without --language runs the same check on a command (a component exporting wasi:cli/run) and refuses one eryx couldn’t run. See External Commands for what children get.

Commands from a Registry

eryx doesn’t fetch commands itself: get the .wasm with an OCI client, check it against your pinned digest, then check and precompile it. Commands are published as single-layer Wasm OCI artifacts, so wkg writes one straight to a file:

wkg oci pull ghcr.io/eryx-org/commands/eryx/rg:15.1.0-r.2 -o rg.wasm

With oras, fetch the layer by its digest. oras pull skips it, because the layer carries no file name, but oras blob fetch gets it and checks the digest:

oras blob fetch ghcr.io/eryx-org/commands/eryx/rg@sha256:<layer digest> --output rg.wasm

The pin is the digest of the .wasm itself, which is the manifest’s layer digest and what wkg.lock records. It isn’t the manifest digest that wkg oci push prints. Check it, then check and compile the command:

echo "<hex>  rg.wasm" | sha256sum -c
eryx-precompile commands inspect rg.wasm
eryx-precompile compile rg.wasm --output rg.cwasm

A Rust host can skip the separate steps: Command::load_cached(path, Some("sha256:<hex>")) checks the digest, refuses a command eryx can’t run, and compiles it once into eryx’s cache directory (commands/ under the first usable of $XDG_CACHE_HOME/eryx, the platform cache directory (~/.cache/eryx on Linux) and $TMPDIR/eryx-<uid>; with none usable, it compiles in memory and warns). Command::from_file_expecting(path, "sha256:<hex>") checks the digest without caching.

Upgrading to a New eryx Version

A precompiled runtime records the eryx version that built it and only loads in that version. Loading it in any other fails with an error naming both versions, so precompiled runtimes have to be rebuilt after every eryx upgrade, patch releases included.

An eryx built from a git checkout also records the hash of the guest’s sources (eryx-guest-python, eryx-guest-js or eryx-guest-shell, and the code and WIT they share) in each runtime it precompiles, and refuses a runtime built from other guest sources, with an error naming the language and its mise run precompile-runtime-<language> task. Between releases the version stays the same, so this is what catches a stale runtime in development. Builds from crates.io have no guest sources, so they only check the version.

The output of compile --preinit stores what it was built from after the component: its site-packages, imports, setup code, callback declarations, target and CPU feature level. upgrade rebuilds it with the installed eryx-precompile’s version, without the original packages or flags:

cargo binstall [email protected]
eryx-precompile upgrade numpy-runtime.cwasm -o numpy-runtime-0.13.0.cwasm

The output must be a new file: processes still on the old version may have the old file mapped, and keeping it lets you roll back. Pre-initialization uses the runtime built into eryx-precompile and its embedded stdlib, so upgrade needs no download.

Storing the build inputs adds about the compressed size of the packages to the file (around 9 MB for numpy, whose native extensions are most of it, and a few hundred bytes without packages). It adds nothing to memory use, because only the component is mapped. Pass --no-build-inputs to compile to write the bare component instead; upgrade then can’t rebuild it.

pyeryx’s saved factories are the same format, so upgrade rebuilds them too. pyeryx also has its own command, python -m eryx factory upgrade OLD NEW, and SandboxFactory.load(path, rebuild_if_incompatible=True).

Runtimes precompiled before eryx 0.13.0 don’t store their build inputs: build those once more with compile.

Common Recipes

Local Development (Rust)

# One-time setup
cargo binstall eryx-precompile
eryx-precompile setup

# Then in your project
cargo build --features embedded

Fly.io Deployment

# Pre-compile for x86-64-v3 (matches Fly.io hardware)
eryx-precompile compile -o runtime-python.cwasm \
  --preinit --stdlib ./python-stdlib \
  --target x86-64-v3

Two-stage Cross-compilation

Create an architecture-independent snapshot, then compile for each target:

# Stage 1: Pre-initialize (architecture-independent)
eryx-precompile compile -o runtime-preinit.wasm \
  --preinit --stdlib ./python-stdlib --wasm-only

# Stage 2: AOT compile for each target
eryx-precompile compile runtime-preinit.wasm -o runtime-x86.cwasm \
  --target x86-64-v3
eryx-precompile compile runtime-preinit.wasm -o runtime-arm.cwasm \
  --target aarch64-unknown-linux-gnu

Custom Runtime with Packages

# Build a runtime with numpy baked in
eryx-precompile compile -o numpy-runtime.cwasm \
  --preinit --stdlib ./python-stdlib \
  --package numpy-2.2.3-wasi.tar.gz \
  --import numpy \
  --verify-code "import numpy; print(numpy.zeros((2,2)))"

Environment Variables for CPU Targeting

When using eryx as a library — from Python (pyeryx), Rust (Sandbox::embedded()), or JavaScript — the embedded runtime is compiled to native code at build time. By default it targets the host CPU, which may include instructions (e.g. AVX-512) that aren’t available on the machine where your application actually runs.

If the build machine has different CPU features from the deployment target, the sandbox will fail to initialize at runtime with an error like:

Compilation settings are not compatible with the native host

Set these environment variables at build time (when the .cwasm is generated) or at import time (when the library first loads its embedded runtime) to control which CPU features the compiled code uses:

ERYX_CPU_FEATURES

Choose an x86-64 microarchitecture level:

ValueCPU FeaturesUse Case
nativeHost CPU (default)Local development
x86-64-v3AVX2, FMA, BMI1/2Recommended for cloud VMs (Fly.io, AWS, GCP)
x86-64-v2SSE4.2, POPCNT, SSSE3Older servers (~2008+)
x86-64Baseline SSE2 onlyMaximum compatibility
x86-64-v4AVX-512Skylake-X and newer

Example with Python:

ERYX_CPU_FEATURES=x86-64-v3 python my_app.py

Example with Rust:

ERYX_CPU_FEATURES=x86-64-v3 cargo build --features embedded --release

ERYX_TARGET

For cross-compilation, specify a full target triple:

ERYX_TARGET=aarch64-unknown-linux-gnu cargo build --features embedded --release

ERYX_CRANELIFT_FLAGS

For fine-grained control over individual Cranelift compiler flags:

ERYX_CRANELIFT_FLAGS=has_avx512f=false,has_avx512bw=false cargo build --features embedded --release

Tip: For most deployments, ERYX_CPU_FEATURES=x86-64-v3 is the right choice. It covers all cloud VMs from the last decade while avoiding AVX-512 instructions that many virtualized environments don’t expose.

Cache Location

The setup command caches compiled runtimes at:

PlatformPath
Linux/macOS~/.cache/eryx/ (or $XDG_CACHE_HOME/eryx/)
Windows%USERPROFILE%\.cache\eryx\

Files are named runtime-{language}-v{version}-{arch}-{os}.cwasm. To force a re-download, delete the cached file and run setup again.

Next Steps