A Rust implementation of the Varlink IPC protocol. zlink provides a safe, async API for building Varlink services and clients.
Overview
Varlink is a simple, JSON-based IPC protocol that enables communication between system services and applications. zlink makes it easy to implement Varlink services in Rust with:
- Async-first design: Built on async/await for efficient concurrent operations.
- Type safety: Leverage Rust's type system with derive macros and code generation.
- Unix domain sockets: Communicate over Unix domain socket transports.
- Any byte stream: Run a connection over a TLS stream, a WebSocket or any other byte stream
through the
byte_streammodule of each runtime. - File descriptor passing: Pass file descriptors along with messages over Unix domain
sockets, compatible with systemd's
sd-varlink. - Runtime choice: Use the zruntime (the default), Tokio or smol async runtime, each with a
block_onfor programs that have no async runtime of their own. - Code generation: Generate Rust code from Varlink IDL files.
📖 To learn all about using zlink (and Varlink) step by step, check out
the zlink book. Its sources are in the
book directory of the repository.
Project Structure
The zlink project consists of several subcrates:
zlink: The main unified API crate that re-exports functionality based on enabled features. This is the only crate you will want to use directly in your application and services.zlink-core: Core no-std foundation providing essential Varlink types and traits.zlink-names: Varlink name types (interface, type and field names) and their validation.zlink-macros: Contains the attribute and derive macros.zlink-tokio:Tokio-based transport implementations and runtime integration.zlink-smol:smol-based transport implementations and runtime integration.zlink-zruntime:zruntime-based transport implementations and runtime integration.zlink-codegen: Code generation tool for creating Rust bindings from Varlink IDL files.
Examples
Example: Calculator Service and Client
Here's a complete example showing both service and client implementations using zlink's attribute and derive macros:
use ;
use Path;
use ;
use ;
async
async
// The client proxy.
// Types shared between client and server.
async
// The calculator service, which has the one interface.
;
// The interface of the calculator.
Note: Typically you would want to spawn the server in a separate task but that's not what we did in the example above. Please refer to
Server::rundocs for the reason.
For ready-to-run examples you can execute with cargo run, see the examples
directory.
Async runtimes
zlink spawns no tasks of its own: its connections and servers make progress as their futures are
polled, by whichever executor polls them. Its runtime-specific API resides in a module for each
runtime: zlink::zruntime for zruntime (the default), and zlink::tokio and zlink::smol for
Tokio and smol. Each of these modules has a block_on too, which runs a future to completion on the
calling thread, driving the runtime of the module, for a program that has no async runtime of its
own: put the async code in one call to it, as the examples in this README do with
zlink::zruntime::block_on. The sockets of zruntime and smol can be used from any other executor
as well, while those of Tokio need a Tokio runtime, such as that of zlink::tokio::block_on.
To use Tokio or smol instead of zruntime, enable the tokio or smol feature, and use the API
under its module:
[]
= { = "0.9", = false, = [
"tokio",
"service",
"proxy",
"tracing",
] }
Code Generation from IDL
zlink-codegen can generate Rust code from Varlink interface description files:
# Install the code generator
# Let's create a file containing Varlink IDL
# Generate Rust code from the IDL
The generated code includes type definitions and proxy traits ready to use in your application.
Pipelining
zlink supports method call pipelining for improved throughput and reduced latency. The proxy macro
adds variants for each method named chain_<method_name> and a trait named <TraitName>Chain that
allow you to batch multiple requests and send them out at once without waiting for individual
responses.
Note: Chain methods are only generated for proxy methods that use owned types (
DeserializeOwned) in their return type. Methods with borrowed types (non-static lifetimes) don't get chain variants since the internal buffer may be reused between stream iterations. Input arguments can still use borrowed types.
use ;
use ;
use ;
async
// Proxy trait with owned return types - chain methods are generated.
// Input types can use borrowed data (they implement Serialize, not DeserializeOwned).
// Owned reply types - required for chain API (DeserializeOwned).
Features
Main Features
zruntime(default): Enable zruntime runtime integration, under thezruntimemodule.tokio: Enable tokio runtime integration, under thetokiomodule.smol: Enable smol runtime integration, under thesmolmodule.server(default): Enable server-related functionality (Server, Listener, Service).service(default): Enable the#[interface]macro and theServicederive. Impliesserverandintrospection.proxy(default): Enable the#[proxy]macro for type-safe client code.tracing(default): Enabletracing-based logging.defmt: Enabledefmt-based logging. If bothtracinganddefmtis enabled,tracingis used.handover: Enable handing connections over to other processes, such as the next instance of a service that restarts under systemd, through zoter. It builds nothing on platforms other than Linux. zoter errors convert toError::Handover, so?works in functions returningzlink::Result.
The runtime features are additive: several can be enabled at the same time, with the
runtime-specific API (currently block_on and the unix, byte_stream and notified modules) of
each residing in its own module. The rest of the API is runtime-agnostic and available at the crate
root.
IDL and Introspection
idl: Support for IDL type representations.introspection: Enable runtime introspection of service interfaces.idl-parse: Parse Varlink IDL files at runtime.
Getting Help and/or Contributing
If you need help in using these crates, are looking for ways to contribute, or just want to hang out
with the cool kids, please come chat with us in the
#zlink:matrix.org Matrix room. If something doesn't seem
right, please file an issue.
We welcome contributions! Please see our Contributing Guide for details.
License
This project is licensed under the MIT License.