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
465
466
467
468
469
470
| | ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017 Peter Mikkelsen <petermikkelsen10@gmail.com>
;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2022 Bruno Victal <mirai@makinata.eu>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (gnu services audio)
#:use-module (guix gexp)
#:use-module (gnu services)
#:use-module (gnu services admin)
#:use-module (gnu services configuration)
#:use-module (gnu services shepherd)
#:use-module (gnu system shadow)
#:use-module (gnu packages admin)
#:use-module (gnu packages mpd)
#:use-module (guix records)
#:use-module (ice-9 match)
#:use-module (ice-9 format)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
#:export (mpd-output
mpd-output?
mpd-configuration
mpd-configuration?
mpd-service-type
mympd-service-type
mympd-configuration
mympd-configuration?
mympd-configuration-package
mympd-configuration-shepherd-requirement
mympd-configuration-user
mympd-configuration-group
mympd-configuration-work-directory
mympd-configuration-cache-directory
mympd-configuration-acl
mympd-configuration-covercache-ttl
mympd-configuration-http?
mympd-configuration-host
mympd-configuration-port
mympd-configuration-log-level
mympd-configuration-log-to
mympd-configuration-lualibs
mympd-configuration-script-acl
mympd-configuration-ssl?
mympd-configuration-ssl-port
mympd-configuration-ssl-cert
mympd-configuration-ssl-key
mympd-configuration-pin-hash
ip-acl
ip-acl?
ip-acl-allow
ip-acl-deny))
;;; Commentary:
;;;
;;; Audio related services
;;;
;;; Code:
(define-record-type* <mpd-output>
mpd-output make-mpd-output
mpd-output?
(type mpd-output-type
(default "pulse"))
(name mpd-output-name
(default "MPD"))
(enabled? mpd-output-enabled?
(default #t))
(tags? mpd-output-tags?
(default #t))
(always-on? mpd-output-always-on?
(default #f))
(mixer-type mpd-output-mixer-type
;; valid: hardware, software, null, none
(default #f))
(extra-options mpd-output-extra-options
(default '())))
(define-record-type* <mpd-configuration>
mpd-configuration make-mpd-configuration
mpd-configuration?
(user mpd-configuration-user
(default "mpd"))
(music-dir mpd-configuration-music-dir
(default "~/Music"))
(playlist-dir mpd-configuration-playlist-dir
(default "~/.mpd/playlists"))
(db-file mpd-configuration-db-file
(default "~/.mpd/tag_cache"))
(state-file mpd-configuration-state-file
(default "~/.mpd/state"))
(sticker-file mpd-configuration-sticker-file
(default "~/.mpd/sticker.sql"))
(port mpd-configuration-port
(default "6600"))
(address mpd-configuration-address
(default "any"))
(outputs mpd-configuration-outputs
(default (list (mpd-output)))))
(define (mpd-output->string output)
"Convert the OUTPUT of type <mpd-output> to a configuration file snippet."
(let ((extra (string-join
(map (match-lambda
((key . value)
(format #f " ~a \"~a\""
(string-map
(lambda (c) (if (char=? c #\-) #\_ c))
(symbol->string key))
value)))
(mpd-output-extra-options output))
"\n")))
(format #f "\
audio_output {
type \"~a\"
name \"~a\"
~:[ enabled \"no\"~%~;~]\
~:[ tags \"no\"~%~;~]\
~:[~; always_on \"yes\"~%~]\
~@[ mixer_type \"~a\"~%~]\
~a~%}~%"
(mpd-output-type output)
(mpd-output-name output)
(mpd-output-enabled? output)
(mpd-output-tags? output)
(mpd-output-always-on? output)
(mpd-output-mixer-type output)
extra)))
(define (mpd-config->file config)
(apply
mixed-text-file "mpd.conf"
"pid_file \"" (mpd-file-name config "pid") "\"\n"
(append (map mpd-output->string
(mpd-configuration-outputs config))
(map (match-lambda
((config-name config-val)
(string-append config-name " \"" (config-val config) "\"\n")))
`(("user" ,mpd-configuration-user)
("music_directory" ,mpd-configuration-music-dir)
("playlist_directory" ,mpd-configuration-playlist-dir)
("db_file" ,mpd-configuration-db-file)
("state_file" ,mpd-configuration-state-file)
("sticker_file" ,mpd-configuration-sticker-file)
("port" ,mpd-configuration-port)
("bind_to_address" ,mpd-configuration-address))))))
(define (mpd-file-name config file)
"Return a path in /var/run/mpd/ that is writable
by @code{user} from @code{config}."
(string-append "/var/run/mpd/"
(mpd-configuration-user config)
"/" file))
(define (mpd-shepherd-service config)
(shepherd-service
(documentation "Run the MPD (Music Player Daemon)")
(requirement '(user-processes))
(provision '(mpd))
(start #~(make-forkexec-constructor
(list #$(file-append mpd "/bin/mpd")
"--no-daemon"
#$(mpd-config->file config))
#:environment-variables
;; Required to detect PulseAudio when run under a user account.
(list (string-append
"XDG_RUNTIME_DIR=/run/user/"
(number->string
(passwd:uid
(getpwnam #$(mpd-configuration-user config))))))
#:log-file #$(mpd-file-name config "log")))
(stop #~(make-kill-destructor))))
(define (mpd-service-activation config)
(with-imported-modules '((guix build utils))
#~(begin
(use-modules (guix build utils))
(define %user
(getpw #$(mpd-configuration-user config)))
(let ((directory #$(mpd-file-name config ".mpd")))
(mkdir-p directory)
(chown directory (passwd:uid %user) (passwd:gid %user))
;; Make /var/run/mpd/USER user-owned as well.
(chown (dirname directory)
(passwd:uid %user) (passwd:gid %user))))))
(define %mpd-accounts
;; Default account and group for MPD.
(list (user-group (name "mpd") (system? #t))
(user-account
(name "mpd")
(group "mpd")
(system? #t)
(comment "Music Player Daemon (MPD) user")
;; Note: /var/run/mpd hosts one sub-directory per user, of which
;; /var/run/mpd/mpd corresponds to the "mpd" user.
(home-directory "/var/run/mpd/mpd")
(shell (file-append shadow "/sbin/nologin")))))
(define mpd-service-type
(service-type
(name 'mpd)
(description "Run the Music Player Daemon (MPD).")
(extensions
(list (service-extension shepherd-root-service-type
(compose list mpd-shepherd-service))
(service-extension account-service-type
(const %mpd-accounts))
(service-extension activation-service-type
mpd-service-activation)))
(default-value (mpd-configuration))))
\f
;;;
;;; myMPD
;;;
(define list-of-symbol?
(list-of symbol?))
(define list-of-string?
(list-of string?))
(define (port? n)
(and (integer? n)
(<= 0 n 65535)))
(define (string-or-symbol? x)
(or (symbol? x) (string? x)))
(define-configuration/no-serialization ip-acl
(allow
(list-of-string '())
"Allowed IP addresses.")
(deny
(list-of-string '())
"Disallowed IP addresses."))
(define-maybe/no-serialization port)
(define-maybe/no-serialization integer)
(define-maybe/no-serialization string)
(define-maybe/no-serialization ip-acl)
;; XXX: The serialization procedures are insufficient since we require
;; access to multiple fields at once.
;; Fields marked with empty-serializer are never serialized and are
;; used for command-line arguments or by the service definition.
(define-configuration/no-serialization mympd-configuration
(package
(file-like mympd)
"The package object of the myMPD server."
empty-serializer)
(shepherd-requirement
(list-of-symbol '())
"This is a list of symbols naming Shepherd services that this service
will depend on."
empty-serializer)
(user
(string "mympd")
"Owner of the @command{mympd} process."
empty-serializer)
(group
(string "nogroup")
"Owner group of the @command{mympd} process."
empty-serializer)
(work-directory
(string "/var/lib/mympd")
"Where myMPD will store its data."
empty-serializer)
(cache-directory
(string "/var/cache/mympd")
"Where myMPD will store its cache."
empty-serializer)
(acl
maybe-ip-acl
"ACL to access the myMPD webserver. See
@uref{https://jcorporation.github.io/myMPD/configuration/acl,myMPD ACL}
for syntax.")
(covercache-ttl
(maybe-integer 31)
"How long to keep cached covers, @code{0} disables cover caching.")
(http?
(boolean #t)
"HTTP support.")
(host
(string "[::]")
"Host name to listen on.")
(port
(maybe-port 80)
"HTTP port to listen on.")
(log-level
(integer 5)
"How much detail to include in logs, possible values: @code{0} to @code{7}.")
(log-to
(string-or-symbol "/var/log/mympd/log")
"Where to send logs. By default, the service logs to
@file{/var/log/mympd.log}. The alternative is @code{'syslog}, which
sends output to the running syslog service under the @samp{daemon} facility."
empty-serializer)
(lualibs
(maybe-string "all")
"See
@url{https://jcorporation.github.io/myMPD/scripting/#lua-standard-libraries}.")
(script-acl
(maybe-ip-acl (ip-acl
(allow '("127.0.0.1"))))
"ACL to access the myMPD script backend.")
(ssl?
(boolean #f)
"SSL/TLS support.")
(ssl-port
(maybe-port 443)
"Port to listen for HTTPS.")
(ssl-cert
maybe-string
"Path to PEM encoded X.509 SSL/TLS certificate (public key).")
(ssl-key
maybe-string
"Path to PEM encoded SSL/TLS private key.")
(pin-hash
maybe-string
"SHA-256 hashed pin used by myMPD to control settings access by
prompting a pin from the user."))
(define (mympd-serialize-configuration config)
(define serialize-value
(match-lambda
((? boolean? val) (if val "true" "false"))
((or (? port? val) (? integer? val)) (number->string val))
((? ip-acl? val) (ip-acl-serialize-configuration val))
((? string? val) val)))
(define (ip-acl-serialize-configuration config)
(define (serialize-list-of-string prefix lst)
(map (cut format #f "~a~a" prefix <>) lst))
(string-join
(append
(serialize-list-of-string "+" (ip-acl-allow config))
(serialize-list-of-string "-" (ip-acl-deny config))) ","))
;; myMPD configuration fields are serialized as individual files under
;; <work-directory>/config/.
(match-record config <mympd-configuration> (work-directory acl
covercache-ttl http? host port
log-level lualibs script-acl
ssl? ssl-port ssl-cert ssl-key
pin-hash)
(define (serialize-field filename value)
(when (maybe-value-set? value)
(list (format #f "~a/config/~a" work-directory filename)
(mixed-text-file filename (serialize-value value)))))
(let ((filename-to-field `(("acl" . ,acl)
("covercache_keep_days" . ,covercache-ttl)
("http" . ,http?)
("http_host" . ,host)
("http_port" . ,port)
("loglevel" . ,log-level)
("lualibs" . ,lualibs)
("scriptacl" . ,script-acl)
("ssl" . ,ssl?)
("ssl_port" . ,ssl-port)
("ssl_cert" . ,ssl-cert)
("ssl_key" . ,ssl-key)
("pin_hash" . ,pin-hash))))
(filter list?
(generic-serialize-alist list serialize-field
filename-to-field)))))
(define (mympd-shepherd-service config)
(match-record config <mympd-configuration> (package shepherd-requirement
user work-directory
cache-directory log-level log-to)
(let ((log-level* (format #f "MYMPD_LOGLEVEL=~a" log-level)))
(shepherd-service
(documentation "Run the myMPD daemon.")
(requirement `(loopback user-processes ,@shepherd-requirement))
(provision '(mympd))
(start #~(begin
(let* ((pw (getpwnam #$user))
(uid (passwd:uid pw))
(gid (passwd:gid pw)))
(for-each (lambda (dir)
(mkdir-p dir)
(chown dir uid gid))
(list #$work-directory #$cache-directory)))
(make-forkexec-constructor
`(#$(file-append package "/bin/mympd")
"--user" #$user
#$@(if (eqv? log-to 'syslog) '("--syslog") '())
"--workdir" #$work-directory
"--cachedir" #$cache-directory)
#:environment-variables (list #$log-level*)
#:log-file #$(if (string? log-to) log-to #f))))
(stop #~(make-kill-destructor))))))
(define (mympd-accounts config)
(match-record config <mympd-configuration> (user group)
(list (user-group (name group)
(system? #t))
(user-account (name user)
(group group)
(system? #t)
(comment "myMPD user")
(home-directory "/var/empty")
(shell (file-append shadow "/sbin/nologin"))))))
(define (mympd-log-rotation config)
(match-record config <mympd-configuration> (log-to)
(if (string? log-to)
(list (log-rotation
(files (list log-to))))
'())))
(define mympd-service-type
(service-type
(name 'mympd)
(extensions
(list (service-extension shepherd-root-service-type
(compose list mympd-shepherd-service))
(service-extension account-service-type
mympd-accounts)
(service-extension special-files-service-type
mympd-serialize-configuration)
(service-extension rottlog-service-type
mympd-log-rotation)))
(description "Run myMPD, a frontend for MPD. (Music Player Daemon)")
(default-value (mympd-configuration))))
|