#Goose: Pure Zig D-Bus
A pure Zig implementation of the D-Bus protocol.
D-Bus is everywhere on Linux. If you want to talk to NetworkManager, send a desktop notification, control your media player via MPRIS, or query systemd, you have to talk D-Bus.
For years, doing this from low-level systems languages meant picking between two rough options:
libdbus: The venerable C library. It is callback-heavy, manually allocates memory for every single variant and container, and requires hundreds of lines of iterator ceremonies just to read a string and an integer.GDBus: Tied to GLib and GIO. Excellent ergonomics, but dragging in the entire GLib ecosystem just to speak to a local daemon is overkill for a minimal Zig project.
I wanted something clean, fast, and enjoyable to use in Zig. That is why I wrote Goose.
#What Makes Goose Special
- Zero C Dependencies: Built from scratch in pure Zig. No
libc, nolibdbus.so, no external headers. It compiles directly into your static binary. - Two Concurrency Flavors: Pick the right execution model for your workload:
.threaded: Reader thread, background worker pool, and futex wakeups for concurrent apps..poll: Strictly zero background threads. Retrieve the raw socket file descriptor and integrate it directly into your own event loop.
- Comptime Reflection: Zig's
@typeInfohandles marshaling. You pass native structs, tuples, and slices, and Goose handles the D-Bus wire alignment, padding, and signatures at compile time. - Real File Descriptor Passing: First-class support for
SCM_RIGHTS. You can send and receive live operating system file descriptors out-of-band across Unix domain sockets without copying payloads. - Built-in Developer Tools: Command-line helpers to introspect running services and generate typed Zig client code from XML.
#Architecture
At its core, Goose bridges your native Zig code to the local Unix domain socket:
Goose System Architecture
Click each architectural layer to inspect how your code connects to the kernel transport.
Uses Zig @typeInfo to determine wire signatures and alignments at compile time. Injects runtime metadata without cluttering public D-Bus interfaces.
#Quick Installation
Add Goose to your project using zig fetch:
zig fetch --save git+https://github.com/luxluth/goose.git#master
In your build.zig, expose the dependency to your executable module:
const goose_dep = b.dependency("goose", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("goose", goose_dep.module("goose"));
#30-Second Example: Connecting to the Bus
Here is a minimal program that connects to your user session bus and queries the daemon for its unique bus ID:
const std = @import("std");
const goose = @import("goose");
const GStr = goose.core.value.GStr;
pub fn main(init: std.process.Init) !void {
const allocator = init.gpa;
const io = init.io;
// Connect to the session bus (uses default .threaded backend)
var conn = try goose.Connection.init(allocator, .Session, io, init.environ_map);
defer conn.close();
// Create a client proxy for the D-Bus broker itself
const dbus = goose.proxy.Proxy.init(
&conn,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
);
// Call GetId
var result = try dbus.call("GetId", .{});
defer result.deinit();
// Decode the response
const bus_id = try result.expect(GStr);
std.debug.print("Connected to D-Bus! Bus ID: {s}\n", .{bus_id.s});
}
#Live Source Code Inspection
Goose keeps its data structures straightforward. Below is the live SignalHandler definition extracted directly from the repository:
/// Represents a handler for a D-Bus signal.
pub const SignalHandler = struct {
interface: []const u8,
member: []const u8,
callback: *const fn (ctx: ?*anyopaque, msg: core.Message) void,
ctx: ?*anyopaque,
};#Documentation Map
Dive into the detailed guides below:
- Architecture and Concurrency: How connections work, the SASL
EXTERNALhandshake, and choosing between.threadedand.pollmodes. - Types and the Wire: D-Bus type signatures, alignment rules, wrappers like
GStrandGPath, and comptime reflection. - Calling Methods and Signals: Using client proxies, borrowed reads with
.expect(), managing properties, and listening to signals. - Exporting Services and Objects: Turning native Zig structs into D-Bus services with automatic routing and context parameter injection.
- Passing File Descriptors: Zero-copy IPC with
SCM_RIGHTS,ResolvedFd, and memory-leak guarantees. - Introspection and Code Generation: Introspection XML generation and using the
goose-generateCLI.