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