services: 'references-file' depends on Guile-Gcrypt.
[jackhill/guix/guix.git] / gnu / services / audio.scm
CommitLineData
06465d2b
PM
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Peter Mikkelsen <petermikkelsen10@gmail.com>
878e0e1b 3;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
7209d7cb 4;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
06465d2b
PM
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (gnu services audio)
22 #:use-module (guix gexp)
23 #:use-module (gnu services)
24 #:use-module (gnu services shepherd)
7209d7cb
LC
25 #:use-module (gnu system shadow)
26 #:use-module (gnu packages admin)
06465d2b
PM
27 #:use-module (gnu packages mpd)
28 #:use-module (guix records)
29 #:use-module (ice-9 match)
ad263045 30 #:use-module (ice-9 format)
878e0e1b
RW
31 #:export (mpd-output
32 mpd-output?
33 mpd-configuration
06465d2b
PM
34 mpd-configuration?
35 mpd-service-type))
36
37;;; Commentary:
38;;;
39;;; Audio related services
40;;;
41;;; Code:
42
878e0e1b
RW
43(define-record-type* <mpd-output>
44 mpd-output make-mpd-output
45 mpd-output?
46 (type mpd-output-type
47 (default "pulse"))
48 (name mpd-output-name
49 (default "MPD"))
50 (enabled? mpd-output-enabled?
51 (default #t))
52 (tags? mpd-output-tags?
53 (default #t))
54 (always-on? mpd-output-always-on?
55 (default #f))
56 (mixer-type mpd-output-mixer-type
57 ;; valid: hardware, software, null, none
58 (default #f))
59 (extra-options mpd-output-extra-options
60 (default '())))
61
06465d2b
PM
62(define-record-type* <mpd-configuration>
63 mpd-configuration make-mpd-configuration
64 mpd-configuration?
65 (user mpd-configuration-user
66 (default "mpd"))
67 (music-dir mpd-configuration-music-dir
68 (default "~/Music"))
69 (playlist-dir mpd-configuration-playlist-dir
70 (default "~/.mpd/playlists"))
c71b7b5b 71 (db-file mpd-configuration-db-file
72 (default "~/.mpd/tag_cache"))
73 (state-file mpd-configuration-state-file
74 (default "~/.mpd/state"))
75 (sticker-file mpd-configuration-sticker-file
76 (default "~/.mpd/sticker.sql"))
06465d2b
PM
77 (port mpd-configuration-port
78 (default "6600"))
79 (address mpd-configuration-address
878e0e1b
RW
80 (default "any"))
81 (outputs mpd-configuration-outputs
82 (default (list (mpd-output)))))
83
84(define (mpd-output->string output)
85 "Convert the OUTPUT of type <mpd-output> to a configuration file snippet."
86 (let ((extra (string-join
87 (map (match-lambda
88 ((key . value)
89 (format #f " ~a \"~a\""
90 (string-map
91 (lambda (c) (if (char=? c #\-) #\_ c))
92 (symbol->string key))
93 value)))
94 (mpd-output-extra-options output))
95 "\n")))
96 (format #f "\
97audio_output {
98 type \"~a\"
99 name \"~a\"
100~:[ enabled \"no\"~%~;~]\
101~:[ tags \"no\"~%~;~]\
102~:[~; always_on \"yes\"~%~]\
103~@[ mixer_type \"~a\"~%~]\
104~a~%}~%"
105 (mpd-output-type output)
106 (mpd-output-name output)
107 (mpd-output-enabled? output)
108 (mpd-output-tags? output)
109 (mpd-output-always-on? output)
110 (mpd-output-mixer-type output)
111 extra)))
06465d2b
PM
112
113(define (mpd-config->file config)
114 (apply
115 mixed-text-file "mpd.conf"
b6dc69af 116 "pid_file \"" (mpd-file-name config "pid") "\"\n"
878e0e1b
RW
117 (append (map mpd-output->string
118 (mpd-configuration-outputs config))
119 (map (match-lambda
120 ((config-name config-val)
121 (string-append config-name " \"" (config-val config) "\"\n")))
122 `(("user" ,mpd-configuration-user)
123 ("music_directory" ,mpd-configuration-music-dir)
124 ("playlist_directory" ,mpd-configuration-playlist-dir)
125 ("db_file" ,mpd-configuration-db-file)
126 ("state_file" ,mpd-configuration-state-file)
127 ("sticker_file" ,mpd-configuration-sticker-file)
128 ("port" ,mpd-configuration-port)
129 ("bind_to_address" ,mpd-configuration-address))))))
06465d2b 130
b6dc69af
PM
131(define (mpd-file-name config file)
132 "Return a path in /var/run/mpd/ that is writable
133 by @code{user} from @code{config}."
134 (string-append "/var/run/mpd/"
135 (mpd-configuration-user config)
136 "/" file))
137
138(define (mpd-shepherd-service config)
06465d2b
PM
139 (shepherd-service
140 (documentation "Run the MPD (Music Player Daemon)")
bb124f6e 141 (requirement '(user-processes))
06465d2b
PM
142 (provision '(mpd))
143 (start #~(make-forkexec-constructor
144 (list #$(file-append mpd "/bin/mpd")
145 "--no-daemon"
146 #$(mpd-config->file config))
970cb5ce
RS
147 #:environment-variables
148 ;; Required to detect PulseAudio when run under a user account.
0c5d0c57
MC
149 (list (string-append
150 "XDG_RUNTIME_DIR=/run/user/"
151 (number->string
970cb5ce 152 (passwd:uid
0c5d0c57 153 (getpwnam #$(mpd-configuration-user config))))))
b6dc69af 154 #:log-file #$(mpd-file-name config "log")))
06465d2b
PM
155 (stop #~(make-kill-destructor))))
156
b6dc69af
PM
157(define (mpd-service-activation config)
158 (with-imported-modules '((guix build utils))
159 #~(begin
160 (use-modules (guix build utils))
161 (define %user
162 (getpw #$(mpd-configuration-user config)))
163
bb124f6e 164 (let ((directory #$(mpd-file-name config ".mpd")))
b6dc69af 165 (mkdir-p directory)
ce3b5e5a
LC
166 (chown directory (passwd:uid %user) (passwd:gid %user))
167
168 ;; Make /var/run/mpd/USER user-owned as well.
169 (chown (dirname directory)
170 (passwd:uid %user) (passwd:gid %user))))))
b6dc69af 171
7209d7cb
LC
172
173(define %mpd-accounts
174 ;; Default account and group for MPD.
175 (list (user-group (name "mpd") (system? #t))
176 (user-account
177 (name "mpd")
178 (group "mpd")
179 (system? #t)
180 (comment "Music Player Daemon (MPD) user")
181
182 ;; Note: /var/run/mpd hosts one sub-directory per user, of which
183 ;; /var/run/mpd/mpd corresponds to the "mpd" user.
184 (home-directory "/var/run/mpd/mpd")
185
186 (shell (file-append shadow "/sbin/nologin")))))
187
06465d2b
PM
188(define mpd-service-type
189 (service-type
190 (name 'mpd)
21b71b01 191 (description "Run the Music Player Daemon (MPD).")
06465d2b
PM
192 (extensions
193 (list (service-extension shepherd-root-service-type
b6dc69af 194 (compose list mpd-shepherd-service))
7209d7cb
LC
195 (service-extension account-service-type
196 (const %mpd-accounts))
b6dc69af
PM
197 (service-extension activation-service-type
198 mpd-service-activation)))
06465d2b 199 (default-value (mpd-configuration))))