2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2011-12-05 01:51:05 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2011-12-05 01:51:05 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2017-02-16 00:17:44 +00:00
|
|
|
name = "mpd";
|
|
|
|
|
2011-12-05 01:51:05 +00:00
|
|
|
uid = config.ids.uids.mpd;
|
|
|
|
gid = config.ids.gids.mpd;
|
2013-01-27 19:21:04 +00:00
|
|
|
cfg = config.services.mpd;
|
2011-12-05 01:51:05 +00:00
|
|
|
|
2020-12-23 23:04:10 +00:00
|
|
|
credentialsPlaceholder = (creds:
|
|
|
|
let
|
|
|
|
placeholders = (imap0
|
|
|
|
(i: c: ''password "{{password-${toString i}}}@${concatStringsSep "," c.permissions}"'')
|
|
|
|
creds);
|
|
|
|
in
|
|
|
|
concatStringsSep "\n" placeholders);
|
|
|
|
|
2013-01-27 19:21:04 +00:00
|
|
|
mpdConf = pkgs.writeText "mpd.conf" ''
|
2020-09-10 14:51:28 +01:00
|
|
|
# This file was automatically generated by NixOS. Edit mpd's configuration
|
|
|
|
# via NixOS' configuration.nix, as this file will be rewritten upon mpd's
|
|
|
|
# restart.
|
|
|
|
|
2013-01-27 19:21:04 +00:00
|
|
|
music_directory "${cfg.musicDirectory}"
|
2017-08-20 21:34:34 +01:00
|
|
|
playlist_directory "${cfg.playlistDirectory}"
|
2018-06-01 12:37:43 +01:00
|
|
|
${lib.optionalString (cfg.dbFile != null) ''
|
|
|
|
db_file "${cfg.dbFile}"
|
|
|
|
''}
|
2013-01-27 19:21:04 +00:00
|
|
|
state_file "${cfg.dataDir}/state"
|
|
|
|
sticker_file "${cfg.dataDir}/sticker.sql"
|
2015-03-14 12:08:22 +00:00
|
|
|
|
2015-12-23 23:04:04 +00:00
|
|
|
${optionalString (cfg.network.listenAddress != "any") ''bind_to_address "${cfg.network.listenAddress}"''}
|
2015-03-14 12:08:22 +00:00
|
|
|
${optionalString (cfg.network.port != 6600) ''port "${toString cfg.network.port}"''}
|
2020-05-08 15:38:16 +01:00
|
|
|
${optionalString (cfg.fluidsynth) ''
|
|
|
|
decoder {
|
|
|
|
plugin "fluidsynth"
|
|
|
|
soundfont "${pkgs.soundfont-fluid}/share/soundfonts/FluidR3_GM2-2.sf2"
|
|
|
|
}
|
|
|
|
''}
|
2015-03-14 12:08:22 +00:00
|
|
|
|
2021-01-10 21:56:39 +00:00
|
|
|
${optionalString (cfg.credentials != []) (credentialsPlaceholder cfg.credentials)}
|
2020-12-23 23:04:10 +00:00
|
|
|
|
2013-01-27 19:21:04 +00:00
|
|
|
${cfg.extraConfig}
|
2014-09-14 23:06:57 +01:00
|
|
|
'';
|
2011-12-05 01:51:05 +00:00
|
|
|
|
2013-01-27 19:21:04 +00:00
|
|
|
in {
|
2011-12-05 01:51:05 +00:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
2014-09-14 23:06:57 +01:00
|
|
|
options = {
|
2011-12-05 01:51:05 +00:00
|
|
|
|
2014-09-14 23:06:57 +01:00
|
|
|
services.mpd = {
|
2011-12-05 01:51:05 +00:00
|
|
|
|
|
|
|
enable = mkOption {
|
2016-09-09 01:45:53 +01:00
|
|
|
type = types.bool;
|
2011-12-05 01:51:05 +00:00
|
|
|
default = false;
|
|
|
|
description = ''
|
2013-01-27 19:21:04 +00:00
|
|
|
Whether to enable MPD, the music player daemon.
|
2014-09-14 23:06:57 +01:00
|
|
|
'';
|
|
|
|
};
|
2013-01-27 19:21:04 +00:00
|
|
|
|
2017-06-30 22:51:42 +01:00
|
|
|
startWhenNeeded = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
If set, <command>mpd</command> is socket-activated; that
|
|
|
|
is, instead of having it permanently running as a daemon,
|
|
|
|
systemd will start it on the first incoming connection.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-01-27 19:21:04 +00:00
|
|
|
musicDirectory = mkOption {
|
2018-07-21 22:19:34 +01:00
|
|
|
type = with types; either path (strMatching "(http|https|nfs|smb)://.+");
|
2013-01-27 19:21:04 +00:00
|
|
|
default = "${cfg.dataDir}/music";
|
2021-01-24 09:19:10 +00:00
|
|
|
defaultText = "\${dataDir}/music";
|
2013-01-27 19:21:04 +00:00
|
|
|
description = ''
|
2020-11-25 13:14:48 +00:00
|
|
|
The directory or NFS/SMB network share where MPD reads music from. If left
|
|
|
|
as the default value this directory will automatically be created before
|
|
|
|
the MPD server starts, otherwise the sysadmin is responsible for ensuring
|
|
|
|
the directory exists with appropriate ownership and permissions.
|
2014-09-14 23:06:57 +01:00
|
|
|
'';
|
|
|
|
};
|
2013-01-27 19:21:04 +00:00
|
|
|
|
2017-08-20 21:34:34 +01:00
|
|
|
playlistDirectory = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "${cfg.dataDir}/playlists";
|
2021-01-24 09:19:10 +00:00
|
|
|
defaultText = "\${dataDir}/playlists";
|
2017-08-20 21:34:34 +01:00
|
|
|
description = ''
|
2020-11-25 13:14:48 +00:00
|
|
|
The directory where MPD stores playlists. If left as the default value
|
|
|
|
this directory will automatically be created before the MPD server starts,
|
|
|
|
otherwise the sysadmin is responsible for ensuring the directory exists
|
|
|
|
with appropriate ownership and permissions.
|
2017-08-20 21:34:34 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-01-27 19:21:04 +00:00
|
|
|
extraConfig = mkOption {
|
2016-10-23 18:33:41 +01:00
|
|
|
type = types.lines;
|
2014-09-14 23:06:57 +01:00
|
|
|
default = "";
|
2013-01-27 19:21:04 +00:00
|
|
|
description = ''
|
|
|
|
Extra directives added to to the end of MPD's configuration file,
|
|
|
|
mpd.conf. Basic configuration like file location and uid/gid
|
2017-02-16 00:17:44 +00:00
|
|
|
is added automatically to the beginning of the file. For available
|
|
|
|
options see <literal>man 5 mpd.conf</literal>'.
|
2014-09-14 23:06:57 +01:00
|
|
|
'';
|
|
|
|
};
|
2011-12-05 01:51:05 +00:00
|
|
|
|
|
|
|
dataDir = mkOption {
|
2016-09-09 01:45:53 +01:00
|
|
|
type = types.path;
|
2017-02-16 00:17:44 +00:00
|
|
|
default = "/var/lib/${name}";
|
2011-12-05 01:51:05 +00:00
|
|
|
description = ''
|
2020-11-25 13:14:48 +00:00
|
|
|
The directory where MPD stores its state, tag cache, playlists etc. If
|
|
|
|
left as the default value this directory will automatically be created
|
|
|
|
before the MPD server starts, otherwise the sysadmin is responsible for
|
|
|
|
ensuring the directory exists with appropriate ownership and permissions.
|
2014-09-14 23:06:57 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-03-08 10:12:20 +00:00
|
|
|
user = mkOption {
|
2016-09-09 01:45:53 +01:00
|
|
|
type = types.str;
|
2017-02-16 00:17:44 +00:00
|
|
|
default = name;
|
2015-03-08 10:12:20 +00:00
|
|
|
description = "User account under which MPD runs.";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
2016-09-09 01:45:53 +01:00
|
|
|
type = types.str;
|
2017-02-16 00:17:44 +00:00
|
|
|
default = name;
|
2015-03-08 10:12:20 +00:00
|
|
|
description = "Group account under which MPD runs.";
|
|
|
|
};
|
|
|
|
|
2014-09-14 23:06:57 +01:00
|
|
|
network = {
|
|
|
|
|
2015-12-23 23:04:04 +00:00
|
|
|
listenAddress = mkOption {
|
2016-09-09 01:45:53 +01:00
|
|
|
type = types.str;
|
2017-01-01 12:46:39 +00:00
|
|
|
default = "127.0.0.1";
|
|
|
|
example = "any";
|
2014-09-14 23:06:57 +01:00
|
|
|
description = ''
|
2017-01-01 12:46:39 +00:00
|
|
|
The address for the daemon to listen on.
|
|
|
|
Use <literal>any</literal> to listen on all addresses.
|
2014-09-14 23:06:57 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2016-09-09 01:45:53 +01:00
|
|
|
type = types.int;
|
2014-09-14 23:06:57 +01:00
|
|
|
default = 6600;
|
|
|
|
description = ''
|
|
|
|
This setting is the TCP port that is desired for the daemon to get assigned
|
|
|
|
to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2015-04-10 22:10:14 +01:00
|
|
|
|
|
|
|
dbFile = mkOption {
|
2018-06-01 12:37:43 +01:00
|
|
|
type = types.nullOr types.str;
|
2015-04-10 22:10:14 +01:00
|
|
|
default = "${cfg.dataDir}/tag_cache";
|
2021-01-24 09:19:10 +00:00
|
|
|
defaultText = "\${dataDir}/tag_cache";
|
2015-04-10 22:10:14 +01:00
|
|
|
description = ''
|
2018-06-01 12:37:43 +01:00
|
|
|
The path to MPD's database. If set to <literal>null</literal> the
|
2018-06-03 08:55:26 +01:00
|
|
|
parameter is omitted from the configuration.
|
2015-04-10 22:10:14 +01:00
|
|
|
'';
|
|
|
|
};
|
2020-05-08 15:38:16 +01:00
|
|
|
|
2020-12-23 23:04:10 +00:00
|
|
|
credentials = mkOption {
|
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = ''
|
|
|
|
Path to file containing the password.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
permissions = let
|
|
|
|
perms = ["read" "add" "control" "admin"];
|
|
|
|
in mkOption {
|
|
|
|
type = types.listOf (types.enum perms);
|
|
|
|
default = [ "read" ];
|
|
|
|
description = ''
|
|
|
|
List of permissions that are granted with this password.
|
|
|
|
Permissions can be "${concatStringsSep "\", \"" perms}".
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
description = ''
|
|
|
|
Credentials and permissions for accessing the mpd server.
|
|
|
|
'';
|
|
|
|
default = [];
|
|
|
|
example = [
|
|
|
|
{passwordFile = "/var/lib/secrets/mpd_readonly_password"; permissions = [ "read" ];}
|
|
|
|
{passwordFile = "/var/lib/secrets/mpd_admin_password"; permissions = ["read" "add" "control" "admin"];}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2020-05-08 15:38:16 +01:00
|
|
|
fluidsynth = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
If set, add fluidsynth soundfont and configure the plugin.
|
|
|
|
'';
|
|
|
|
};
|
2014-09-14 23:06:57 +01:00
|
|
|
};
|
2011-12-05 01:51:05 +00:00
|
|
|
|
2014-09-14 23:06:57 +01:00
|
|
|
};
|
2011-12-05 01:51:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2013-01-27 19:21:04 +00:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2017-06-30 22:51:42 +01:00
|
|
|
systemd.sockets.mpd = mkIf cfg.startWhenNeeded {
|
|
|
|
description = "Music Player Daemon Socket";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
listenStreams = [
|
|
|
|
"${optionalString (cfg.network.listenAddress != "any") "${cfg.network.listenAddress}:"}${toString cfg.network.port}"
|
|
|
|
];
|
|
|
|
socketConfig = {
|
|
|
|
Backlog = 5;
|
|
|
|
KeepAlive = true;
|
|
|
|
PassCredentials = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-01-27 19:21:04 +00:00
|
|
|
systemd.services.mpd = {
|
|
|
|
after = [ "network.target" "sound.target" ];
|
|
|
|
description = "Music Player Daemon";
|
2017-06-30 22:51:42 +01:00
|
|
|
wantedBy = optional (!cfg.startWhenNeeded) "multi-user.target";
|
2016-09-09 01:49:09 +01:00
|
|
|
|
2020-11-25 13:14:48 +00:00
|
|
|
serviceConfig = mkMerge [
|
|
|
|
{
|
|
|
|
User = "${cfg.user}";
|
2020-12-23 23:04:10 +00:00
|
|
|
ExecStart = "${pkgs.mpd}/bin/mpd --no-daemon /run/mpd/mpd.conf";
|
|
|
|
ExecStartPre = pkgs.writeShellScript "mpd-start-pre" ''
|
2020-12-26 16:53:01 +00:00
|
|
|
set -euo pipefail
|
|
|
|
install -m 600 ${mpdConf} /run/mpd/mpd.conf
|
2021-01-10 21:56:39 +00:00
|
|
|
${optionalString (cfg.credentials != [])
|
|
|
|
"${pkgs.replace}/bin/replace-literal -fe ${
|
2020-12-23 23:04:10 +00:00
|
|
|
concatStringsSep " -a " (imap0 (i: c: "\"{{password-${toString i}}}\" \"$(cat ${c.passwordFile})\"") cfg.credentials)
|
2021-01-10 21:56:39 +00:00
|
|
|
} /run/mpd/mpd.conf"}
|
2020-11-25 13:14:48 +00:00
|
|
|
'';
|
2020-12-23 23:04:10 +00:00
|
|
|
RuntimeDirectory = "mpd";
|
2020-11-25 13:14:48 +00:00
|
|
|
Type = "notify";
|
|
|
|
LimitRTPRIO = 50;
|
|
|
|
LimitRTTIME = "infinity";
|
|
|
|
ProtectSystem = true;
|
|
|
|
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" ];
|
|
|
|
})
|
|
|
|
];
|
2013-01-27 19:21:04 +00:00
|
|
|
};
|
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.users = optionalAttrs (cfg.user == name) {
|
|
|
|
${name} = {
|
|
|
|
inherit uid;
|
|
|
|
group = cfg.group;
|
|
|
|
extraGroups = [ "audio" ];
|
|
|
|
description = "Music Player Daemon user";
|
|
|
|
home = "${cfg.dataDir}";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups = optionalAttrs (cfg.group == name) {
|
|
|
|
${name}.gid = gid;
|
|
|
|
};
|
2011-12-05 01:51:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|