1# D-Bus configuration and system bus daemon.
2
3{ config, lib, pkgs, ... }:
4
5with lib;
6
7let
8
9 cfg = config.services.dbus;
10
11 homeDir = "/run/dbus";
12
13 configDir = pkgs.makeDBusConf {
14 suidHelper = "${config.security.wrapperDir}/dbus-daemon-launch-helper";
15 serviceDirectories = cfg.packages;
16 };
17
18in
19
20{
21
22 ###### interface
23
24 options = {
25
26 services.dbus = {
27
28 enable = mkOption {
29 type = types.bool;
30 default = false;
31 internal = true;
32 description = ''
33 Whether to start the D-Bus message bus daemon, which is
34 required by many other system services and applications.
35 '';
36 };
37
38 packages = mkOption {
39 type = types.listOf types.path;
40 default = [ ];
41 description = ''
42 Packages whose D-Bus configuration files should be included in
43 the configuration of the D-Bus system-wide or session-wide
44 message bus. Specifically, files in the following directories
45 will be included into their respective DBus configuration paths:
46 <filename><replaceable>pkg</replaceable>/etc/dbus-1/system.d</filename>
47 <filename><replaceable>pkg</replaceable>/share/dbus-1/system-services</filename>
48 <filename><replaceable>pkg</replaceable>/etc/dbus-1/session.d</filename>
49 <filename><replaceable>pkg</replaceable>/share/dbus-1/services</filename>
50 '';
51 };
52
53 socketActivated = mkOption {
54 type = types.bool;
55 default = false;
56 description = ''
57 Make the user instance socket activated.
58 '';
59 };
60 };
61 };
62
63 ###### implementation
64
65 config = mkIf cfg.enable {
66
67 environment.systemPackages = [ pkgs.dbus.daemon pkgs.dbus ];
68
69 environment.etc = singleton
70 { source = configDir;
71 target = "dbus-1";
72 };
73
74 users.extraUsers.messagebus = {
75 uid = config.ids.uids.messagebus;
76 description = "D-Bus system message bus daemon user";
77 home = homeDir;
78 group = "messagebus";
79 };
80
81 users.extraGroups.messagebus.gid = config.ids.gids.messagebus;
82
83 systemd.packages = [ pkgs.dbus.daemon ];
84
85 security.wrappers.dbus-daemon-launch-helper = {
86 source = "${pkgs.dbus.daemon}/libexec/dbus-daemon-launch-helper";
87 owner = "root";
88 group = "messagebus";
89 setuid = true;
90 setgid = false;
91 permissions = "u+rx,g+rx,o-rx";
92 };
93
94 services.dbus.packages = [
95 pkgs.dbus.out
96 config.system.path
97 ];
98
99 systemd.services.dbus = {
100 # Don't restart dbus-daemon. Bad things tend to happen if we do.
101 reloadIfChanged = true;
102 restartTriggers = [ configDir ];
103 };
104
105 systemd.user = {
106 services.dbus = {
107 # Don't restart dbus-daemon. Bad things tend to happen if we do.
108 reloadIfChanged = true;
109 restartTriggers = [ configDir ];
110 };
111 sockets.dbus.wantedBy = mkIf cfg.socketActivated [ "sockets.target" ];
112 };
113
114 environment.pathsToLink = [ "/etc/dbus-1" "/share/dbus-1" ];
115 };
116}