about summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/crabfit.nix
blob: d58027a6965df82b35f8fe60fb110132f63b2c4e (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
{
  config,
  lib,
  pkgs,
  ...
}:

let
  inherit (lib)
    literalExpression
    mkEnableOption
    mkIf
    mkOption
    mkPackageOption
    ;

  inherit (lib.types)
    attrsOf
    package
    port
    str
    ;

  cfg = config.services.crabfit;
in

{
  options.services.crabfit = {
    enable = mkEnableOption "Crab Fit, a meeting scheduler based on peoples' availability";

    frontend = {
      package = mkPackageOption pkgs "crabfit-frontend" { };

      finalDrv = mkOption {
        readOnly = true;
        type = package;
        default = cfg.frontend.package.override {
          api_url = "https://${cfg.api.host}";
          frontend_url = cfg.frontend.host;
        };

        defaultText = literalExpression ''
          cfg.package.override {
            api_url = "https://''${cfg.api.host}";
            frontend_url = cfg.frontend.host;
          };
        '';

        description = ''
          The patched frontend, using the correct urls for the API and frontend.
        '';
      };

      environment = mkOption {
        type = attrsOf str;
        default = { };
        description = ''
          Environment variables for the crabfit frontend.
        '';
      };

      host = mkOption {
        type = str;
        description = ''
          The hostname of the frontend.
        '';
      };

      port = mkOption {
        type = port;
        default = 3001;
        description = ''
          The internal listening port of the frontend.
        '';
      };
    };

    api = {
      package = mkPackageOption pkgs "crabfit-api" { };

      environment = mkOption {
        type = attrsOf str;
        default = { };
        description = ''
          Environment variables for the crabfit API.
        '';
      };

      host = mkOption {
        type = str;
        description = ''
          The hostname of the API.
        '';
      };

      port = mkOption {
        type = port;
        default = 3000;
        description = ''
          The internal listening port of the API.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services = {
      crabfit-api = {
        description = "The API for Crab Fit.";

        wantedBy = [ "multi-user.target" ];
        after = [ "postgresql.service" ];

        serviceConfig = {
          # TODO: harden
          ExecStart = lib.getExe cfg.api.package;
          User = "crabfit";
        };

        environment = {
          API_LISTEN = "127.0.0.1:${builtins.toString cfg.api.port}";
          DATABASE_URL = "postgres:///crabfit?host=/run/postgresql";
          FRONTEND_URL = "https://${cfg.frontend.host}";
        } // cfg.api.environment;
      };

      crabfit-frontend = {
        description = "The frontend for Crab Fit.";

        wantedBy = [ "multi-user.target" ];

        serviceConfig = {
          # TODO: harden
          CacheDirectory = "crabfit";
          DynamicUser = true;
          ExecStart = "${lib.getExe pkgs.nodejs} standalone/server.js";
          WorkingDirectory = cfg.frontend.finalDrv;
        };

        environment = {
          NEXT_PUBLIC_API_URL = "https://${cfg.api.host}";
          PORT = builtins.toString cfg.frontend.port;
        } // cfg.frontend.environment;
      };
    };

    users = {
      groups.crabfit = { };

      users.crabfit = {
        group = "crabfit";
        isSystemUser = true;
      };
    };

    services = {
      postgresql = {
        enable = true;

        ensureDatabases = [ "crabfit" ];

        ensureUsers = [
          {
            name = "crabfit";
            ensureDBOwnership = true;
          }
        ];
      };
    };
  };
}