audio.scm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Peter Mikkelsen <petermikkelsen10@gmail.com>
  3. ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu services audio)
  21. #:use-module (guix gexp)
  22. #:use-module (gnu services)
  23. #:use-module (gnu services shepherd)
  24. #:use-module (gnu system shadow)
  25. #:use-module (gnu packages admin)
  26. #:use-module (gnu packages mpd)
  27. #:use-module (guix records)
  28. #:use-module (ice-9 match)
  29. #:use-module (ice-9 format)
  30. #:export (mpd-output
  31. mpd-output?
  32. mpd-configuration
  33. mpd-configuration?
  34. mpd-service-type))
  35. ;;; Commentary:
  36. ;;;
  37. ;;; Audio related services
  38. ;;;
  39. ;;; Code:
  40. (define-record-type* <mpd-output>
  41. mpd-output make-mpd-output
  42. mpd-output?
  43. (type mpd-output-type
  44. (default "pulse"))
  45. (name mpd-output-name
  46. (default "MPD"))
  47. (enabled? mpd-output-enabled?
  48. (default #t))
  49. (tags? mpd-output-tags?
  50. (default #t))
  51. (always-on? mpd-output-always-on?
  52. (default #f))
  53. (mixer-type mpd-output-mixer-type
  54. ;; valid: hardware, software, null, none
  55. (default #f))
  56. (extra-options mpd-output-extra-options
  57. (default '())))
  58. (define-record-type* <mpd-configuration>
  59. mpd-configuration make-mpd-configuration
  60. mpd-configuration?
  61. (user mpd-configuration-user
  62. (default "mpd"))
  63. (music-dir mpd-configuration-music-dir
  64. (default "~/Music"))
  65. (playlist-dir mpd-configuration-playlist-dir
  66. (default "~/.mpd/playlists"))
  67. (db-file mpd-configuration-db-file
  68. (default "~/.mpd/tag_cache"))
  69. (state-file mpd-configuration-state-file
  70. (default "~/.mpd/state"))
  71. (sticker-file mpd-configuration-sticker-file
  72. (default "~/.mpd/sticker.sql"))
  73. (port mpd-configuration-port
  74. (default "6600"))
  75. (address mpd-configuration-address
  76. (default "any"))
  77. (outputs mpd-configuration-outputs
  78. (default (list (mpd-output)))))
  79. (define (mpd-output->string output)
  80. "Convert the OUTPUT of type <mpd-output> to a configuration file snippet."
  81. (let ((extra (string-join
  82. (map (match-lambda
  83. ((key . value)
  84. (format #f " ~a \"~a\""
  85. (string-map
  86. (lambda (c) (if (char=? c #\-) #\_ c))
  87. (symbol->string key))
  88. value)))
  89. (mpd-output-extra-options output))
  90. "\n")))
  91. (format #f "\
  92. audio_output {
  93. type \"~a\"
  94. name \"~a\"
  95. ~:[ enabled \"no\"~%~;~]\
  96. ~:[ tags \"no\"~%~;~]\
  97. ~:[~; always_on \"yes\"~%~]\
  98. ~@[ mixer_type \"~a\"~%~]\
  99. ~a~%}~%"
  100. (mpd-output-type output)
  101. (mpd-output-name output)
  102. (mpd-output-enabled? output)
  103. (mpd-output-tags? output)
  104. (mpd-output-always-on? output)
  105. (mpd-output-mixer-type output)
  106. extra)))
  107. (define (mpd-config->file config)
  108. (apply
  109. mixed-text-file "mpd.conf"
  110. "pid_file \"" (mpd-file-name config "pid") "\"\n"
  111. (append (map mpd-output->string
  112. (mpd-configuration-outputs config))
  113. (map (match-lambda
  114. ((config-name config-val)
  115. (string-append config-name " \"" (config-val config) "\"\n")))
  116. `(("user" ,mpd-configuration-user)
  117. ("music_directory" ,mpd-configuration-music-dir)
  118. ("playlist_directory" ,mpd-configuration-playlist-dir)
  119. ("db_file" ,mpd-configuration-db-file)
  120. ("state_file" ,mpd-configuration-state-file)
  121. ("sticker_file" ,mpd-configuration-sticker-file)
  122. ("port" ,mpd-configuration-port)
  123. ("bind_to_address" ,mpd-configuration-address))))))
  124. (define (mpd-file-name config file)
  125. "Return a path in /var/run/mpd/ that is writable
  126. by @code{user} from @code{config}."
  127. (string-append "/var/run/mpd/"
  128. (mpd-configuration-user config)
  129. "/" file))
  130. (define (mpd-shepherd-service config)
  131. (shepherd-service
  132. (documentation "Run the MPD (Music Player Daemon)")
  133. (requirement '(user-processes))
  134. (provision '(mpd))
  135. (start #~(make-forkexec-constructor
  136. (list #$(file-append mpd "/bin/mpd")
  137. "--no-daemon"
  138. #$(mpd-config->file config))
  139. #:environment-variables
  140. ;; Required to detect PulseAudio when run under a user account.
  141. (list (string-append
  142. "XDG_RUNTIME_DIR=/run/user/"
  143. (number->string
  144. (passwd:uid
  145. (getpwnam #$(mpd-configuration-user config))))))
  146. #:log-file #$(mpd-file-name config "log")))
  147. (stop #~(make-kill-destructor))))
  148. (define (mpd-service-activation config)
  149. (with-imported-modules '((guix build utils))
  150. #~(begin
  151. (use-modules (guix build utils))
  152. (define %user
  153. (getpw #$(mpd-configuration-user config)))
  154. (let ((directory #$(mpd-file-name config ".mpd")))
  155. (mkdir-p directory)
  156. (chown directory (passwd:uid %user) (passwd:gid %user))
  157. ;; Make /var/run/mpd/USER user-owned as well.
  158. (chown (dirname directory)
  159. (passwd:uid %user) (passwd:gid %user))))))
  160. (define %mpd-accounts
  161. ;; Default account and group for MPD.
  162. (list (user-group (name "mpd") (system? #t))
  163. (user-account
  164. (name "mpd")
  165. (group "mpd")
  166. (system? #t)
  167. (comment "Music Player Daemon (MPD) user")
  168. ;; Note: /var/run/mpd hosts one sub-directory per user, of which
  169. ;; /var/run/mpd/mpd corresponds to the "mpd" user.
  170. (home-directory "/var/run/mpd/mpd")
  171. (shell (file-append shadow "/sbin/nologin")))))
  172. (define mpd-service-type
  173. (service-type
  174. (name 'mpd)
  175. (description "Run the Music Player Daemon (MPD).")
  176. (extensions
  177. (list (service-extension shepherd-root-service-type
  178. (compose list mpd-shepherd-service))
  179. (service-extension account-service-type
  180. (const %mpd-accounts))
  181. (service-extension activation-service-type
  182. mpd-service-activation)))
  183. (default-value (mpd-configuration))))