Merge branch 'core-updates'.
[jackhill/guix/guix.git] / gnu / services / dbus.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
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 dbus)
20 #:use-module (gnu services)
21 #:use-module (gnu system shadow)
22 #:use-module (gnu packages glib)
23 #:use-module (guix monads)
24 #:use-module (guix store)
25 #:use-module (guix gexp)
26 #:export (dbus-service))
27
28 ;;; Commentary:
29 ;;;
30 ;;; This module supports the configuration of the D-Bus message bus
31 ;;; (http://dbus.freedesktop.org/). D-Bus is an inter-process communication
32 ;;; facility. Its "system bus" is used to allow system services to
33 ;;; communicate and be notified of system-wide events.
34 ;;;
35 ;;; Code:
36
37 (define (dbus-configuration-directory dbus services)
38 "Return a configuration directory for @var{dbus} that includes the
39 @code{etc/dbus-1/system.d} directories of each package listed in
40 @var{services}."
41 (define build
42 #~(begin
43 (use-modules (sxml simple)
44 (srfi srfi-1))
45
46 (define (services->sxml services)
47 ;; Return the SXML 'includedir' clauses for DIRS.
48 `(busconfig
49 ,@(append-map (lambda (dir)
50 `((includedir
51 ,(string-append dir "/etc/dbus-1/system.d"))
52 (servicedir ;for '.service' files
53 ,(string-append dir "/share/dbus-1/services"))))
54 services)))
55
56 (mkdir #$output)
57 (copy-file (string-append #$dbus "/etc/dbus-1/system.conf")
58 (string-append #$output "/system.conf"))
59
60 ;; The default 'system.conf' has an <includedir> clause for
61 ;; 'system.d', so create it.
62 (mkdir (string-append #$output "/system.d"))
63
64 ;; 'system-local.conf' is automatically included by the default
65 ;; 'system.conf', so this is where we stuff our own things.
66 (call-with-output-file (string-append #$output "/system-local.conf")
67 (lambda (port)
68 (sxml->xml (services->sxml (list #$@services))
69 port)))))
70
71 (gexp->derivation "dbus-configuration" build))
72
73 (define* (dbus-service services #:key (dbus dbus))
74 "Return a service that runs the system bus, using @var{dbus}, with support
75 for @var{services}.
76
77 @var{services} must be a list of packages that provide an
78 @file{etc/dbus-1/system.d} directory containing additional D-Bus configuration
79 and policy files. For example, to allow avahi-daemon to use the system bus,
80 @var{services} must be equal to @code{(list avahi)}."
81 (mlet %store-monad ((conf (dbus-configuration-directory dbus services)))
82 (return
83 (service
84 (documentation "Run the D-Bus system daemon.")
85 (provision '(dbus-system))
86 (requirement '(user-processes))
87 (start #~(make-forkexec-constructor
88 (list (string-append #$dbus "/bin/dbus-daemon")
89 "--nofork"
90 (string-append "--config-file=" #$conf "/system.conf"))))
91 (stop #~(make-kill-destructor))
92 (user-groups (list (user-group
93 (name "messagebus")
94 (system? #t))))
95 (user-accounts (list (user-account
96 (name "messagebus")
97 (group "messagebus")
98 (system? #t)
99 (comment "D-Bus system bus user")
100 (home-directory "/var/run/dbus")
101 (shell
102 "/run/current-system/profile/sbin/nologin"))))
103 (activate #~(begin
104 (use-modules (guix build utils))
105
106 (mkdir-p "/var/run/dbus")
107
108 (let ((user (getpwnam "messagebus")))
109 (chown "/var/run/dbus"
110 (passwd:uid user) (passwd:gid user)))
111
112 (unless (file-exists? "/etc/machine-id")
113 (format #t "creating /etc/machine-id...~%")
114 (let ((prog (string-append #$dbus "/bin/dbus-uuidgen")))
115 ;; XXX: We can't use 'system' because the initrd's
116 ;; guile system(3) only works when 'sh' is in $PATH.
117 (let ((pid (primitive-fork)))
118 (if (zero? pid)
119 (call-with-output-file "/etc/machine-id"
120 (lambda (port)
121 (close-fdes 1)
122 (dup2 (port->fdes port) 1)
123 (execl prog)))
124 (waitpid pid)))))))))))
125
126 ;;; dbus.scm ends here