Skip to main content
Every code block below is copy-runnable: use statements, #[tokio::main], and error handling are already filled in. The crate itself is the source of truth (src/boxlite/src/); check the version you have with boxlite::VERSION, which equals CARGO_PKG_VERSION.

Prerequisites

  • The boxlite crate and a machine with hardware virtualization — see Installation.
Cargo.toml (the crate name is boxlite):
Note: BoxLite is currently distributed primarily alongside the per-language SDKs and the CLI. If your environment has no publish source configured yet, depend on this repository’s src/boxlite/ by path (boxlite = { path = "..." }). The crate version is managed centrally by the workspace and corresponds to the latest published release (boxlite::VERSION reads CARGO_PKG_VERSION at compile time).

Quick Example (minimal happy path)

The shortest “create a box -> run a command -> read the exit code -> clean up” loop. The execution method is exec (not run); once it completes you obtain an ExecResult via Execution::wait():
Key points
  • The runtime constructors (with_defaults / new / rest) are synchronous fn; only the operations (create / exec / start / stop, etc.) are async.
  • The execution method is exec and returns an Execution. There is no litebox.run(...).
  • ExecResult has only exit_code and error_message, not stdout/stderr — output is read as a stream (see Execution).
  • To delete a box use runtime.remove(id_or_name, force), not box.remove().

Core types and methods

BoxliteRuntime (runtime entry point)

The main entry point for creating and managing boxes. Source: src/boxlite/src/runtime/core.rs. Constructors (all synchronous fn) Operations (all async, &self) Non-async handles: images() -> BoxliteResult<ImageHandle>, auth() -> BoxliteResult<AuthHandle>.
Note: metrics() returns BoxliteResult<RuntimeMetrics> (needs ?). To enumerate boxes use list_info() (not list()).

LiteBox (box handle)

A handle to a box instance. All methods take &self, so it can be shared across threads via Arc. Source: src/boxlite/src/litebox/mod.rs. Lifecycle: create() -> Configured (persisted, no VM); start() (or the first exec()) -> Running; stop() -> Stopped (can be restarted).

BoxCommand (command builder)

Builder style, chained methods that consume self. Source: src/boxlite/src/litebox/exec.rs.

Execution (handle to a running command)

Returned by exec(). Source: src/boxlite/src/litebox/exec.rs. stdout/stderr implement futures::Stream<Item = String> and can each be taken only once.
Note: wait/kill/signal/resize_tty all take &self (only stdin/stdout/stderr, which take a stream, take &mut self). This means you can share an Arc<Execution> and call kill() concurrently.
Streaming read of stdout (complete, runnable):
Writing to stdin (ExecStdin provides write / write_all / close):

ExecResult

No stdout/stderr fields (this differs from the C / Go / Python wrapper layers, which add output fields). At the Rust core layer, output comes from the streams in Execution.

Source: src/boxlite/src/runtime/options.rs. BoxOptions implements Default; fill the remaining fields with ..Default::default().

BoxOptions fields

About the cpus / memory_mib defaults: the field default is None. When None, the runtime allocates 1 vCPU / 1024 MiB from vm_defaults in src/boxlite/src/runtime/constants.rs. Pass explicit values in production. See Compute resources.

RootfsSpec

VolumeSpec (third field is a bool)

Note: the ro/rw strings are only used by the CLI form -v host:guest:ro, which lives in the CLI parsing layer; the SDK API uses a bool.

NetworkSpec

PortSpec

Secret (secrets / vault entry point)

Code inside the box sees the placeholder; when traffic goes out to one of the hosts listed in hosts, the proxy substitutes the real value, so the plaintext never lands in the guest:

Security, metrics, and runtime options

SecurityOptions / AdvancedBoxOptions

Security options are passed through advanced; the top-level BoxOptions has no security field. Source: src/boxlite/src/runtime/advanced_options.rs. The Rust core layer exposes a two-state switch plus a builder (unlike the Python binding’s development()/standard()/maximum() preset static methods, which do not exist here):

SecurityOptions fields

The struct fields and their meanings (source: SecurityOptions in src/boxlite/src/runtime/advanced_options.rs). Several fields are Linux-only or macOS-only; on a platform that does not support a feature the field is inert.
Field defaults under enabled() (== default(), source: SecurityOptions::enabled in advanced_options.rs): disabled() sets jailer_enabled = false and turns off every sub-protection (uid/gid become None).

SecurityOptionsBuilder methods

Obtain a builder with SecurityOptions::builder() (or SecurityOptionsBuilder::enabled() / ::disabled() / ::new()). The setters take &mut self (non-consuming), and build() produces the final SecurityOptions (source SecurityOptionsBuilder in advanced_options.rs):
Note: the development() / standard() / maximum() presets available in the Python binding are not present on the Rust SecurityOptionsBuilder. Use enabled() / disabled() / new() as starting points and override individual fields.

ResourceLimits

RLIMIT_* caps applied to the jailed process (source: ResourceLimits in advanced_options.rs). Each field maps to a POSIX resource limit; None means “do not set this limit”.
The builder takes &mut self (non-consuming) and ends with build(). The following is a complete, runnable example: build custom security options with the builder and attach them to AdvancedBoxOptions.security:
AdvancedBoxOptions fields: security: SecurityOptions, isolate_mounts: bool (default false, requires CAP_SYS_ADMIN on Linux), health_check: Option<HealthCheckOptions>.

Metrics fields (real field and method names)

Accessor methods on RuntimeMetrics (runtime.metrics().await?): Public fields on BoxMetrics (litebox.metrics().await?, selected):

BoxliteOptions / ImageRegistry (runtime options)

Customize the home directory and image registries (including resolution order, authentication, plain HTTP, and skipping TLS verification):
ImageRegistry constructors: https(host) / http(host), chained with .with_search(bool) / .with_skip_verify(bool) / .with_basic_auth(user, pass) / .with_bearer_auth(token).

State queries (BoxInfo / BoxStatus / BoxState)

litebox.info().await? returns a BoxInfo. At the Rust core layer, BoxInfo has a status: BoxStatus field directly (this differs from the Python/Node binding layer, where it is the two-level info.state.status). BoxInfo fields: id: BoxID, name: Option<String>, status: BoxStatus, created_at, last_updated, pid: Option<u32>, image: String, cpus: u8, memory_mib: u32, labels, health_status: HealthStatus.
Predicate methods: is_running() / is_configured() / is_stopped() / is_active() / is_transient() / can_start() / can_stop() / can_remove() / can_exec().

BoxState

The dynamic state of a box (changes over the lifecycle), as opposed to the static configuration. Source src/boxlite/src/litebox/state.rs (struct BoxState):
When a box reports Failed, error_reason and exit_code are where the cause is. Both are None while the box is healthy.

Type utilities (Bytes / Seconds / BoxID / ContainerID)

These newtypes appear across the API (resource sizes, durations, identifiers). Source src/boxlite/src/runtime/types.rs and src/boxlite/src/runtime/id.rs.

Bytes

A semantic newtype for byte sizes, so call sites carry units instead of bare integers.

Seconds

A semantic newtype for durations.

BoxID

A box identifier. Locally minted IDs are 12-character Base62 (~71 bits of entropy); the type also accepts other formats from remote REST servers (up to MAX_LENGTH = 128 characters) so prefixed or namespaced IDs round-trip.

ContainerID

A container identifier in OCI format: 64-character lowercase hex.

Error types (BoxliteError)

Source src/shared/src/errors.rs. BoxliteResult<T> = Result<T, BoxliteError>. Real variants (there is no BoxliteError::Run — command-execution errors are Execution): UnsupportedEngine, Engine(String), Config(String), Storage(String), Image(String), Portal(String), Network(String), Rpc(String), RpcTransport(String), Internal(String), Execution(String), Unsupported(String), NotFound(String), AlreadyExists(String), InvalidState(String), Database(String), MetadataError(String), InvalidArgument(String), Stopped(String), ResourceExhausted(String), SessionReaped(String).

Concurrency and thread safety

BoxliteRuntime and LiteBox are both Send + Sync and can be shared across tasks via Arc:

Troubleshooting (common problems and typical errors)


  • Resource defaults (cpus/memory_mib): the field default is None, and the runtime then allocates 1 vCPU / 1024 MiB; pass explicit values in production. See Compute resources.
  • Binding-layer differences: the Python/Node two-level BoxInfo.state.status, the Python SecurityOptions.maximum() preset, and so on — see the corresponding SDK reference pages.