From c8746fb58808aa4378b4e2c376c6b031c2230f3b Mon Sep 17 00:00:00 2001 From: arcnmx Date: Fri, 2 Oct 2020 13:50:48 -0700 Subject: [PATCH] mpd: systemd socket activation --- modules/services/mpd.nix | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/modules/services/mpd.nix b/modules/services/mpd.nix index 3f05c22a3..a6ed0a483 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" ]; + }; + }; }; }