audio.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Peter Mikkelsen <petermikkelsen10@gmail.com>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu services audio)
  19. #:use-module (guix gexp)
  20. #:use-module (gnu services)
  21. #:use-module (gnu services shepherd)
  22. #:use-module (gnu packages mpd)
  23. #:use-module (guix records)
  24. #:use-module (ice-9 match)
  25. #:export (mpd-configuration
  26. mpd-configuration?
  27. mpd-service-type))
  28. ;;; Commentary:
  29. ;;;
  30. ;;; Audio related services
  31. ;;;
  32. ;;; Code:
  33. (define-record-type* <mpd-configuration>
  34. mpd-configuration make-mpd-configuration
  35. mpd-configuration?
  36. (user mpd-configuration-user
  37. (default "mpd"))
  38. (music-dir mpd-configuration-music-dir
  39. (default "~/Music"))
  40. (playlist-dir mpd-configuration-playlist-dir
  41. (default "~/.mpd/playlists"))
  42. (port mpd-configuration-port
  43. (default "6600"))
  44. (address mpd-configuration-address
  45. (default "any")))
  46. (define (mpd-config->file config)
  47. (apply
  48. mixed-text-file "mpd.conf"
  49. "audio_output {\n"
  50. " type \"pulse\"\n"
  51. " name \"MPD\"\n"
  52. "}\n"
  53. "pid_file \"" (mpd-file-name config "pid") "\"\n"
  54. (map (match-lambda
  55. ((config-name config-val)
  56. (string-append config-name " \"" (config-val config) "\"\n")))
  57. `(("user" ,mpd-configuration-user)
  58. ("music_directory" ,mpd-configuration-music-dir)
  59. ("playlist_directory" ,mpd-configuration-playlist-dir)
  60. ("port" ,mpd-configuration-port)
  61. ("bind_to_address" ,mpd-configuration-address)))))
  62. (define (mpd-file-name config file)
  63. "Return a path in /var/run/mpd/ that is writable
  64. by @code{user} from @code{config}."
  65. (string-append "/var/run/mpd/"
  66. (mpd-configuration-user config)
  67. "/" file))
  68. (define (mpd-shepherd-service config)
  69. (shepherd-service
  70. (documentation "Run the MPD (Music Player Daemon)")
  71. (provision '(mpd))
  72. (start #~(make-forkexec-constructor
  73. (list #$(file-append mpd "/bin/mpd")
  74. "--no-daemon"
  75. #$(mpd-config->file config))
  76. #:pid-file #$(mpd-file-name config "pid")
  77. #:log-file #$(mpd-file-name config "log")))
  78. (stop #~(make-kill-destructor))))
  79. (define (mpd-service-activation config)
  80. (with-imported-modules '((guix build utils))
  81. #~(begin
  82. (use-modules (guix build utils))
  83. (define %user
  84. (getpw #$(mpd-configuration-user config)))
  85. (let ((directory #$(mpd-file-name config "")))
  86. (mkdir-p directory)
  87. (chown directory (passwd:uid %user) (passwd:gid %user))))))
  88. (define mpd-service-type
  89. (service-type
  90. (name 'mpd)
  91. (description "Run the Music Player Daemon (MPD).")
  92. (extensions
  93. (list (service-extension shepherd-root-service-type
  94. (compose list mpd-shepherd-service))
  95. (service-extension activation-service-type
  96. mpd-service-activation)))
  97. (default-value (mpd-configuration))))