1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 uid = config.ids.uids.mpd;
8 gid = config.ids.gids.mpd;
9 cfg = config.services.mpd;
10
11 mpdConf = pkgs.writeText "mpd.conf" ''
12 music_directory "${cfg.musicDirectory}"
13 playlist_directory "${cfg.dataDir}/playlists"
14 db_file "${cfg.dbFile}"
15 state_file "${cfg.dataDir}/state"
16 sticker_file "${cfg.dataDir}/sticker.sql"
17 log_file "syslog"
18 user "${cfg.user}"
19 group "${cfg.group}"
20
21 ${optionalString (cfg.network.host != "any") ''bind_to_address "${cfg.network.host}"''}
22 ${optionalString (cfg.network.port != 6600) ''port "${toString cfg.network.port}"''}
23
24 ${cfg.extraConfig}
25 '';
26
27in {
28
29 ###### interface
30
31 options = {
32
33 services.mpd = {
34
35 enable = mkOption {
36 default = false;
37 description = ''
38 Whether to enable MPD, the music player daemon.
39 '';
40 };
41
42 musicDirectory = mkOption {
43 default = "${cfg.dataDir}/music";
44 description = ''
45 The directory where mpd reads music from.
46 '';
47 };
48
49 extraConfig = mkOption {
50 default = "";
51 description = ''
52 Extra directives added to to the end of MPD's configuration file,
53 mpd.conf. Basic configuration like file location and uid/gid
54 is added automatically to the beginning of the file.
55 '';
56 };
57
58 dataDir = mkOption {
59 default = "/var/lib/mpd";
60 description = ''
61 The directory where MPD stores its state, tag cache,
62 playlists etc.
63 '';
64 };
65
66 user = mkOption {
67 default = "mpd";
68 description = "User account under which MPD runs.";
69 };
70
71 group = mkOption {
72 default = "mpd";
73 description = "Group account under which MPD runs.";
74 };
75
76 network = {
77
78 host = mkOption {
79 default = "any";
80 description = ''
81 This setting sets the address for the daemon to listen on. Careful attention
82 should be paid if this is assigned to anything other then the default, any.
83 This setting can deny access to control of the daemon.
84 '';
85 };
86
87 port = mkOption {
88 default = 6600;
89 description = ''
90 This setting is the TCP port that is desired for the daemon to get assigned
91 to.
92 '';
93 };
94
95 };
96
97 dbFile = mkOption {
98 type = types.str;
99 default = "${cfg.dataDir}/tag_cache";
100 description = ''
101 The path to MPD's database.
102 '';
103 };
104 };
105
106 };
107
108
109 ###### implementation
110
111 config = mkIf cfg.enable {
112
113 systemd.services.mpd = {
114 after = [ "network.target" "sound.target" ];
115 description = "Music Player Daemon";
116 wantedBy = [ "multi-user.target" ];
117 path = [ pkgs.mpd ];
118 preStart = "mkdir -p ${cfg.dataDir} && chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}";
119 script = "exec mpd --no-daemon ${mpdConf}";
120 serviceConfig = {
121 User = "${cfg.user}";
122 PermissionsStartOnly = true;
123 };
124 };
125
126 users.extraUsers = optionalAttrs (cfg.user == "mpd") (singleton {
127 inherit uid;
128 name = "mpd";
129 group = cfg.group;
130 extraGroups = [ "audio" ];
131 description = "Music Player Daemon user";
132 home = "${cfg.dataDir}";
133 });
134
135 users.extraGroups = optionalAttrs (cfg.group == "mpd") (singleton {
136 name = "mpd";
137 gid = gid;
138 });
139 };
140
141}