1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
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 dbus)
20 #:use-module (gnu services)
21 #:use-module (gnu services dmd)
22 #:use-module (gnu system shadow)
23 #:use-module (gnu packages glib)
24 #:use-module (gnu packages admin)
25 #:use-module (guix gexp)
26 #:use-module (guix records)
27 #:use-module (srfi srfi-1)
28 #:use-module (ice-9 match)
29 #:export (dbus-root-service-type
36 (define-record-type* <dbus-configuration>
37 dbus-configuration make-dbus-configuration
39 (dbus dbus-configuration-dbus ;<package>
41 (services dbus-configuration-services ;list of <package>
44 (define (dbus-configuration-directory dbus services)
45 "Return a configuration directory for @var{dbus} that includes the
46 @code{etc/dbus-1/system.d} directories of each package listed in
50 (use-modules (sxml simple)
53 (define (services->sxml services)
54 ;; Return the SXML 'includedir' clauses for DIRS.
56 ,@(append-map (lambda (dir)
58 ,(string-append dir "/etc/dbus-1/system.d"))
59 (servicedir ;for '.service' files
60 ,(string-append dir "/share/dbus-1/services"))
61 (servicedir ;likewise, for auto-activation
64 "/share/dbus-1/system-services"))))
68 (copy-file (string-append #$dbus "/etc/dbus-1/system.conf")
69 (string-append #$output "/system.conf"))
71 ;; The default 'system.conf' has an <includedir> clause for
72 ;; 'system.d', so create it.
73 (mkdir (string-append #$output "/system.d"))
75 ;; 'system-local.conf' is automatically included by the default
76 ;; 'system.conf', so this is where we stuff our own things.
77 (call-with-output-file (string-append #$output "/system-local.conf")
79 (sxml->xml (services->sxml (list #$@services))
82 (computed-file "dbus-configuration" build))
84 (define %dbus-accounts
85 ;; Accounts used by the system bus.
86 (list (user-group (name "messagebus") (system? #t))
91 (comment "D-Bus system bus user")
92 (home-directory "/var/run/dbus")
93 (shell #~(string-append #$shadow "/sbin/nologin")))))
95 (define (dbus-activation config)
96 "Return an activation gexp for D-Bus using @var{config}."
98 (use-modules (guix build utils))
100 (mkdir-p "/var/run/dbus")
102 (let ((user (getpwnam "messagebus")))
103 (chown "/var/run/dbus"
104 (passwd:uid user) (passwd:gid user)))
106 (unless (file-exists? "/etc/machine-id")
107 (format #t "creating /etc/machine-id...~%")
108 (let ((prog (string-append #$(dbus-configuration-dbus config)
109 "/bin/dbus-uuidgen")))
110 ;; XXX: We can't use 'system' because the initrd's
111 ;; guile system(3) only works when 'sh' is in $PATH.
112 (let ((pid (primitive-fork)))
114 (call-with-output-file "/etc/machine-id"
117 (dup2 (port->fdes port) 1)
121 (define dbus-dmd-service
123 (($ <dbus-configuration> dbus services)
124 (let ((conf (dbus-configuration-directory dbus services)))
126 (documentation "Run the D-Bus system daemon.")
127 (provision '(dbus-system))
128 (requirement '(user-processes))
129 (start #~(make-forkexec-constructor
130 (list (string-append #$dbus "/bin/dbus-daemon")
132 (string-append "--config-file=" #$conf
134 (stop #~(make-kill-destructor))))))))
136 (define dbus-root-service-type
137 (service-type (name 'dbus)
139 (list (service-extension dmd-root-service-type
141 (service-extension activation-service-type
143 (service-extension account-service-type
144 (const %dbus-accounts))))
146 ;; Extensions consist of lists of packages (representing D-Bus
147 ;; services) that we just concatenate.
149 ;; FIXME: We need 'dbus-daemon-launch-helper' to be
150 ;; setuid-root for auto-activation to work.
151 (compose concatenate)
153 ;; The service's parameters field is extended by augmenting
154 ;; its <dbus-configuration> 'services' field.
155 (extend (lambda (config services)
159 (append (dbus-configuration-services config)
162 (define* (dbus-service #:key (dbus dbus) (services '()))
163 "Return a service that runs the \"system bus\", using @var{dbus}, with
164 support for @var{services}.
166 @uref{http://dbus.freedesktop.org/, D-Bus} is an inter-process communication
167 facility. Its system bus is used to allow system services to communicate and
168 be notified of system-wide events.
170 @var{services} must be a list of packages that provide an
171 @file{etc/dbus-1/system.d} directory containing additional D-Bus configuration
172 and policy files. For example, to allow avahi-daemon to use the system bus,
173 @var{services} must be equal to @code{(list avahi)}."
174 (service dbus-root-service-type
175 (dbus-configuration (dbus dbus)
176 (services services))))
178 ;;; dbus.scm ends here