selfprivacy-nixos-config/letsencrypt/acme.nix

63 lines
1.9 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
2021-11-15 10:02:05 +00:00
let
cfg = config.selfprivacy;
dnsCredentialsTemplates = {
DIGITALOCEAN = "DO_AUTH_TOKEN=$TOKEN";
CLOUDFLARE = ''
CF_API_KEY=$TOKEN
CLOUDFLARE_DNS_API_TOKEN=$TOKEN
CLOUDFLARE_ZONE_API_TOKEN=$TOKEN
'';
DESEC = "DESEC_TOKEN=$TOKEN";
};
dnsCredentialsTemplate = dnsCredentialsTemplates.${cfg.dns.provider};
acme-env-filepath = "/var/lib/selfprivacy/acme-env";
secrets-filepath = "/etc/selfprivacy/secrets.json";
dnsPropagationCheckExceptions = [ "DIGITALOCEAN" ];
2021-11-15 10:02:05 +00:00
in
{
2023-12-01 04:42:03 +00:00
users.groups.acmereceivers.members = [ "nginx" ];
2021-11-15 10:02:05 +00:00
security.acme = {
acceptTerms = true;
defaults = {
email = "${cfg.username}@${cfg.domain}";
server = if cfg.dns.useStagingACME then "https://acme-staging-v02.api.letsencrypt.org/directory" else "https://acme-v02.api.letsencrypt.org/directory";
dnsPropagationCheck =
! (lib.elem cfg.dns.provider dnsPropagationCheckExceptions);
2023-06-14 16:06:58 +00:00
reloadServices = [ "nginx" ];
};
certs = lib.mkForce {
2023-12-18 21:07:05 +00:00
"wildcard-${cfg.domain}" = {
2021-11-15 10:02:05 +00:00
domain = "*.${cfg.domain}";
extraDomainNames = [ "${cfg.domain}" ];
group = "acmereceivers";
dnsProvider = lib.strings.toLower cfg.dns.provider;
credentialsFile = acme-env-filepath;
2021-11-15 10:02:05 +00:00
};
2023-12-18 21:07:05 +00:00
"${cfg.domain}" = {
domain = cfg.domain;
group = "acmereceivers";
webroot = "/var/lib/acme/acme-challenge";
};
2021-11-15 10:02:05 +00:00
};
};
systemd.services.acme-secrets = {
before = [ "acme-${cfg.domain}.service" ];
requiredBy = [ "acme-${cfg.domain}.service" ];
serviceConfig.Type = "oneshot";
path = with pkgs; [ coreutils jq ];
script = ''
set -o nounset
TOKEN="$(jq -re '.dns.apiKey' ${secrets-filepath})"
filecontents=$(cat <<- EOF
${dnsCredentialsTemplate}
EOF
)
install -m 0440 -o root -g acmereceivers -DT \
<(printf "%s\n" "$filecontents") ${acme-env-filepath}
'';
};
2021-11-15 10:02:05 +00:00
}