1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Peter Mikkelsen <petermikkelsen10@gmail.com>
3 ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services audio)
21 #:use-module (guix gexp)
22 #:use-module (gnu services)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu packages mpd)
25 #:use-module (guix records)
26 #:use-module (ice-9 match)
27 #:use-module (ice-9 format)
36 ;;; Audio related services
40 (define-record-type* <mpd-output>
41 mpd-output make-mpd-output
47 (enabled? mpd-output-enabled?
49 (tags? mpd-output-tags?
51 (always-on? mpd-output-always-on?
53 (mixer-type mpd-output-mixer-type
54 ;; valid: hardware, software, null, none
56 (extra-options mpd-output-extra-options
59 (define-record-type* <mpd-configuration>
60 mpd-configuration make-mpd-configuration
62 (user mpd-configuration-user
64 (music-dir mpd-configuration-music-dir
66 (playlist-dir mpd-configuration-playlist-dir
67 (default "~/.mpd/playlists"))
68 (db-file mpd-configuration-db-file
69 (default "~/.mpd/tag_cache"))
70 (state-file mpd-configuration-state-file
71 (default "~/.mpd/state"))
72 (sticker-file mpd-configuration-sticker-file
73 (default "~/.mpd/sticker.sql"))
74 (port mpd-configuration-port
76 (address mpd-configuration-address
78 (outputs mpd-configuration-outputs
79 (default (list (mpd-output)))))
81 (define (mpd-output->string output)
82 "Convert the OUTPUT of type <mpd-output> to a configuration file snippet."
83 (let ((extra (string-join
86 (format #f " ~a \"~a\""
88 (lambda (c) (if (char=? c #\-) #\_ c))
91 (mpd-output-extra-options output))
97 ~:[ enabled \"no\"~%~;~]\
98 ~:[ tags \"no\"~%~;~]\
99 ~:[~; always_on \"yes\"~%~]\
100 ~@[ mixer_type \"~a\"~%~]\
102 (mpd-output-type output)
103 (mpd-output-name output)
104 (mpd-output-enabled? output)
105 (mpd-output-tags? output)
106 (mpd-output-always-on? output)
107 (mpd-output-mixer-type output)
110 (define (mpd-config->file config)
112 mixed-text-file "mpd.conf"
113 "pid_file \"" (mpd-file-name config "pid") "\"\n"
114 (append (map mpd-output->string
115 (mpd-configuration-outputs config))
117 ((config-name config-val)
118 (string-append config-name " \"" (config-val config) "\"\n")))
119 `(("user" ,mpd-configuration-user)
120 ("music_directory" ,mpd-configuration-music-dir)
121 ("playlist_directory" ,mpd-configuration-playlist-dir)
122 ("db_file" ,mpd-configuration-db-file)
123 ("state_file" ,mpd-configuration-state-file)
124 ("sticker_file" ,mpd-configuration-sticker-file)
125 ("port" ,mpd-configuration-port)
126 ("bind_to_address" ,mpd-configuration-address))))))
128 (define (mpd-file-name config file)
129 "Return a path in /var/run/mpd/ that is writable
130 by @code{user} from @code{config}."
131 (string-append "/var/run/mpd/"
132 (mpd-configuration-user config)
135 (define (mpd-shepherd-service config)
137 (documentation "Run the MPD (Music Player Daemon)")
139 (start #~(make-forkexec-constructor
140 (list #$(file-append mpd "/bin/mpd")
142 #$(mpd-config->file config))
143 #:pid-file #$(mpd-file-name config "pid")
144 #:environment-variables
145 ;; Required to detect PulseAudio when run under a user account.
147 "XDG_RUNTIME_DIR=/run/user/"
150 (getpwnam (mpd-configuration-user config))))))
151 #:log-file #$(mpd-file-name config "log")))
152 (stop #~(make-kill-destructor))))
154 (define (mpd-service-activation config)
155 (with-imported-modules '((guix build utils))
157 (use-modules (guix build utils))
159 (getpw #$(mpd-configuration-user config)))
161 (let ((directory #$(mpd-file-name config "")))
163 (chown directory (passwd:uid %user) (passwd:gid %user))))))
165 (define mpd-service-type
168 (description "Run the Music Player Daemon (MPD).")
170 (list (service-extension shepherd-root-service-type
171 (compose list mpd-shepherd-service))
172 (service-extension activation-service-type
173 mpd-service-activation)))
174 (default-value (mpd-configuration))))