1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Peter Mikkelsen <petermikkelsen10@gmail.com>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
32 ;;; Audio related services
36 (define-record-type* <mpd-configuration>
37 mpd-configuration make-mpd-configuration
39 (user mpd-configuration-user
41 (music-dir mpd-configuration-music-dir
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
53 (address mpd-configuration-address
56 (define (mpd-config->file config)
58 mixed-text-file "mpd.conf"
63 "pid_file \"" (mpd-file-name config "pid") "\"\n"
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)))))
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)
83 (define (mpd-shepherd-service config)
85 (documentation "Run the MPD (Music Player Daemon)")
87 (start #~(make-forkexec-constructor
88 (list #$(file-append mpd "/bin/mpd")
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))))
95 (define (mpd-service-activation config)
96 (with-imported-modules '((guix build utils))
98 (use-modules (guix build utils))
100 (getpw #$(mpd-configuration-user config)))
102 (let ((directory #$(mpd-file-name config "")))
104 (chown directory (passwd:uid %user) (passwd:gid %user))))))
106 (define mpd-service-type
109 (description "Run the Music Player Daemon (MPD).")
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))))