ENGINEERING NOTES / AUDIO ENGINEERING

A PipeWire Virtual Sink That Survives kill -9

Route all Linux audio through your own PipeWire virtual sink with no root or driver, and restore the default output after kill -9. Rust code and a kill test.

A PipeWire Virtual Sink That Survives kill -9

On Linux, an ordinary user-session program can process every app’s audio by creating a PipeWire stream that is itself a sink, making it the configured default output, and playing the result to the real device through a second stream. The sink dies with your process. The default-output setting does not: WirePlumber persists it, so a kill -9 leaves the user’s configuration pointing at a device that no longer exists. The fix is to write the user’s choice to disk before you replace it, and hand it back on the next start.

Here is how nomus, the real-time music filter CloudGuys builds, does both, and the kill test that exposed the bug. The code is Rust with the pipewire crate. PipeWire and WirePlumber facts link to their official documentation, checked on 8 November 2026. Every test result below comes from a headless container, not real hardware.

The short answer

  • Two streams, like PipeWire’s own loopback module: a capture stream with media.class = Audio/Sink that apps play into, and a playback stream pinned to the real device.
  • Take over by writing default.configured.audio.sink in the default metadata, the same key desktop sound settings write.
  • Pin the playback stream with target.object, node.dont-reconnect and node.dont-fallback, or the session manager may route your output into your own sink.
  • Save the previous default to a state file first, atomically, then write the metadata.
  • Hand back only if the key still names your sink. If the user picked another device meanwhile, their choice stands.

How do you intercept all app audio on Linux without root?

You become the default output. PipeWire is a graph of nodes; the session manager, WirePlumber on current distributions, links each new playback stream to the default sink unless the stream names another target. WirePlumber’s linking policy documents the key: default.configured.audio.sink in metadata object 0, of type Spa:String:JSON, with a value such as {"name":"the-node-name"}. If the configured node does not exist, WirePlumber picks the best available device instead.

A stream whose media.class is Audio/Sink is presented to every other client as an output device. So nomus needs no driver, no root and no kernel module. It is the same idea as PipeWire’s loopback module, which passes a capture stream to a playback stream and can be used to create virtual sinks.

Approach Root or driver Who owns the sink On crash Used by nomus
pw_stream sink in your process, default set via metadata No Your process Sink vanishes; the configured default keeps naming it (tested, below) Yes
Loopback or filter-chain module in the user’s PipeWire config No The PipeWire daemon Not tested by us No: it means editing the user’s config
Kernel or HAL-style virtual audio driver Yes The system Not tested by us No

The shape

nomus-sink receives what the apps play, runs the engine in its process callback, and pushes the result into a ring buffer. nomus-playback pops from the ring and plays to the real sink. Both streams carry the same node.group and node.link-group, so PipeWire runs them in the same graph cycle and the ring only absorbs the order in which the two callbacks run. Per the PipeWire properties manual, nodes in one link group are also not linked to each other by the session manager.

The constants from crates/nomus-system/src/linux.rs:

const SINK_NAME: &str = state::SINK_NAME;
const CONFIGURED_SINK: &str = "default.configured.audio.sink";
const DEFAULT_SINK: &str = "default.audio.sink";
const RATE: u32 = 48_000;
/// Frames held between the sink and the playback stream before playback
/// starts. Both streams share a graph cycle, so this is only slack for the
/// order in which the two run.
const PREFILL: usize = 256;

Pin the playback stream, or you build a loop

The easy mistake is in the playback stream’s properties:

"target.object" => target.name.as_str(),
"node.dont-reconnect" => "true",
"node.dont-fallback" => "true",
"node.group" => group.as_str(),
"node.link-group" => group.as_str(),
"stream.dont-remix" => "true",

The default is now nomus. If the real sink disappears and the playback stream is allowed to fall back to the default target, WirePlumber would link nomus’s output into nomus’s input. The properties manual says node.dont-fallback stops the session manager from linking the node to the default target when linking to its specified target fails, and node.dont-reconnect stops the node from being moved to another sink when its target is destroyed (the node is destroyed too). So nothing moves the stream on its own. nomus watches for the stream entering Error or Unconnected and rebuilds the session on the new target itself. stream.dont-remix keeps PipeWire from changing the channel layout.

Two more choices I would keep:

  • One thread for all PipeWire objects. The Rust bindings’ MainLoopRc and StreamRc are Rc, not Send. nomus runs the whole session on a nomus-pipewire thread, and Drop sends a quit message and joins, so the default has been handed back before close returns.
  • Let PipeWire convert formats. Both streams offer F32LE, 48 kHz, FL/FR, and PipeWire converts at the edges. In a test with the graph forced to 44.1 kHz, this path reported the same ~31 ms delay as 48 kHz; nomus’s own resampler would have added more.

Taking the default, and handing it back

Order matters here. The user’s choice goes to disk before nomus replaces it.

// Remember the user's choice on disk before replacing it, so a nomus that
// dies cannot lose it (see `recover`).
let have_metadata = graph.borrow().metadata.is_some();
let state_path = state::default_state_path();
if have_metadata {
    let path = state_path.as_deref().ok_or_else(|| Error::Platform("cannot find a state directory (HOME is not set)".into()))?;
    let saved = Saved { pid: std::process::id(), previous: original_configured.clone() };
    state::save(path, &saved)
        .map_err(|e| Error::Platform(format!("cannot save the current output device to {}: {e}", path.display())))?;
    conn.write_default(&Action::Set(SINK_NAME.into()))?;
}
armed.set(true);

The state file lives at $XDG_STATE_HOME/nomus/default-sink (else ~/.local/state/nomus/default-sink) and is tiny:

# nomus: the output device to hand back if nomus exits without restoring it.
version=1
pid=64
previous=speakers

It is written atomically: a temporary file, sync_all, rename, then a sync of the directory. If it cannot be written, the start fails rather than take a default it could not give back. armed is set only after the write, so nomus’s own change to the key is not mistaken for the user choosing another device.

On a normal stop, the policy is one pure function in linux_state.rs:

pub fn hand_back(configured: Option<&str>, previous: Option<&str>) -> Action {
    match configured {
        None | Some(SINK_NAME) => match previous.filter(|p| !p.is_empty() && *p != SINK_NAME) {
            Some(p) => Action::Set(p.to_string()),
            None => Action::Clear,
        },
        Some(_) => Action::Keep,
    }
}

If the key still reads nomus (or nothing), it is nomus’s to change. Any other value is a choice the user made after nomus started, so it stays. A saved device that is missing right now, such as a Bluetooth headset that is off, is still written back by name: WirePlumber plays through the best available sink and switches back when the device returns. Clearing the key would forget the choice.

What does kill -9 leave behind?

Sound survives; configuration does not. nomus’s first crash test, on 26 September 2026 in a headless Docker container with a null sink called speakers, logged this:

Moment default.configured.audio.sink effective default
before speakers speakers
nomus running nomus nomus
after kill -9 nomus speakers
after a normal run and stop (old build) (cleared) speakers

WirePlumber fell back to speakers, so audio kept playing. But the configured key still named a sink that no longer existed, and the next normal stop cleared it because nomus had never recorded the earlier choice. On a machine with several outputs, the device the user picked was forgotten. Ctrl-C took the same path, because the CLI installed no signal handler.

After the fix, the same unchanged script ends with configured=speakers.

Recovery: three places read the state file

  1. The next start of a session repairs a leftover before saving anything of its own.
  2. App launch: the desktop app runs recovery once, so opening it repairs a leftover even if the filter is never switched on.
  3. nomus-system recover, a CLI subcommand that does only that.

Recovery has one guard: another nomus that is still running. It finds any sink named nomus, maps it to its owner through the node’s client.id and that client’s pipewire.sec.pid, and leaves a live instance alone. A second nomus that tries to start exits with “already running”.

You can watch the key yourself while testing any virtual sink:

pw-metadata 0 default.configured.audio.sink   # the user's configured choice
pw-metadata -m                                # monitor metadata changes live
wpctl status                                  # sinks, and which is default

The seven-scenario kill test

kill-test.sh runs in the same container with two null sinks, speakers and headphones, and a metadata monitor. The saved log (26 September 2026) ends in PASS:

Scenario Result
A. kill -9, then the next run hand-back at start (speakers), takeover (nomus), speakers at stop
B. kill -9, then nomus-system recover speakers restored at once; a second recover finds nothing
C. Ctrl-C (exit 130), then recover speakers restored
D. kill -9, the user picks headphones, then a run headphones kept
E. the user picks headphones while nomus runs nomus rebuilds onto them; headphones after stop
F. a second nomus while one runs the second exits 1; recover leaves the first alone; speakers after it stops
G. the saved device is removed before recover configured headphones, effective speakers; back to headphones when it returns

Scenario D is the one that guards against a “fix” that undoes the user’s own choice.

The headless smoke test also checks the audio itself. In the 48 kHz run from the same day: what the speakers received equalled nomus’s output bit for bit, the live output matched the offline engine to −77.8 dB at a 20.0 ms lag, 17.6 dB of music was removed in the pauses between words, and the log line read before=speakers during=nomus saved=speakers after=speakers state-file-after=removed -> restored.

Is the ~31 ms delay real?

Partly. nomus reports the engine’s 20 ms plus two 256-frame prefills, about 31 ms. That leaves out the PipeWire quantum: each stream adds at least one quantum of buffering. The meter log implies blocks of roughly 1,000 and 2,000 frames (21 and 42 ms), but that is an inference from callback counts, not a measurement.

Limits

  • Pinned streams bypass the sink. An app that targets a specific device does not follow the default.
  • PipeWire only. On a PulseAudio system nomus says so and stops; it needs PipeWire 0.3.49 or newer. Ubuntu 22.04 ships 0.3.48 and was tested only for that message.
  • No real hardware yet. Every run above is a headless container on an arm64 Mac (Ubuntu 24.04 and Debian 12 images). Desktops, Bluetooth and suspend are untested.

The nomus Linux app is in development, so treat this as a design write-up, not a release note.

The general rule

Record what you change before you change it, and undo leftovers on the next start. nomus follows the same rule on Android, where an audio effect outlives its process. The macOS version in Muting every app on macOS with Core Audio process taps gets that guarantee from the OS for free. The whole architecture is in One Rust engine, four hosts, and the nomus product page lists the series.

FAQ

Do I need root or a kernel driver for a virtual sink on Linux? No. A pw_stream with media.class = Audio/Sink in your own user process is enough, and the session manager treats it as an output device.

What happens to the default output if the app is killed with kill -9? The sink disappears, but WirePlumber keeps the configured default naming it. Audio keeps playing through the best available device, yet the user’s real choice is lost unless you saved it first.

Does this work on PulseAudio? No. nomus needs PipeWire 0.3.49 or newer and says so, then stops, on a PulseAudio system.

Has this been tested on real desktops? Not yet. All runs were headless containers (Ubuntu 24.04, Debian 12) on an arm64 Mac.

CloudGuys builds nomus. If you want to hear the engine, try the demo or the Chrome extension on the nomus page.

BUILT AND OPERATED BY CLOUDGUYS

nomus: real-time music filter
Keep the voice, lose the music, on your own device.

nomus removes background music from live audio and keeps the voices, speech and singing. One Rust engine and a small neural network run in a Chrome tab, system-wide on macOS and Linux, and on Android, with 20 ms of delay and no server.

Visit nomus ↗ About nomus ↗
Prefer a text version?Read this page as Markdown · All pages