Commit | Line | Data |
---|---|---|
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> |
06465d2b PM |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
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. | |
11 | ;;; | |
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. | |
16 | ;;; | |
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/>. | |
19 | ||
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) | |
878e0e1b RW |
27 | #:export (mpd-output |
28 | mpd-output? | |
29 | mpd-configuration | |
06465d2b PM |
30 | mpd-configuration? |
31 | mpd-service-type)) | |
32 | ||
33 | ;;; Commentary: | |
34 | ;;; | |
35 | ;;; Audio related services | |
36 | ;;; | |
37 | ;;; Code: | |
38 | ||
878e0e1b RW |
39 | (define-record-type* <mpd-output> |
40 | mpd-output make-mpd-output | |
41 | mpd-output? | |
42 | (type mpd-output-type | |
43 | (default "pulse")) | |
44 | (name mpd-output-name | |
45 | (default "MPD")) | |
46 | (enabled? mpd-output-enabled? | |
47 | (default #t)) | |
48 | (tags? mpd-output-tags? | |
49 | (default #t)) | |
50 | (always-on? mpd-output-always-on? | |
51 | (default #f)) | |
52 | (mixer-type mpd-output-mixer-type | |
53 | ;; valid: hardware, software, null, none | |
54 | (default #f)) | |
55 | (extra-options mpd-output-extra-options | |
56 | (default '()))) | |
57 | ||
06465d2b PM |
58 | (define-record-type* <mpd-configuration> |
59 | mpd-configuration make-mpd-configuration | |
60 | mpd-configuration? | |
61 | (user mpd-configuration-user | |
62 | (default "mpd")) | |
63 | (music-dir mpd-configuration-music-dir | |
64 | (default "~/Music")) | |
65 | (playlist-dir mpd-configuration-playlist-dir | |
66 | (default "~/.mpd/playlists")) | |
c71b7b5b | 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")) | |
06465d2b PM |
73 | (port mpd-configuration-port |
74 | (default "6600")) | |
75 | (address mpd-configuration-address | |
878e0e1b RW |
76 | (default "any")) |
77 | (outputs mpd-configuration-outputs | |
78 | (default (list (mpd-output))))) | |
79 | ||
80 | (define (mpd-output->string output) | |
81 | "Convert the OUTPUT of type <mpd-output> to a configuration file snippet." | |
82 | (let ((extra (string-join | |
83 | (map (match-lambda | |
84 | ((key . value) | |
85 | (format #f " ~a \"~a\"" | |
86 | (string-map | |
87 | (lambda (c) (if (char=? c #\-) #\_ c)) | |
88 | (symbol->string key)) | |
89 | value))) | |
90 | (mpd-output-extra-options output)) | |
91 | "\n"))) | |
92 | (format #f "\ | |
93 | audio_output { | |
94 | type \"~a\" | |
95 | name \"~a\" | |
96 | ~:[ enabled \"no\"~%~;~]\ | |
97 | ~:[ tags \"no\"~%~;~]\ | |
98 | ~:[~; always_on \"yes\"~%~]\ | |
99 | ~@[ mixer_type \"~a\"~%~]\ | |
100 | ~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) | |
107 | extra))) | |
06465d2b PM |
108 | |
109 | (define (mpd-config->file config) | |
110 | (apply | |
111 | mixed-text-file "mpd.conf" | |
b6dc69af | 112 | "pid_file \"" (mpd-file-name config "pid") "\"\n" |
878e0e1b RW |
113 | (append (map mpd-output->string |
114 | (mpd-configuration-outputs config)) | |
115 | (map (match-lambda | |
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)))))) | |
06465d2b | 126 | |
b6dc69af PM |
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) | |
132 | "/" file)) | |
133 | ||
134 | (define (mpd-shepherd-service config) | |
06465d2b PM |
135 | (shepherd-service |
136 | (documentation "Run the MPD (Music Player Daemon)") | |
137 | (provision '(mpd)) | |
138 | (start #~(make-forkexec-constructor | |
139 | (list #$(file-append mpd "/bin/mpd") | |
140 | "--no-daemon" | |
141 | #$(mpd-config->file config)) | |
b6dc69af | 142 | #:pid-file #$(mpd-file-name config "pid") |
970cb5ce RS |
143 | #:environment-variables |
144 | ;; Required to detect PulseAudio when run under a user account. | |
145 | '(#$(string-append | |
146 | "XDG_RUNTIME_DIR=/run/user/" | |
147 | (number->string | |
148 | (passwd:uid | |
149 | (getpwnam (mpd-configuration-user config)))))) | |
b6dc69af | 150 | #:log-file #$(mpd-file-name config "log"))) |
06465d2b PM |
151 | (stop #~(make-kill-destructor)))) |
152 | ||
b6dc69af PM |
153 | (define (mpd-service-activation config) |
154 | (with-imported-modules '((guix build utils)) | |
155 | #~(begin | |
156 | (use-modules (guix build utils)) | |
157 | (define %user | |
158 | (getpw #$(mpd-configuration-user config))) | |
159 | ||
160 | (let ((directory #$(mpd-file-name config ""))) | |
161 | (mkdir-p directory) | |
162 | (chown directory (passwd:uid %user) (passwd:gid %user)))))) | |
163 | ||
06465d2b PM |
164 | (define mpd-service-type |
165 | (service-type | |
166 | (name 'mpd) | |
21b71b01 | 167 | (description "Run the Music Player Daemon (MPD).") |
06465d2b PM |
168 | (extensions |
169 | (list (service-extension shepherd-root-service-type | |
b6dc69af PM |
170 | (compose list mpd-shepherd-service)) |
171 | (service-extension activation-service-type | |
172 | mpd-service-activation))) | |
06465d2b | 173 | (default-value (mpd-configuration)))) |