sound.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. ;;; Copyright © 2020 Marius Bakke <mbakke@fastmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  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. pulseaudio-configuration
  38. pulseaudio-service-type
  39. ladspa-configuration
  40. ladspa-service-type))
  41. ;;; Commentary:
  42. ;;;
  43. ;;; Sound services.
  44. ;;;
  45. ;;; Code:
  46. ;;;
  47. ;;; ALSA
  48. ;;;
  49. (define-record-type* <alsa-configuration>
  50. alsa-configuration make-alsa-configuration alsa-configuration?
  51. (alsa-plugins alsa-configuration-alsa-plugins ;<package>
  52. (default alsa-plugins))
  53. (pulseaudio? alsa-configuration-pulseaudio? ;boolean
  54. (default #t))
  55. (extra-options alsa-configuration-extra-options ;string
  56. (default "")))
  57. (define alsa-config-file
  58. ;; Return the ALSA configuration file.
  59. (match-lambda
  60. (($ <alsa-configuration> alsa-plugins pulseaudio? extra-options)
  61. (apply mixed-text-file "asound.conf"
  62. `("# Generated by 'alsa-service'.\n\n"
  63. ,@(if pulseaudio?
  64. `("# Use PulseAudio by default
  65. pcm_type.pulse {
  66. lib \"" ,#~(string-append #$alsa-plugins:pulseaudio
  67. "/lib/alsa-lib/libasound_module_pcm_pulse.so") "\"
  68. }
  69. ctl_type.pulse {
  70. lib \"" ,#~(string-append #$alsa-plugins:pulseaudio
  71. "/lib/alsa-lib/libasound_module_ctl_pulse.so") "\"
  72. }
  73. pcm.!default {
  74. type pulse
  75. fallback \"sysdefault\"
  76. hint {
  77. show on
  78. description \"Default ALSA Output (currently PulseAudio Sound Server)\"
  79. }
  80. }
  81. ctl.!default {
  82. type pulse
  83. fallback \"sysdefault\"
  84. }\n\n")
  85. '())
  86. ,extra-options)))))
  87. (define (alsa-etc-service config)
  88. (list `("asound.conf" ,(alsa-config-file config))))
  89. (define alsa-service-type
  90. (service-type
  91. (name 'alsa)
  92. (extensions
  93. (list (service-extension etc-service-type alsa-etc-service)))
  94. (default-value (alsa-configuration))
  95. (description "Configure low-level Linux sound support, ALSA.")))
  96. ;;;
  97. ;;; PulseAudio
  98. ;;;
  99. (define-record-type* <pulseaudio-configuration>
  100. pulseaudio-configuration make-pulseaudio-configuration
  101. pulseaudio-configuration?
  102. (client-conf pulseaudio-client-conf
  103. (default '()))
  104. (daemon-conf pulseaudio-daemon-conf
  105. ;; Flat volumes may cause unpleasant experiences to users
  106. ;; when applications inadvertently max out the system volume
  107. ;; (see e.g. <https://bugs.gnu.org/38172>).
  108. (default '((flat-volumes . no))))
  109. (script-file pulseaudio-script-file
  110. (default (file-append pulseaudio "/etc/pulse/default.pa")))
  111. (system-script-file pulseaudio-system-script-file
  112. (default
  113. (file-append pulseaudio "/etc/pulse/system.pa"))))
  114. (define (pulseaudio-conf-entry arg)
  115. (match arg
  116. ((key . value)
  117. (format #f "~a = ~s~%" key value))
  118. ((? string? _)
  119. (string-append arg "\n"))))
  120. (define pulseaudio-environment
  121. (match-lambda
  122. (($ <pulseaudio-configuration> client-conf daemon-conf default-script-file)
  123. `(("PULSE_CONFIG" . ,(apply mixed-text-file "daemon.conf"
  124. "default-script-file = " default-script-file "\n"
  125. (map pulseaudio-conf-entry daemon-conf)))
  126. ("PULSE_CLIENTCONFIG" . ,(apply mixed-text-file "client.conf"
  127. (map pulseaudio-conf-entry client-conf)))))))
  128. (define pulseaudio-etc
  129. (match-lambda
  130. (($ <pulseaudio-configuration> _ _ default-script-file system-script-file)
  131. `(("pulse"
  132. ,(file-union
  133. "pulse"
  134. `(("default.pa" ,default-script-file)
  135. ("system.pa" ,system-script-file))))))))
  136. (define pulseaudio-service-type
  137. (service-type
  138. (name 'pulseaudio)
  139. (extensions
  140. (list (service-extension session-environment-service-type
  141. pulseaudio-environment)
  142. (service-extension etc-service-type pulseaudio-etc)))
  143. (default-value (pulseaudio-configuration))
  144. (description "Configure PulseAudio sound support.")))
  145. ;;;
  146. ;;; LADSPA
  147. ;;;
  148. (define-record-type* <ladspa-configuration>
  149. ladspa-configuration make-ladspa-configuration
  150. ladspa-configuration?
  151. (plugins ladspa-plugins (default '())))
  152. (define (ladspa-environment config)
  153. ;; Define this variable in the global environment such that
  154. ;; pulseaudio swh-plugins (and similar LADSPA plugins) work.
  155. `(("LADSPA_PATH" .
  156. (string-join
  157. ',(map (lambda (package) (file-append package "/lib/ladspa"))
  158. (ladspa-plugins config))
  159. ":"))))
  160. (define ladspa-service-type
  161. (service-type
  162. (name 'ladspa)
  163. (extensions
  164. (list (service-extension session-environment-service-type
  165. ladspa-environment)))
  166. (default-value (ladspa-configuration))
  167. (description "Configure LADSPA plugins.")))
  168. ;;; sound.scm ends here