1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Peter Mikkelsen <petermikkelsen10@gmail.com>
3 ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
4 ;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
21 (define-module (gnu services audio)
22 #:use-module (guix gexp)
23 #:use-module (gnu services)
24 #:use-module (gnu services shepherd)
25 #:use-module (gnu system shadow)
26 #:use-module (gnu packages admin)
27 #:use-module (gnu packages mpd)
28 #:use-module (guix records)
29 #:use-module (ice-9 match)
30 #:use-module (ice-9 format)
39 ;;; Audio related services
43 (define-record-type* <mpd-output>
44 mpd-output make-mpd-output
50 (enabled? mpd-output-enabled?
52 (tags? mpd-output-tags?
54 (always-on? mpd-output-always-on?
56 (mixer-type mpd-output-mixer-type
57 ;; valid: hardware, software, null, none
59 (extra-options mpd-output-extra-options
62 (define-record-type* <mpd-configuration>
63 mpd-configuration make-mpd-configuration
65 (user mpd-configuration-user
67 (music-dir mpd-configuration-music-dir
69 (playlist-dir mpd-configuration-playlist-dir
70 (default "~/.mpd/playlists"))
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"))
77 (port mpd-configuration-port
79 (address mpd-configuration-address
81 (outputs mpd-configuration-outputs
82 (default (list (mpd-output)))))
84 (define (mpd-output->string output)
85 "Convert the OUTPUT of type <mpd-output> to a configuration file snippet."
86 (let ((extra (string-join
89 (format #f " ~a \"~a\""
91 (lambda (c) (if (char=? c #\-) #\_ c))
94 (mpd-output-extra-options output))
100 ~:[ enabled \"no\"~%~;~]\
101 ~:[ tags \"no\"~%~;~]\
102 ~:[~; always_on \"yes\"~%~]\
103 ~@[ mixer_type \"~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)
113 (define (mpd-config->file config)
115 mixed-text-file "mpd.conf"
116 "pid_file \"" (mpd-file-name config "pid") "\"\n"
117 (append (map mpd-output->string
118 (mpd-configuration-outputs config))
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))))))
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)
138 (define (mpd-shepherd-service config)
140 (documentation "Run the MPD (Music Player Daemon)")
141 (requirement '(user-processes))
143 (start #~(make-forkexec-constructor
144 (list #$(file-append mpd "/bin/mpd")
146 #$(mpd-config->file config))
147 #:environment-variables
148 ;; Required to detect PulseAudio when run under a user account.
150 "XDG_RUNTIME_DIR=/run/user/"
153 (getpwnam #$(mpd-configuration-user config))))))
154 #:log-file #$(mpd-file-name config "log")))
155 (stop #~(make-kill-destructor))))
157 (define (mpd-service-activation config)
158 (with-imported-modules '((guix build utils))
160 (use-modules (guix build utils))
162 (getpw #$(mpd-configuration-user config)))
164 (let ((directory #$(mpd-file-name config ".mpd")))
166 (chown directory (passwd:uid %user) (passwd:gid %user))
168 ;; Make /var/run/mpd/USER user-owned as well.
169 (chown (dirname directory)
170 (passwd:uid %user) (passwd:gid %user))))))
173 (define %mpd-accounts
174 ;; Default account and group for MPD.
175 (list (user-group (name "mpd") (system? #t))
180 (comment "Music Player Daemon (MPD) user")
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")
186 (shell (file-append shadow "/sbin/nologin")))))
188 (define mpd-service-type
191 (description "Run the Music Player Daemon (MPD).")
193 (list (service-extension shepherd-root-service-type
194 (compose list mpd-shepherd-service))
195 (service-extension account-service-type
196 (const %mpd-accounts))
197 (service-extension activation-service-type
198 mpd-service-activation)))
199 (default-value (mpd-configuration))))