diff mbox series

[bug#69692] gnu: Add home-jellyfin-mpv-shim-service-type.

Message ID 7d396f735f82369731c90195c7b8e7dc0fcf9acd.1710048182.git.ian@retrospec.tv
State New
Headers show
Series [bug#69692] gnu: Add home-jellyfin-mpv-shim-service-type. | expand

Commit Message

Ian Eure March 10, 2024, 5:24 a.m. UTC
Split off from 67120.

* gnu/home/services/media.scm (home-jellyfin-mpv-shim-service-type): New variable.
* doc/guix.texi (Jellyfin Services): New section.
* doc/guix.texi (Kodi Services): Add subheading.

Change-Id: I037ab0602214fdaa1b032be51ff98ecf6b7ee16c
---
 doc/guix.texi               | 19 +++++++++++++++++
 gnu/home/services/media.scm | 42 +++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)


base-commit: 7758e63f7a89f53fbb7c7a265ae472af0a8dfab0

Comments

Skyler Ferris March 18, 2024, 10:14 p.m. UTC | #1
Hi Ian,

I don't have the setup required to try running this service but 2 things stand out to me when reading through it.

On 3/9/24 21:24, Ian Eure wrote:

> +To enable, add this to your home services:
> +
> +@lisp
> +(service home-jellyfin-mpv-shim-service-type #f)
> +@end lisp

You can add a default-value field to the service definition like so:

(define-public home-jellyfin-mpv-shim-service-type
  (service-type
   (name 'home-jellyfin-mpv-shim)
   (default-value #f)
   (extensions (list (service-extension home-shepherd-service-type
                                        jellyfin-mpv-shim-shepherd-service)
                     ;; Ensure 'home-x11-service-type' is instantiated so we
                     ;; can depend on the Shepherd 'x11-display' service.
                     (service-extension home-x11-service-type
                                        (const #t))))
   (description "Run Jellyfin MPV Shim.")))

Then, users can simply use (service home-jellyfish-mpv-shim-service-type) without having to specify #f manually And if the service ever changes in the future and this value becomes useful then you can provide a reasonable default without requiring users to change their code. (https://guix.gnu.org/manual/en/html_node/Service-Reference.html)

> +
> +The service only starts if @code{jellyfin-mpv-shim} has been configured with a remote server and credentials.  This must be done manually, by launching @code{jellyfin-mpv-shim}.  After configuring the server, the service will start automatically when you log in.

Would it make sense to launch this program automatically if it is not configured? Presumably if someone adds the service then they want to configure a server. The value passed to the service could be used to specify whether or not the program should automatically launch so that users who do not want this behavior can disable it (note that if you decide to implement this then the configuration value should be an instance of a new structure defined to store configuration for this service, not a simple boolean; again, this makes things easier in the future so that if you want to add more fields pre-existing code will still work).

Regards,
Skyler
diff mbox series

Patch

diff --git a/doc/guix.texi b/doc/guix.texi
index 858d5751bf..8a3aead56a 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -46383,6 +46383,8 @@  Media Home Services
 @subsection Media Home Services
 
 @cindex kodi
+@subsubheading Kodi Services
+
 The @uref{https://kodi.tv, Kodi media center} can be run as a daemon on
 a media server.  With the @code{(gnu home services kodi)} service, you
 can configure Kodi to run upon login.
@@ -46415,6 +46417,23 @@  Media Home Services
 @end table
 @end deftp
 
+@cindex jellyfin
+@subsubheading Jellyfin Services
+
+The @code{home-jellyfin-mpv-shim-service-type} in the @code{(gnu home services media)} module runs a cast client for the @uref{https://jellyfin.org/, Jellyfin} media system.
+
+To enable, add this to your home services:
+
+@lisp
+(service home-jellyfin-mpv-shim-service-type #f)
+@end lisp
+
+The service only starts if @code{jellyfin-mpv-shim} has been configured with a remote server and credentials.  This must be done manually, by launching @code{jellyfin-mpv-shim}.  After configuring the server, the service will start automatically when you log in.
+
+@defvar home-jellyfin-mpv-shim-service-type
+The type of the Jellyfin MPV Shim service.
+@end defvar
+
 @node Networking Home Services
 @subsection Networking Home Services
 
diff --git a/gnu/home/services/media.scm b/gnu/home/services/media.scm
index c6454dfe15..a7fcf75b06 100644
--- a/gnu/home/services/media.scm
+++ b/gnu/home/services/media.scm
@@ -19,8 +19,10 @@ 
 (define-module (gnu home services media)
   #:use-module (srfi srfi-26)
   #:use-module (gnu home services)
+  #:use-module (gnu home services desktop)
   #:use-module (gnu home services shepherd)
   #:use-module (gnu packages kodi)
+  #:use-module (gnu packages video)
   #:use-module (gnu services configuration)
   #:use-module (gnu services shepherd)
   #:use-module (guix records)
@@ -66,3 +68,43 @@  (define home-kodi-service-type
    (description
     "Install and configure the Kodi media center so that it runs as a Shepherd
 service.")))
+
+(define (jellyfin-mpv-shim-shepherd-service _ignore)
+  (list (shepherd-service
+         (documentation "Jellyfin MPV Shim.")
+         (provision '(jellyfin-mpv-shim jellyfin-client))
+
+         ;; Depend on 'x11-display', which sets 'DISPLAY' if an X11 server is
+         ;; available, and fails to start otherwise.
+         (requirement '(x11-display))
+
+         (modules '((srfi srfi-1)
+                    (srfi srfi-26)
+                    (srfi srfi-98)))
+         (start #~(lambda _
+                    ;; Only start if a server has been configured.
+                    (if (file-exists?
+                         (string-append
+                          (get-environment-variable "XDG_CONFIG_HOME")
+                          "/jellyfin-mpv-shim/cred.json"))
+                        (fork+exec-command
+                         (list #$(file-append jellyfin-mpv-shim "/bin/jellyfin-mpv-shim"))
+
+                         ;; Inherit the 'DISPLAY' variable set by 'x11-display'.
+                         #:environment-variables
+                         (cons (string-append "DISPLAY=" (getenv "DISPLAY"))
+                               (remove (cut string-prefix? "DISPLAY=" <>)
+                                       (default-environment-variables))))
+                        #f)))
+         (stop #~(make-kill-destructor)))))
+
+(define-public home-jellyfin-mpv-shim-service-type
+  (service-type
+   (name 'home-jellyfin-mpv-shim)
+   (extensions (list (service-extension home-shepherd-service-type
+                                        jellyfin-mpv-shim-shepherd-service)
+                     ;; Ensure 'home-x11-service-type' is instantiated so we
+                     ;; can depend on the Shepherd 'x11-display' service.
+                     (service-extension home-x11-service-type
+                                        (const #t))))
+   (description "Run Jellyfin MPV Shim.")))