6629433780de9b355a911e5e7b41830233ff4bd4
[jackhill/guix/guix.git] / gnu / services / audio.scm
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
19 (define-module (gnu services audio)
20 #:use-module (guix gexp)
21 #:use-module (gnu services)
22 #:use-module (gnu services shepherd)
23 #:use-module (gnu packages mpd)
24 #:use-module (guix records)
25 #:use-module (ice-9 match)
26 #:export (mpd-configuration
27 mpd-configuration?
28 mpd-service-type))
29
30 ;;; Commentary:
31 ;;;
32 ;;; Audio related services
33 ;;;
34 ;;; Code:
35
36 (define-record-type* <mpd-configuration>
37 mpd-configuration make-mpd-configuration
38 mpd-configuration?
39 (user mpd-configuration-user
40 (default "mpd"))
41 (music-dir mpd-configuration-music-dir
42 (default "~/Music"))
43 (playlist-dir mpd-configuration-playlist-dir
44 (default "~/.mpd/playlists"))
45 (port mpd-configuration-port
46 (default "6600"))
47 (address mpd-configuration-address
48 (default "any")))
49
50 (define (mpd-config->file config)
51 (apply
52 mixed-text-file "mpd.conf"
53 "audio_output {\n"
54 " type \"pulse\"\n"
55 " name \"MPD\"\n"
56 "}\n"
57 "pid_file \"" (mpd-file-name config "pid") "\"\n"
58 (map (match-lambda
59 ((config-name config-val)
60 (string-append config-name " \"" (config-val config) "\"\n")))
61 `(("user" ,mpd-configuration-user)
62 ("music_directory" ,mpd-configuration-music-dir)
63 ("playlist_directory" ,mpd-configuration-playlist-dir)
64 ("port" ,mpd-configuration-port)
65 ("bind_to_address" ,mpd-configuration-address)))))
66
67 (define (mpd-file-name config file)
68 "Return a path in /var/run/mpd/ that is writable
69 by @code{user} from @code{config}."
70 (string-append "/var/run/mpd/"
71 (mpd-configuration-user config)
72 "/" file))
73
74 (define (mpd-shepherd-service config)
75 (shepherd-service
76 (documentation "Run the MPD (Music Player Daemon)")
77 (provision '(mpd))
78 (start #~(make-forkexec-constructor
79 (list #$(file-append mpd "/bin/mpd")
80 "--no-daemon"
81 #$(mpd-config->file config))
82 #:pid-file #$(mpd-file-name config "pid")
83 #:log-file #$(mpd-file-name config "log")))
84 (stop #~(make-kill-destructor))))
85
86 (define (mpd-service-activation config)
87 (with-imported-modules '((guix build utils))
88 #~(begin
89 (use-modules (guix build utils))
90 (define %user
91 (getpw #$(mpd-configuration-user config)))
92
93 (let ((directory #$(mpd-file-name config "")))
94 (mkdir-p directory)
95 (chown directory (passwd:uid %user) (passwd:gid %user))))))
96
97 (define mpd-service-type
98 (service-type
99 (name 'mpd)
100 (description "Run the Music Player Daemon (MPD).")
101 (extensions
102 (list (service-extension shepherd-root-service-type
103 (compose list mpd-shepherd-service))
104 (service-extension activation-service-type
105 mpd-service-activation)))
106 (default-value (mpd-configuration))))