#Goose: Pure Zig D-Bus

Goose Logo

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:

  1. 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.
  2. 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, no libdbus.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 @typeInfo handles 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:

Architecture

Goose System Architecture

Click each architectural layer to inspect how your code connects to the kernel transport.

1. Your Application Idiomatic Zig Code
Client Proxies (proxy.Proxy)Exported Structs as Services
2. Goose Core Engine Compile-Time Reflection & Dispatch
Comptime Type Engine (BodyEncoder / BodyDecoder)Method & Property DispatcherContext Parameter Injection (*Connection, Message, ResolvedFd)
3. Concurrency Backends Flexible Threading Models
.threaded (Background reader thread + worker queue + futex wakeups).poll (Zero background threads, non-blocking dispatch)
4. Linux Kernel Transport Unix Domain Sockets & Ancillary Messages
Unix Domain Socket (/run/user/1000/bus)Ancillary SCM_RIGHTS Control Messages
2. Goose Core Engine Details

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:

bash
zig fetch --save git+https://github.com/luxluth/goose.git#master

In your build.zig, expose the dependency to your executable module:

zig
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:

zig
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:

SignalHandler Definition (Live Extract) ~/goose/src/common.zig
zig
/// 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: