diff --git a/modules/services/cloudflared.nix b/modules/services/cloudflared.nix index 9f6b894..1d413ae 100644 --- a/modules/services/cloudflared.nix +++ b/modules/services/cloudflared.nix @@ -1,5 +1,5 @@ { - den.quirks.tunnel.description = "cloudflared ingress: { subdomain, port }"; + den.quirks.tunnel.description = "cloudflared ingress: { subdomain, port, scheme ? \"http\" }"; # per host: den.hosts...cloudflared = { uuid = "..."; protocol = "http2"; # optional }; den.aspects.services.cloudflared = { @@ -20,7 +20,7 @@ ingress = lib.listToAttrs (map (t: { name = "${t.subdomain}.${domain}"; - value = "http://127.0.0.1:${toString t.port}"; + value = "${t.scheme or "http"}://127.0.0.1:${toString t.port}"; }) tunnel); }; }; diff --git a/modules/services/mpd.nix b/modules/services/mpd.nix index 7cd7359..951a13e 100644 --- a/modules/services/mpd.nix +++ b/modules/services/mpd.nix @@ -1,41 +1,43 @@ { + # MPD on the server: plays ~/Music into an Opus HTTP stream, no local speakers. + # music. = audio stream, mpd. = MPD protocol (via `cloudflared access tcp` on clients). + # Password: ~/.config/mpd/password (same file on every device, chmod 600). den.aspects.services.mpd = { - homeManager = { - services.mpd-mpris.enable = true; - }; + tunnel = [ + { subdomain = "music"; port = 8000; } + { subdomain = "mpd"; port = 6600; scheme = "tcp"; } + ]; nixos = { host, config, ... }: let user = host.owner; home = config.users.users.${user}.home; in { - services = { - mpd = { - enable = true; + services.mpd = { + enable = true; + inherit user; - settings = { - music_directory = "${home}/Music"; + credentials = [{ + passwordFile = "${home}/.config/mpd/password"; + permissions = [ "read" "add" "control" "admin" ]; + }]; - decoder = [ - { - plugin = "ffmpeg"; - enabled = "yes"; - } - { - plugin = "opus"; - enabled = "no"; - } - ]; + settings = { + music_directory = "${home}/Music"; - audio_output = [{ - type = "pipewire"; - name = "PipeWire Sound Server"; - }]; - }; + decoder = [ + { plugin = "ffmpeg"; enabled = "yes"; } + { plugin = "opus"; enabled = "no"; } + ]; - inherit user; - }; - }; - - systemd.services = { - mpd.environment = { - XDG_RUNTIME_DIR = "/run/user/1000"; + audio_output = [{ + type = "httpd"; + name = "stream"; + encoder = "opus"; + bitrate = "128000"; + format = "48000:16:2"; + bind_to_address = "127.0.0.1"; + port = "8000"; + always_on = "yes"; + tags = "yes"; + max_clients = "0"; + }]; }; }; }; diff --git a/modules/software/music/default.nix b/modules/software/music/default.nix index a901a8f..4a38fbe 100644 --- a/modules/software/music/default.nix +++ b/modules/software/music/default.nix @@ -1,13 +1,69 @@ -{ den, ... }: { +{ + # Remote MPD client side: MPD + stream live on the server (services/mpd.nix). + # mpd-forward: mpd. -> 127.0.0.1:6600 through the cloudflare tunnel (works on any network). + # mpd-listen: plays music. while MPD is playing, stops when it pauses/stops (all devices in sync). + # Password: ~/.config/mpd/password, same as on the server. den.aspects.software.music = { - includes = [ den.aspects.services.mpd ]; + homeManager = { pkgs, lib, host, config, ... }: let + pwFile = "${config.xdg.configHome}/mpd/password"; + service = desc: exec: { + Unit = { Description = desc; After = [ "network-online.target" ]; }; + Service = { ExecStart = exec; Restart = "always"; RestartSec = 5; }; + Install.WantedBy = [ "default.target" ]; + }; - homeManager = { pkgs, ... }: { + listen = pkgs.writeShellApplication { + name = "mpd-listen"; + runtimeInputs = with pkgs; [ mpc mpv coreutils gnugrep ]; + text = '' + MPD_HOST="$(cat ${pwFile})@127.0.0.1" + export MPD_HOST + pid= + while true; do + if mpc status 2>/dev/null | grep -q '^\[playing\]'; then + if [ -z "$pid" ] || ! kill -0 "$pid" 2>/dev/null; then + mpv --no-video --no-terminal --really-quiet https://music.${host.domain} & + pid=$! + fi + elif [ -n "$pid" ]; then + kill "$pid" 2>/dev/null || true + pid= + fi + # ponytail: polls every 30s on top of idle events, catches a dropped stream or forward + timeout 30 mpc idle player >/dev/null 2>&1 || sleep 2 + done + ''; + }; + in { home.packages = with pkgs; [ audacious nicotine-plus songrec + euphonica + mpc ]; + + # Euphonica talks to the local forward; the password is entered once in its settings (stored in the keyring) + dconf.settings."io/github/htkhiem/Euphonica/client" = { + mpd-host = "127.0.0.1"; + mpd-port = lib.hm.gvariant.mkUint32 6600; + mpd-use-unix-socket = false; + }; + + systemd.user.services = { + mpd-forward = service "MPD over cloudflare tunnel" + "${lib.getExe pkgs.cloudflared} access tcp --hostname mpd.${host.domain} --url 127.0.0.1:6600"; + + mpd-listen = service "Play the MPD stream while MPD is playing" (lib.getExe listen) // { + Unit = { Description = "Play the MPD stream while MPD is playing"; After = [ "mpd-forward.service" ]; Wants = [ "mpd-forward.service" ]; }; + }; + + mpd-mpris = service "MPRIS for remote MPD" + "${lib.getExe pkgs.mpd-mpris} -host 127.0.0.1 -pwd-file ${pwFile} -no-instance" // { + Unit = { Description = "MPRIS for remote MPD"; After = [ "mpd-forward.service" "graphical-session.target" ]; PartOf = [ "graphical-session.target" ]; }; + Install.WantedBy = [ "graphical-session.target" ]; + }; + }; }; nixos = {