Subject: bug#38172: fixing dangerous PulseAudio defaults and giving it a record type

From: Leo Prikler <[email-masked]>
Date: Tue, 12 Nov 2019 12:00:40 +0100

From d48594a3e7e02aef0c5ff9fff719c1d0fb45207e Mon Sep 17 00:00:00 2001
From: Leo Prikler <[email-masked]>
Date: Tue, 12 Nov 2019 02:08:40 +0100
Subject: [PATCH] services: Add PulseAudio service

* gnu/services/sound.scm: (<pulseaudio-configuration>): New record type.
(pulseaudio-service-type): New service type.
---
 gnu/services/sound.scm | 58 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)

diff --git a/gnu/services/sound.scm b/gnu/services/sound.scm
index f2dd24402f..2aedc03c75 100644
--- a/gnu/services/sound.scm
+++ b/gnu/services/sound.scm
@@ -30,7 +30,9 @@
   #:use-module (gnu packages pulseaudio)
   #:use-module (ice-9 match)
   #:export (alsa-configuration
-            alsa-service-type))
+            alsa-service-type
+            pulseaudio-configuration
+            pulseaudio-service-type))
 
 ;;; Commentary:
 ;;;
@@ -97,4 +99,58 @@ ctl.!default {
    (default-value (alsa-configuration))
    (description "Configure low-level Linux sound support, ALSA.")))
 
+
+;;;
+;;; PulseAudio
+;;;
+
+(define-record-type* <pulseaudio-configuration>
+  pulseaudio-configuration make-pulseaudio-configuration
+  pulseaudio-configuration?
+  (package pulseaudio-package (default pulseaudio))
+  (client-conf pulseaudio-client-conf (default '()))
+  (daemon-conf pulseaudio-daemon-conf (default '((flat-volumes no))))
+  (default-script pulseaudio-default-script (default #f))
+  (system-script pulseaudio-system-script (default #f)))
+
+(define (pulseaudio-conf-entry arg)
+  (match arg
+    ((key value)
+     (format #f "~a = ~a~%" key value))
+    ((? string? _)
+     (string-append arg "\n"))))
+
+(define pulseaudio-etc-service
+  (match-lambda
+    (($ <pulseaudio-configuration> package client-conf daemon-conf
+                                   default-script system-script)
+     (let ((default.pa (if default-script
+                           (apply mixed-text-file "default.pa"
+                                  default-script)
+                           (file-append package "/etc/pulse/default.pa"))))
+       `(("pulse"
+          ,(file-union
+            "pulse"
+            `(("client.conf"
+               ,(apply mixed-text-file "client.conf"
+                       (map pulseaudio-conf-entry client-conf)))
+              ("daemon.conf"
+               ,(apply mixed-text-file "daemon.conf"
+                       "default-script-file = " default.pa "\n"
+                       (map pulseaudio-conf-entry daemon-conf)))
+              ("default.pa" ,default.pa)
+              ("system.pa"
+               ,(if default-script
+                    (apply mixed-text-file "system.pa"
+                           system-script)
+                    (file-append package "/etc/pulse/system.pa")))))))))))
+
+(define pulseaudio-service-type
+  (service-type
+   (name 'pulseaudio)
+   (extensions
+    (list (service-extension etc-service-type pulseaudio-etc-service)))
+   (default-value (pulseaudio-configuration))
+   (description "Configure PulseAudio.")))
+
 ;;; sound.scm ends here
-- 
2.24.0