summary refs log tree commit diff stats
path: root/module/default.nix
blob: 4dcb17ee8dfe4317e6a96f6b60e0af2dbf7c0530 (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
{
  rocie-mobile,
  rocie-server,
}: {
  lib,
  config,
  ...
}: let
  cfg = config.rocie;
in {
  options.rocie = {
    enable = lib.mkEnableOption "rocie";
    domain = lib.mkOption {
      description = "Under which domain (vhost) to deploy rocie?";
      type = lib.types.str;
    };

    port = lib.mkOption {
      description = "Which port to use";
      type = lib.types.port;
      default = 8543;
    };
    address = lib.mkOption {
      description = "Which address to use";
      type = lib.types.str;
      default = "127.0.0.1";
    };

    dbPath = lib.mkOption {
      description = "Where to store the database";
      type = lib.types.str;
      default = "$STATE_DIRECTORY/storage.db";
    };

    secretKeyFile = lib.mkOption {
      description = "Which file to use as signing key for user log-ins";
      type = lib.types.nullOr lib.types.str;
    };

    webFrontEnd = lib.mkOption {
      description = "Which package to use for the web front end";
      type = lib.types.package;
      default = rocie-mobile;
    };
    server = lib.mkOption {
      description = "Which package to use for the server";
      type = lib.types.package;
      default = rocie-server;
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.packages = [cfg.server];
    systemd.services.rocie = {
      wantedBy = ["default.target"];
      serviceConfig =
        {
          StateDirectory = "rocie";

          User = "rocie";
          Group = "rocie";

          ReadOnlyPaths = [
            cfg.secretKeyFile
          ];

          # Hardening
          LockPersonality = true;
          MemoryDenyWriteExecute = true;
          NoNewPrivileges = true;
          PrivateDevices = true;
          PrivateIPC = true;
          PrivateTmp = true;
          ProcSubset = "pid";
          ProtectClock = true;
          ProtectControlGroups = true;
          ProtectHome = true;
          ProtectHostname = true;
          ProtectKernelLogs = true;
          ProtectKernelModules = true;
          ProtectKernelTunables = true;
          ProtectProc = "invisible";
          ProtectSystem = "full";
          RemoveIPC = true;
          RestrictAddressFamilies = [
            "AF_INET"
            "AF_INET6"
          ];
          RestrictNamespaces = true;
          RestrictRealtime = true;
          RestrictSUIDSGID = true;
          SystemCallArchitectures = "native";
          SystemCallFilter = [
            "~@mount"
            "~@swap"
            "~@resources"
            "~@reboot"
            "~@raw-io"
            "~@obsolete"
            "~@module"
            "~@debug"
            "~@cpu-emulation"
            "~@clock"
            "~@privileged"
          ];
          UMask = "0027";

          ExecStart = [
            ""
            (builtins.concatStringsSep " " [
              "${lib.getExe cfg.server}"
              "serve"
              "--port ${builtins.toString cfg.port}"
              "${lib.strings.optionalString (cfg.secretKeyFile != null) "--secret-key-file ${cfg.secretKeyFile}"}"
              "--host ${cfg.address}"
              "--db-path ${cfg.dbPath}"
            ])
          ];
        }
        // (
          if (cfg.port < 1024)
          then {
            AmbientCapabilities = ["CAP_NET_BIND_SERVICE"];
            CapabilityBoundingSet = ["CAP_NET_BIND_SERVICE"];
          }
          else {
            # A private user cannot have process capabilities on the host's user
            # namespace and thus CAP_NET_BIND_SERVICE has no effect.
            PrivateUsers = true;
            CapabilityBoundingSet = false;
          }
        );
    };

    services.nginx.virtualHosts."${cfg.domain}" = {
      locations."/api" = {
        proxyPass = "http://127.0.0.1:${builtins.toString cfg.port}";

        recommendedProxySettings = true;
        proxyWebsockets = true;
      };

      root = "${cfg.webFrontEnd}";

      enableACME = true;
      forceSSL = true;
    };
  };
}