services: Split ladspa-service-type from pulseaudio-service-type.
[jackhill/guix/guix.git] / gnu / services / sound.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2020 Oleg Pykhalov <go.wigust@gmail.com>
3 ;;; Copyright © 2020 Leo Prikler <leo.prikler@student.tugraz.at>
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 sound)
21 #:use-module (gnu services base)
22 #:use-module (gnu services configuration)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu services)
25 #:use-module (gnu system pam)
26 #:use-module (gnu system shadow)
27 #:use-module (guix gexp)
28 #:use-module (guix packages)
29 #:use-module (guix records)
30 #:use-module (guix store)
31 #:use-module (gnu packages audio)
32 #:use-module (gnu packages linux)
33 #:use-module (gnu packages pulseaudio)
34 #:use-module (ice-9 match)
35 #:export (alsa-configuration
36 alsa-service-type
37
38 pulseaudio-configuration
39 pulseaudio-service-type
40
41 ladspa-configuration
42 ladspa-service-type))
43
44 ;;; Commentary:
45 ;;;
46 ;;; Sound services.
47 ;;;
48 ;;; Code:
49
50 \f
51 ;;;
52 ;;; ALSA
53 ;;;
54
55 (define-record-type* <alsa-configuration>
56 alsa-configuration make-alsa-configuration alsa-configuration?
57 (alsa-plugins alsa-configuration-alsa-plugins ;<package>
58 (default alsa-plugins))
59 (pulseaudio? alsa-configuration-pulseaudio? ;boolean
60 (default #t))
61 (extra-options alsa-configuration-extra-options ;string
62 (default "")))
63
64 (define alsa-config-file
65 ;; Return the ALSA configuration file.
66 (match-lambda
67 (($ <alsa-configuration> alsa-plugins pulseaudio? extra-options)
68 (apply mixed-text-file "asound.conf"
69 `("# Generated by 'alsa-service'.\n\n"
70 ,@(if pulseaudio?
71 `("# Use PulseAudio by default
72 pcm_type.pulse {
73 lib \"" ,#~(string-append #$alsa-plugins:pulseaudio
74 "/lib/alsa-lib/libasound_module_pcm_pulse.so") "\"
75 }
76
77 ctl_type.pulse {
78 lib \"" ,#~(string-append #$alsa-plugins:pulseaudio
79 "/lib/alsa-lib/libasound_module_ctl_pulse.so") "\"
80 }
81
82 pcm.!default {
83 type pulse
84 fallback \"sysdefault\"
85 hint {
86 show on
87 description \"Default ALSA Output (currently PulseAudio Sound Server)\"
88 }
89 }
90
91 ctl.!default {
92 type pulse
93 fallback \"sysdefault\"
94 }\n\n")
95 '())
96 ,extra-options)))))
97
98 (define (alsa-etc-service config)
99 (list `("asound.conf" ,(alsa-config-file config))))
100
101 (define alsa-service-type
102 (service-type
103 (name 'alsa)
104 (extensions
105 (list (service-extension etc-service-type alsa-etc-service)))
106 (default-value (alsa-configuration))
107 (description "Configure low-level Linux sound support, ALSA.")))
108
109 \f
110 ;;;
111 ;;; PulseAudio
112 ;;;
113
114 (define-record-type* <pulseaudio-configuration>
115 pulseaudio-configuration make-pulseaudio-configuration
116 pulseaudio-configuration?
117 (client-conf pulseaudio-client-conf
118 (default '()))
119 (daemon-conf pulseaudio-daemon-conf
120 ;; Flat volumes may cause unpleasant experiences to users
121 ;; when applications inadvertently max out the system volume
122 ;; (see e.g. <https://bugs.gnu.org/38172>).
123 (default '((flat-volumes . no))))
124 (script-file pulseaudio-script-file
125 (default (file-append pulseaudio "/etc/pulse/default.pa")))
126 (system-script-file pulseaudio-system-script-file
127 (default
128 (file-append pulseaudio "/etc/pulse/system.pa"))))
129
130 (define (pulseaudio-environment config)
131 `(;; Define these variables, so that pulseaudio honors /etc.
132 ("PULSE_CONFIG" . "/etc/pulse/daemon.conf")
133 ("PULSE_CLIENTCONFIG" . "/etc/pulse/client.conf")))
134
135 (define (pulseaudio-conf-entry arg)
136 (match arg
137 ((key . value)
138 (format #f "~a = ~s~%" key value))
139 ((? string? _)
140 (string-append arg "\n"))))
141
142 (define pulseaudio-etc
143 (match-lambda
144 (($ <pulseaudio-configuration> client-conf daemon-conf
145 default-script-file system-script-file)
146 `(("pulse"
147 ,(file-union
148 "pulse"
149 `(("client.conf"
150 ,(apply mixed-text-file "client.conf"
151 (map pulseaudio-conf-entry client-conf)))
152 ("daemon.conf"
153 ,(apply mixed-text-file "daemon.conf"
154 "default-script-file = " default-script-file "\n"
155 (map pulseaudio-conf-entry daemon-conf)))
156 ("default.pa" ,default-script-file)
157 ("system.pa" ,system-script-file))))))))
158
159 (define pulseaudio-service-type
160 (service-type
161 (name 'pulseaudio)
162 (extensions
163 (list (service-extension session-environment-service-type
164 pulseaudio-environment)
165 (service-extension etc-service-type pulseaudio-etc)))
166 (default-value (pulseaudio-configuration))
167 (description "Configure PulseAudio sound support.")))
168
169 \f
170 ;;;
171 ;;; LADSPA
172 ;;;
173
174 (define-record-type* <ladspa-configuration>
175 ladspa-configuration make-ladspa-configuration
176 ladspa-configuration?
177 (plugins ladspa-plugins (default '())))
178
179 (define (ladspa-environment config)
180 ;; Define this variable in the global environment such that
181 ;; pulseaudio swh-plugins (and similar LADSPA plugins) work.
182 `(("LADSPA_PATH" .
183 (string-join
184 ',(map (lambda (package) (file-append package "/lib/ladspa"))
185 (ladspa-plugins config))
186 ":"))))
187
188 (define ladspa-service-type
189 (service-type
190 (name 'ladspa)
191 (extensions
192 (list (service-extension session-environment-service-type
193 ladspa-environment)))
194 (default-value (ladspa-configuration))
195 (description "Configure LADSPA plugins.")))
196
197 ;;; sound.scm ends here