1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
{
config,
lib,
pkgs,
...
}:
let
name = "mpd";
uid = config.ids.uids.mpd;
gid = config.ids.gids.mpd;
cfg = config.services.mpd;
mkKeyValue =
a:
lib.mapAttrsToList (
k: v:
k
+ " "
+ (
if builtins.isBool v then
# Mainly for https://mpd.readthedocs.io/en/stable/user.html#zeroconf
"\"" + (lib.boolToYesNo v) + "\""
else
"\"" + (toString v) + "\""
)
) a;
settings = lib.filterAttrs (n: v: !(isNull v)) cfg.settings;
nonBlockSettings = lib.filterAttrs (n: v: !(builtins.isAttrs v || builtins.isList v)) settings;
pureBlockSettings = removeAttrs settings (builtins.attrNames nonBlockSettings);
blocks =
pureBlockSettings
// lib.optionalAttrs cfg.fluidsynth {
decoder = (pureBlockSettings.decoder or [ ]) ++ [
{
plugin = "fluidsynth";
soundfont = "${pkgs.soundfont-fluid}/share/soundfonts/FluidR3_GM2-2.sf2";
}
];
};
processSingleBlock =
n: v:
[
(n + " {")
]
# Add indentation, for better readability
++ (map (l: " " + l) (mkKeyValue v))
++ [ "}" ];
mpdConf = pkgs.writeTextFile {
name = "mpd.conf";
text = ''
# 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.
''
+ lib.concatStringsSep "\n" (
mkKeyValue (
{
state_file = "${cfg.dataDir}/state";
sticker_file = "${cfg.dataDir}/sticker.sql";
}
// nonBlockSettings
)
++ lib.flatten (
lib.mapAttrsToList (
n: v: if builtins.isList v then (map (b: processSingleBlock n b) v) else (processSingleBlock n v)
) blocks
)
++ lib.imap0 (
i: a: "password \"{{password-${toString i}}}@${lib.concatStringsSep "," a.permissions}\""
) cfg.credentials
);
derivationArgs = {
expectScript = ''
spawn ${lib.getExe pkgs.buildPackages.mpd} --no-daemon "$env(out)"
expect {
"exception: Error in \"$env(out)\"" {
puts "Config file invalid\n"
exit 1
}
"exception:" {
exit 0
}
}
'';
};
checkPhase = ''
printf "%s" "$expectScript" | ${lib.getExe pkgs.buildPackages.expect} -f -
'';
};
in
{
###### interface
options = {
services.mpd = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to enable MPD, the music player daemon.
'';
};
startWhenNeeded = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
If set, {command}`mpd` is socket-activated; that
is, instead of having it permanently running as a daemon,
systemd will start it on the first incoming connection.
'';
};
user = lib.mkOption {
type = lib.types.str;
default = name;
description = "User account under which MPD runs.";
};
group = lib.mkOption {
type = lib.types.str;
default = name;
description = "Group account under which MPD runs.";
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/${name}";
description = ''
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.
'';
};
openFirewall = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = null;
description = ''
Open ports in the firewall for mpd. If `null` (default), you might
get a warning asking you to set it explicitly to `true` or `false`,
depending upon the value of `services.mpd.settings.bind_to_address`.
'';
};
settings = lib.mkOption {
type = lib.types.submodule {
freeformType =
let
inherit (lib.types)
oneOf
attrsOf
listOf
str
int
bool
path
;
atomType = oneOf [
str
int
bool
path
];
in
attrsOf (oneOf [
atomType
(listOf (attrsOf atomType))
]);
options = {
music_directory = lib.mkOption {
type = with lib.types; either path (strMatching "([a-z]+)://.+");
default = "${cfg.dataDir}/music";
defaultText = lib.literalExpression ''"''${dataDir}/music"'';
description = ''
The directory or URI 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.
'';
};
playlist_directory = lib.mkOption {
type = lib.types.path;
default = "${cfg.dataDir}/playlists";
defaultText = lib.literalExpression ''"''${dataDir}/playlists"'';
description = ''
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.
'';
};
bind_to_address = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
example = "any";
description = ''
The address for the daemon to listen on.
Use `any` to listen on all addresses.
'';
};
port = lib.mkOption {
type = lib.types.port;
default = 6600;
description = ''
This setting is the TCP port that is desired for the daemon to get assigned
to.
'';
};
db_file = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = "${cfg.dataDir}/tag_cache";
defaultText = lib.literalExpression ''"''${dataDir}/tag_cache"'';
description = ''
The path to MPD's database.
'';
};
};
};
default = { };
description = ''
Configuration for MPD. MPD supports key-value like blocks for settings
like `audio_output` and `neighbor`. Some of these blocks can be
specified multiple times, so the following configuration:
```txt
audio_output {
device "iec958:CARD=Intel,DEV=0"
mixer_control "PCM"
name "My specific ALSA output"
type "alsa"
}
audio_output {
mixer_type "null"
name "ALSA Null"
type "alsa"
}
audio_output {
name "The Pulse"
type "pulse"
}
```
Can be inserted with:
```nix
audio_output = [
{
type = "alsa";
name = "My specific ALSA output";
device = "iec958:CARD=Intel,DEV=0";
mixer_control = "PCM";
}
{
type = "alsa";
name = "ALSA Null";
mixer_type = "null";
}
{
type = "pulse";
name = "The Pulse";
}
];
```
'';
};
credentials = lib.mkOption {
type = lib.types.listOf (
lib.types.submodule {
options = {
passwordFile = lib.mkOption {
type = lib.types.path;
description = ''
Path to file containing the password.
'';
};
permissions =
let
perms = [
"read"
"add"
"player"
"control"
"admin"
];
in
lib.mkOption {
type = lib.types.listOf (lib.types.enum perms);
default = [ "read" ];
description = ''
List of permissions that are granted with this password.
Permissions can be "${lib.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"
"player"
"control"
"admin"
];
}
];
};
fluidsynth = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
If set, add fluidsynth soundfont `decoder` block.
'';
};
};
};
###### implementation
imports = [
(lib.mkRenamedOptionModule
[ "services" "mpd" "musicDirectory" ]
[ "services" "mpd" "settings" "music_directory" ]
)
(lib.mkRenamedOptionModule
[ "services" "mpd" "playlistDirectory" ]
[ "services" "mpd" "settings" "playlist_directory" ]
)
(lib.mkRenamedOptionModule [ "services" "mpd" "dbFile" ] [ "services" "mpd" "settings" "db_file" ])
(lib.mkRenamedOptionModule
[ "services" "mpd" "network" "listenAddress" ]
[ "services" "mpd" "settings" "bind_to_address" ]
)
(lib.mkRenamedOptionModule
[ "services" "mpd" "network" "port" ]
[ "services" "mpd" "settings" "port" ]
)
(lib.mkRemovedOptionModule
[
"services"
"mpd"
"extraConfig"
]
"services.mpd.extraConfig was replaced by the declarative services.mpd.settings option, per RFC42."
)
];
config = lib.mkIf cfg.enable {
warnings =
lib.optional
(
!(
(builtins.elem cfg.settings.bind_to_address [
"localhost"
"127.0.0.1"
])
|| (lib.hasPrefix "/" cfg.settings.bind_to_address)
)
&& (isNull cfg.openFirewall)
)
"Using '${cfg.settings.bind_to_address}' as services.mpd.settings.bind_to_address without enabling services.mpd.openFirewall, might prevent you from accessing MPD from other clients. To suppress this warning, set services.mpd.openFirewall explicitly to `false`";
# install mpd units
systemd.packages = [ pkgs.mpd ];
systemd.sockets.mpd = lib.mkIf cfg.startWhenNeeded {
wantedBy = [ "sockets.target" ];
listenStreams = [
"" # Note: this is needed to override the upstream unit
(
if pkgs.lib.hasPrefix "/" cfg.settings.bind_to_address then
cfg.settings.bind_to_address
else
"${
lib.optionalString (cfg.settings.bind_to_address != "any") "${cfg.settings.bind_to_address}:"
}${toString cfg.settings.port}"
)
];
};
systemd.services.mpd = {
wantedBy = lib.optional (!cfg.startWhenNeeded) "multi-user.target";
preStart = ''
set -euo pipefail
install -m 600 ${mpdConf} /run/mpd/mpd.conf
''
+ lib.optionalString (cfg.credentials != [ ]) (
lib.concatStringsSep "\n" (
lib.imap0 (
i: c:
"${pkgs.replace-secret}/bin/replace-secret '{{password-${toString i}}}' '${c.passwordFile}' /run/mpd/mpd.conf"
) cfg.credentials
)
);
serviceConfig = {
User = "${cfg.user}";
# Note: the first "" overrides the ExecStart from the upstream unit
ExecStart = [
""
"${pkgs.mpd}/bin/mpd --systemd /run/mpd/mpd.conf"
];
RuntimeDirectory = "mpd";
StateDirectory =
[ ]
++ lib.optionals (cfg.dataDir == "/var/lib/${name}") [ name ]
++ lib.optionals (cfg.settings.playlist_directory == "/var/lib/${name}/playlists") [
name
"${name}/playlists"
]
++ lib.optionals (cfg.settings.music_directory == "/var/lib/${name}/music") [
name
"${name}/music"
];
};
};
networking.firewall.allowedTCPPorts = lib.optionals (
builtins.isBool cfg.openFirewall && cfg.openFirewall
) [ cfg.settings.port ];
users.users = lib.optionalAttrs (cfg.user == name) {
${name} = {
inherit uid;
group = cfg.group;
extraGroups = [ "audio" ];
description = "Music Player Daemon user";
home = "${cfg.dataDir}";
};
};
users.groups = lib.optionalAttrs (cfg.group == name) {
${name}.gid = gid;
};
};
}
|