Merge branch 'master' into staging
[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 (db-file mpd-configuration-db-file
46 (default "~/.mpd/tag_cache"))
47 (state-file mpd-configuration-state-file
48 (default "~/.mpd/state"))
49 (sticker-file mpd-configuration-sticker-file
50 (default "~/.mpd/sticker.sql"))
51 (port mpd-configuration-port
52 (default "6600"))
53 (address mpd-configuration-address
54 (default "any")))
55
56 (define (mpd-config->file config)
57 (apply
58 mixed-text-file "mpd.conf"
59 "audio_output {\n"
60 " type \"pulse\"\n"
61 " name \"MPD\"\n"
62 "}\n"
63 "pid_file \"" (mpd-file-name config "pid") "\"\n"
64 (map (match-lambda
65 ((config-name config-val)
66 (string-append config-name " \"" (config-val config) "\"\n")))
67 `(("user" ,mpd-configuration-user)
68 ("music_directory" ,mpd-configuration-music-dir)
69 ("playlist_directory" ,mpd-configuration-playlist-dir)
70 ("db_file" ,mpd-configuration-db-file)
71 ("state_file" ,mpd-configuration-state-file)
72 ("sticker_file" ,mpd-configuration-sticker-file)
73 ("port" ,mpd-configuration-port)
74 ("bind_to_address" ,mpd-configuration-address)))))
75
76 (define (mpd-file-name config file)
77 "Return a path in /var/run/mpd/ that is writable
78 by @code{user} from @code{config}."
79 (string-append "/var/run/mpd/"
80 (mpd-configuration-user config)
81 "/" file))
82
83 (define (mpd-shepherd-service config)
84 (shepherd-service
85 (documentation "Run the MPD (Music Player Daemon)")
86 (provision '(mpd))
87 (start #~(make-forkexec-constructor
88 (list #$(file-append mpd "/bin/mpd")
89 "--no-daemon"
90 #$(mpd-config->file config))
91 #:pid-file #$(mpd-file-name config "pid")
92 #:log-file #$(mpd-file-name config "log")))
93 (stop #~(make-kill-destructor))))
94
95 (define (mpd-service-activation config)
96 (with-imported-modules '((guix build utils))
97 #~(begin
98 (use-modules (guix build utils))
99 (define %user
100 (getpw #$(mpd-configuration-user config)))
101
102 (let ((directory #$(mpd-file-name config "")))
103 (mkdir-p directory)
104 (chown directory (passwd:uid %user) (passwd:gid %user))))))
105
106 (define mpd-service-type
107 (service-type
108 (name 'mpd)
109 (description "Run the Music Player Daemon (MPD).")
110 (extensions
111 (list (service-extension shepherd-root-service-type
112 (compose list mpd-shepherd-service))
113 (service-extension activation-service-type
114 mpd-service-activation)))
115 (default-value (mpd-configuration))))