nixos/mpd: use upstream units

This commit is contained in:
rnhmjoj
2022-01-11 13:07:21 +01:00
parent b0f154fd44
commit 746e627b40

View File

@@ -209,62 +209,42 @@ in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
# install mpd units
systemd.packages = [ pkgs.mpd ];
systemd.sockets.mpd = mkIf cfg.startWhenNeeded { systemd.sockets.mpd = mkIf cfg.startWhenNeeded {
description = "Music Player Daemon Socket";
wantedBy = [ "sockets.target" ]; wantedBy = [ "sockets.target" ];
listenStreams = [ listenStreams = [
(if pkgs.lib.hasPrefix "/" cfg.network.listenAddress (if pkgs.lib.hasPrefix "/" cfg.network.listenAddress
then cfg.network.listenAddress then cfg.network.listenAddress
else "${optionalString (cfg.network.listenAddress != "any") "${cfg.network.listenAddress}:"}${toString cfg.network.port}") else "${optionalString (cfg.network.listenAddress != "any") "${cfg.network.listenAddress}:"}${toString cfg.network.port}")
]; ];
socketConfig = {
Backlog = 5;
KeepAlive = true;
PassCredentials = true;
};
}; };
systemd.services.mpd = { systemd.services.mpd = {
after = [ "network.target" "sound.target" ];
description = "Music Player Daemon";
wantedBy = optional (!cfg.startWhenNeeded) "multi-user.target"; wantedBy = optional (!cfg.startWhenNeeded) "multi-user.target";
serviceConfig = mkMerge [ preStart =
''
set -euo pipefail
install -m 600 ${mpdConf} /run/mpd/mpd.conf
'' + optionalString (cfg.credentials != [])
(concatStringsSep "\n"
(imap0
(i: c: ''${pkgs.replace-secret}/bin/replace-secret '{{password-${toString i}}}' '${c.passwordFile}' /run/mpd/mpd.conf'')
cfg.credentials));
serviceConfig =
{ {
User = "${cfg.user}"; User = "${cfg.user}";
ExecStart = "${pkgs.mpd}/bin/mpd --no-daemon /run/mpd/mpd.conf"; # Note: the first "" overrides the ExecStart from the upstream unit
ExecStartPre = pkgs.writeShellScript "mpd-start-pre" ('' ExecStart = [ "" "${pkgs.mpd}/bin/mpd --systemd /run/mpd/mpd.conf" ];
set -euo pipefail
install -m 600 ${mpdConf} /run/mpd/mpd.conf
'' + optionalString (cfg.credentials != [])
(concatStringsSep "\n"
(imap0
(i: c: ''${pkgs.replace-secret}/bin/replace-secret '{{password-${toString i}}}' '${c.passwordFile}' /run/mpd/mpd.conf'')
cfg.credentials))
);
RuntimeDirectory = "mpd"; RuntimeDirectory = "mpd";
Type = "notify"; StateDirectory = []
LimitRTPRIO = 50; ++ optionals (cfg.dataDir == "/var/lib/${name}") [ name ]
LimitRTTIME = "infinity"; ++ optionals (cfg.playlistDirectory == "/var/lib/${name}/playlists") [ name "${name}/playlists" ]
ProtectSystem = true; ++ optionals (cfg.musicDirectory == "/var/lib/${name}/music") [ name "${name}/music" ];
NoNewPrivileges = true; };
ProtectKernelTunables = true;
ProtectControlGroups = true;
ProtectKernelModules = true;
RestrictAddressFamilies = "AF_INET AF_INET6 AF_UNIX AF_NETLINK";
RestrictNamespaces = true;
Restart = "always";
}
(mkIf (cfg.dataDir == "/var/lib/${name}") {
StateDirectory = [ name ];
})
(mkIf (cfg.playlistDirectory == "/var/lib/${name}/playlists") {
StateDirectory = [ name "${name}/playlists" ];
})
(mkIf (cfg.musicDirectory == "/var/lib/${name}/music") {
StateDirectory = [ name "${name}/music" ];
})
];
}; };
users.users = optionalAttrs (cfg.user == name) { users.users = optionalAttrs (cfg.user == name) {