summary refs log tree commit diff
path: root/nixos/modules/services/security/physlock.nix
blob: 690eb70079d860e189bfaedb187a3a1982e1f72f (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.physlock;
in

{

  ###### interface

  options = {

    services.physlock = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to enable the <command>physlock</command> screen locking mechanism.

          Enable this and then run <command>systemctl start physlock</command>
          to securely lock the screen.

          This will switch to a new virtual terminal, turn off console
          switching and disable SysRq mechanism (when
          <option>services.physlock.disableSysRq</option> is set)
          until the root or user password is given.
        '';
      };

      allowAnyUser = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to allow any user to lock the screen. This will install a
          setuid wrapper to allow any user to start physlock as root, which
          is a minor security risk. Call the physlock binary to use this instead
          of using the systemd service.

          Note that you might need to relog to have the correct binary in your
          PATH upon changing this option.
        '';
      };

      disableSysRq = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Whether to disable SysRq when locked with physlock.
        '';
      };

      lockOn = {

        suspend = mkOption {
          type = types.bool;
          default = true;
          description = ''
            Whether to lock screen with physlock just before suspend.
          '';
        };

        hibernate = mkOption {
          type = types.bool;
          default = true;
          description = ''
            Whether to lock screen with physlock just before hibernate.
          '';
        };

        extraTargets = mkOption {
          type = types.listOf types.str;
          default = [];
          example = [ "display-manager.service" ];
          description = ''
            Other targets to lock the screen just before.

            Useful if you want to e.g. both autologin to X11 so that
            your <filename>~/.xsession</filename> gets executed and
            still to have the screen locked so that the system can be
            booted relatively unattended.
          '';
        };

      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable (mkMerge [
    {

      # for physlock -l and physlock -L
      environment.systemPackages = [ pkgs.physlock ];

      systemd.services.physlock = {
        enable = true;
        description = "Physlock";
        wantedBy = optional cfg.lockOn.suspend   "suspend.target"
                ++ optional cfg.lockOn.hibernate "hibernate.target"
                ++ cfg.lockOn.extraTargets;
        before   = optional cfg.lockOn.suspend   "systemd-suspend.service"
                ++ optional cfg.lockOn.hibernate "systemd-hibernate.service"
                ++ optional (cfg.lockOn.hibernate || cfg.lockOn.suspend) "systemd-suspend-then-hibernate.service"
                ++ cfg.lockOn.extraTargets;
        serviceConfig = {
          Type = "forking";
          ExecStart = "${pkgs.physlock}/bin/physlock -d${optionalString cfg.disableSysRq "s"}";
        };
      };

      security.pam.services.physlock = {};

    }

    (mkIf cfg.allowAnyUser {

      security.wrappers.physlock = { source = "${pkgs.physlock}/bin/physlock"; user = "root"; };

    })
  ]);

}