Skip to main content
Use the Rust crate boxlite to run a command in an isolated microVM sandbox within minutes, getting streaming output and an exit code without managing virtual machines, images, or networking yourself. This is the shortest path to safely executing untrusted code, AI-generated code, or third-party commands.

Prerequisites

  • The boxlite crate and a machine with hardware virtualization — see Installation for the supported platforms.
  • Rust 1.88 or newer (rustc --version to check).
Add the dependencies to your project:

Quick Example

Write the following into src/main.rs, then run cargo run. This code is standalone and includes the full imports, runtime initialization, and error propagation (? + Box<dyn Error>).
Expected output:

Parameters and Returns (Core API)

BoxliteRuntime (runtime handle)

The constructors are synchronous fns (do not .await); only create / exec / remove and similar operations are async.

LiteBox (box handle)

BoxCommand (command builder)

Chained construction; all methods return Self:

Execution / ExecResult (execution handle and result)

Important: the Rust core-layer ExecResult contains only exit_code (and error_message), not stdout/stderr; output must be read via the streaming stdout() / stderr() above. This differs from the C/Go/Python SDKs.

BoxOptions (common fields)

Mount a host directory (read_only is a bool, not a string):

Troubleshooting

Method run not found / BoxliteError::Run

The method to execute a command is exec, not run. Likewise, the error enum has no BoxliteError::Run variant; the variant for a command execution failure is BoxliteError::Execution(String). When matching errors:

A non-zero exit code was not reported as an error

Even when the command fails, execution.wait() still returns Ok(ExecResult): a non-zero exit code is not turned into an Err. Check it yourself:

Compile error: missing StreamExt / .next() not found

Reading the streaming output requires importing the futures::StreamExt trait, and the futures dependency must be present:

Mixing async and synchronous methods

BoxliteRuntime::default_runtime() / with_defaults() / new(), and litebox.id() / litebox.info(), are synchronous; do not .await them. Only create / start / exec / wait / stop / metrics are async.

Startup failure: no hardware virtualization (environment constraint)

On Linux without KVM (or a container/CI that does not pass through /dev/kvm), create / start fail:
  • Linux: confirm /dev/kvm exists and the current user is in the kvm group (ls -l /dev/kvm).
  • macOS (Apple Silicon): uses Hypervisor.framework and does not need /dev/kvm; runs directly.
  • WSL2: enable nested virtualization and install KVM.
This kind of failure is an environment constraint; the process does not crash. Catch the Err with the match above to handle it cleanly.

The first run stalls while pulling the image

The first use of an image (such as alpine:latest) downloads it from a registry, which can be slow depending on the network; a failure usually surfaces as BoxliteError::Image(...) or BoxliteError::Network(...), which is retryable.

Next Steps

  • Run any language’s code inside a box, mount volumes, or configure networking: replace BoxCommand with your target command and RootfsSpec::Image(...) with the image you need (such as python:3.11-slim).
  • For stronger isolation, configure the security options under BoxOptions.advanced.
  • Other languages: see the Python quickstart, the Node.js quickstart, and the Go quickstart.