;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2017 Peter Mikkelsen ;;; Copyright © 2019 Ricardo Wurmus ;;; Copyright © 2020 Ludovic Courtès ;;; Copyright © 2022 Bruno Victal ;;; ;;; 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 . (define-module (gnu services audio) #:use-module (guix gexp) #:use-module (guix deprecation) #:use-module (guix diagnostics) #:use-module (guix i18n) #:use-module (gnu services) #:use-module (gnu services configuration) #:use-module (gnu services shepherd) #:use-module (gnu services admin) #:use-module (gnu system shadow) #:use-module (gnu packages admin) #:use-module (gnu packages mpd) #:use-module (guix records) #:use-module (ice-9 match) #:use-module (srfi srfi-1) #:use-module (srfi srfi-8) #:use-module (srfi srfi-26) #:export (mpd-output mpd-output? mpd-plugin mpd-plugin? mpd-partition mpd-partition? mpd-configuration mpd-configuration? mpd-service-type)) ;;; Commentary: ;;; ;;; Audio related services ;;; ;;; Code: (define (uglify-field-name field-name) (let ((str (symbol->string field-name))) (string-join (string-split (if (string-suffix? "?" str) (string-drop-right str 1) str) #\-) "_"))) (define list-of-string? (list-of string?)) (define list-of-symbol? (list-of symbol?)) (define (mpd-serialize-field field-name value) (let ((field (if (string? field-name) field-name (uglify-field-name field-name))) (value (if (boolean? value) (if value "yes" "no") value))) #~(format #f "~a \"~a\"~%" #$field #$value))) (define mpd-serialize-number mpd-serialize-field) (define mpd-serialize-string mpd-serialize-field) (define mpd-serialize-boolean mpd-serialize-field) (define* (mpd-serialize-alist field-name value) #~(string-append #$@(generic-serialize-alist list mpd-serialize-field value))) (define-maybe string (prefix mpd-)) (define-maybe list-of-string (prefix mpd-)) (define-maybe boolean (prefix mpd-)) ;;; TODO: Procedures for deprecated fields, to be removed. (define mpd-deprecated-fields '((music-dir . music-directory) (playlist-dir . playlist-directory) (address . addresses))) (define (port? value) (or (string? value) (integer? value))) (define (mpd-serialize-deprecated-field field-name value) (if (maybe-value-set? value) (begin (warn-about-deprecation field-name #f #:replacement (assoc-ref mpd-deprecated-fields field-name)) (match field-name ('playlist-dir (mpd-serialize-string "playlist_directory" value)) ('music-dir (mpd-serialize-string "music_directory" value)) ('address (mpd-serialize-string "bind_to_address" value)))) "")) (define (mpd-serialize-port field-name value) (when (string? value) (warning (G_ "string value for '~a' is deprecated, use integer instead~%") field-name)) (mpd-serialize-field field-name value)) (define-maybe port (prefix mpd-)) ;;; ;; Generic MPD plugin record, lists only the most prevalent fields. (define-configuration mpd-plugin (plugin maybe-string "Plugin name.") (name maybe-string "Name.") (enabled? maybe-boolean "Whether the plugin is enabled/disabled.") (extra-options (alist '()) "An association list of option symbols/strings to string values to be appended to the plugin configuration. See @uref{https://mpd.readthedocs.io/en/latest/plugins.html,MPD plugin reference} for available options.") (prefix mpd-)) (define (mpd-serialize-mpd-plugin field-name value) #~(format #f "~a {~%~a}~%" '#$field-name #$(serialize-configuration value mpd-plugin-fields))) (define (mpd-serialize-list-of-mpd-plugin field-name value) #~(string-append #$@(map (cut mpd-serialize-mpd-plugin field-name <>) value))) (define list-of-mpd-plugin? (list-of mpd-plugin?)) (define-maybe mpd-plugin (prefix mpd-)) (define-configuration mpd-partition (name string "Partition name.") (extra-options (alist '()) "An association list of option symbols/strings to string values to be appended to the partition configuration. See @uref{https://mpd.readthedocs.io/en/latest/user.html#configuring-partitions,Configuring Partitions} for available options.") (prefix mpd-)) (define (mpd-serialize-mpd-partition field-name value) #~(format #f "partition {~%~a}~%" #$(serialize-configuration value mpd-partition-fields))) (define (mpd-serialize-list-of-mpd-partition field-name value) #~(string-append #$@(map (cut mpd-serialize-mpd-partition #f <>) value))) (define list-of-mpd-partition? (list-of mpd-partition?)) (define-configuration mpd-output (name (string "MPD") "The name of the audio output.") (type (string "pulse") "The type of audio output.") (enabled? (boolean #t) "Specifies whether this audio output is enabled when MPD is started. By default, all audio outputs are enabled. This is just the default setting when there is no state file; with a state file, the previous state is restored.") (format maybe-string "Force a specific audio format on output. See @uref{https://mpd.readthedocs.io/en/latest/user.html#audio-output-format,Global Audio Format} for a more detailed description.") (tags? (boolean #t) "If set to @code{#f}, then MPD will not send tags to this output. This is only useful for output plugins that can receive tags, for example the @code{httpd} output plugin.") (always-on? (boolean #f) "If set to @code{#t}, then MPD attempts to keep this audio output always open. This may be useful for streaming servers, when you don’t want to disconnect all listeners even when playback is accidentally stopped.") (mixer-type (string "none") "This field accepts a string that specifies which mixer should be used for this audio output: the @code{hardware} mixer, the @code{software} mixer, the @code{null} mixer (allows setting the volume, but with no effect; this can be used as a trick to implement an external mixer External Mixer) or no mixer (@code{none}).") (replay-gain-handler maybe-string "This field accepts a string that specifies how @uref{https://mpd.readthedocs.io/en/latest/user.html#replay-gain,Replay Gain} is to be applied. @code{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) (receive (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 (string "mpd") "The user to run mpd as.") (group maybe-string "The group to run mpd as.") (shepherd-requirement (list-of-symbol '()) "This is a list of symbols naming Shepherd services that this service will depend on." empty-serializer) (log-file (maybe-string "/var/log/mpd/log") "The location of the log file. Set to @code{syslog} to use the local syslog daemon or @code{%unset-value} to omit this directive from the configuration file.") (log-level maybe-string "Supress any messages below this threshold. Available values: @code{notice}, @code{info}, @code{verbose}, @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." 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." mpd-serialize-deprecated-field) (db-file maybe-string "The location of the music database.") (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.") (port (maybe-port 6600) ; TODO: switch to integer "The port to run mpd on.") (endpoints maybe-list-of-string "The addresses that mpd will bind to. To use a Unix domain socket, an absolute path can be specified here." (lambda (_ x) (if (maybe-value-set? x) #~(string-append #$@(map (cut mpd-serialize-field "bind_to_address" <>) x)) ""))) (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." 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." (lambda (_ x) (mpd-serialize-list-of-mpd-plugin "input" x))) (archive-plugins (list-of-mpd-plugin '()) "List of MPD archive plugin configurations." (lambda (_ x) (mpd-serialize-list-of-mpd-plugin "archive_plugin" x))) (input-cache-size maybe-string "MPD input cache size." (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." (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." (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." (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-log-rotation config) (match-record config (log-file) (log-rotation (files (list log-file)) (post-rotate #~(begin (use-modules (gnu services herd)) (with-shepherd-action 'mpd ('reopen) #f)))))) (define (mpd-shepherd-service config) (match-record config (user package shepherd-requirement) (let* ((config-file (mpd-serialize-configuration config))) (shepherd-service (documentation "Run the MPD (Music Player Daemon)") (requirement `(user-processes loopback ,@shepherd-requirement)) (provision '(mpd)) (start #~(make-forkexec-constructor (list #$(file-append package "/bin/mpd") "--no-daemon" #$config-file) #:environment-variables ;; Required to detect PulseAudio when run under a user account. (list (string-append "XDG_RUNTIME_DIR=/run/user/" (number->string (passwd:uid (getpwnam #$user))))))) (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-service-activation config) (match-record config (user log-file) #~(begin (use-modules (guix build utils)) (let* ((user (getpw #$user)) (deprecated-directory (string-append "/var/run/mpd/" #$user "/.mpd")) (new-directory (string-append (passwd:dir user) "/.config/mpd"))) ;; TODO: remove me, migrates from the old location at /var/run/mpd to the new one at /var/lib/mpd. (when (and (file-exists? deprecated-directory) (not (file-exists? new-directory))) (rename-file deprecated-directory new-directory) (chown new-directory (passwd:uid user) (passwd:gid user))) (mkdir-p (dirname #$log-file)))))) (define (mpd-accounts config) (match-record config (user) (list (user-account (name user) (group "nogroup") (system? #t) (comment "Music Player Daemon (MPD) user") (home-directory "/var/lib/mpd") ; MPD can use $HOME (or $XDG_CONFIG_HOME) to place its data (shell (file-append shadow "/sbin/nologin")))))) (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) (service-extension activation-service-type mpd-service-activation) (service-extension rottlog-service-type (compose list mpd-log-rotation)))) (default-value (mpd-configuration))))