2024-08-09 18:14:51 +02:00
|
|
|
{
|
|
|
|
|
config,
|
|
|
|
|
lib,
|
|
|
|
|
pkgs,
|
|
|
|
|
...
|
|
|
|
|
}:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
cfg = config.services.archtika;
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
options.services.archtika = {
|
|
|
|
|
enable = mkEnableOption "archtika service";
|
|
|
|
|
|
2024-08-09 23:18:07 +02:00
|
|
|
package = mkPackageOption pkgs "archtika" { };
|
|
|
|
|
|
2024-08-09 18:14:51 +02:00
|
|
|
user = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "archtika";
|
|
|
|
|
description = "User account under which archtika runs.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "archtika";
|
|
|
|
|
description = "Group under which archtika runs.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
databaseName = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "archtika";
|
|
|
|
|
description = "Name of the PostgreSQL database for archtika.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
jwtSecret = mkOption {
|
2024-08-13 18:27:43 +02:00
|
|
|
type = types.either types.str types.path;
|
|
|
|
|
description = "JWT secret for archtika. Can be a string or a path to a file containing the secret";
|
2024-08-09 18:14:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
|
type = types.port;
|
2024-08-10 22:20:57 +02:00
|
|
|
default = 5000;
|
2024-08-09 18:14:51 +02:00
|
|
|
description = "Port on which the API runs.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
webAppPort = mkOption {
|
|
|
|
|
type = types.port;
|
2024-08-10 22:20:57 +02:00
|
|
|
default = 10000;
|
2024-08-09 18:14:51 +02:00
|
|
|
description = "Port on which the web application runs.";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
users.users.${cfg.user} = {
|
|
|
|
|
isSystemUser = true;
|
|
|
|
|
group = cfg.group;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.groups.${cfg.group} = { };
|
|
|
|
|
|
2024-08-10 22:20:57 +02:00
|
|
|
systemd.tmpfiles.rules = [ "d /var/www/archtika-websites 0755 ${cfg.user} ${cfg.group} -" ];
|
|
|
|
|
|
2024-08-09 18:14:51 +02:00
|
|
|
systemd.services.archtika-api = {
|
|
|
|
|
description = "archtika API service";
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
after = [
|
|
|
|
|
"network.target"
|
|
|
|
|
"postgresql.service"
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
User = cfg.user;
|
|
|
|
|
Group = cfg.group;
|
|
|
|
|
Restart = "always";
|
|
|
|
|
};
|
2024-08-09 20:12:31 +02:00
|
|
|
|
2024-08-13 18:27:43 +02:00
|
|
|
script =
|
|
|
|
|
let
|
|
|
|
|
getSecret = if isPath cfg.jwtSecret then "cat ${cfg.jwtSecret}" else "echo -n '${cfg.jwtSecret}'";
|
|
|
|
|
in
|
|
|
|
|
''
|
|
|
|
|
JWT_SECRET=$(${getSecret})
|
2024-08-09 20:12:31 +02:00
|
|
|
|
2024-08-13 18:27:43 +02:00
|
|
|
${pkgs.postgresql_16}/bin/psql postgres://postgres@localhost:5432/${cfg.databaseName} -c "ALTER DATABASE ${cfg.databaseName} SET \"app.jwt_secret\" TO '$JWT_SECRET'"
|
2024-08-09 20:12:31 +02:00
|
|
|
|
2024-08-13 18:27:43 +02:00
|
|
|
${pkgs.dbmate}/bin/dbmate --url postgres://postgres@localhost:5432/archtika?sslmode=disable --migrations-dir ${cfg.package}/rest-api/db/migrations up
|
|
|
|
|
|
|
|
|
|
PGRST_SERVER_PORT=${toString cfg.port} PGRST_DB_SCHEMAS="api" PGRST_DB_ANON_ROLE="anon" PGRST_OPENAPI_MODE="ignore-privileges" PGRST_DB_URI="postgres://authenticator@localhost:5432/${cfg.databaseName}" PGRST_JWT_SECRET="$JWT_SECRET" ${pkgs.postgrest}/bin/postgrest
|
|
|
|
|
'';
|
2024-08-09 18:14:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
systemd.services.archtika-web = {
|
2024-08-09 20:12:31 +02:00
|
|
|
description = "archtika Web App service";
|
2024-08-09 18:14:51 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
after = [ "network.target" ];
|
|
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
User = cfg.user;
|
|
|
|
|
Group = cfg.group;
|
|
|
|
|
Restart = "always";
|
2024-08-17 20:21:23 +02:00
|
|
|
WorkingDirectory = "${cfg.package}/web-app";
|
2024-08-09 18:14:51 +02:00
|
|
|
};
|
2024-08-09 20:12:31 +02:00
|
|
|
|
|
|
|
|
script = ''
|
2024-08-13 22:14:47 +02:00
|
|
|
ORIGIN=https://demo.archtika.com PORT=${toString cfg.webAppPort} ${pkgs.nodejs_22}/bin/node ${cfg.package}/web-app
|
2024-08-09 20:12:31 +02:00
|
|
|
'';
|
2024-08-09 18:14:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
services.postgresql = {
|
|
|
|
|
enable = true;
|
|
|
|
|
package = pkgs.postgresql_16;
|
|
|
|
|
ensureDatabases = [ cfg.databaseName ];
|
|
|
|
|
authentication = lib.mkForce ''
|
2024-08-09 20:12:31 +02:00
|
|
|
# IPv4 local connections:
|
|
|
|
|
host all all 127.0.0.1/32 trust
|
|
|
|
|
# IPv6 local connections:
|
|
|
|
|
host all all ::1/128 trust
|
|
|
|
|
# Local socket connections:
|
|
|
|
|
local all all trust
|
2024-08-09 18:14:51 +02:00
|
|
|
'';
|
|
|
|
|
extraPlugins = with pkgs.postgresql16Packages; [ pgjwt ];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
services.nginx = {
|
|
|
|
|
enable = true;
|
|
|
|
|
recommendedProxySettings = true;
|
|
|
|
|
recommendedTlsSettings = true;
|
2024-08-10 22:20:57 +02:00
|
|
|
|
2024-08-13 22:14:47 +02:00
|
|
|
virtualHosts = {
|
|
|
|
|
"demo.archtika.com" = {
|
|
|
|
|
enableACME = true;
|
|
|
|
|
forceSSL = true;
|
|
|
|
|
locations = {
|
|
|
|
|
"/" = {
|
|
|
|
|
proxyPass = "http://localhost:${toString cfg.webAppPort}";
|
|
|
|
|
};
|
2024-08-17 22:07:16 +02:00
|
|
|
"/previews/" = {
|
|
|
|
|
alias = "/var/www/archtika-websites/previews/";
|
2024-08-13 22:14:47 +02:00
|
|
|
index = "index.html";
|
|
|
|
|
tryFiles = "$uri $uri/ $uri/index.html =404";
|
|
|
|
|
extraConfig = ''
|
|
|
|
|
autoindex on;
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
"/api/" = {
|
|
|
|
|
proxyPass = "http://localhost:${toString cfg.port}/";
|
|
|
|
|
extraConfig = ''
|
|
|
|
|
default_type application/json;
|
|
|
|
|
proxy_hide_header Content-Location;
|
|
|
|
|
add_header Content-Location /api/$upstream_http_content_location;
|
|
|
|
|
proxy_set_header Connection "";
|
|
|
|
|
proxy_http_version 1.1;
|
|
|
|
|
'';
|
|
|
|
|
};
|
2024-08-10 22:20:57 +02:00
|
|
|
};
|
|
|
|
|
};
|
2024-08-17 22:18:14 +02:00
|
|
|
"archtika-wildcard" = {
|
|
|
|
|
serverName = "~^(?<subdomain>[^.]+)\\.demo\\.archtika\\.com$";
|
2024-08-17 22:07:16 +02:00
|
|
|
enableACME = true;
|
|
|
|
|
forceSSL = true;
|
|
|
|
|
locations = {
|
|
|
|
|
"/" = {
|
2024-08-17 22:18:14 +02:00
|
|
|
root = "/var/www/archtika-websites";
|
2024-08-17 22:07:16 +02:00
|
|
|
index = "index.html";
|
|
|
|
|
tryFiles = "$uri $uri/ $uri/index.html =404";
|
2024-08-17 22:18:14 +02:00
|
|
|
extraConfig = ''
|
|
|
|
|
alias /var/www/archtika-websites/$subdomain/;
|
|
|
|
|
'';
|
2024-08-17 22:07:16 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
2024-08-10 22:20:57 +02:00
|
|
|
};
|
2024-08-09 18:14:51 +02:00
|
|
|
};
|
2024-08-13 22:14:47 +02:00
|
|
|
|
|
|
|
|
security.acme = {
|
|
|
|
|
acceptTerms = true;
|
|
|
|
|
defaults.email = "thilo.hohlt@tutanota.com";
|
2024-08-17 22:13:27 +02:00
|
|
|
certs."demo.archtika.com" = {
|
|
|
|
|
domain = "*.demo.archtika.com";
|
|
|
|
|
extraDomainNames = ["demo.archtika.com"];
|
|
|
|
|
dnsProvider = "porkbun";
|
|
|
|
|
};
|
2024-08-13 22:14:47 +02:00
|
|
|
};
|
2024-08-09 18:14:51 +02:00
|
|
|
};
|
|
|
|
|
}
|