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)
35 ;;; Audio related services
39 (define-record-type* <mpd-output>
40 mpd-output make-mpd-output
46 (enabled? mpd-output-enabled?
48 (tags? mpd-output-tags?
50 (always-on? mpd-output-always-on?
52 (mixer-type mpd-output-mixer-type
53 ;; valid: hardware, software, null, none
55 (extra-options mpd-output-extra-options
58 (define-record-type* <mpd-configuration>
59 mpd-configuration make-mpd-configuration
61 (user mpd-configuration-user
63 (music-dir mpd-configuration-music-dir
65 (playlist-dir mpd-configuration-playlist-dir
66 (default "~/.mpd/playlists"))
67 (db-file mpd-configuration-db-file
68 (default "~/.mpd/tag_cache"))
69 (state-file mpd-configuration-state-file
70 (default "~/.mpd/state"))
71 (sticker-file mpd-configuration-sticker-file
72 (default "~/.mpd/sticker.sql"))
73 (port mpd-configuration-port
75 (address mpd-configuration-address
77 (outputs mpd-configuration-outputs
78 (default (list (mpd-output)))))
80 (define (mpd-output->string output)
81 "Convert the OUTPUT of type <mpd-output> to a configuration file snippet."
82 (let ((extra (string-join
85 (format #f " ~a \"~a\""
87 (lambda (c) (if (char=? c #\-) #\_ c))
90 (mpd-output-extra-options output))
96 ~:[ enabled \"no\"~%~;~]\
97 ~:[ tags \"no\"~%~;~]\
98 ~:[~; always_on \"yes\"~%~]\
99 ~@[ mixer_type \"~a\"~%~]\
101 (mpd-output-type output)
102 (mpd-output-name output)
103 (mpd-output-enabled? output)
104 (mpd-output-tags? output)
105 (mpd-output-always-on? output)
106 (mpd-output-mixer-type output)
109 (define (mpd-config->file config)
111 mixed-text-file "mpd.conf"
112 "pid_file \"" (mpd-file-name config "pid") "\"\n"
113 (append (map mpd-output->string
114 (mpd-configuration-outputs config))
116 ((config-name config-val)
117 (string-append config-name " \"" (config-val config) "\"\n")))
118 `(("user" ,mpd-configuration-user)
119 ("music_directory" ,mpd-configuration-music-dir)
120 ("playlist_directory" ,mpd-configuration-playlist-dir)
121 ("db_file" ,mpd-configuration-db-file)
122 ("state_file" ,mpd-configuration-state-file)
123 ("sticker_file" ,mpd-configuration-sticker-file)
124 ("port" ,mpd-configuration-port)
125 ("bind_to_address" ,mpd-configuration-address))))))
127 (define (mpd-file-name config file)
128 "Return a path in /var/run/mpd/ that is writable
129 by @code{user} from @code{config}."
130 (string-append "/var/run/mpd/"
131 (mpd-configuration-user config)
134 (define (mpd-shepherd-service config)
136 (documentation "Run the MPD (Music Player Daemon)")
138 (start #~(make-forkexec-constructor
139 (list #$(file-append mpd "/bin/mpd")
141 #$(mpd-config->file config))
142 #:pid-file #$(mpd-file-name config "pid")
143 #:environment-variables
144 ;; Required to detect PulseAudio when run under a user account.
146 "XDG_RUNTIME_DIR=/run/user/"
149 (getpwnam (mpd-configuration-user config))))))
150 #:log-file #$(mpd-file-name config "log")))
151 (stop #~(make-kill-destructor))))
153 (define (mpd-service-activation config)
154 (with-imported-modules '((guix build utils))
156 (use-modules (guix build utils))
158 (getpw #$(mpd-configuration-user config)))
160 (let ((directory #$(mpd-file-name config "")))
162 (chown directory (passwd:uid %user) (passwd:gid %user))))))
164 (define mpd-service-type
167 (description "Run the Music Player Daemon (MPD).")
169 (list (service-extension shepherd-root-service-type
170 (compose list mpd-shepherd-service))
171 (service-extension activation-service-type
172 mpd-service-activation)))
173 (default-value (mpd-configuration))))