1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.physlock;
7in
8
9{
10
11 ###### interface
12
13 options = {
14
15 services.physlock = {
16
17 enable = mkOption {
18 type = types.bool;
19 default = false;
20 description = ''
21 Whether to enable the <command>physlock</command> screen locking mechanism.
22
23 Enable this and then run <command>systemctl start physlock</command>
24 to securely lock the screen.
25
26 This will switch to a new virtual terminal, turn off console
27 switching and disable SysRq mechanism (when
28 <option>services.physlock.disableSysRq</option> is set)
29 until the root or user password is given.
30 '';
31 };
32
33 allowAnyUser = mkOption {
34 type = types.bool;
35 default = false;
36 description = ''
37 Whether to allow any user to lock the screen. This will install a
38 setuid wrapper to allow any user to start physlock as root, which
39 is a minor security risk. Call the physlock binary to use this instead
40 of using the systemd service.
41
42 Note that you might need to relog to have the correct binary in your
43 PATH upon changing this option.
44 '';
45 };
46
47 disableSysRq = mkOption {
48 type = types.bool;
49 default = true;
50 description = ''
51 Whether to disable SysRq when locked with physlock.
52 '';
53 };
54
55 lockMessage = mkOption {
56 type = types.str;
57 default = "";
58 description = ''
59 Message to show on physlock login terminal.
60 '';
61 };
62
63 lockOn = {
64
65 suspend = mkOption {
66 type = types.bool;
67 default = true;
68 description = ''
69 Whether to lock screen with physlock just before suspend.
70 '';
71 };
72
73 hibernate = mkOption {
74 type = types.bool;
75 default = true;
76 description = ''
77 Whether to lock screen with physlock just before hibernate.
78 '';
79 };
80
81 extraTargets = mkOption {
82 type = types.listOf types.str;
83 default = [];
84 example = [ "display-manager.service" ];
85 description = ''
86 Other targets to lock the screen just before.
87
88 Useful if you want to e.g. both autologin to X11 so that
89 your <filename>~/.xsession</filename> gets executed and
90 still to have the screen locked so that the system can be
91 booted relatively unattended.
92 '';
93 };
94
95 };
96
97 };
98
99 };
100
101
102 ###### implementation
103
104 config = mkIf cfg.enable (mkMerge [
105 {
106
107 # for physlock -l and physlock -L
108 environment.systemPackages = [ pkgs.physlock ];
109
110 systemd.services.physlock = {
111 enable = true;
112 description = "Physlock";
113 wantedBy = optional cfg.lockOn.suspend "suspend.target"
114 ++ optional cfg.lockOn.hibernate "hibernate.target"
115 ++ cfg.lockOn.extraTargets;
116 before = optional cfg.lockOn.suspend "systemd-suspend.service"
117 ++ optional cfg.lockOn.hibernate "systemd-hibernate.service"
118 ++ optional (cfg.lockOn.hibernate || cfg.lockOn.suspend) "systemd-suspend-then-hibernate.service"
119 ++ cfg.lockOn.extraTargets;
120 serviceConfig = {
121 Type = "forking";
122 ExecStart = "${pkgs.physlock}/bin/physlock -d${optionalString cfg.disableSysRq "s"}${optionalString (cfg.lockMessage != "") " -p \"${cfg.lockMessage}\""}";
123 };
124 };
125
126 security.pam.services.physlock = {};
127
128 }
129
130 (mkIf cfg.allowAnyUser {
131
132 security.wrappers.physlock = { source = "${pkgs.physlock}/bin/physlock"; user = "root"; };
133
134 })
135 ]);
136
137}