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 = "/var/run/dbus"; 12 13 configDir = pkgs.stdenv.mkDerivation { 14 name = "dbus-conf"; 15 16 preferLocalBuild = true; 17 allowSubstitutes = false; 18 19 buildCommand = '' 20 mkdir -p $out 21 22 cp -v ${pkgs.dbus.daemon}/etc/dbus-1/system.conf $out/system.conf 23 24 # !!! Hm, these `sed' calls are rather error-prone... 25 26 # Tell the daemon where the setuid wrapper around 27 # dbus-daemon-launch-helper lives. 28 sed -i $out/system.conf \ 29 -e 's|<servicehelper>.*/libexec/dbus-daemon-launch-helper|<servicehelper>${config.security.wrapperDir}/dbus-daemon-launch-helper|' 30 31 # Add the system-services and system.d directories to the system 32 # bus search path. 33 sed -i $out/system.conf \ 34 -e 's|<standard_system_servicedirs/>|${systemServiceDirs}|' \ 35 -e 's|<includedir>system.d</includedir>|${systemIncludeDirs}|' 36 37 cp ${pkgs.dbus.daemon}/etc/dbus-1/session.conf $out/session.conf 38 39 # Add the services and session.d directories to the session bus 40 # search path. 41 sed -i $out/session.conf \ 42 -e 's|<standard_session_servicedirs />|${sessionServiceDirs}&|' \ 43 -e 's|<includedir>session.d</includedir>|${sessionIncludeDirs}|' 44 ''; # */ 45 }; 46 47 systemServiceDirs = concatMapStrings 48 (d: "<servicedir>${d}/share/dbus-1/system-services</servicedir> ") 49 cfg.packages; 50 51 systemIncludeDirs = concatMapStrings 52 (d: "<includedir>${d}/etc/dbus-1/system.d</includedir> ") 53 cfg.packages; 54 55 sessionServiceDirs = concatMapStrings 56 (d: "<servicedir>${d}/share/dbus-1/services</servicedir> ") 57 cfg.packages; 58 59 sessionIncludeDirs = concatMapStrings 60 (d: "<includedir>${d}/etc/dbus-1/session.d</includedir> ") 61 cfg.packages; 62 63in 64 65{ 66 67 ###### interface 68 69 options = { 70 71 services.dbus = { 72 73 enable = mkOption { 74 type = types.bool; 75 default = true; 76 internal = true; 77 description = '' 78 Whether to start the D-Bus message bus daemon, which is 79 required by many other system services and applications. 80 ''; 81 }; 82 83 packages = mkOption { 84 type = types.listOf types.path; 85 default = []; 86 description = '' 87 Packages whose D-Bus configuration files should be included in 88 the configuration of the D-Bus system-wide message bus. 89 Specifically, every file in 90 <filename><replaceable>pkg</replaceable>/etc/dbus-1/system.d</filename> 91 is included. 92 ''; 93 }; 94 95 }; 96 97 }; 98 99 100 ###### implementation 101 102 config = mkIf cfg.enable { 103 104 environment.systemPackages = [ pkgs.dbus.daemon pkgs.dbus_tools ]; 105 106 environment.etc = singleton 107 { source = configDir; 108 target = "dbus-1"; 109 }; 110 111 users.extraUsers.messagebus = { 112 uid = config.ids.uids.messagebus; 113 description = "D-Bus system message bus daemon user"; 114 home = homeDir; 115 group = "messagebus"; 116 }; 117 118 users.extraGroups.messagebus.gid = config.ids.gids.messagebus; 119 120 systemd.packages = [ pkgs.dbus.daemon ]; 121 122 security.setuidOwners = singleton 123 { program = "dbus-daemon-launch-helper"; 124 source = "${pkgs.dbus_daemon}/libexec/dbus-daemon-launch-helper"; 125 owner = "root"; 126 group = "messagebus"; 127 setuid = true; 128 setgid = false; 129 permissions = "u+rx,g+rx,o-rx"; 130 }; 131 132 services.dbus.packages = 133 [ "/nix/var/nix/profiles/default" 134 config.system.path 135 ]; 136 137 # Don't restart dbus-daemon. Bad things tend to happen if we do. 138 systemd.services.dbus.reloadIfChanged = true; 139 140 systemd.services.dbus.restartTriggers = [ configDir ]; 141 142 environment.pathsToLink = [ "/etc/dbus-1" "/share/dbus-1" ]; 143 144 }; 145 146}