summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/wayland/cage.nix
blob: a18044fb6fd48c3edf77a16d0bc902f1acc7f508 (plain)
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
{
  config,
  pkgs,
  lib,
  utils,
  ...
}:

with lib;

let
  cfg = config.services.cage;
in
{
  options.services.cage = {
    enable = mkEnableOption "cage kiosk service";

    restartIfChanged = mkOption {
      type = types.bool;
      default = false;
      example = true;
      description = "Whether to restart the 'cage-tty1' service when its configuration changes.";
    };

    user = mkOption {
      type = types.str;
      default = "demo";
      description = ''
        User to log-in as.
      '';
    };

    extraArguments = mkOption {
      type = types.listOf types.str;
      default = [ ];
      defaultText = literalExpression "[]";
      description = "Additional command line arguments to pass to Cage.";
      example = [ "-d" ];
    };

    environment = mkOption {
      type = types.attrsOf types.str;
      default = { };
      example = {
        WLR_LIBINPUT_NO_DEVICES = "1";
      };
      description = "Additional environment variables to pass to Cage.";
    };

    program = mkOption {
      type = types.path;
      default = "${pkgs.xterm}/bin/xterm";
      defaultText = literalExpression ''"''${pkgs.xterm}/bin/xterm"'';
      description = ''
        Program to run in cage.
      '';
    };

    package = mkPackageOption pkgs "cage" { };
  };

  config = mkIf cfg.enable {

    # The service is partially based off of the one provided in the
    # cage wiki at
    # https://github.com/Hjdskes/cage/wiki/Starting-Cage-on-boot-with-systemd.
    systemd.services."cage-tty1" = {
      enable = true;
      after = [
        "systemd-user-sessions.service"
        "plymouth-start.service"
        "plymouth-quit.service"
        "systemd-logind.service"
        "getty@tty1.service"
      ];
      before = [ "graphical.target" ];
      wants = [
        "dbus.socket"
        "systemd-logind.service"
        "plymouth-quit.service"
      ];
      wantedBy = [ "graphical.target" ];
      conflicts = [ "getty@tty1.service" ];

      restartIfChanged = cfg.restartIfChanged;
      unitConfig.ConditionPathExists = "/dev/tty1";
      serviceConfig = {
        ExecStart = ''
          ${cfg.package}/bin/cage \
            ${escapeShellArgs cfg.extraArguments} \
            -- ${cfg.program}
        '';
        User = cfg.user;

        IgnoreSIGPIPE = "no";

        # Log this user with utmp, letting it show up with commands 'w' and
        # 'who'. This is needed since we replace (a)getty.
        UtmpIdentifier = "%n";
        UtmpMode = "user";
        # A virtual terminal is needed.
        TTYPath = "/dev/tty1";
        TTYReset = "yes";
        TTYVHangup = "yes";
        TTYVTDisallocate = "yes";
        # Fail to start if not controlling the virtual terminal.
        StandardInput = "tty-fail";
        StandardOutput = "journal";
        StandardError = "journal";
        # Set up a full (custom) user session for the user, required by Cage.
        PAMName = "cage";
      };
      environment = cfg.environment;
    };

    security.polkit.enable = true;

    security.pam.services.cage = {
      useDefaultRules = false;
      rules = {
        auth = utils.pam.autoOrderRules [
          {
            name = "unix";
            control = "required";
            modulePath = config.security.pam.pam_unixModulePath;
            settings.nullok = true;
          }
        ];
        account = utils.pam.autoOrderRules [
          {
            name = "unix";
            control = "required";
            modulePath = config.security.pam.pam_unixModulePath;
          }
        ];
        session = utils.pam.autoOrderRules [
          {
            name = "unix";
            control = "required";
            modulePath = config.security.pam.pam_unixModulePath;
          }
          {
            name = "env";
            control = "required";
            modulePath = "${config.security.pam.package}/lib/security/pam_env.so";
            settings.conffile = "/etc/pam/environment";
            settings.readenv = 0;
          }
          {
            name = "systemd";
            control = "required";
            modulePath = "${config.systemd.package}/lib/security/pam_systemd.so";
          }
        ];
      };
    };

    hardware.graphics.enable = mkDefault true;

    systemd.targets.graphical.wants = [ "cage-tty1.service" ];

    systemd.defaultUnit = "graphical.target";
  };

  meta.maintainers = [ ];

}