summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/matrix/lk-jwt-service.nix
blob: 6adc0465e9dfb15fd8a81384baa1199f23f77f89 (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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.lk-jwt-service;
in
{
  meta.maintainers = [ lib.maintainers.quadradical ];
  options.services.lk-jwt-service = {
    enable = lib.mkEnableOption "lk-jwt-service";
    package = lib.mkPackageOption pkgs "lk-jwt-service" { };

    livekitUrl = lib.mkOption {
      type = lib.types.strMatching "^wss?://.*";
      example = "wss://example.com/livekit/sfu";
      description = ''
        The public websocket URL for livekit.
        The proto needs to be either  `wss://` (recommended) or `ws://` (insecure).
      '';
    };

    keyFile = lib.mkOption {
      type = lib.types.path;
      description = ''
        Path to a file containing the credential mapping (`<keyname>: <secret>`) to access LiveKit.

        Example:
        `lk-jwt-service: f6lQGaHtM5HfgZjIcec3cOCRfiDqIine4CpZZnqdT5cE`

        For more information, see <https://github.com/element-hq/lk-jwt-service#configuration>.
      '';
    };

    port = lib.mkOption {
      type = lib.types.port;
      default = 8080;
      description = "Port that lk-jwt-service should listen on.";
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.lk-jwt-service = {
      description = "Minimal service to issue LiveKit JWTs for MatrixRTC";
      documentation = [ "https://github.com/element-hq/lk-jwt-service" ];
      wantedBy = [ "multi-user.target" ];
      wants = [ "network-online.target" ];
      after = [ "network-online.target" ];
      environment = {
        LIVEKIT_URL = cfg.livekitUrl;
        LIVEKIT_JWT_BIND = ":${toString cfg.port}";
        LIVEKIT_KEY_FILE = "/run/credentials/lk-jwt-service.service/livekit-secrets";
      };

      serviceConfig = {
        LoadCredential = [ "livekit-secrets:${cfg.keyFile}" ];
        ExecStart = lib.getExe cfg.package;
        DynamicUser = true;
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        PrivateDevices = true;
        PrivateMounts = true;
        PrivateUsers = true;
        RestrictAddressFamilies = [
          "AF_INET"
          "AF_INET6"
        ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        ProtectHome = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [
          "@system-service"
          "~@privileged"
          "~@resources"
        ];
        Restart = "on-failure";
        RestartSec = 5;
        UMask = "077";
      };
    };
  };
}