WIP
This commit is contained in:
1 parent
13e407847f
commit
374428658a
80 files changed
+2517
-1058
No files matched your search
@@ -0,0 +1,160 @@
|
||||
{ config, lib, ... }:
|
||||
with lib;
|
||||
let
|
||||
rootCfg = config.selfhosted.mail;
|
||||
cfg = rootCfg.stalwart;
|
||||
domain = config.sops.placeholder."domains/serv";
|
||||
pocketIdSubdomain = config.selfhosted.pocket-id.subdomain;
|
||||
adminPassword = config.sops.placeholder."stalwart/admin_password";
|
||||
in
|
||||
{
|
||||
config = mkIf rootCfg.enable {
|
||||
sops.templates."stalwart.env".content = ''
|
||||
STALWART_PUBLIC_URL=https://${cfg.subdomain}.${domain}
|
||||
STALWART_RECOVERY_MODE=0
|
||||
STALWART_RECOVERY_ADMIN=admin:${adminPassword}
|
||||
'';
|
||||
|
||||
sops.templates."stalwart-cli.env".content = ''
|
||||
STALWART_URL=http://127.0.0.1:${toString cfg.hostHttpPort}
|
||||
STALWART_USER=admin
|
||||
STALWART_PASSWORD="${adminPassword}"
|
||||
'';
|
||||
|
||||
sops.templates."stalwart-config.json".content = ''
|
||||
{
|
||||
"@type": "RocksDb",
|
||||
"path": "/var/lib/stalwart/"
|
||||
}
|
||||
'';
|
||||
|
||||
sops.templates."stalwart-oidc-plan.json".content = ''
|
||||
[
|
||||
{
|
||||
"@type": "upsert",
|
||||
"object": "DnsServer",
|
||||
"matchOn": ["description"],
|
||||
"value": {
|
||||
"dns-porkbun": {
|
||||
"@type": "Porkbun",
|
||||
"description": "Porkbun",
|
||||
"apiKey": "${config.sops.placeholder."porkbun/serv/api_key"}",
|
||||
"secretApiKey": {
|
||||
"@type": "Value",
|
||||
"secret": "${config.sops.placeholder."porkbun/serv/secret_key"}"
|
||||
},
|
||||
"timeout": 30000,
|
||||
"ttl": 300000,
|
||||
"pollingInterval": 15000,
|
||||
"propagationTimeout": 120000,
|
||||
"propagationDelay": 15000
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"@type": "upsert",
|
||||
"object": "AcmeProvider",
|
||||
"matchOn": ["directory", "challengeType"],
|
||||
"value": {
|
||||
"acme-letsencrypt": {
|
||||
"directory": "https://acme-v02.api.letsencrypt.org/directory",
|
||||
"challengeType": "Dns01",
|
||||
"contact": {
|
||||
"${config.sops.placeholder."letsEncrypt/email"}": true
|
||||
},
|
||||
"renewBefore": "R23",
|
||||
"maxRetries": 10
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"@type": "upsert",
|
||||
"object": "Directory",
|
||||
"matchOn": ["issuerUrl"],
|
||||
"value": {
|
||||
"dir-oidc": {
|
||||
"@type": "Oidc",
|
||||
"description": "Pocket-ID",
|
||||
"issuerUrl": "https://${pocketIdSubdomain}.${domain}",
|
||||
"requireAudience": null,
|
||||
"requireScopes": {},
|
||||
"claimUsername": "preferred_username",
|
||||
"usernameDomain": "${domain}",
|
||||
"claimName": "name",
|
||||
"claimGroups": null
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"@type": "upsert",
|
||||
"object": "Domain",
|
||||
"matchOn": ["name"],
|
||||
"value": {
|
||||
"dom-mail": {
|
||||
"name": "${domain}",
|
||||
"isEnabled": true,
|
||||
"directoryId": "#dir-oidc",
|
||||
"subAddressing": {
|
||||
"@type": "Enabled"
|
||||
},
|
||||
"certificateManagement": {
|
||||
"@type": "Automatic",
|
||||
"acmeProviderId": "#acme-letsencrypt",
|
||||
"subjectAlternativeNames": {
|
||||
"${cfg.subdomain}": true
|
||||
}
|
||||
},
|
||||
"dkimManagement": {
|
||||
"@type": "Automatic"
|
||||
},
|
||||
"dnsManagement": {
|
||||
"@type": "Automatic",
|
||||
"dnsServerId": "#dns-porkbun",
|
||||
"origin": "${domain}",
|
||||
"publishRecords": {
|
||||
"autoConfig": true,
|
||||
"autoConfigLegacy": true,
|
||||
"autoDiscover": true,
|
||||
"caa": true,
|
||||
"dkim": true,
|
||||
"dmarc": true,
|
||||
"mtaSts": true,
|
||||
"mx": true,
|
||||
"spf": true,
|
||||
"srv": true,
|
||||
"tlsRpt": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"@type": "upsert",
|
||||
"object": "AllowedIp",
|
||||
"matchOn": ["address"],
|
||||
"value": {
|
||||
"allow-netbird-cgnat": {
|
||||
"address": "100.64.0.0/10",
|
||||
"reason": "NetBird reverse-proxy and peers (CGNAT)"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"@type": "update",
|
||||
"object": "SystemSettings",
|
||||
"value": {
|
||||
"defaultDomainId": "#dom-mail",
|
||||
"defaultHostname": "${cfg.subdomain}.${domain}"
|
||||
}
|
||||
},
|
||||
{
|
||||
"@type": "update",
|
||||
"object": "Authentication",
|
||||
"value": {
|
||||
"directoryId": "#dir-oidc"
|
||||
}
|
||||
}
|
||||
]
|
||||
'';
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
{ config, lib, pkgs, ... }:
|
||||
with lib;
|
||||
let
|
||||
common = config.selfhosted.common;
|
||||
rootCfg = config.selfhosted.mail;
|
||||
cfg = rootCfg.stalwart;
|
||||
planPath = config.sops.templates."stalwart-oidc-plan.json".path;
|
||||
cliEnvPath = config.sops.templates."stalwart-cli.env".path;
|
||||
hostDataPath = "${common.dataDir}/stalwart";
|
||||
in
|
||||
{
|
||||
imports = [ ./config.nix ];
|
||||
|
||||
options.selfhosted.mail.stalwart = {
|
||||
name = mkOption {
|
||||
type = types.str;
|
||||
};
|
||||
subdomain = mkOption {
|
||||
type = types.str;
|
||||
};
|
||||
hostHttpPort = mkOption {
|
||||
type = types.port;
|
||||
};
|
||||
hostImapsPort = mkOption {
|
||||
type = types.port;
|
||||
};
|
||||
hostSubmissionPort = mkOption {
|
||||
type = types.port;
|
||||
};
|
||||
hostSmtpsPort = mkOption {
|
||||
type = types.port;
|
||||
};
|
||||
hostManageSievePort = mkOption {
|
||||
type = types.port;
|
||||
};
|
||||
hostSmtpPort = mkOption {
|
||||
type = types.port;
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf rootCfg.enable {
|
||||
virtualisation.oci-containers.containers."${cfg.name}" = {
|
||||
image = "docker.io/stalwartlabs/stalwart:v0.16";
|
||||
ports = [
|
||||
"0.0.0.0:${toString cfg.hostHttpPort}:8080"
|
||||
"0.0.0.0:${toString cfg.hostImapsPort}:993"
|
||||
"0.0.0.0:${toString cfg.hostManageSievePort}:4190"
|
||||
"0.0.0.0:${toString cfg.hostSmtpPort}:25"
|
||||
"0.0.0.0:${toString cfg.hostSmtpsPort}:465"
|
||||
"0.0.0.0:${toString cfg.hostSubmissionPort}:587"
|
||||
];
|
||||
environmentFiles = [ config.sops.templates."stalwart.env".path ];
|
||||
volumes = [
|
||||
"${hostDataPath}/data:/var/lib/stalwart"
|
||||
"${hostDataPath}/config:/etc/stalwart"
|
||||
];
|
||||
cmd = [
|
||||
"--config"
|
||||
"/etc/stalwart/config.json"
|
||||
];
|
||||
log-driver = "journald";
|
||||
extraOptions = [
|
||||
"--network=${common.network.name}"
|
||||
"--health-cmd=curl -sf -o /dev/null http://localhost:8080/healthz/ready"
|
||||
"--health-interval=20s"
|
||||
"--health-start-period=30s"
|
||||
"--health-timeout=5s"
|
||||
"--health-retries=10"
|
||||
];
|
||||
};
|
||||
|
||||
systemd.services."${common.backend}-${cfg.name}" = {
|
||||
serviceConfig = {
|
||||
Restart = lib.mkOverride 90 "always";
|
||||
};
|
||||
wantedBy = [ "${common.backend}-${rootCfg.targetName}.target" ];
|
||||
partOf = [ "${common.backend}-${rootCfg.targetName}.target" ];
|
||||
preStart = ''
|
||||
mkdir -p ${hostDataPath}/{config,data}
|
||||
chown -R 2000:2000 ${hostDataPath} 2>/dev/null || chmod 775 ${hostDataPath}
|
||||
chmod 775 ${hostDataPath}/{config,data}
|
||||
cp -f ${config.sops.templates."stalwart-config.json".path} ${hostDataPath}/config/config.json
|
||||
chmod 644 ${hostDataPath}/config/config.json
|
||||
'';
|
||||
};
|
||||
|
||||
systemd.services."${cfg.name}-oidc-apply" = {
|
||||
description = "Apply Stalwart OIDC directory plan";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
EnvironmentFile = cliEnvPath;
|
||||
};
|
||||
wantedBy = [ "${common.backend}-${rootCfg.targetName}.target" ];
|
||||
partOf = [ "${common.backend}-${rootCfg.targetName}.target" ];
|
||||
requires = [ "${common.backend}-${cfg.name}.service" ];
|
||||
after = [ "${common.backend}-${cfg.name}.service" ];
|
||||
restartTriggers = [
|
||||
planPath
|
||||
cliEnvPath
|
||||
config.sops.templates."stalwart.env".path
|
||||
config.sops.secrets."porkbun/serv/api_key".path
|
||||
config.sops.secrets."porkbun/serv/secret_key".path
|
||||
config.sops.secrets."letsEncrypt/email".path
|
||||
];
|
||||
path = [
|
||||
pkgs.curl
|
||||
pkgs.jq
|
||||
pkgs.${common.backend}
|
||||
];
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
|
||||
cli_image="ghcr.io/stalwartlabs/cli:1.0.12"
|
||||
|
||||
ok=0
|
||||
for i in $(seq 1 60); do
|
||||
if curl -sf -o /dev/null "http://127.0.0.1:${toString cfg.hostHttpPort}/healthz/ready"; then
|
||||
ok=1
|
||||
break
|
||||
fi
|
||||
echo "Waiting for Stalwart container to become healthy... ($i/60)"
|
||||
sleep 1
|
||||
done
|
||||
if [ "$ok" -ne 1 ]; then
|
||||
echo "Stalwart did not become healthy in time" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! ${common.backend} image inspect "$cli_image" >/dev/null 2>&1; then
|
||||
${common.backend} pull "$cli_image"
|
||||
fi
|
||||
|
||||
jq -c '.[]' ${planPath} | ${common.backend} run --rm -i --network host \
|
||||
-e STALWART_URL \
|
||||
-e STALWART_USER \
|
||||
-e STALWART_PASSWORD \
|
||||
"$cli_image" \
|
||||
apply --stdin
|
||||
|
||||
${common.backend} restart ${cfg.name}
|
||||
|
||||
ok=0
|
||||
for i in $(seq 1 60); do
|
||||
if curl -sf -o /dev/null "http://127.0.0.1:${toString cfg.hostHttpPort}/healthz/ready"; then
|
||||
ok=1
|
||||
break
|
||||
fi
|
||||
echo "Waiting for Stalwart after restart... ($i/60)"
|
||||
sleep 2
|
||||
done
|
||||
if [ "$ok" -ne 1 ]; then
|
||||
echo "Stalwart did not become healthy after OIDC apply restart" >&2
|
||||
exit 1
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user