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