about summary refs log tree commit diff
path: root/users/grfn/bbbg/module.nix
blob: 297d846f22b6faeeb67d22619552087c3dbac26b (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
{ config, lib, pkgs, depot, ... }:

let
  bbbg = depot.users.grfn.bbbg;
  cfg = config.services.bbbg;
in {
  options = with lib; {
    services.bbbg = {
      enable = mkEnableOption "BBBG Server";

      port = mkOption {
        type = types.int;
        default = 7222;
        description = "Port to listen to for the HTTP server";
      };

      domain = mkOption {
        type = types.str;
        default = "bbbg.gws.fyi";
        description = "Domain to host under";
      };

      proxy = {
        enable = mkEnableOption "NGINX reverse proxy";
      };

      database = {
        enable = mkEnableOption "BBBG Database Server";

        user = mkOption {
          type = types.str;
          default = "bbbg";
          description = "Database username";
        };

        host = mkOption {
          type = types.str;
          default = "localhost";
          description = "Database host";
        };

        name = mkOption {
          type = types.str;
          default = "bbbg";
          description = "Database name";
        };

        port = mkOption {
          type = types.int;
          default = 5432;
          description = "Database host";
        };
      };
    };
  };

  config = lib.mkMerge [
    (lib.mkIf cfg.enable {
      systemd.services.bbbg-server = {
        wantedBy = [ "multi-user.target" ];
        after = [ "network.target" ];

        serviceConfig = {
          DynamicUser = true;
          Restart = "always";
          EnvironmentFile = "/run/agenix/bbbg";
        };

        environment = {
          PGHOST = cfg.database.host;
          PGUSER = cfg.database.user;
          PGDATABASE = cfg.database.name;
          PORT = toString cfg.port;
          BASE_URL = "https://${cfg.domain}";
        };

        script = "${bbbg.server}/bin/bbbg-server";
      };

      systemd.services.migrate-bbbg = {
        description = "Run database migrations for BBBG";
        wantedBy = [ "bbbg-server.service" ];
        after = ([ "network.target" ]
                 ++ (if cfg.database.enable
                     then ["postgresql.service"]
                     else []));

        serviceConfig = {
          Type = "oneshot";
          EnvironmentFile = "/run/agenix/bbbg";
        };

        environment = {
          PGHOST = cfg.database.host;
          PGUSER = cfg.database.user;
          PGDATABASE = cfg.database.name;
        };

        script = "${bbbg.db-util}/bin/bbbg-db-util migrate";
      };
    })
    (lib.mkIf cfg.database.enable {
      services.postgresql = {
        enable = true;
        authentication = lib.mkForce ''
          local all all trust
          host all all 127.0.0.1/32 password
          host all all ::1/128 password
          hostnossl all all 127.0.0.1/32 password
          hostnossl all all ::1/128  password
        '';

        ensureDatabases = [
          cfg.database.name
        ];

        ensureUsers = [{
          name = cfg.database.user;
          ensurePermissions = {
            "DATABASE ${cfg.database.name}" = "ALL PRIVILEGES";
          };
        }];
      };
    })
    (lib.mkIf cfg.proxy.enable {
      services.nginx = {
        enable = true;
        virtualHosts."${cfg.domain}" = {
          enableACME = true;
          forceSSL = true;
          locations."/".proxyPass = "http://localhost:${toString cfg.port}";
        };
      };
    })
  ];
}