Skip to content

Instantly share code, notes, and snippets.

@AnonymerNiklasistanonym
Last active September 30, 2025 19:35
Show Gist options
  • Select an option

  • Save AnonymerNiklasistanonym/af792a22e8a95883032b7852ffeb823b to your computer and use it in GitHub Desktop.

Select an option

Save AnonymerNiklasistanonym/af792a22e8a95883032b7852ffeb823b to your computer and use it in GitHub Desktop.
Linux virtual audio sinks (pipewire, service)

Create virtual audio sinks for pipewire automatically using a system (user) service. With the patchbay software helvum the audio can then be routed/multiplexed for e.g. obs. It per default loops back to the default audio output.

# Create the script to unload the sink automatically
mkdir -p ~/.local/bin
nano ~/.local/bin/unload-virtual-sink.sh
chmod +x ~/.local/bin/unload-virtual-sink.sh
# Create the service
mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/virtual-sink@.service

Example

systemctl --user daemon-reload
# First time
systemctl --user enable --now virtual-sink@Music.service
systemctl --user enable --now virtual-sink@Game.service
systemctl --user enable --now virtual-sink@Chat.service
# Restart after chaning the service
systemctl --user restart virtual-sink@Music.service
systemctl --user restart virtual-sink@Game.service
systemctl --user restart virtual-sink@Chat.service

Verify

pactl list short modules
# ...
536870928       module-null-sink        sink_name=VIRTUAL_SINK_Music
536870929       module-null-sink        sink_name=VIRTUAL_SINK_Game
536870930       module-null-sink        sink_name=VIRTUAL_SINK_Chat
# Example to fix bad config
pactl unload-module 536870928
pactl list short sinks
# ...
759     VIRTUAL_SINK_Music      PipeWire        float32le 2ch 48000Hz   SUSPENDED
768     VIRTUAL_SINK_Game       PipeWire        float32le 2ch 48000Hz   SUSPENDED
777     VIRTUAL_SINK_Chat       PipeWire        float32le 2ch 48000Hz   SUSPENDED
#!/bin/bash
# $1 = sink name
if [ -z "$1" ]; then
echo "Usage: $0 SINK_NAME"
exit 1
fi
sink="$1"
modid=$(pactl list short modules | awk -v n="$sink" '$2 ~ /module-null-sink/ && $0 ~ n {print $1}')
if [ -n "$modid" ]; then
pactl unload-module "$modid"
fi
[Unit]
Description=PipeWire Virtual Sink %i
Wants=pipewire.service pipewire-pulse.service
After=pipewire.service pipewire-pulse.service
[Service]
Type=oneshot
RemainAfterExit=yes
# Create virtual audio sink (selectable as audio source in e.g. obs)
ExecStart=/usr/bin/pactl load-module module-null-sink sink_name=VIRTUAL_SINK_%i
# Loop the audio output back to the default output
ExecStartPost=/usr/bin/pactl load-module module-loopback source=VIRTUAL_SINK_%i.monitor sink=@DEFAULT_SINK@
ExecStop=%h/.local/bin/unload-virtual-sink.sh VIRTUAL_SINK_%i
[Install]
WantedBy=default.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment