1 # D-Bus configuration and system bus daemon.
3 { config, lib, pkgs, ... }:
7 cfg = config.services.dbus;
9 configDir = pkgs.makeDBusConf.override {
10 inherit (cfg) apparmor;
11 dbus = cfg.dbusPackage;
12 suidHelper = "${config.security.wrapperDir}/dbus-daemon-launch-helper";
13 serviceDirectories = cfg.packages;
16 inherit (lib) mkOption mkEnableOption mkIf mkMerge types;
23 boot.initrd.systemd.dbus = {
24 enable = mkEnableOption "dbus in stage 1";
34 Whether to start the D-Bus message bus daemon, which is
35 required by many other system services and applications.
39 dbusPackage = lib.mkPackageOption pkgs "dbus" {};
41 brokerPackage = lib.mkPackageOption pkgs "dbus-broker" {};
43 implementation = mkOption {
44 type = types.enum [ "dbus" "broker" ];
47 The implementation to use for the message bus defined by the D-Bus specification.
48 Can be either the classic dbus daemon or dbus-broker, which aims to provide high
49 performance and reliability, while keeping compatibility to the D-Bus
50 reference implementation.
55 type = types.listOf types.path;
58 Packages whose D-Bus configuration files should be included in
59 the configuration of the D-Bus system-wide or session-wide
60 message bus. Specifically, files in the following directories
61 will be included into their respective DBus configuration paths:
62 {file}`«pkg»/etc/dbus-1/system.d`
63 {file}`«pkg»/share/dbus-1/system.d`
64 {file}`«pkg»/share/dbus-1/system-services`
65 {file}`«pkg»/etc/dbus-1/session.d`
66 {file}`«pkg»/share/dbus-1/session.d`
67 {file}`«pkg»/share/dbus-1/services`
72 type = types.enum [ "enabled" "disabled" "required" ];
74 AppArmor mode for dbus.
76 `enabled` enables mediation when it's
77 supported in the kernel, `disabled`
78 always disables AppArmor even with kernel support, and
79 `required` fails when AppArmor was not found
87 config = mkIf cfg.enable (mkMerge [
89 environment.etc."dbus-1".source = configDir;
91 environment.pathsToLink = [
96 users.users.messagebus = {
97 uid = config.ids.uids.messagebus;
98 description = "D-Bus system message bus daemon user";
101 group = "messagebus";
104 users.groups.messagebus.gid = config.ids.gids.messagebus;
106 # Install dbus for dbus tools even when using dbus-broker
107 environment.systemPackages = [
111 # You still need the dbus reference implementation installed to use dbus-broker
116 services.dbus.packages = [
121 systemd.user.sockets.dbus.wantedBy = [
126 (mkIf config.boot.initrd.systemd.dbus.enable {
127 boot.initrd.systemd = {
128 users.messagebus = { };
129 groups.messagebus = { };
130 contents."/etc/dbus-1".source = pkgs.makeDBusConf.override {
131 inherit (cfg) apparmor;
132 dbus = cfg.dbusPackage;
133 suidHelper = "/bin/false";
134 serviceDirectories = [ cfg.dbusPackage config.boot.initrd.systemd.package ];
136 packages = [ cfg.dbusPackage ];
138 "${cfg.dbusPackage}/bin/dbus-daemon"
139 "${config.boot.initrd.systemd.package}/share/dbus-1/system-services"
140 "${config.boot.initrd.systemd.package}/share/dbus-1/system.d"
142 targets.sockets.wants = [ "dbus.socket" ];
146 (mkIf (cfg.implementation == "dbus") {
147 security.wrappers.dbus-daemon-launch-helper = {
148 source = "${cfg.dbusPackage}/libexec/dbus-daemon-launch-helper";
150 group = "messagebus";
153 permissions = "u+rx,g+rx,o-rx";
156 systemd.services.dbus = {
158 # hack aiding to prevent dbus from restarting when switching from dbus-broker back to dbus
159 "dbus-broker.service"
161 # Don't restart dbus-daemon. Bad things tend to happen if we do.
162 reloadIfChanged = true;
167 LD_LIBRARY_PATH = config.system.nssModules.path;
171 systemd.user.services.dbus = {
173 # hack aiding to prevent dbus from restarting when switching from dbus-broker back to dbus
174 "dbus-broker.service"
176 # Don't restart dbus-daemon. Bad things tend to happen if we do.
177 reloadIfChanged = true;
185 (mkIf (cfg.implementation == "broker") {
186 environment.systemPackages = [
194 # Just to be sure we don't restart through the unit alias
195 systemd.services.dbus.reloadIfChanged = true;
196 systemd.user.services.dbus.reloadIfChanged = true;
198 # NixOS Systemd Module doesn't respect 'Install'
199 # https://github.com/NixOS/nixpkgs/issues/108643
200 systemd.services.dbus-broker = {
202 # allow other services to just depend on dbus,
203 # but also a hack aiding to prevent dbus from restarting when switching from dbus-broker back to dbus
207 # We get errors when reloading the dbus-broker service
208 # if /tmp got remounted after this service started
209 RequiresMountsFor = [ "/tmp" ];
211 # Don't restart dbus. Bad things tend to happen if we do.
212 reloadIfChanged = true;
217 LD_LIBRARY_PATH = config.system.nssModules.path;
221 systemd.user.services.dbus-broker = {
223 # allow other services to just depend on dbus,
224 # but also a hack aiding to prevent dbus from restarting when switching from dbus-broker back to dbus
227 # Don't restart dbus. Bad things tend to happen if we do.
228 reloadIfChanged = true;