Subject: [PATCH] gnu: home: Add support for home-pipewire-service
From: Brian Cully <[email-masked]>
To: [email-masked]
Cc: Brian Cully <[email-masked]>
Date: Fri, 2 Jun 2023 19:04:27 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

This adds a set of home shepherd services which will start the required
services for a functional pipewire setup.

* gnu/home/services/sound.scm (home-pipewire-shepherd-service),
(home-pipewire-pulse-shepherd-service), (home-wireplumber-shepherd-service),
(home-pipewire-shepherd-services), (generate-doc): new procedures.
(home-pipewire-service-type): new service type.
(home-pipewire-configuration): new struct.
* doc/guix.texi (Sound Home Services): document it.
---
 doc/guix.texi               | 34 +++++++++++++++++
 gnu/home/services/sound.scm | 74 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 107 insertions(+), 1 deletion(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 7f8d8d66e9..0b19c9301f 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -116,6 +116,7 @@
 Copyright @copyright{} 2023 Karl Hallsby@*
 Copyright @copyright{} 2023 Nathaniel Nicandro@*
 Copyright @copyright{} 2023 Tanguy Le Carrour@*
+Copyright @copyright{} 2023 Brian Cully@*
 
 Permission is granted to copy, distribute and/or modify this document
 under the terms of the GNU Free Documentation License, Version 1.3 or
@@ -43563,6 +43564,39 @@ Sound Home Services
 This is the multicast address used by default by the two services above.
 @end defvar
 
+@cindex PipeWire, home service
+
+@uref{https://pipewire.org, PipeWire} provides a low-latency,
+graph-based audio and video processing service. In addition to its
+native protocol, it can also be used as a replacement for both JACK and
+PulseAudio.
+
+@defvar home-pipewire-service-type
+This provides the service definition for @command{pipewire}, which will
+run on login. Its value is a @code{home-pipewire-configuration} object.
+
+To start the service, add it to the @code{service} field of your
+@code{home-environment}, such as:
+
+@lisp
+(service home-pipewire-service-type)
+@end lisp
+
+@deftp {Data Type} home-pipewire-configuration
+Available @code{home-pipewire-configuration} fields are:
+
+@table @asis
+@item @code{pipewire} (default: @code{pipewire}) (type: file-like)
+The PipeWire package to use.
+
+@item @code{wireplumber} (default: @code{wireplumber}) (type: file-like)
+The WirePlumber package to use.
+
+@item @code{enable-pulseaudio?} (default: @code{#t}) (type: boolean)
+Enable PulseAudio replacement.
+@end table
+@end deftp
+
 @node Mail Home Services
 @subsection Mail Home Services
  
diff --git a/gnu/home/services/sound.scm b/gnu/home/services/sound.scm
index 22c1a99250..94d8bc7482 100644
--- a/gnu/home/services/sound.scm
+++ b/gnu/home/services/sound.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2023 Ludovic Courtès <[email-masked]>
+;;; Copyright © 2023 Brian Cully <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -19,13 +20,77 @@
 (define-module (gnu home services sound)
   #:use-module (gnu home services)
   #:use-module (gnu home services shepherd)
+  #:use-module (gnu packages linux)
+  #:use-module (gnu services configuration)
   #:use-module (guix records)
   #:use-module (guix gexp)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-26)
   #:use-module (ice-9 match)
   #:export (home-pulseaudio-rtp-sink-service-type
             home-pulseaudio-rtp-source-service-type
-            %pulseaudio-rtp-multicast-address))
+            %pulseaudio-rtp-multicast-address
+
+            home-pipewire-configuration
+            home-pipewire-service-type))
+
+
+;;;
+;;; PipeWire support.
+;;;
+(define-configuration/no-serialization home-pipewire-configuration
+  (pipewire (file-like pipewire) "The PipeWire package to use.")
+  (wireplumber (file-like wireplumber) "The WirePlumber package to use.")
+  (enable-pulseaudio? (boolean #t) "Enable PulseAudio replacement."))
+
+(define (home-pipewire-shepherd-service config)
+  (shepherd-service
+   (documentation "PipeWire screen and audio sharing.")
+   (provision '(pipewire))
+   (requirement '(dbus))
+   (start #~(make-forkexec-constructor
+             (list #$(file-append
+                      (home-pipewire-configuration-pipewire config)
+                      "/bin/pipewire"))))))
+
+(define (home-pipewire-pulseaudio-shepherd-service config)
+  (shepherd-service
+   (documentation "Drop-in PulseAudio replacement service for PipeWire.")
+   (provision '(pipewire-pulseaudio))
+   (requirement '(pipewire))
+   (start #~(make-forkexec-constructor
+             (list #$(file-append
+                      (home-pipewire-configuration-pipewire config)
+                      "/bin/pipewire-pulse"))))))
+
+(define (home-wireplumber-shepherd-service config)
+  (shepherd-service
+   (documentation "WirePlumber session management for PipeWire.")
+   (provision '(wireplumber))
+   (requirement '(pipewire))
+   (start #~(make-forkexec-constructor
+             (list #$(file-append
+                      (home-pipewire-configuration-wireplumber config)
+                      "/bin/wireplumber"))))))
+
+(define (home-pipewire-shepherd-services config)
+  (define shepherd-services
+    (filter
+     identity
+     (list home-pipewire-shepherd-service home-wireplumber-shepherd-service
+           (and (home-pipewire-configuration-enable-pulseaudio? config)
+                home-pipewire-pulseaudio-shepherd-service))))
+  (map (cut <> config) shepherd-services))
+
+(define home-pipewire-service-type
+  (service-type
+   (name 'pipewire)
+   (extensions
+    (list (service-extension home-shepherd-service-type
+                             home-pipewire-shepherd-services)))
+   (description
+    "Start essential PipeWire services.")
+   (default-value (home-pipewire-configuration))))
 
 
 ;;;
@@ -149,3 +214,10 @@ (define home-pulseaudio-rtp-source-service-type
     "Define a PulseAudio source to receive audio broadcasted over RTP by
 another PulseAudio instance.")
    (default-value %pulseaudio-rtp-multicast-address)))
+
+
+;;;
+;;; Generate documentation.
+;;;
+(define (generate-doc)
+  (configuration->documentation 'home-pipewire-configuration))

base-commit: c11b92a8aae6fe7fad0da8257ec28f5009c37b35
-- 
2.40.1