diff --git a/modules/services/mpd.nix b/modules/services/mpd.nix index 3f05c22a336..a6ed0a48312 100644 --- a/modules/services/mpd.nix +++ b/modules/services/mpd.nix @@ -50,7 +50,7 @@ in { ''; }; - musicDirectory = mkOption { + musicDirectory = mkOption { type = with types; either path str; default = "${config.home.homeDirectory}/music"; defaultText = "$HOME/music"; @@ -96,7 +96,14 @@ in { ''; }; - network = { + network = { + startWhenNeeded = mkOption { + type = types.bool; + default = false; + description = '' + Enable systemd socket activation. + ''; + }; listenAddress = mkOption { type = types.str; @@ -143,7 +150,7 @@ in { Description = "Music Player Daemon"; }; - Install = { + Install = mkIf (!cfg.network.startWhenNeeded) { WantedBy = [ "default.target" ]; }; @@ -154,6 +161,23 @@ in { ExecStartPre = ''${pkgs.bash}/bin/bash -c "${pkgs.coreutils}/bin/mkdir -p '${cfg.dataDir}' '${cfg.playlistDirectory}'"''; }; }; + systemd.user.sockets.mpd = mkIf cfg.network.startWhenNeeded { + Socket = { + ListenStream = let + listen = + if cfg.network.listenAddress == "any" + then toString cfg.network.port + else "${cfg.network.listenAddress}:${toString cfg.network.port}"; + in [ listen "%t/mpd/socket" ]; + + Backlog = 5; + KeepAlive = true; + }; + + Install = { + WantedBy = [ "sockets.target" ]; + }; + }; }; }