This commit is contained in:
aloslider committed 2026-10-01 13:59:54 +03:00
1 parent 13e407847f
commit 374428658a
80 files changed
+2517 -1058

No files matched your search

@@ -0,0 +1,34 @@
{ config, lib, ... }:
with lib;
let
rootCfg = config.selfhosted.mail;
domain = config.sops.placeholder."domains/serv";
sessionSecret = config.sops.placeholder."bulwark/session_secret";
in
{
config = mkIf rootCfg.enable {
sops.templates."bulwark.env".content = ''
HOSTNAME=0.0.0.0
PORT=3000
APP_NAME=Mail
STALWART_FEATURES=true
BULWARK_TELEMETRY=off
SETTINGS_SYNC_ENABLED=true
JMAP_SERVER_URL=https://${rootCfg.stalwart.subdomain}.${domain}
ADMIN_PASSWORD=${config.sops.placeholder."bulwark/admin_password"}
SESSION_SECRET=${sessionSecret}
COOKIE_SAME_SITE=none
ALLOWED_FRAME_ANCESTORS=https://${rootCfg.stalwart.subdomain}.${domain}
NEXT_PUBLIC_PARENT_ORIGIN=https://${rootCfg.stalwart.subdomain}.${domain}
OAUTH_ENABLED=true
OAUTH_ONLY=true
AUTO_SSO_ENABLED=true
OAUTH_ISSUER_URL=https://${config.selfhosted.pocket-id.subdomain}.${domain}
OAUTH_CLIENT_ID=bulwark
OAUTH_SCOPES=openid email profile
OAUTH_ALLOW_PRIVATE_ENDPOINTS=true
'';
};
}
@@ -0,0 +1,61 @@
{ config, lib, ... }:
with lib;
let
common = config.selfhosted.common;
rootCfg = config.selfhosted.mail;
cfg = config.selfhosted.mail.bulwark;
hostDataPath = "${common.dataDir}/bulwark";
in
{
imports = [ ./config.nix ];
options.selfhosted.mail.bulwark = {
name = mkOption {
type = types.str;
};
subdomain = mkOption {
type = types.str;
};
hostHttpPort = mkOption {
type = types.port;
};
};
config = mkIf rootCfg.enable {
virtualisation.oci-containers.containers."${cfg.name}" = {
image = "ghcr.io/bulwarkmail/webmail-beta:sha-62e0760";
ports = [
"0.0.0.0:${toString cfg.hostHttpPort}:3000"
];
environmentFiles = [ config.sops.templates."bulwark.env".path ];
volumes = [
"${hostDataPath}/settings:/app/data/settings"
"${hostDataPath}/admin:/app/data/admin"
"${hostDataPath}/admin-state:/app/data/admin-state"
];
log-driver = "journald";
extraOptions = [
"--network=${common.network.name}"
"--health-cmd=wget --no-verbose --tries=1 --spider http://127.0.0.1:3000/api/health || exit 1"
"--health-interval=30s"
"--health-start-period=20s"
"--health-timeout=5s"
"--health-retries=5"
];
};
systemd.services."${common.backend}-${cfg.name}" = {
serviceConfig = {
Restart = lib.mkOverride 90 "always";
};
wantedBy = [ "${common.backend}-${rootCfg.targetName}.target" ];
partOf = [ "${common.backend}-${rootCfg.targetName}.target" ];
requires = [ "${common.backend}-${rootCfg.stalwart.name}.service" ];
after = [ "${common.backend}-${rootCfg.stalwart.name}.service" ];
preStart = ''
mkdir -p ${hostDataPath}/{settings,admin,admin-state}
chown -R 1001:65533 ${hostDataPath}/{settings,admin,admin-state}
'';
};
};
}
@@ -0,0 +1,44 @@
{ config, lib, ... }:
with lib;
let
common = config.selfhosted.common;
cfg = config.selfhosted.mail;
in
{
imports = [
./bulwark
./stalwart
];
options.selfhosted.mail = {
enable = mkEnableOption "Enable ${cfg.targetName} root target";
targetName = mkOption {
type = types.str;
};
hostPorts = mkOption {
type = types.listOf types.port;
default = [ ];
};
};
config = mkIf cfg.enable {
sops.secrets = {
"bulwark/admin_password" = { };
"bulwark/session_secret" = { };
"domains/serv" = { };
"letsEncrypt/email" = { };
"porkbun/serv/api_key" = { };
"porkbun/serv/secret_key" = { };
"stalwart/admin_password" = { };
};
systemd.targets."${common.backend}-${cfg.targetName}" = {
unitConfig = {
Description = "Root target for ${cfg.targetName}";
};
wantedBy = [ "${common.backend}-${common.rootTarget.name}.target" ];
partOf = [ "${common.backend}-${common.rootTarget.name}.target" ];
after = [ "${common.backend}-${common.rootTarget.name}.target" ];
};
};
}
@@ -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
'';
};
};
}