me
/
guix
Archived
1
0
Fork 0

services: mpd; Refactor start slot directory initialization.

* gnu/services/audio.scm (mpd-shepherd-service): Standardize the way the log
file parent and other directories are initialized in the start slot.
(mympd-shepherd-service): Likewise.
Maxim Cournoyer 2023-04-28 22:10:42 -04:00
parent 98a46c9da6
commit a5d611c19b
No known key found for this signature in database
GPG Key ID: 1260E46482E63562
1 changed files with 74 additions and 52 deletions

View File

@ -24,6 +24,7 @@
#:use-module (guix deprecation) #:use-module (guix deprecation)
#:use-module (guix diagnostics) #:use-module (guix diagnostics)
#:use-module (guix i18n) #:use-module (guix i18n)
#:use-module (guix modules)
#:use-module (gnu services) #:use-module (gnu services)
#:use-module (gnu services admin) #:use-module (gnu services admin)
#:use-module (gnu services configuration) #:use-module (gnu services configuration)
@ -575,7 +576,8 @@ appended to the configuration.")
(with-shepherd-action 'mpd ('reopen) #f)))))) (with-shepherd-action 'mpd ('reopen) #f))))))
(define (mpd-shepherd-service config) (define (mpd-shepherd-service config)
(match-record config <mpd-configuration> (user package shepherd-requirement (match-record config <mpd-configuration>
(user package shepherd-requirement
log-file playlist-directory log-file playlist-directory
db-file state-file sticker-file db-file state-file sticker-file
environment-variables) environment-variables)
@ -585,26 +587,34 @@ appended to the configuration.")
(documentation "Run the MPD (Music Player Daemon)") (documentation "Run the MPD (Music Player Daemon)")
(requirement `(user-processes loopback ,@shepherd-requirement)) (requirement `(user-processes loopback ,@shepherd-requirement))
(provision '(mpd)) (provision '(mpd))
(start #~(begin (start
(and=> #$(maybe-value log-file) (with-imported-modules (source-module-closure
(compose mkdir-p dirname)) '((gnu build activation)))
#~(begin
(use-modules (gnu build activation))
(let ((user (getpw #$username))) (let ((user (getpw #$username)))
(define (init-directory directory)
(unless (file-exists? directory)
(mkdir-p/perms directory user #o755)))
(for-each (for-each
(lambda (x) init-directory
(when (and x (not (file-exists? x))) '#$(map dirname
(mkdir-p x) ;; XXX: Delete the potential "syslog"
(chown x (passwd:uid user) (passwd:gid user)))) ;; log-file value, which is not a directory.
(list #$(maybe-value playlist-directory) (delete "syslog"
(and=> #$(maybe-value db-file) dirname) (filter-map maybe-value
(and=> #$(maybe-value state-file) dirname) (list db-file
(and=> #$(maybe-value sticker-file) dirname)))) log-file
state-file
sticker-file))))))
(make-forkexec-constructor (make-forkexec-constructor
(list #$(file-append package "/bin/mpd") (list #$(file-append package "/bin/mpd") "--no-daemon"
"--no-daemon"
#$config-file) #$config-file)
#:environment-variables '#$environment-variables))) #:environment-variables '#$environment-variables))))
(stop #~(make-kill-destructor)) (stop #~(make-kill-destructor))
(actions (actions
(list (shepherd-configuration-action config-file) (list (shepherd-configuration-action config-file)
@ -871,11 +881,9 @@ prompting a pin from the user.")
filename-to-field))))) filename-to-field)))))
(define (mympd-shepherd-service config) (define (mympd-shepherd-service config)
(match-record config <mympd-configuration> (package shepherd-requirement (match-record config <mympd-configuration>
user work-directory (package shepherd-requirement user work-directory cache-directory
cache-directory log-level log-to) log-level log-to)
(let ((log-level* (format #f "MYMPD_LOGLEVEL=~a" log-level))
(username (user-account-name user)))
(shepherd-service (shepherd-service
(documentation "Run the myMPD daemon.") (documentation "Run the myMPD daemon.")
(requirement `(loopback user-processes (requirement `(loopback user-processes
@ -884,24 +892,38 @@ prompting a pin from the user.")
'()) '())
,@shepherd-requirement)) ,@shepherd-requirement))
(provision '(mympd)) (provision '(mympd))
(start #~(begin (start
(let* ((pw (getpwnam #$username)) (let ((username (user-account-name user)))
(uid (passwd:uid pw)) (with-imported-modules (source-module-closure
(gid (passwd:gid pw))) '((gnu build activation)))
(for-each (lambda (dir) #~(begin
(mkdir-p dir) (use-modules (gnu build activation))
(chown dir uid gid))
(list #$work-directory #$cache-directory)))
(let ((user (getpw #$username)))
(define (init-directory directory)
(unless (file-exists? directory)
(mkdir-p/perms directory user #o755)))
(for-each
init-directory
'#$(map dirname
;; XXX: Delete the potential 'syslog log-file value,
;; which is not a directory.
(delete 'syslog
(filter-map maybe-value
(list log-to
work-directory
cache-directory))))))
(make-forkexec-constructor (make-forkexec-constructor
`(#$(file-append package "/bin/mympd") `(#$(file-append package "/bin/mympd")
"--user" #$username "--user" #$username
#$@(if (eq? log-to 'syslog) '("--syslog") '()) #$@(if (eq? log-to 'syslog) '("--syslog") '())
"--workdir" #$work-directory "--workdir" #$work-directory
"--cachedir" #$cache-directory) "--cachedir" #$cache-directory)
#:environment-variables (list #$log-level*) #:environment-variables
#:log-file #$(if (string? log-to) log-to #f)))) (list #$(format #f "MYMPD_LOGLEVEL=~a" log-level))
(stop #~(make-kill-destructor)))))) #:log-file #$(if (string? log-to) log-to #f)))))))))
(define (mympd-accounts config) (define (mympd-accounts config)
(match-record config <mympd-configuration> (user group) (match-record config <mympd-configuration> (user group)