Skip to main content

Crate mxc_sdk

Crate mxc_sdk 

Source
Expand description

mxc-sdk: an importable library for running MXC containers in-process.

See the consumer glossary for terminology.

Create a v1::ContainerRequest with its command and restrictions, then either:

  • hand it to v1::run to run the contained process to completion and get its captured stdout/stderr and exit outcome in one call, or
  • hand it to v1::spawn for a live v1::MxcProcess handle you can stream stdio through, feed stdin, and kill while it runs.
  • hand it to v1::spawn_with_pty for an v1::MxcPtyProcess with merged terminal output and resize support.

Either way the right containment backend is selected for the host and the ordinary run and spawn paths allocate no PTY.

use mxc_sdk::v1::{self, WaitResult};

let request = v1::ContainerRequest::new("echo hi");
let output = v1::run(request, Default::default())?;
match output.outcome {
    WaitResult::Exited(code) => println!("exit={code}"),
    WaitResult::TimedOut => println!("timed out"),
}
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));

§Backend support

The selected backend is driven by the request’s v1::Containment. Requests default to the host’s native process backend.

BackendHostSelected by
BubblewrapLinuxv1::Containment::Process or v1::Containment::Bubblewrap
LXCLinuxv1::Containment::Lxc
SeatbeltmacOSv1::Containment::Process or v1::Containment::Seatbelt
ProcessContainer (PSEC)Windowsv1::Containment::Process
Explicit ProcessContainer configurationWindowsv1::Containment::ProcessContainer
WSLC (WSL Container)Windowsv1::Containment::Wslc
IsolationSessionWindowsv1::Containment::IsolationSession

LXC is reachable only by naming it: v1::Containment::Process resolves to Bubblewrap on Linux. It needs root. Ordinary streaming uses pipes, while v1::spawn_with_pty allocates a caller-controlled terminal.

WSLC requires the crate’s wslc build feature, and IsolationSession requires the isolation_session build feature. WSLC and IsolationSession are also available through the typed lifecycle API.

The wslc feature stages wslcsdk.dll into the cargo profile directory, and WSLC loads it from beside the module holding this code, so an executable copied or installed out of that directory needs the DLL brought along. The crate README covers that and the separate wxc-wslc-daemon.exe that the container lifecycle needs.

A concrete backend selected on another host returns an Error with ErrorCode::UnsupportedContainment.

§Diagnosing a failure

Error carries an ErrorCode and a message. When an underlying platform API fails, it also identifies the call and its status:

if let Some(operation) = &error.operation {
    eprintln!("{operation} failed with {:?}", error.native_code);
}
if let Some(hint) = &error.remediation {
    eprintln!("  try: {hint}");
}

Error::operation and Error::native_code are absent for a failure raised before any API call was reached (a malformed policy, say) and a native code only ever appears alongside the operation it belongs to. Display renders both, so logging the error alone does not lose them.

use mxc_sdk::v1;

// Run a command inside a WSL container (Windows, --features wslc).
let wslc = v1::configs::WslcConfig { image: "python:3.12".to_string(), ..Default::default() };
let request = v1::ContainerRequest {
    containment: v1::Containment::Wslc(wslc),
    ..v1::ContainerRequest::new("python3 -c 'print(42)'")
};
let output = v1::run(request, Default::default())?;

§Choosing an entry point

container requestexisting containerStdio
capturev1::runv1::container::run_in_containercaptured stdout and stderr
handlev1::spawnv1::container::spawn_in_containerseparate live pipes
PTYv1::spawn_with_ptyv1::container::spawn_in_container_with_ptymerged output, resize
inheritedv1::spawn_with_extended_options with v1::SpawnStdio::Inherit—the caller’s own stdin, stdout, and stderr

v1::container::provision_container, v1::container::start_container, v1::container::stop_container, and v1::container::deprovision_container drive the lifecycle with typed Rust requests. All SDK execution APIs take typed requests.

§Standard streams

The V1 run and spawn APIs use ordinary pipes; PTY allocation is explicit through v1::spawn_with_pty and v1::container::spawn_in_container_with_pty. v1::run captures both output streams. With v1::spawn or v1::container::spawn_in_container, callers can take the streams from MxcProcess or let wait drain and discard any stream they did not take.

v1::spawn_with_pty allocates a caller-controlled PTY for create-and-run execution on supported backends, including IsolationSession, Bubblewrap, LXC, and Seatbelt direct execution. v1::container::spawn_in_container_with_pty does the same for a process in an existing IsolationSession container. Unsupported backends reject the request before creating a sandbox.

v1::spawn_with_extended_options takes extensible options. With v1::SpawnStdio::Inherit it attaches the workload to the calling process’s own stdin, stdout, and stderr, such as a terminal or pseudo-console the caller already set up; ProcessContainer, Bubblewrap, and Seatbelt support that mode.

Policy and operational warnings are available through MxcProcess::warnings and ExecutionResult::warnings. These include security warnings, network rules that cannot carry traffic, and operational warnings such as unavailable telemetry routing.

§Internal module boundaries

This crate root is the curated public facade. Backend dispatch, host probing, execution, and backend implementations are doc-hidden compatibility exports for workspace consumers rather than supported SDK entry points.

Modules§

v1
V1 contract-mapped request and typed lifecycle APIs.