summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/web-apps/chhoto-url.nix
blob: 54cb6bd81e0087658dba92e14a07c6655af74fa6 (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
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.services.chhoto-url;

  environment = lib.mapAttrs (
    _: value:
    if value == true then
      "True"
    else if value == false then
      "False"
    else
      toString value
  ) cfg.settings;
in

{
  meta.maintainers = with lib.maintainers; [ defelo ];

  options.services.chhoto-url = {
    enable = lib.mkEnableOption "Chhoto URL";

    package = lib.mkPackageOption pkgs "chhoto-url" { };

    settings = lib.mkOption {
      description = ''
        Configuration of Chhoto URL.
        See <https://github.com/SinTan1729/chhoto-url/blob/main/docs/INSTALLATION.md#configuration-options> for a list of options.
      '';
      example = {
        port = 4567;
      };

      type = lib.types.submodule {
        freeformType =
          with lib.types;
          attrsOf (oneOf [
            str
            int
            bool
          ]);

        options = {
          db_url = lib.mkOption {
            type = lib.types.path;
            description = "The path of the sqlite database.";
            default = "/var/lib/chhoto-url/urls.sqlite";
          };

          port = lib.mkOption {
            type = lib.types.port;
            description = "The port to listen on.";
            example = 4567;
          };

          cache_control_header = lib.mkOption {
            type = lib.types.nullOr lib.types.str;
            description = "The Cache-Control header to send.";
            default = null;
            example = "no-cache, private";
          };

          disable_frontend = lib.mkOption {
            type = lib.types.bool;
            description = "Whether to disable the frontend.";
            default = false;
          };

          public_mode = lib.mkOption {
            type = lib.types.bool;
            description = "Whether to enable public mode.";
            default = false;
            apply = x: if x then "Enable" else "Disable";
          };

          public_mode_expiry_delay = lib.mkOption {
            type = lib.types.nullOr lib.types.ints.unsigned;
            description = "The maximum expiry delay in seconds to force in public mode.";
            default = null;
            example = 3600;
          };

          redirect_method = lib.mkOption {
            type = lib.types.enum [
              "TEMPORARY"
              "PERMANENT"
            ];
            description = "The redirect method to use.";
            default = "PERMANENT";
          };

          hash_algorithm = lib.mkOption {
            type = lib.types.nullOr (lib.types.enum [ "Argon2" ]);
            description = ''
              The hash algorithm to use for passwords and API keys.
              Set to `null` if you want to provide these secrets as plaintext.
            '';
            default = null;
          };

          site_url = lib.mkOption {
            type = lib.types.nullOr lib.types.str;
            description = "The URL under which Chhoto URL is externally reachable.";
            default = null;
          };

          slug_style = lib.mkOption {
            type = lib.types.enum [
              "Pair"
              "UID"
            ];
            description = "The slug style to use for auto-generated URLs.";
            default = "Pair";
          };

          slug_length = lib.mkOption {
            type = lib.types.addCheck lib.types.int (x: x >= 4);
            description = "The length of auto-generated slugs.";
            default = 8;
          };

          try_longer_slugs = lib.mkOption {
            type = lib.types.bool;
            description = "Whether to try a longer UID upon collision.";
            default = false;
          };

          allow_capital_letters = lib.mkOption {
            type = lib.types.bool;
            description = "Whether to allow capital letters in slugs.";
            default = false;
          };

          custom_landing_directory = lib.mkOption {
            type = lib.types.nullOr lib.types.path;
            description = "The path of a directory which contains a custom landing page.";
            default = null;
          };
        };
      };
    };

    environmentFiles = lib.mkOption {
      type = lib.types.listOf lib.types.path;
      default = [ ];
      example = [ "/run/secrets/chhoto-url.env" ];
      description = ''
        Files to load environment variables from in addition to [](#opt-services.chhoto-url.settings).
        This is useful to avoid putting secrets into the nix store.
        See <https://github.com/SinTan1729/chhoto-url/blob/main/docs/INSTALLATION.md#configuration-options> for a list of options.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.chhoto-url = {
      wantedBy = [ "multi-user.target" ];

      inherit environment;

      serviceConfig = {
        User = "chhoto-url";
        Group = "chhoto-url";
        DynamicUser = true;
        StateDirectory = "chhoto-url";
        EnvironmentFile = cfg.environmentFiles;

        ExecStart = lib.getExe cfg.package;

        # hardening
        AmbientCapabilities = "";
        CapabilityBoundingSet = [ "" ];
        DevicePolicy = "closed";
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateTmp = true;
        PrivateUsers = true;
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = "strict";
        RemoveIPC = true;
        RestrictAddressFamilies = [ "AF_INET AF_INET6" ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SocketBindAllow = "tcp:${toString cfg.settings.port}";
        SocketBindDeny = "any";
        SystemCallArchitectures = "native";
        SystemCallFilter = [
          "@system-service"
          "~@privileged"
          "~@resources"
        ];
        UMask = "0077";
      };
    };
  };
}