summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/matrix/rust-federation-tester.nix
blob: 4b7a3420d2e08812dbe7d7d45c925f047a33adfe (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
{
  config,
  lib,
  pkgs,
  utils,
  ...
}:

let
  cfg = config.services.rust-federation-tester;

  configFile = "/run/rust-federation-tester/config.yaml";
  commonServiceConfig = {
    DynamicUser = true;
    RuntimeDirectory = "rust-federation-tester";
    RuntimeDirectoryPreserve = true;
    StateDirectory = "rust-federation-tester";
    User = "rust-federation-tester";
    WorkingDirectory = "%t/rust-federation-tester";

    # Hardening
    UMask = "0077";
    CapabilityBoundingSet = [ "" ];
    DevicePolicy = "closed";
    PrivateTmp = true;
    ProtectHome = true;
    ProtectSystem = "strict";
    MemoryDenyWriteExecute = true;
    NoNewPrivileges = true;
    LockPersonality = true;
    PrivateDevices = true;
    PrivateMounts = true;
    ProcSubset = "pid";
    ProtectClock = true;
    ProtectControlGroups = true;
    ProtectHostname = true;
    ProtectKernelLogs = true;
    ProtectKernelModules = true;
    ProtectKernelTunables = true;
    ProtectProc = "invisible";
    RemoveIPC = true;
    RestrictAddressFamilies = [
      "AF_INET"
      "AF_INET6"
      "AF_UNIX"
    ];
    RestrictNamespaces = true;
    RestrictRealtime = true;
    RestrictSUIDSGID = true;
    SystemCallArchitectures = "native";
    SystemCallFilter = [
      "@system-service"
    ];
  };

  secretsInjection = utils.genJqSecretsReplacement {
    loadCredential = true;
  } cfg.settings configFile;
in
{
  options.services.rust-federation-tester = {
    enable = lib.mkEnableOption "rust-federation-tester";

    settings = lib.mkOption {
      type = lib.types.submodule {
        freeformType = lib.types.json;
        options = {
          frontend_url = lib.mkOption {
            type = lib.types.str;
            example = "federation-tester.example.org";
            description = ''
              URL of the service's frontend.
            '';
          };

          database_url = lib.mkOption {
            type = lib.types.str;
            default = "sqlite:///var/lib/rust-federation-tester/db?mode=rwc";
            example = "postgres://localhost/db?currentSchema=schema";
            description = ''
              The database to store accounts and statistics.
            '';
          };

          smtp = {
            enabled = lib.mkEnableOption "mail delivery for configured alerts";
          };

          listen_addr = lib.mkOption {
            type = lib.types.str;
            default = "[::]:8080";
            example = "unix:/run/rust-federation-tester/rust-federation-tester.sock";
            description = ''
              Address the API server should listen on.
            '';
          };
        };
      };

      description = ''
        Settings representing the values in {file}`config.yaml` of the service.

        Refer to [`config.yaml.example`] for supported values.

        [`config.yaml.example`]: https://github.com/MTRNord/rust-federation-tester/blob/main/config.yaml.example
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.sockets.rust-federation-tester = {
      description = "Matrix-Federation-Tester in Rust socket";
      wantedBy = [ "sockets.target" ];
      listenStreams = [ (lib.removePrefix "unix:" cfg.settings.listen_addr) ];
    };

    systemd.services.rust-federation-tester-setup = {
      description = "Matrix-Federation-Tester in Rust";
      path = [ pkgs.rust-federation-tester ];

      serviceConfig = lib.mkMerge [
        commonServiceConfig
        {
          Type = "oneshot";
          RemainAfterExit = true;
          LoadCredential = secretsInjection.credentials;
          ExecStart = "${pkgs.writeShellScript "rust-federation-tester-setup" ''
            ${secretsInjection.script}

            migration up
          ''}";
        }
      ];
    };

    systemd.services.rust-federation-tester = {
      description = "Matrix-Federation-Tester in Rust";
      wantedBy = [ "multi-user.target" ];
      documentation = [ "https://github.com/MTRNord/rust-federation-tester" ];
      after = [ "rust-federation-tester-setup.service" ];
      requires = [ "rust-federation-tester-setup.service" ];

      serviceConfig = lib.mkMerge [
        commonServiceConfig
        {
          ExecSearchPath = lib.makeBinPath [ pkgs.rust-federation-tester ];
          ExecStart = "rust-federation-tester";
          Restart = "on-failure";
        }
      ];
    };
  };
}