summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/security/tinyauth.nix
blob: 17b9826da813151579185ad0bf21df9fda5bbe24 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
{
  lib,
  pkgs,
  config,
  ...
}:

let
  inherit (lib)
    getExe
    maintainers
    mapAttrs'
    mkEnableOption
    mkIf
    mkOption
    mkPackageOption
    nameValuePair
    optionalAttrs
    types
    ;

  cfg = config.services.tinyauth;

  format = pkgs.formats.keyValue { };
  settingsFile = format.generate "tinyauth-env-vars" (
    mapAttrs' (name: value: nameValuePair "TINYAUTH_${name}" value) cfg.settings
  );
in
{
  options.services.tinyauth = {
    enable = mkEnableOption "Tinyauth server";

    package = mkPackageOption pkgs "tinyauth" { };

    environmentFile = mkOption {
      type = types.path;
      description = ''
        Path to an environment file loaded for Tinyauth.

        This can be used to securely store tokens and secrets outside of the world-readable Nix store.

        Example contents of the file:
        ```
        TINYAUTH_AUTH_USERS=user-hash
        TINYAUTH_OAUTH_PROVIDERS_GOOGLE_CLIENTSECRET=client-secret
        ```
      '';
      default = "/dev/null";
      example = "/var/lib/secrets/tinyauth";
    };

    settings = mkOption {
      type = types.submodule {
        freeformType = format.type;

        options = {
          SERVER_ADDRESS = mkOption {
            type = types.str;
            description = ''
              Address to bind the server to.
            '';
            default = "0.0.0.0";
          };

          SERVER_PORT = mkOption {
            type = types.port;
            description = ''
              The port to run the server on.
            '';
            default = 3000;
          };

          APPURL = mkOption {
            type = types.str;
            description = ''
              URL of the app.
            '';
            example = "https://auth.example.com";
          };

          ANALYTICS_ENABLED = mkOption {
            type = types.bool;
            description = ''
              Whether to enable anonymous version collection.
            '';
            default = false;
          };

          RESOURCES_ENABLED = mkOption {
            type = types.bool;
            description = ''
              Whether to enable the resources server.
            '';
            default = true;
          };

          AUTH_LOGINMAXRETRIES = mkOption {
            type = types.ints.unsigned;
            description = ''
              Maximum login attempts before timeout (0 to disable).
            '';
            default = 3;
          };

          AUTH_LOGINTIMEOUT = mkOption {
            type = types.ints.unsigned;
            description = ''
              Login timeout in seconds after max retries reached (0 to disable).
            '';
            default = 300;
          };

          AUTH_TRUSTEDPROXIES = mkOption {
            type = types.str;
            description = ''
              Comma-separated list of trusted proxy addresses.
            '';
            default = "";
          };
        };
      };

      default = { };

      description = ''
        Environment variables that will be passed to Tinyauth.
        The "TINYAUTH_" prefix will be prepended to the setting names.
        See [configuration options](https://tinyauth.app/docs/reference/configuration)
        for supported values.
      '';
    };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/tinyauth";
      description = ''
        The directory where Tinyauth will store its data.
      '';
    };

    user = mkOption {
      type = types.str;
      default = "tinyauth";
      description = "User account under which Tinyauth runs.";
    };

    group = mkOption {
      type = types.str;
      default = "tinyauth";
      description = "Group account under which Tinyauth runs.";
    };

    enableUnixSocket = mkEnableOption (
      "a UNIX domain socket at `/run/tinyauth/tinyauth.sock`"
      + " instead of listening on an IP address and port."
    );
  };

  config = mkIf cfg.enable {
    systemd.tmpfiles.settings.tinyauth = {
      "${cfg.dataDir}".d = {
        mode = "0750";
        user = cfg.user;
        group = cfg.group;
      };
    };

    systemd.services.tinyauth = {
      description = "Tinyauth";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      restartTriggers = [
        cfg.package
        cfg.environmentFile
        settingsFile
      ];

      environment = {
        GIN_MODE = "release";
        TINYAUTH_DATABASE_PATH = "${cfg.dataDir}/tinyauth.db";
        TINYAUTH_RESOURCES_PATH = "${cfg.dataDir}/resources";
        TINYAUTH_SERVER_SOCKETPATH = mkIf cfg.enableUnixSocket "%t/tinyauth/tinyauth.sock";
      };

      serviceConfig = {
        Type = "simple";
        User = cfg.user;
        Group = cfg.group;
        WorkingDirectory = cfg.dataDir;
        RuntimeDirectory = mkIf cfg.enableUnixSocket "tinyauth";
        RuntimeDirectoryMode = mkIf cfg.enableUnixSocket "750";
        ExecStart = getExe cfg.package;
        Restart = "always";

        EnvironmentFile = [
          cfg.environmentFile
          settingsFile
        ];

        # Hardening
        AmbientCapabilities = "";
        CapabilityBoundingSet = "";
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateTmp = "disconnected";
        PrivateUsers = true;
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = "strict";
        ReadWritePaths = [ cfg.dataDir ];
        RemoveIPC = true;
        RestrictAddressFamilies = [
          "AF_UNIX"
          "AF_INET"
          "AF_INET6"
        ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [
          "@system-service"
          "~@privileged"
          "~@resources"
        ];
        UMask = if cfg.enableUnixSocket then "0007" else "0077";
      };
    };

    users.users = optionalAttrs (cfg.user == "tinyauth") {
      tinyauth = {
        isSystemUser = true;
        group = cfg.group;
        description = "Tinyauth user";
        home = cfg.dataDir;
      };
    };

    users.groups = optionalAttrs (cfg.group == "tinyauth") {
      tinyauth = { };
    };
  };

  meta.maintainers = with maintainers; [ shaunren ];
}