{ # 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 = { 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" ]; }; 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 = { services.flatpak.packages = [ "org.pipewire.Helvum" ]; }; }; }