1 # D-Bus configuration and system bus daemon.
3 { config, lib, pkgs, ... }:
7 cfg = config.services.dbus;
11 configDir = pkgs.makeDBusConf {
12 inherit (cfg) apparmor;
13 suidHelper = "${config.security.wrapperDir}/dbus-daemon-launch-helper";
14 serviceDirectories = cfg.packages;
17 inherit (lib) mkOption mkEnableOption mkIf mkMerge types;
24 boot.initrd.systemd.dbus = {
25 enable = mkEnableOption "dbus in stage 1";
35 Whether to start the D-Bus message bus daemon, which is
36 required by many other system services and applications.
40 implementation = mkOption {
41 type = types.enum [ "dbus" "broker" ];
44 The implementation to use for the message bus defined by the D-Bus specification.
45 Can be either the classic dbus daemon or dbus-broker, which aims to provide high
46 performance and reliability, while keeping compatibility to the D-Bus
47 reference implementation.
53 type = types.listOf types.path;
56 Packages whose D-Bus configuration files should be included in
57 the configuration of the D-Bus system-wide or session-wide
58 message bus. Specifically, files in the following directories
59 will be included into their respective DBus configuration paths:
60 {file}`«pkg»/etc/dbus-1/system.d`
61 {file}`«pkg»/share/dbus-1/system.d`
62 {file}`«pkg»/share/dbus-1/system-services`
63 {file}`«pkg»/etc/dbus-1/session.d`
64 {file}`«pkg»/share/dbus-1/session.d`
65 {file}`«pkg»/share/dbus-1/services`
70 type = types.enum [ "enabled" "disabled" "required" ];
72 AppArmor mode for dbus.
74 `enabled` enables mediation when it's
75 supported in the kernel, `disabled`
76 always disables AppArmor even with kernel support, and
77 `required` fails when AppArmor was not found
85 config = mkIf cfg.enable (mkMerge [
87 environment.etc."dbus-1".source = configDir;
89 environment.pathsToLink = [
94 users.users.messagebus = {
95 uid = config.ids.uids.messagebus;
96 description = "D-Bus system message bus daemon user";
102 users.groups.messagebus.gid = config.ids.gids.messagebus;
104 # Install dbus for dbus tools even when using dbus-broker
105 environment.systemPackages = [
109 # You still need the dbus reference implementation installed to use dbus-broker
114 services.dbus.packages = [
119 systemd.user.sockets.dbus.wantedBy = [
124 (mkIf config.boot.initrd.systemd.dbus.enable {
125 boot.initrd.systemd = {
126 users.messagebus = { };
127 groups.messagebus = { };
128 contents."/etc/dbus-1".source = pkgs.makeDBusConf {
129 inherit (cfg) apparmor;
130 suidHelper = "/bin/false";
131 serviceDirectories = [ pkgs.dbus config.boot.initrd.systemd.package ];
133 packages = [ pkgs.dbus ];
135 "${pkgs.dbus}/bin/dbus-daemon"
136 "${config.boot.initrd.systemd.package}/share/dbus-1/system-services"
137 "${config.boot.initrd.systemd.package}/share/dbus-1/system.d"
139 targets.sockets.wants = [ "dbus.socket" ];
143 (mkIf (cfg.implementation == "dbus") {
144 security.wrappers.dbus-daemon-launch-helper = {
145 source = "${pkgs.dbus}/libexec/dbus-daemon-launch-helper";
147 group = "messagebus";
150 permissions = "u+rx,g+rx,o-rx";
153 systemd.services.dbus = {
155 # hack aiding to prevent dbus from restarting when switching from dbus-broker back to dbus
156 "dbus-broker.service"
158 # Don't restart dbus-daemon. Bad things tend to happen if we do.
159 reloadIfChanged = true;
164 LD_LIBRARY_PATH = config.system.nssModules.path;
168 systemd.user.services.dbus = {
170 # hack aiding to prevent dbus from restarting when switching from dbus-broker back to dbus
171 "dbus-broker.service"
173 # Don't restart dbus-daemon. Bad things tend to happen if we do.
174 reloadIfChanged = true;
182 (mkIf (cfg.implementation == "broker") {
183 environment.systemPackages = [
191 # Just to be sure we don't restart through the unit alias
192 systemd.services.dbus.reloadIfChanged = true;
193 systemd.user.services.dbus.reloadIfChanged = true;
195 # NixOS Systemd Module doesn't respect 'Install'
196 # https://github.com/NixOS/nixpkgs/issues/108643
197 systemd.services.dbus-broker = {
199 # allow other services to just depend on dbus,
200 # but also a hack aiding to prevent dbus from restarting when switching from dbus-broker back to dbus
204 # We get errors when reloading the dbus-broker service
205 # if /tmp got remounted after this service started
206 RequiresMountsFor = [ "/tmp" ];
208 # Don't restart dbus. Bad things tend to happen if we do.
209 reloadIfChanged = true;
214 LD_LIBRARY_PATH = config.system.nssModules.path;
218 systemd.user.services.dbus-broker = {
220 # allow other services to just depend on dbus,
221 # but also a hack aiding to prevent dbus from restarting when switching from dbus-broker back to dbus
224 # Don't restart dbus. Bad things tend to happen if we do.
225 reloadIfChanged = true;