unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 9127506b5507ef7d442f098891d51d61b6f4ccdb 9068 bytes (raw)
name: gnu/services/upnp.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2024 Fabio Natali <me@fabionatali.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu services upnp)
  #:use-module (gnu build linux-container)
  #:use-module (gnu packages admin)
  #:use-module (gnu packages upnp)
  #:use-module (gnu services admin)
  #:use-module (gnu services base)
  #:use-module (gnu services shepherd)
  #:use-module (gnu services)
  #:use-module (gnu system file-systems)
  #:use-module (gnu system shadow)
  #:use-module (guix gexp)
  #:use-module (guix least-authority)
  #:use-module (guix records)
  #:use-module (ice-9 match)
  #:export (%readymedia-default-cache-directory
            %readymedia-default-log-directory
            %readymedia-default-port
            %readymedia-log-file
            %readymedia-user-account
            %readymedia-user-group
            readymedia-configuration
            readymedia-configuration-cache-directory
            readymedia-configuration-extra-config
            readymedia-configuration-friendly-name
            readymedia-configuration-log-directory
            readymedia-configuration-media-directories
            readymedia-configuration-port
            readymedia-configuration-readymedia
            readymedia-configuration?
            readymedia-media-directory
            readymedia-media-directory-path
            readymedia-media-directory-types
            readymedia-media-directory?
            readymedia-service-type))

;;; Commentary:
;;;
;;; UPnP services.
;;;
;;; Code:

(define %readymedia-default-cache-directory "/var/cache/readymedia")
(define %readymedia-default-log-directory "/var/log/readymedia")
(define %readymedia-log-file
  (string-append %readymedia-default-log-directory "/minidlna.log"))
(define %readymedia-user-group "readymedia")
(define %readymedia-user-account "readymedia")

(define-record-type* <readymedia-configuration>
  readymedia-configuration make-readymedia-configuration
  readymedia-configuration?
  (readymedia readymedia-configuration-readymedia
              (default readymedia))
  (cache-directory readymedia-configuration-cache-directory
                   (default %readymedia-default-cache-directory))
  (log-directory readymedia-configuration-log-directory
                 (default %readymedia-default-log-directory))
  (friendly-name readymedia-configuration-friendly-name
                 (default #f))
  (media-directories readymedia-configuration-media-directories)
  (port readymedia-configuration-port
        (default #f))
  (extra-config readymedia-configuration-extra-config
                (default '())))

;; READYMEDIA-MEDIA-DIR is a record that indicates the path of a media folder
;; and the types of media included within it. Allowed individual types are the
;; symbols 'A' for audio, 'V' for video, and 'P' for pictures. The types field
;; can contain any combination of individual types; an empty list means no type
;; specified.
(define-record-type* <readymedia-media-directory>
  readymedia-media-directory make-readymedia-media-directory
  readymedia-media-directory?
  (path readymedia-media-directory-path)
  (types readymedia-media-directory-types (default '())))

(define (readymedia-media-directory->string entry)
  "Convert a media-directory ENTRY to a ReadyMedia/MiniDLNA media dir string."
  (match-record
   entry <readymedia-media-directory> (path types)
   (if (null? types)
       (format #f "media_dir=~a" path)
       (format #f
               "media_dir=~a,~a"
               (string-join (map symbol->string types) "")
               path))))

(define (readymedia-extra-config-entry->string entry)
  "Convert a extra-config ENTRY to a ReadyMedia/MiniDLNA configuration string."
  (let ((key (car entry))
        (value (cdr entry)))
    (format #f "~a=~a" key value)))

(define (readymedia-configuration->config-file config)
  "Return the ReadyMedia/MiniDLNA configuration file corresponding to CONFIG."
  (let ((friendly-name (readymedia-configuration-friendly-name config))
        (media-directories (readymedia-configuration-media-directories config))
        (cache-directory (readymedia-configuration-cache-directory config))
        (log-directory (readymedia-configuration-log-directory config))
        (port (readymedia-configuration-port config))
        (extra-config (readymedia-configuration-extra-config config)))
    (mixed-text-file
     "minidlna.conf"
     "db_dir=" cache-directory "\n"
     "log_dir=" log-directory "\n"
     (if friendly-name (format #f "friendly_name=~a\n" friendly-name) "")
     (if port (format #f "port=~a\n" port) "")
     (string-join
      (map readymedia-media-directory->string media-directories) "\n" 'suffix)
     (string-join
      (map readymedia-extra-config-entry->string extra-config) "\n" 'suffix))))

(define (readymedia-shepherd-service config)
  "Return a least-authority ReadyMedia/MiniDLNA Shepherd service."
  (let* ((minidlna-conf (readymedia-configuration->config-file config))
         (media-directories (readymedia-configuration-media-directories config))
         (cache-directory (readymedia-configuration-cache-directory config))
         (log-directory (readymedia-configuration-log-directory config))
         (readymedia (least-authority-wrapper
                      (file-append
                       (readymedia-configuration-readymedia config)
                       "/sbin/minidlnad")
                      #:name "minidlna"
                      #:mappings
                      (cons* (file-system-mapping
                              (source cache-directory)
                              (target source)
                              (writable? #t))
                             (file-system-mapping
                              (source log-directory)
                              (target source)
                              (writable? #t))
                             (file-system-mapping
                              (source minidlna-conf)
                              (target source))
                             (map
                              (lambda (e)
                                (file-system-mapping
                                 (source (readymedia-media-directory-path e))
                                 (target source)
                                 (writable? #f)))
                              media-directories))
                      #:namespaces (delq 'net %namespaces))))
    (list (shepherd-service
           (documentation "Run the ReadyMedia/MiniDLNA daemon.")
           (provision '(readymedia))
           (requirement '(networking user-processes))
           (start
            #~(begin
                (use-modules (gnu build activation))
                (let* ((user (getpw #$%readymedia-user-account))
                       (dirs (list
                              #$cache-directory
                              #$log-directory
                              #$@(map (lambda (e)
                                        (readymedia-media-directory-path e))
                                      media-directories)))
                       (init-directory (lambda (d)
                                         (unless (file-exists? d)
                                           (mkdir-p/perms d user #o755)))))
                  (for-each init-directory dirs))
                (make-forkexec-constructor
                 ;; "-S" is to daemonise minidlnad.
                 (list #$readymedia "-f" #$minidlna-conf "-S")
                 #:log-file #$%readymedia-log-file
                 #:user #$%readymedia-user-account
                 #:group #$%readymedia-user-group)))
           (stop #~(make-kill-destructor))))))

(define readymedia-accounts
  (list (user-group
         (name "readymedia")
         (system? #t))
        (user-account
         (name "readymedia")
         (group "readymedia")
         (system? #t)
         (comment "ReadyMedia/MiniDLNA daemon user")
         (home-directory "/var/empty")
         (shell (file-append shadow "/sbin/nologin")))))

(define readymedia-service-type
  (service-type
   (name 'readymedia)
   (extensions
    (list
     (service-extension shepherd-root-service-type readymedia-shepherd-service)
     (service-extension account-service-type (const readymedia-accounts))))
   (description
    "Run @command{minidlnad}, the ReadyMedia/MiniDLNA media server.")))

debug log:

solving 9127506b55 ...
found 9127506b55 in https://yhetil.org/guix-patches/a53c63eb4306b19741f0c4ed531ae6067d47ac2a.1724666874.git.me@fabionatali.com/

applying [1/1] https://yhetil.org/guix-patches/a53c63eb4306b19741f0c4ed531ae6067d47ac2a.1724666874.git.me@fabionatali.com/
diff --git a/gnu/services/upnp.scm b/gnu/services/upnp.scm
new file mode 100644
index 0000000000..9127506b55

Checking patch gnu/services/upnp.scm...
Applied patch gnu/services/upnp.scm cleanly.

index at:
100644 9127506b5507ef7d442f098891d51d61b6f4ccdb	gnu/services/upnp.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).