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 lockOn = {
56
57 suspend = mkOption {
58 type = types.bool;
59 default = true;
60 description = ''
61 Whether to lock screen with physlock just before suspend.
62 '';
63 };
64
65 hibernate = mkOption {
66 type = types.bool;
67 default = true;
68 description = ''
69 Whether to lock screen with physlock just before hibernate.
70 '';
71 };
72
73 extraTargets = mkOption {
74 type = types.listOf types.str;
75 default = [];
76 example = [ "display-manager.service" ];
77 description = ''
78 Other targets to lock the screen just before.
79
80 Useful if you want to e.g. both autologin to X11 so that
81 your <filename>~/.xsession</filename> gets executed and
82 still to have the screen locked so that the system can be
83 booted relatively unattended.
84 '';
85 };
86
87 };
88
89 };
90
91 };
92
93
94 ###### implementation
95
96 config = mkIf cfg.enable (mkMerge [
97 {
98
99 # for physlock -l and physlock -L
100 environment.systemPackages = [ pkgs.physlock ];
101
102 systemd.services."physlock" = {
103 enable = true;
104 description = "Physlock";
105 wantedBy = optional cfg.lockOn.suspend "suspend.target"
106 ++ optional cfg.lockOn.hibernate "hibernate.target"
107 ++ cfg.lockOn.extraTargets;
108 before = optional cfg.lockOn.suspend "systemd-suspend.service"
109 ++ optional cfg.lockOn.hibernate "systemd-hibernate.service"
110 ++ cfg.lockOn.extraTargets;
111 serviceConfig = {
112 Type = "forking";
113 ExecStart = "${pkgs.physlock}/bin/physlock -d${optionalString cfg.disableSysRq "s"}";
114 };
115 };
116
117 security.pam.services.physlock = {};
118
119 }
120
121 (mkIf cfg.allowAnyUser {
122
123 security.wrappers.physlock = { source = "${pkgs.physlock}/bin/physlock"; user = "root"; };
124
125 })
126 ]);
127
128}