2021-12-26 22:06:07 +01:00
|
|
|
{ config, lib, pkgs, depot, ... }:
|
|
|
|
|
|
|
|
let
|
2024-02-12 04:00:40 +01:00
|
|
|
bbbg = depot.users.aspen.bbbg;
|
2021-12-26 22:06:07 +01:00
|
|
|
cfg = config.services.bbbg;
|
2022-01-30 17:06:58 +01:00
|
|
|
in
|
|
|
|
{
|
2021-12-26 22:06:07 +01:00
|
|
|
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";
|
2022-05-22 23:51:49 +02:00
|
|
|
EnvironmentFile = config.age.secretsDir + "/bbbg";
|
2021-12-26 22:06:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
environment = {
|
|
|
|
PGHOST = cfg.database.host;
|
|
|
|
PGUSER = cfg.database.user;
|
|
|
|
PGDATABASE = cfg.database.name;
|
|
|
|
PORT = toString cfg.port;
|
2021-12-27 04:48:36 +01:00
|
|
|
BASE_URL = "https://${cfg.domain}";
|
2021-12-26 22:06:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
script = "${bbbg.server}/bin/bbbg-server";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.migrate-bbbg = {
|
|
|
|
description = "Run database migrations for BBBG";
|
|
|
|
wantedBy = [ "bbbg-server.service" ];
|
|
|
|
after = ([ "network.target" ]
|
2022-01-30 17:06:58 +01:00
|
|
|
++ (if cfg.database.enable
|
|
|
|
then [ "postgresql.service" ]
|
|
|
|
else [ ]));
|
2021-12-26 22:06:07 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
2022-05-22 23:51:49 +02:00
|
|
|
EnvironmentFile = config.age.secretsDir + "/bbbg";
|
2021-12-26 22:06:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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}";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|