aboutsummaryrefslogtreecommitdiff
#!/bin/sh
exec guile --no-auto-compile -e main -s "$0" "$@"
!#
;;;; test-driver.scm - Guile test driver for Automake testsuite harness

(define script-version "2023-12-08.14") ;UTC

;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;;
;;; This program 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.
;;;
;;; This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

;;;; Commentary:
;;;
;;; This script provides a Guile test driver using the SRFI-64 Scheme API for
;;; test suites.  SRFI-64 is distributed with Guile since version 2.0.9.
;;;
;;;; Code:

(use-modules (ice-9 format)
             (ice-9 getopt-long)
             (ice-9 pretty-print)
             (ice-9 regex)
             (srfi srfi-1)
             (srfi srfi-19)
             (srfi srfi-26)
             (srfi srfi-64))

(define (show-help)
  (display "Usage:
   test-driver --test-name=NAME --log-file=PATH --trs-file=PATH
               [--expect-failure={yes|no}] [--color-tests={yes|no}]
               [--select=REGEXP] [--exclude=REGEXP] [--errors-only={yes|no}]
               [--enable-hard-errors={yes|no}] [--brief={yes|no}}]
               [--show-duration={yes|no}] [--]
               TEST-SCRIPT [TEST-SCRIPT-ARGUMENTS]
The '--test-name' option is mandatory.  The '--select' and '--exclude' options
allow selecting or excluding individual test cases via a regexp, respectively.
The '--errors-only' option can be set to \"yes\" to limit the logged test case
metadata to only those test cases that failed.  When set to \"yes\", the
'--brief' option disables printing the individual test case result to the
console.  When '--show-duration' is set to \"yes\", the time elapsed per test
case is shown.\n"))

(define %options
  '((test-name                 (value #t))
    (log-file                  (value #t))
    (trs-file                  (value #t))
    (select                    (value #t))
    (exclude                   (value #t))
    (errors-only               (value #t))
    (color-tests               (value #t))
    (expect-failure            (value #t)) ;XXX: not implemented yet
    (enable-hard-errors        (value #t)) ;not implemented in SRFI-64
    (brief                     (value #t))
    (show-duration             (value #t))
    (help    (single-char #\h) (value #f))
    (version (single-char #\V) (value #f))))

(define (option->boolean options key)
  "Return #t if the value associated with KEY in OPTIONS is \"yes\"."
  (and=> (option-ref options key #f) (cut string=? <> "yes")))

(define* (test-display field value  #:optional (port (current-output-port))
                       #:key pretty?)
  "Display \"FIELD: VALUE\\n\" on PORT."
  (if pretty?
      (begin
        (format port "~A:~%" field)
        (pretty-print value port #:per-line-prefix "+ "))
      (format port "~A: ~S~%" field value)))

(define* (result->string symbol #:key colorize?)
  "Return SYMBOL as an upper case string.  Use colors when COLORIZE is #t."
  (let ((result (string-upcase (symbol->string symbol))))
    (if colorize?
        (string-append (case symbol
                         ((pass)       "")  ;green
                         ((xfail)      "")  ;light green
                         ((skip)       "")  ;blue
                         ((fail xpass) "")  ;red
                         ((error)      "")) ;magenta
                       result
                       "")          ;no color
        result)))


;;;
;;; SRFI 64 custom test runner.
;;;

(define* (test-runner-gnu test-name #:key color? brief? errors-only?
                          show-duration?
                          (out-port (current-output-port))
                          (trs-port (%make-void-port "w"))
                          select exclude)
  "Return an custom SRFI-64 test runner.  TEST-NAME is a string specifying the
file name of the current the test.  COLOR? specifies whether to use colors.
When BRIEF? is true, the individual test cases results are masked and only the
summary is shown.  ERRORS-ONLY? reduces the amount of test case metadata
logged to only that of the failed test cases.  OUT-PORT and TRS-PORT must be
output ports.  OUT-PORT defaults to the current output port, while TRS-PORT
defaults to a void port, which means no TRS output is logged.  SELECT and
EXCLUDE may take a regular expression to select or exclude individual test
cases based on their names."

  (define test-cases-start-time (make-hash-table))

  (define (test-on-test-begin-gnu runner)
    ;; Procedure called at the start of an individual test case, before the
    ;; test expression (and expected value) are evaluated.
    (let ((test-case-name (test-runner-test-name runner))
          (start-time     (current-time time-monotonic)))
      (hash-set! test-cases-start-time test-case-name start-time)))

  (define (test-skipped? runner)
    (eq? 'skip (test-result-kind runner)))

  (define (test-failed? runner)
    (not (or (test-passed? runner)
             (test-skipped? runner))))

  (define (test-on-test-end-gnu runner)
    ;; Procedure called at the end of an individual test case, when the result
    ;; of the test is available.
    (let* ((results (test-result-alist runner))
           (result? (cut assq <> results))
           (result  (cut assq-ref results <>))
           (test-case-name (test-runner-test-name runner))
           (start (hash-ref test-cases-start-time test-case-name))
           (end (current-time time-monotonic))
           (time-elapsed (time-difference end start))
           (time-elapsed-seconds (+ (time-second time-elapsed)
                                    (* 1e-9 (time-nanosecond time-elapsed)))))
      (unless (or brief? (and errors-only? (test-skipped? runner)))
        ;; Display the result of each test case on the console.
        (format out-port "~a: ~a - ~a ~@[[~,3fs]~]~%"
                (result->string (test-result-kind runner) #:colorize? color?)
                test-name test-case-name
                (and show-duration? time-elapsed-seconds)))

      (unless (and errors-only? (not (test-failed? runner)))
        (format #t "test-name: ~A~%" (result 'test-name))
        (format #t "location: ~A~%"
                (string-append (result 'source-file) ":"
                               (number->string (result 'source-line))))
        (test-display "source" (result 'source-form) #:pretty? #t)
        (when (result? 'expected-value)
          (test-display "expected-value" (result 'expected-value)))
        (when (result? 'expected-error)
          (test-display "expected-error" (result 'expected-error) #:pretty? #t))
        (when (result? 'actual-value)
          (test-display "actual-value" (result 'actual-value)))
        (when (result? 'actual-error)
          (test-display "actual-error" (result 'actual-error) #:pretty? #t))
        (format #t "result: ~a~%" (result->string (result 'result-kind)))
        (newline))

      (format trs-port ":test-result: ~A ~A [~,3fs]~%"
              (result->string (test-result-kind runner))
              (test-runner-test-name runner) time-elapsed-seconds)))

  (define (test-on-group-end-gnu runner)
    ;; Procedure called by a 'test-end', including at the end of a test-group.
    (let ((fail (or (positive? (test-runner-fail-count runner))
                    (positive? (test-runner-xpass-count runner))))
          (skip (or (positive? (test-runner-skip-count runner))
                    (positive? (test-runner-xfail-count runner)))))
      ;; XXX: The global results need some refinements for XPASS.
      (format trs-port ":global-test-result: ~A~%"
              (if fail "FAIL" (if skip "SKIP" "PASS")))
      (format trs-port ":recheck: ~A~%"
              (if fail "yes" "no"))
      (format trs-port ":copy-in-global-log: ~A~%"
              (if (or fail skip) "yes" "no"))
      (when brief?
        ;; Display the global test group result on the console.
        (format out-port "~A: ~A~%"
                (result->string (if fail 'fail (if skip 'skip 'pass))
                                #:colorize? color?)
                test-name))
      #f))

  (let ((runner (test-runner-null)))
    (test-runner-on-test-begin! runner test-on-test-begin-gnu)
    (test-runner-on-test-end! runner test-on-test-end-gnu)
    (test-runner-on-group-end! runner test-on-group-end-gnu)
    (test-runner-on-bad-end-name! runner test-on-bad-end-name-simple)
    runner))


;;;
;;; SRFI 64 test specifiers.
;;;
(define (test-match-name* regexp)
  "Return a test specifier that matches a test name against REGEXP."
  (lambda (runner)
    (string-match regexp (test-runner-test-name runner))))

(define (test-match-name*/negated regexp)
  "Return a negated test specifier version of test-match-name*."
  (lambda (runner)
    (not (string-match regexp (test-runner-test-name runner)))))

;;; XXX: test-match-all is a syntax, which isn't convenient to use with a list
;;; of test specifiers computed at run time.  Copy this SRFI 64 internal
;;; definition here, which is the procedural equivalent of 'test-match-all'.
(define (%test-match-all . pred-list)
  (lambda (runner)
    (let ((result #t))
      (let loop ((l pred-list))
	(if (null? l)
	    result
	    (begin
	      (if (not ((car l) runner))
		  (set! result #f))
	      (loop (cdr l))))))))


;;;
;;; Entry point.
;;;

(define (main . args)
  (let* ((opts   (getopt-long (command-line) %options))
         (option (cut option-ref opts <> <>)))
    (cond
     ((option 'help #f)    (show-help))
     ((option 'version #f) (format #t "test-driver.scm ~A~%" script-version))
     (else
      (let* ((log (and=> (option 'log-file #f) (cut open-file <> "w0")))
             (trs (and=> (option 'trs-file #f) (cut open-file <> "wl")))
             (out (duplicate-port (current-output-port) "wl"))
             (test-name (option 'test-name #f))
             (select (option 'select #f))
             (exclude (option 'exclude #f))
             (test-specifiers (filter-map
                               identity
                               (list (and=> select test-match-name*)
                                     (and=> exclude test-match-name*/negated))))
             (test-specifier (apply %test-match-all test-specifiers))
             (color-tests (if (assoc 'color-tests opts)
                              (option->boolean opts 'color-tests)
                              #t)))
        (when log
          (redirect-port log (current-output-port))
          (redirect-port log (current-warning-port))
          (redirect-port log (current-error-port)))
        (test-with-runner
            (test-runner-gnu test-name
                             #:color? color-tests
                             #:brief? (option->boolean opts 'brief)
                             #:errors-only? (option->boolean opts 'errors-only)
                             #:show-duration? (option->boolean
                                               opts 'show-duration)
                             #:out-port out #:trs-port trs)
          (test-apply test-specifier
                      (lambda _
                        (load-from-path test-name))))
        (and=> log close-port)
        (and=> trs close-port)
        (close-port out))))
    (exit 0)))

;;; Local Variables:
;;; mode: scheme
;;; eval: (add-hook 'write-file-functions 'time-stamp)
;;; time-stamp-start: "(define script-version \""
;;; time-stamp-format: "%:y-%02m-%02d.%02H"
;;; time-stamp-time-zone: "UTC"
;;; time-stamp-end: "\") ;UTC"
;;; End:

;;;; test-driver.scm ends here.
ode{software} uses an internal software volume control, @code{mixer} uses the configured (hardware) mixer control and @code{none} disables replay gain on this audio output.") (extra-options (alist '()) "An association list of option symbols/strings to string values to be appended to the audio output configuration.") (prefix mpd-)) (define (mpd-serialize-mpd-output field-name value) #~(format #f "audio_output {~%~a}~%" #$(serialize-configuration value mpd-output-fields))) (define (mpd-serialize-list-of-mpd-plugin-or-output field-name value) (let ((plugins outputs (partition mpd-plugin? value))) #~(string-append #$@(map (cut mpd-serialize-mpd-plugin "audio_output" <>) plugins) #$@(map (cut mpd-serialize-mpd-output #f <>) outputs)))) (define list-of-mpd-plugin-or-output? (list-of (lambda (x) (or (mpd-output? x) (mpd-plugin? x))))) (define-configuration mpd-configuration (package (file-like mpd) "The MPD package." empty-serializer) (user (user-account %mpd-user) "The user to run mpd as." (sanitizer mpd-user-sanitizer)) (group (user-group %mpd-group) "The group to run mpd as." (sanitizer mpd-group-sanitizer)) (shepherd-requirement (list-of-symbols '()) "This is a list of symbols naming Shepherd services that this service will depend on." empty-serializer) (environment-variables (list-of-strings '("PULSE_CLIENTCONFIG=/etc/pulse/client.conf" "PULSE_CONFIG=/etc/pulse/daemon.conf")) "A list of strings specifying environment variables." empty-serializer) (log-file maybe-string "The location of the log file. Unless specified, logs are sent to the local syslog daemon. Alternatively, a log file name can be specified, for example @file{/var/log/mpd.log}." (sanitizer mpd-log-file-sanitizer)) (log-level maybe-string "Supress any messages below this threshold. The available values, in decreasing order of verbosity, are: @code{verbose}, @code{info}, @code{notice}, @code{warning} and @code{error}.") (music-directory maybe-string "The directory to scan for music files.") (music-dir ; TODO: deprecated, remove later maybe-string "The directory to scan for music files." (serializer mpd-serialize-deprecated-field)) (playlist-directory maybe-string "The directory to store playlists.") (playlist-dir ; TODO: deprecated, remove later maybe-string "The directory to store playlists." (serializer mpd-serialize-deprecated-field)) (db-file maybe-string "The location of the music database. When left unspecified, @file{~/.cache/db} is used.") (state-file maybe-string "The location of the file that stores current MPD's state.") (sticker-file maybe-string "The location of the sticker database.") (default-port (maybe-port 6600) "The default port to run mpd on.") (endpoints maybe-list-of-strings "The addresses that mpd will bind to. A port different from @var{default-port} may be specified, e.g. @code{localhost:6602} and IPv6 addresses must be enclosed in square brackets when a different port is used. To use a Unix domain socket, an absolute path or a path starting with @code{~} can be specified here." (serializer (lambda (_ endpoints) (if (maybe-value-set? endpoints) (mpd-serialize-list-of-strings "bind_to_address" endpoints) "")))) (address ; TODO: deprecated, remove later maybe-string "The address that mpd will bind to. To use a Unix domain socket, an absolute path can be specified here." (serializer mpd-serialize-deprecated-field)) (database maybe-mpd-plugin "MPD database plugin configuration.") (partitions (list-of-mpd-partition '()) "List of MPD \"partitions\".") (neighbors (list-of-mpd-plugin '()) "List of MPD neighbor plugin configurations.") (inputs (list-of-mpd-plugin '()) "List of MPD input plugin configurations." (serializer (lambda (_ x) (mpd-serialize-list-of-mpd-plugin "input" x)))) (archive-plugins (list-of-mpd-plugin '()) "List of MPD archive plugin configurations." (serializer (lambda (_ x) (mpd-serialize-list-of-mpd-plugin "archive_plugin" x)))) (auto-update? maybe-boolean "Whether to automatically update the music database when files are changed in the @var{music-directory}.") (input-cache-size maybe-string "MPD input cache size." (serializer (lambda (_ x) (if (maybe-value-set? x) #~(string-append "\ninput_cache {\n" #$(mpd-serialize-string "size" x) "}\n") "")))) (decoders (list-of-mpd-plugin '()) "List of MPD decoder plugin configurations." (serializer (lambda (_ x) (mpd-serialize-list-of-mpd-plugin "decoder" x)))) (resampler maybe-mpd-plugin "MPD resampler plugin configuration.") (filters (list-of-mpd-plugin '()) "List of MPD filter plugin configurations." (serializer (lambda (_ x) (mpd-serialize-list-of-mpd-plugin "filter" x)))) (outputs (list-of-mpd-plugin-or-output (list (mpd-output))) "The audio outputs that MPD can use. By default this is a single output using pulseaudio.") (playlist-plugins (list-of-mpd-plugin '()) "List of MPD playlist plugin configurations." (serializer (lambda (_ x) (mpd-serialize-list-of-mpd-plugin "playlist_plugin" x)))) (extra-options (alist '()) "An association list of option symbols/strings to string values to be appended to the configuration.") (prefix mpd-)) (define (mpd-serialize-configuration configuration) (mixed-text-file "mpd.conf" (serialize-configuration configuration mpd-configuration-fields))) (define (mpd-shepherd-service config) (match-record config <mpd-configuration> (user package shepherd-requirement log-file playlist-directory db-file state-file sticker-file environment-variables) (let ((config-file (mpd-serialize-configuration config)) (username (user-account-name user))) (shepherd-service (documentation "Run the MPD (Music Player Daemon)") (requirement `(user-processes loopback ,@(if (string=? "syslog" log-file) '(syslogd) '()) ,@shepherd-requirement)) (provision '(mpd)) (start (with-imported-modules (source-module-closure '((gnu build activation))) #~(begin (use-modules (gnu build activation)) (let ((home #$(user-account-home-directory user))) (let ((user (getpw #$username)) (default-cache-dir (string-append home "/.cache"))) (define (init-directory directory) (unless (file-exists? directory) (mkdir-p/perms directory user #o755))) ;; Define a cache location that can be automatically used ;; for the database file, in case it hasn't been explicitly ;; specified. (for-each init-directory (cons default-cache-dir '#$(map dirname ;; XXX: Delete the potential "syslog" ;; log-file value, which is not a directory. (delete "syslog" (filter-map maybe-value (list db-file log-file state-file sticker-file))))))) (make-forkexec-constructor (list #$(file-append package "/bin/mpd") "--no-daemon" #$config-file) #:environment-variables ;; Set HOME so MPD can infer default paths, such as ;; for the database file. (cons (string-append "HOME=" home) '#$environment-variables)))))) (stop #~(make-kill-destructor)) (actions (list (shepherd-configuration-action config-file) (shepherd-action (name 'reopen) (documentation "Re-open log files and flush caches.") (procedure #~(lambda (pid) (if pid (begin (kill pid SIGHUP) (format #t "Issued SIGHUP to Service MPD (PID ~a)." pid)) (format #t "Service MPD is not running."))))))))))) (define (mpd-accounts config) (match-record config <mpd-configuration> (user group) ;; TODO: Deprecation code, to be removed. (let ((user (if (eq? (user-account-group user) %lazy-group) (set-user-group user group) user))) (list user group)))) (define mpd-service-type (service-type (name 'mpd) (description "Run the Music Player Daemon (MPD).") (extensions (list (service-extension shepherd-root-service-type (compose list mpd-shepherd-service)) (service-extension account-service-type mpd-accounts))) (default-value (mpd-configuration)))) ;;; ;;; myMPD ;;; (define (string-or-symbol? x) (or (symbol? x) (string? x))) (define-configuration/no-serialization mympd-ip-acl (allow (list-of-strings '()) "Allowed IP addresses.") (deny (list-of-strings '()) "Disallowed IP addresses.")) (define-maybe/no-serialization integer) (define-maybe/no-serialization mympd-ip-acl) (define %mympd-user (user-account (name "mympd") ;; XXX: This is a place-holder to be lazily substituted in 'mympd-accounts' ;; with the value from the 'group' field of <mympd-configuration>. (group %lazy-group) (system? #t) (comment "myMPD user") (home-directory "/var/empty") (shell (file-append shadow "/sbin/nologin")))) (define %mympd-group (user-group (name "mympd") (system? #t))) ;;; TODO: Procedures for unsupported value types, to be removed. (define (mympd-user-sanitizer value) (cond ((user-account? value) value) ((string? value) (warning (G_ "string value for 'user' is not supported, use \ user-account instead~%")) (user-account (inherit %mympd-user) (name value))) (else (configuration-field-error #f 'user value)))) (define (mympd-group-sanitizer value) (cond ((user-group? value) value) ((string? value) (warning (G_ "string value for 'group' is not supported, use \ user-group instead~%")) (user-group (inherit %mympd-group) (name value))) (else (configuration-field-error #f 'group value)))) (define (mympd-log-to-sanitizer value) (match value ('syslog (warning (G_ "syslog symbol value for 'log-to' is deprecated~%")) %unset-value) ((or %unset-value (? string?)) value) (_ (configuration-field-error #f 'log-to value)))) ;; XXX: The serialization procedures are insufficient since we require ;; access to multiple fields at once. ;; Fields marked with empty-serializer are never serialized and are ;; used for command-line arguments or by the service definition. (define-configuration/no-serialization mympd-configuration (package (file-like mympd) "The package object of the myMPD server." empty-serializer) (shepherd-requirement (list-of-symbols '()) "This is a list of symbols naming Shepherd services that this service will depend on." empty-serializer) (user (user-account %mympd-user) "Owner of the @command{mympd} process." (sanitizer mympd-user-sanitizer) empty-serializer) (group (user-group %mympd-group) "Owner group of the @command{mympd} process." (sanitizer mympd-group-sanitizer) empty-serializer) (work-directory (string "/var/lib/mympd") "Where myMPD will store its data." empty-serializer) (cache-directory (string "/var/cache/mympd") "Where myMPD will store its cache." empty-serializer) (acl maybe-mympd-ip-acl "ACL to access the myMPD webserver.") (covercache-ttl (maybe-integer 31) "How long to keep cached covers, @code{0} disables cover caching.") (http? (boolean #t) "HTTP support.") (host (string "[::]") "Host name to listen on.") (port (maybe-port 80) "HTTP port to listen on.") (log-level (integer 5) "How much detail to include in logs, possible values: @code{0} to @code{7}.") (log-to maybe-string "Where to send logs. Unless specified, the service logs to the local syslog service under the @samp{daemon} facility. Alternatively, a log file name can be specified, for example @file{/var/log/mympd.log}." (sanitizer mympd-log-to-sanitizer) empty-serializer) (lualibs (maybe-string "all") "See @url{https://jcorporation.github.io/myMPD/scripting/#lua-standard-libraries}.") (uri maybe-string "Override URI to myMPD. See @url{https://github.com/jcorporation/myMPD/issues/950}.") (script-acl (maybe-mympd-ip-acl (mympd-ip-acl (allow '("127.0.0.1")))) "ACL to access the myMPD script backend.") (ssl? (boolean #f) "SSL/TLS support.") (ssl-port (maybe-port 443) "Port to listen for HTTPS.") (ssl-cert maybe-string "Path to PEM encoded X.509 SSL/TLS certificate (public key).") (ssl-key maybe-string "Path to PEM encoded SSL/TLS private key.") (pin-hash maybe-string "SHA-256 hashed pin used by myMPD to control settings access by prompting a pin from the user.") (save-caches? maybe-boolean "Whether to preserve caches between service restarts.")) (define (mympd-serialize-configuration config) (define serialize-value (match-lambda ((? boolean? val) (if val "true" "false")) ((? integer? val) (number->string val)) ((? mympd-ip-acl? val) (ip-acl-serialize-configuration val)) ((? string? val) val))) (define (ip-acl-serialize-configuration config) (define (serialize-list-of-strings prefix lst) (map (cut format #f "~a~a" prefix <>) lst)) (string-join (append (serialize-list-of-strings "+" (mympd-ip-acl-allow config)) (serialize-list-of-strings "-" (mympd-ip-acl-deny config))) ",")) ;; myMPD configuration fields are serialized as individual files under ;; <work-directory>/config/. (match-record config <mympd-configuration> (work-directory acl covercache-ttl http? host port log-level lualibs uri script-acl ssl? ssl-port ssl-cert ssl-key pin-hash save-caches?) (define (serialize-field filename value) (when (maybe-value-set? value) (list (format #f "~a/config/~a" work-directory filename) (mixed-text-file filename (serialize-value value))))) (let ((filename-to-field `(("acl" . ,acl) ("covercache_keep_days" . ,covercache-ttl) ("http" . ,http?) ("http_host" . ,host) ("http_port" . ,port) ("loglevel" . ,log-level) ("lualibs" . ,lualibs) ("mympd_uri" . ,uri) ("scriptacl" . ,script-acl) ("ssl" . ,ssl?) ("ssl_port" . ,ssl-port) ("ssl_cert" . ,ssl-cert) ("ssl_key" . ,ssl-key) ("pin_hash" . ,pin-hash) ("save_caches" . ,save-caches?)))) (filter list? (generic-serialize-alist list serialize-field filename-to-field))))) (define (mympd-shepherd-service config) (match-record config <mympd-configuration> (package shepherd-requirement user work-directory cache-directory log-level log-to) (shepherd-service (documentation "Run the myMPD daemon.") (requirement `(loopback user-processes ,@(if (maybe-value-set? log-to) '() '(syslogd)) ,@shepherd-requirement)) (provision '(mympd)) (start (let ((username (user-account-name user))) (with-imported-modules (source-module-closure '((gnu build activation))) #~(begin (use-modules (gnu build activation)) (let ((user (getpw #$username))) (define (init-directory directory) (unless (file-exists? directory) (mkdir-p/perms directory user #o755))) (for-each init-directory '#$(map dirname (filter-map maybe-value (list log-to work-directory cache-directory))))) (make-forkexec-constructor `(#$(file-append package "/bin/mympd") "--user" #$username #$@(if (eq? log-to 'syslog) '("--syslog") '()) "--workdir" #$work-directory "--cachedir" #$cache-directory) #:environment-variables (list #$(format #f "MYMPD_LOGLEVEL=~a" log-level)) #:log-file #$(maybe-value log-to))))))))) (define (mympd-accounts config) (match-record config <mympd-configuration> (user group) ;; TODO: Deprecation code, to be removed. (let ((user (if (eq? (user-account-group user) %lazy-group) (set-user-group user group) user))) (list user group)))) (define mympd-service-type (service-type (name 'mympd) (extensions (list (service-extension shepherd-root-service-type (compose list mympd-shepherd-service)) (service-extension account-service-type mympd-accounts) (service-extension special-files-service-type mympd-serialize-configuration))) (description "Run myMPD, a frontend for MPD. (Music Player Daemon)") (default-value (mympd-configuration))))