nixos/hosts/cerithium-telescopium/audio-server.nix

94 lines
2.6 KiB
Nix

# Configure snapcast and everything related
# This configures the bluetooth driver as a sink, connecting every bluetooth
# connection to the snapserver, streaming the input
{
pkgs,
lib,
...
}: let
snapcastPipewireFifo = "/run/snapserver/pipewire";
in {
# === Mopidy ===
# services.mopidy = {
# enable = true;
# extensionPackages = [
# pkgs.mopidy-mpd
# pkgs.mopidy-iris
# ];
# configuration = ''
# [http]
# enabled = true
# hostname = 0.0.0.0
# port = 6680
# [audio]
# mixer = software
# mixer_volume = 100
# output = audioconvert ! audio/x-raw,rate=48000,channels=2,format=S16LE ! wavenc ! filesink location=/run/snapserver/mopidy
# [file]
# enabled = false
# '';
# };
# networking.firewall.allowedTCPPorts = [6680];
# === Snapcast Server ===
services.snapserver = {
enable = true;
openFirewall = true;
http.docRoot = "${pkgs.snapweb}/";
buffer = 500;
streams = {
pipewire = {
type = "pipe";
codec = "flac";
location = snapcastPipewireFifo;
};
};
};
systemd.tmpfiles.settings."99-snapserver"."/run/snapserver".d = {};
# === Pipewire ===
# Create a sink that writes everything to the snapcast fifo
services.pipewire.extraConfig.pipewire."20-snapcast-sink"."context.modules" = lib.singleton {
name = "libpipewire-module-pipe-tunnel";
args = {
"tunnel.mode" = "sink";
"pipe.filename" = snapcastPipewireFifo;
"audio.format" = "S16LE";
"audio.rate" = "48000";
"audio.channels" = "2";
"audio.position" = ["FL" "FR"];
"stream.props" = {
"node.name" = "Snapcast";
};
};
};
# Instruct pipewire to connect bluetooth input streams to the Snapcast sink
services.pipewire.extraConfig.pipewire."21-bluetooth-to-snapcast"."node.rules" = lib.singleton {
matches = lib.singleton {"node.name" = "~bluez_input.*";};
actions.update-props."target.object" = "Snapcast";
};
# === Bluetooth input ===
hardware.bluetooth.settings = {
# Configure the bluetooth adapter to be an audio sink
General.Enable = "Source";
General.DiscoverableTimeout = 0;
General.AlwaysPairable = true;
General.JustWorksRepairing = "always";
};
environment.etc."bluetooth/pin-file".text = ''
* 124356
'';
systemd.user.services.bluetooth-agent = {
wantedBy = [
"default.target"
];
serviceConfig = {
ExecStart = "${pkgs.bluez-tools}/bin/bt-agent --pin /etc/bluetooth/pin-file --capability NoInputNoOutput";
Slice = "session.slice";
};
};
}