Skip to content

Rewrite vmkit in Rust, keeping its contracts - #1

Merged
znaniye merged 14 commits into
mainfrom
rust
Nov 21, 2026
Merged

znaniye merged 14 commits into
mainfrom
rust

Conversation

@znaniye

@znaniye znaniye commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Summary

vmkit becomes one Rust crate, module for module with the Go packages it replaces. I/O is async on tokio.

-ch/        client.go vm.go vsock.go
-vsock/     control/  memory/  virtiofs/  tap/  hostutil/
-go.mod     nix/checks/go.nix
+Cargo.toml  rustfmt.toml  nix/checks/rust.nix  nix/devshell.nix
+src/
+├── ch.rs          # Vm::args, Client (info, resize_memory, power_button), dial_guest
+├── vsock.rs       # guest side: dial, listen
+├── control.rs     # Exchange: one request line, one reply line
+├── memory.rs      # policy (decide/observe/grant), Status files, ranks
+├── virtiofs.rs    # virtiofsd args, store_view
+├── tap.rs
+└── hostutil.rs
+docs/contracts.md  testdata/contract/

The cross-process formats are now written down in docs/contracts.md: the pool status files, the control channel and its stats request, and the module. They are pinned by fixtures that both implementations' tests read. Go reads them on go (9485632, main plus the contract tests), and Rust reads them here. A controller on v0.1 (Go) and one on this crate can share a host.

API changes from Go:

  • Unset times are Options, not zero times: no stats yet, never resized, not calm, never denied. read_statuses returns None when no pool publishes.
  • The control channel uses an Exchange instead of Answer's callback: callers call read_line or read_request, then reply, and reply returns whether the reply was delivered. To add reply fields, a struct holds Reply under #[serde(flatten)].
  • Account::run_as(&mut Command) replaces Credential(): it sets groups, gid and uid in pre_exec, and sets the parent-death signal after them.

The module check's networks and pools are renamed from consumer names to interactive and batch.

Evidence

  • Before: the Go tests (39) pass against testdata/contract on go.
    After: checks.rust passes: rustfmt, clippy pedantic with -D warnings, and 38 tests. These include the same contract cases:

    read status.json, status-offset.json (−03:00, nanoseconds) -> same Status
    write_status(Status{…})        == status.json byte for byte
    stats_from(meminfo, pressure)  == stats-request.json
    Reply::ok(), Reply::error(..)  == reply-ok.json, reply-error.json
    
  • checks.module passes. Every commit was checked on its own with rustfmt, clippy, the Rust tests, and the Go tests where Go is still present.

Merge Danger

Door: two-way

Go stays available on the go branch and the v0.1.0/v0.1.1 tags.

Blast Radius: consumers

A consumer whose vmkit flake input follows main and copies it into its Go build would stop building. Hold the merge until workbench in Rust validates the crate, then tag v0.2.0. Go consumers stay on v0.1.x and get the module from that tag's input.

docs/contracts.md describes what controllers and guests exchange: the
pool status files, the control channel and its stats request, and the
NixOS module. testdata/contract holds the examples every implementation's
tests read, so controllers in other vmkit versions or languages can share
a host.
One library crate, Linux only, async on tokio for I/O. checks.rust runs
rustfmt, clippy (pedantic, warnings denied) and the tests; the dev shell
has the toolchain.
… in Rust

run_as drops to an account in pre_exec, setting the parent-death signal
after the credentials change resets it.
hyper over the API socket, one connection per call, driven to its end
before the call returns, so none is left open against the VMM's
connection limit.
Times that may be unset (no stats yet, never resized, not calm) are
Options instead of zero times.
Status files keep the format docs/contracts.md describes; the tests read
the same fixtures as the Go ones, and what Rust writes matches them byte
for byte.
An Exchange reads one request line and sends one reply within 5 s;
reply() tells whether the reply was delivered. Callers add reply fields
with a struct holding Reply flattened.
The crate does what the Go packages did, module for module, and keeps to
docs/contracts.md, so a controller on v0.1 (Go, kept on the go branch)
shares a host with one on the crate. The README describes the crate.
@znaniye
znaniye marked this pull request as ready for review November 21, 2026 17:58
@znaniye
znaniye merged commit 91c2cb1 into main Nov 21, 2026
1 check passed
@znaniye
znaniye deleted the rust branch November 21, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant