snapcast: add AirPlay 2 support #1294

Merged
JakeHillion merged 1 commits from jj/zooywwrmlyyw into main 2026-08-17 23:58:39 +01:00
3 changed files with 94 additions and 3 deletions

No files matched your search

@@ -105,15 +105,29 @@
80 # HTTP 1-2
443 # HTTPS 1-2
1400 # HA Sonos
5000 # snapcast AirPlay 2 RTSP/control (shairport-sync)
5354 # snapcast Spotify Connect (librespot zeroconf)
7654 # Tang
21063 # HomeKit
];
allowedUDPPorts = lib.mkForce [
319 # snapcast AirPlay 2 PTP (nqptp)
320 # snapcast AirPlay 2 PTP (nqptp)
443 # HTTP 3
5353 # HomeKit / mDNS
49191 # OTBR MeshCoP BorderAgent
];
# AirPlay 2 binds dynamically-assigned ephemeral ports for its event,
# audio, control and timing channels. udp_port_base/range only pin
# AirPlay 1, so AP2's ports can't be constrained; open the kernel
# ephemeral range on the trusted LAN so the sender can reach whatever
# shairport-sync picks per session.
allowedTCPPortRanges = [
{ from = 32768; to = 60999; } # snapcast AirPlay 2 dynamic ports
];
allowedUDPPortRanges = [
{ from = 32768; to = 60999; } # snapcast AirPlay 2 dynamic ports
];
};
iot = {
allowedTCPPorts = lib.mkForce [
+2
View File
@@ -29,6 +29,7 @@
mautrix-discord = 413;
gitea-actions-vm = 415;
backup-git = 416;
shairport = 417;
## Consistent People
jake = 1000;
@@ -59,6 +60,7 @@
mautrix-discord-registration = 414;
gitea-actions-vm = 415;
backup-git = 416;
shairport = 417;
## Consistent Groups
mediaaccess = 1200;
+78 -3
View File
@@ -2,6 +2,8 @@
let
cfg = config.custom.services.snapcast;
shairport = pkgs.shairport-sync.override { enableAirplay2 = true; };
# librespot ships its own mDNS responder that binds 5353 and would collide
# with the system avahi. Build it with avahi support so it registers through
# the system daemon instead (the default build only supports libmdns).
@@ -15,6 +17,12 @@ let
"--zeroconf-port"
(toString cfg.librespotZeroconfPort)
];
# shairport-sync decodes AirPlay audio to this pipe; snapserver reads it as a
# pipe source. tmpfiles pre-creates it owned by shairport, and snapserver uses
# mode=read, so the fifo isn't created (and owned) by snapserver's dynamic
# user, which shairport could then not write to.
airplayPipe = "/run/snapcast/airplay";
in
{
options.custom.services.snapcast = {
@@ -23,7 +31,7 @@ in
deviceName = lib.mkOption {
type = lib.types.str;
default = config.networking.hostName;
description = "Name advertised to Spotify Connect.";
description = "Name advertised to Spotify Connect and AirPlay.";
};
librespotZeroconfPort = lib.mkOption {
@@ -31,6 +39,12 @@ in
default = 5354;
description = "Port librespot advertises the Spotify Connect handshake on.";
};
airplayPort = lib.mkOption {
type = lib.types.port;
default = 5000;
description = "Port shairport-sync listens on for AirPlay.";
};
};
config = lib.mkIf cfg.enable {
@@ -39,6 +53,11 @@ in
settings = {
stream.source = [
"librespot:///${lib.getExe librespot}?name=Spotify&devicename=${cfg.deviceName}&bitrate=320&params=${librespotParams}"
"pipe://${airplayPipe}?name=AirPlay&mode=read&sampleformat=44100:16:2"
# Follows whichever of the above is currently playing, so a client can
# sit on one stream and always hear the active source. Pinned to the
# 44.1kHz rate both real sources emit so nothing gets resampled.
"meta:///Spotify/AirPlay?name=Meta&sampleformat=44100:16:2"
];
# Snapclients connect here; opened per-interface by the host firewall.
@@ -57,10 +76,66 @@ in
};
};
# snapserver runs DynamicUser with a StateDirectory, so its state lands in
# /var/lib/private/snapserver (client names, per-client volumes).
# AirPlay 2 receiver runs as its own service rather than being spawned by
# snapserver, so it gets a config file and a persistent home for its pairing
# identity. Spawned by snapserver it had neither, so the identity reset on
# every restart and iOS refused to reconnect. It decodes to a pipe that
# snapserver reads above.
services.shairport-sync = {
enable = true;
package = shairport;
settings = {
general = {
name = cfg.deviceName;
output_backend = "pipe";
port = cfg.airplayPort;
};
pipe = {
name = airplayPipe;
# snapserver reads this pipe as a fixed 44100:16:2 stream. shairport's
# default "auto" output matches the *input* and can even switch format
# mid-stream with no in-band notification, so snapserver misreads it as
# noise. Pin the output to exactly what snapserver expects.
output_rate = 44100;
output_format = "S16_LE";
};
};
};
users.users.shairport.uid = config.ids.uids.shairport;
users.groups.shairport.gid = config.ids.gids.shairport;
# shairport-sync (AirPlay 2) needs nqptp's PTP clock available before it
# starts.
systemd.services.shairport-sync = {
after = [ "nqptp.service" ];
wants = [ "nqptp.service" ];
};
# Pre-create the audio pipe owned by shairport and world-readable so the
# snapserver dynamic user can read it.
systemd.tmpfiles.rules = [
"d ${builtins.dirOf airplayPipe} 0755 shairport shairport -"
"p ${airplayPipe} 0644 shairport shairport -"
];
# AirPlay 2 keeps time against a PTP clock provided by nqptp, which shares it
# through /dev/shm for shairport-sync to read. It binds privileged UDP ports
# 319/320, so it runs as root.
systemd.services.nqptp = {
description = "nqptp PTP clock for AirPlay 2";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
ExecStart = lib.getExe' pkgs.nqptp "nqptp";
Restart = "on-failure";
};
};
# snapserver state (DynamicUser, so /var/lib/private) and shairport's AirPlay
# pairing identity both need to survive reboots on impermanence hosts.
custom.impermanence.extraDirs = lib.mkIf config.custom.impermanence.enable [
"/var/lib/private/snapserver"
"/var/lib/shairport-sync"
];
custom.www.nebula = {