about summary refs log tree commit diff
path: root/nixos/modules/services/misc/db-rest.nix
blob: fbf8b327af049a17cf33ec3675c31ba65a7024f9 (plain) (blame)
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
{ config, pkgs, lib, ... }:
let
  inherit (lib) mkOption types mkIf mkMerge mkDefault mkEnableOption mkPackageOption maintainers;
  cfg = config.services.db-rest;
in
{
  options = {
    services.db-rest = {
      enable = mkEnableOption "db-rest service";

      user = mkOption {
        type = types.str;
        default = "db-rest";
        description = "User account under which db-rest runs.";
      };

      group = mkOption {
        type = types.str;
        default = "db-rest";
        description = "Group under which db-rest runs.";
      };

      host = mkOption {
        type = types.str;
        default = "127.0.0.1";
        description = "The host address the db-rest server should listen on.";
      };

      port = mkOption {
        type = types.port;
        default = 3000;
        description = "The port the db-rest server should listen on.";
      };

      redis = {
        enable = mkOption {
          type = types.bool;
          default = false;
          description = "Enable caching with redis for db-rest.";
        };

        createLocally = mkOption {
          type = types.bool;
          default = true;
          description = "Configure a local redis server for db-rest.";
        };

        host = mkOption {
          type = with types; nullOr str;
          default = null;
          description = "Redis host.";
        };

        port = mkOption {
          type = with types; nullOr port;
          default = null;
          description = "Redis port.";
        };

        user = mkOption {
          type = with types; nullOr str;
          default = null;
          description = "Optional username used for authentication with redis.";
        };

        passwordFile = mkOption {
          type = with types; nullOr path;
          default = null;
          example = "/run/keys/db-rest/pasword-redis-db";
          description = "Path to a file containing the redis password.";
        };

        useSSL = mkOption {
          type = types.bool;
          default = true;
          description = "Use SSL if using a redis network connection.";
        };
      };

      package = mkPackageOption pkgs "db-rest" { };
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = (cfg.redis.enable && !cfg.redis.createLocally) -> (cfg.redis.host != null && cfg.redis.port != null);
        message = ''
          {option}`services.db-rest.redis.createLocally` and redis network connection ({option}`services.db-rest.redis.host` or {option}`services.db-rest.redis.port`) enabled. Disable either of them.
        '';
      }
      {
        assertion = (cfg.redis.enable && !cfg.redis.createLocally) -> (cfg.redis.passwordFile != null);
        message = ''
          {option}`services.db-rest.redis.createLocally` is disabled, but {option}`services.db-rest.redis.passwordFile` is not set.
        '';
      }
    ];

    systemd.services.db-rest = mkMerge [
      {
        description = "db-rest service";
        after = [ "network.target" ]
          ++ lib.optional cfg.redis.createLocally "redis-db-rest.service";
        requires = lib.optional cfg.redis.createLocally "redis-db-rest.service";
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          Type = "simple";
          Restart = "always";
          RestartSec = 5;
          WorkingDirectory = cfg.package;
          User = cfg.user;
          Group = cfg.group;
          RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
          MemoryDenyWriteExecute = false;
          LoadCredential = lib.optional (cfg.redis.enable && cfg.redis.passwordFile != null) "REDIS_PASSWORD:${cfg.redis.passwordFile}";
          ExecStart = mkDefault "${cfg.package}/bin/db-rest";

          RemoveIPC = true;
          NoNewPrivileges = true;
          PrivateDevices = true;
          ProtectClock = true;
          ProtectKernelLogs = true;
          ProtectControlGroups = true;
          ProtectKernelModules = true;
          PrivateMounts = true;
          SystemCallArchitectures = "native";
          ProtectHostname = true;
          LockPersonality = true;
          ProtectKernelTunables = true;
          RestrictRealtime = true;
          RestrictSUIDSGID = true;
          RestrictNamespaces = true;
          ProtectSystem = "strict";
          ProtectProc = "invisible";
          ProcSubset = "pid";
          ProtectHome = true;
          PrivateUsers = true;
          PrivateTmp = true;
          CapabilityBoundingSet = "";
        };
        environment = {
          NODE_ENV = "production";
          NODE_EXTRA_CA_CERTS = "/etc/ssl/certs/ca-certificates.crt";
          HOSTNAME = cfg.host;
          PORT = toString cfg.port;
        };
      }
      (mkIf cfg.redis.enable (if cfg.redis.createLocally then
        { environment.REDIS_URL = config.services.redis.servers.db-rest.unixSocket; }
      else
        {
          script =
            let
              username = lib.optionalString (cfg.redis.user != null) (cfg.redis.user);
              host = cfg.redis.host;
              port = toString cfg.redis.port;
              protocol = if cfg.redis.useSSL then "rediss" else "redis";
            in
            ''
              export REDIS_URL="${protocol}://${username}:$(${config.systemd.package}/bin/systemd-creds cat REDIS_PASSWORD)@${host}:${port}"
              exec ${cfg.package}/bin/db-rest
            '';
        }))
    ];

    users.users = lib.mkMerge [
      (lib.mkIf (cfg.user == "db-rest") {
        db-rest = {
          isSystemUser = true;
          group = cfg.group;
        };
      })
      (lib.mkIf cfg.redis.createLocally { ${cfg.user}.extraGroups = [ "redis-db-rest" ]; })
    ];

    users.groups = lib.mkIf (cfg.group == "db-rest") { db-rest = { }; };

    services.redis.servers.db-rest.enable = cfg.redis.enable && cfg.redis.createLocally;
  };
  meta.maintainers = with maintainers; [ marie ];
}