9 cfg = config.services.logind;
11 logindHandlerType = lib.types.enum [
20 "suspend-then-hibernate"
25 options.services.logind = {
26 extraConfig = lib.mkOption {
28 type = lib.types.lines;
29 example = "IdleAction=lock";
31 Extra config options for systemd-logind.
32 See [logind.conf(5)](https://www.freedesktop.org/software/systemd/man/logind.conf.html)
33 for available options.
37 killUserProcesses = lib.mkOption {
39 type = lib.types.bool;
41 Specifies whether the processes of a user should be killed
42 when the user logs out. If true, the scope unit corresponding
43 to the session and all processes inside that scope will be
44 terminated. If false, the scope is "abandoned"
45 (see [systemd.scope(5)](https://www.freedesktop.org/software/systemd/man/systemd.scope.html#)),
46 and processes are not killed.
48 See [logind.conf(5)](https://www.freedesktop.org/software/systemd/man/logind.conf.html#KillUserProcesses=)
53 powerKey = lib.mkOption {
56 type = logindHandlerType;
59 Specifies what to do when the power key is pressed.
63 powerKeyLongPress = lib.mkOption {
66 type = logindHandlerType;
69 Specifies what to do when the power key is long-pressed.
73 rebootKey = lib.mkOption {
76 type = logindHandlerType;
79 Specifies what to do when the reboot key is pressed.
83 rebootKeyLongPress = lib.mkOption {
86 type = logindHandlerType;
89 Specifies what to do when the reboot key is long-pressed.
93 suspendKey = lib.mkOption {
96 type = logindHandlerType;
99 Specifies what to do when the suspend key is pressed.
103 suspendKeyLongPress = lib.mkOption {
104 default = "hibernate";
106 type = logindHandlerType;
109 Specifies what to do when the suspend key is long-pressed.
113 hibernateKey = lib.mkOption {
114 default = "hibernate";
116 type = logindHandlerType;
119 Specifies what to do when the hibernate key is pressed.
123 hibernateKeyLongPress = lib.mkOption {
126 type = logindHandlerType;
129 Specifies what to do when the hibernate key is long-pressed.
133 lidSwitch = lib.mkOption {
136 type = logindHandlerType;
139 Specifies what to do when the laptop lid is closed.
143 lidSwitchExternalPower = lib.mkOption {
144 default = cfg.lidSwitch;
145 defaultText = lib.literalExpression "services.logind.lidSwitch";
147 type = logindHandlerType;
150 Specifies what to do when the laptop lid is closed
151 and the system is on external power. By default use
152 the same action as specified in services.logind.lidSwitch.
156 lidSwitchDocked = lib.mkOption {
159 type = logindHandlerType;
162 Specifies what to do when the laptop lid is closed
163 and another screen is added.
169 systemd.additionalUpstreamSystemUnits =
171 "systemd-logind.service"
173 "systemd-user-sessions.service"
175 ++ lib.optionals config.systemd.package.withImportd [
176 "dbus-org.freedesktop.import1.service"
178 ++ lib.optionals config.systemd.package.withMachined [
179 "dbus-org.freedesktop.machine1.service"
181 ++ lib.optionals config.systemd.package.withPortabled [
182 "dbus-org.freedesktop.portable1.service"
185 "dbus-org.freedesktop.login1.service"
187 "user-runtime-dir@.service"
191 "systemd/logind.conf".text = ''
193 KillUserProcesses=${if cfg.killUserProcesses then "yes" else "no"}
194 HandlePowerKey=${cfg.powerKey}
195 HandlePowerKeyLongPress=${cfg.powerKeyLongPress}
196 HandleRebootKey=${cfg.rebootKey}
197 HandleRebootKeyLongPress=${cfg.rebootKeyLongPress}
198 HandleSuspendKey=${cfg.suspendKey}
199 HandleSuspendKeyLongPress=${cfg.suspendKeyLongPress}
200 HandleHibernateKey=${cfg.hibernateKey}
201 HandleHibernateKeyLongPress=${cfg.hibernateKeyLongPress}
202 HandleLidSwitch=${cfg.lidSwitch}
203 HandleLidSwitchExternalPower=${cfg.lidSwitchExternalPower}
204 HandleLidSwitchDocked=${cfg.lidSwitchDocked}
209 # Restarting systemd-logind breaks X11
210 # - upstream commit: https://cgit.freedesktop.org/xorg/xserver/commit/?id=dc48bd653c7e101
211 # - systemd announcement: https://github.com/systemd/systemd/blob/22043e4317ecd2bc7834b48a6d364de76bb26d91/NEWS#L103-L112
212 # - this might be addressed in the future by xorg
213 #systemd.services.systemd-logind.restartTriggers = [ config.environment.etc."systemd/logind.conf".source ];
214 systemd.services.systemd-logind.restartIfChanged = false;
215 systemd.services.systemd-logind.stopIfChanged = false;
217 # The user-runtime-dir@ service is managed by systemd-logind we should not touch it or else we break the users' sessions.
218 systemd.services."user-runtime-dir@".stopIfChanged = false;
219 systemd.services."user-runtime-dir@".restartIfChanged = false;