summary refs log tree commit diff
path: root/nixos/modules/system/boot/systemd/logind.nix
blob: cf01c18828573d40ba18af04fdef1a56faa14237 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
{ config, lib, pkgs, utils, ... }:

with lib;

let
  cfg = config.services.logind;

  logindHandlerType = types.enum [
    "ignore" "poweroff" "reboot" "halt" "kexec" "suspend"
    "hibernate" "hybrid-sleep" "suspend-then-hibernate" "lock"
  ];
in
{
  options.services.logind = {
    extraConfig = mkOption {
      default = "";
      type = types.lines;
      example = "IdleAction=lock";
      description = lib.mdDoc ''
        Extra config options for systemd-logind.
        See [logind.conf(5)](https://www.freedesktop.org/software/systemd/man/logind.conf.html)
        for available options.
      '';
    };

    killUserProcesses = mkOption {
      default = false;
      type = types.bool;
      description = lib.mdDoc ''
        Specifies whether the processes of a user should be killed
        when the user logs out.  If true, the scope unit corresponding
        to the session and all processes inside that scope will be
        terminated.  If false, the scope is "abandoned"
        (see [systemd.scope(5)](https://www.freedesktop.org/software/systemd/man/systemd.scope.html#)),
        and processes are not killed.

        See [logind.conf(5)](https://www.freedesktop.org/software/systemd/man/logind.conf.html#KillUserProcesses=)
        for more details.
      '';
    };

    powerKey = mkOption {
      default = "poweroff";
      example = "ignore";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the power key is pressed.
      '';
    };

    powerKeyLongPress = mkOption {
      default = "ignore";
      example = "reboot";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the power key is long-pressed.
      '';
    };

    rebootKey = mkOption {
      default = "reboot";
      example = "ignore";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the reboot key is pressed.
      '';
    };

    rebootKeyLongPress = mkOption {
      default = "poweroff";
      example = "ignore";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the reboot key is long-pressed.
      '';
    };

    suspendKey = mkOption {
      default = "suspend";
      example = "ignore";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the suspend key is pressed.
      '';
    };

    suspendKeyLongPress = mkOption {
      default = "hibernate";
      example = "ignore";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the suspend key is long-pressed.
      '';
    };

    hibernateKey = mkOption {
      default = "hibernate";
      example = "ignore";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the hibernate key is pressed.
      '';
    };

    hibernateKeyLongPress = mkOption {
      default = "ignore";
      example = "suspend";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the hibernate key is long-pressed.
      '';
    };

    lidSwitch = mkOption {
      default = "suspend";
      example = "ignore";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the laptop lid is closed.
      '';
    };

    lidSwitchExternalPower = mkOption {
      default = cfg.lidSwitch;
      defaultText = literalExpression "services.logind.lidSwitch";
      example = "ignore";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the laptop lid is closed
        and the system is on external power. By default use
        the same action as specified in services.logind.lidSwitch.
      '';
    };

    lidSwitchDocked = mkOption {
      default = "ignore";
      example = "suspend";
      type = logindHandlerType;

      description = lib.mdDoc ''
        Specifies what to do when the laptop lid is closed
        and another screen is added.
      '';
    };
  };

  config = {
    systemd.additionalUpstreamSystemUnits = [
      "systemd-logind.service"
      "autovt@.service"
      "systemd-user-sessions.service"
    ] ++ optionals config.systemd.package.withImportd [
      "dbus-org.freedesktop.import1.service"
    ] ++ optionals config.systemd.package.withMachined [
      "dbus-org.freedesktop.machine1.service"
    ] ++ optionals config.systemd.package.withPortabled [
      "dbus-org.freedesktop.portable1.service"
    ] ++ [
      "dbus-org.freedesktop.login1.service"
      "user@.service"
      "user-runtime-dir@.service"
    ];

    environment.etc = {
      "systemd/logind.conf".text = ''
        [Login]
        KillUserProcesses=${if cfg.killUserProcesses then "yes" else "no"}
        HandlePowerKey=${cfg.powerKey}
        HandlePowerKeyLongPress=${cfg.powerKeyLongPress}
        HandleRebootKey=${cfg.rebootKey}
        HandleRebootKeyLongPress=${cfg.rebootKeyLongPress}
        HandleSuspendKey=${cfg.suspendKey}
        HandleSuspendKeyLongPress=${cfg.suspendKeyLongPress}
        HandleHibernateKey=${cfg.hibernateKey}
        HandleHibernateKeyLongPress=${cfg.hibernateKeyLongPress}
        HandleLidSwitch=${cfg.lidSwitch}
        HandleLidSwitchExternalPower=${cfg.lidSwitchExternalPower}
        HandleLidSwitchDocked=${cfg.lidSwitchDocked}
        ${cfg.extraConfig}
      '';
    };

    # Restarting systemd-logind breaks X11
    # - upstream commit: https://cgit.freedesktop.org/xorg/xserver/commit/?id=dc48bd653c7e101
    # - systemd announcement: https://github.com/systemd/systemd/blob/22043e4317ecd2bc7834b48a6d364de76bb26d91/NEWS#L103-L112
    # - this might be addressed in the future by xorg
    #systemd.services.systemd-logind.restartTriggers = [ config.environment.etc."systemd/logind.conf".source ];
    systemd.services.systemd-logind.restartIfChanged = false;
    systemd.services.systemd-logind.stopIfChanged = false;

    # The user-runtime-dir@ service is managed by systemd-logind we should not touch it or else we break the users' sessions.
    systemd.services."user-runtime-dir@".stopIfChanged = false;
    systemd.services."user-runtime-dir@".restartIfChanged = false;
  };
}