summaryrefslogtreecommitdiffstats
path: root/lass/3modules/xjail.nix
blob: af851760b5b4c1882ceb86b961f846409f1a1c65 (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
{ config, pkgs, ... }:

with import <stockholm/lib>;
{
  options.lass.xjail = mkOption {
    type = types.attrsOf (types.submodule ({ config, ...}: {
      options = {
        user = mkOption {
          type = types.string;
          default = "nobody";
        };
        groups = mkOption {
          type = types.listOf types.str;
          default = [];
        };
        name = mkOption {
          type = types.string;
          default = config._module.args.name;
        };
        display = mkOption {
          type = types.string;
          default = toString (genid_signed config._module.args.name);
        };
        script = mkOption {
          type = types.path;
          default = pkgs.writeScript "echo_lol" "echo lol";
        };
        from = mkOption {
          type = types.string;
          default = "lass";
        };
      };
    }));
    default = {};
  };

  options.lass.xjail-bins = mkOption {
    type = types.attrsOf types.path;
  };

  # implementation
  config = {

    users.users = mapAttrs' (_: cfg:
      nameValuePair cfg.name {
        uid = genid cfg.name;
        home = "/home/${cfg.name}";
        useDefaultShell = true;
        createHome = true;
        extraGroups = cfg.groups;
      }
    ) config.lass.xjail;

    users.groups = mapAttrs' (_: cfg:
      nameValuePair cfg.name {
        members = [
          cfg.name
          cfg.from
        ];
      }
    ) config.lass.xjail;

    security.sudo.extraConfig = (concatStringsSep "\n" (mapAttrsToList (_: cfg:
      # TODO allow just the right script with sudo
      "${cfg.from} ALL=(${cfg.name}) NOPASSWD: ALL"
    ) config.lass.xjail));

    lass.xjail-bins = mapAttrs' (name: cfg:
      let
        sudo-wrapper = pkgs.writeScript name ''
          /var/run/wrappers/bin/sudo -u ${cfg.name} -i ${cfg.script} "$@"
        '';
      in nameValuePair name (pkgs.writeScriptBin cfg.name ''
        export NDISPLAY=${cfg.display}
        DISPLAY=:$NDISPLAY ${pkgs.xorg.xrandr}/bin/xrandr
        if test $? -eq 0; then
          echo xephyr already running
          export DISPLAY=:$NDISPLAY
          ${sudo-wrapper} "$@"
        else
          echo xephyr not running
          DROP_TO_USER=${cfg.name} ${pkgs.xephyrify}/bin/xephyrify ${sudo-wrapper} "$@"
        fi
      '')
    ) config.lass.xjail;
  };
}