2020-06-19 20:27:46 +01:00
|
|
|
{ config, lib, pkgs, options, ... }:
|
2015-12-06 15:55:09 +00:00
|
|
|
with lib;
|
|
|
|
let
|
2015-12-11 16:42:17 +00:00
|
|
|
cfg = config.security.acme;
|
2021-12-05 19:40:24 +00:00
|
|
|
opt = options.security.acme;
|
2021-12-04 19:01:18 +00:00
|
|
|
user = if cfg.useRoot then "root" else "acme";
|
2015-12-06 15:55:09 +00:00
|
|
|
|
2020-06-19 20:27:46 +01:00
|
|
|
# Used to calculate timer accuracy for coalescing
|
|
|
|
numCerts = length (builtins.attrNames cfg.certs);
|
|
|
|
_24hSecs = 60 * 60 * 24;
|
|
|
|
|
2020-12-13 20:22:33 +00:00
|
|
|
# Used to make unique paths for each cert/account config set
|
|
|
|
mkHash = with builtins; val: substring 0 20 (hashString "sha256" val);
|
|
|
|
mkAccountHash = acmeServer: data: mkHash "${toString acmeServer} ${data.keyType} ${data.email}";
|
|
|
|
accountDirRoot = "/var/lib/acme/.lego/accounts/";
|
|
|
|
|
2020-06-19 20:27:46 +01:00
|
|
|
# There are many services required to make cert renewals work.
|
|
|
|
# They all follow a common structure:
|
|
|
|
# - They inherit this commonServiceConfig
|
|
|
|
# - They all run as the acme user
|
|
|
|
# - They all use BindPath and StateDirectory where possible
|
|
|
|
# to set up a sort of build environment in /tmp
|
|
|
|
# The Group can vary depending on what the user has specified in
|
|
|
|
# security.acme.certs.<cert>.group on some of the services.
|
|
|
|
commonServiceConfig = {
|
2021-05-16 17:27:10 +01:00
|
|
|
Type = "oneshot";
|
2021-12-04 19:01:18 +00:00
|
|
|
User = user;
|
2021-05-16 17:27:10 +01:00
|
|
|
Group = mkDefault "acme";
|
|
|
|
UMask = 0022;
|
|
|
|
StateDirectoryMode = 750;
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ReadWritePaths = [
|
|
|
|
"/var/lib/acme"
|
|
|
|
];
|
|
|
|
PrivateTmp = true;
|
2020-06-19 20:27:46 +01:00
|
|
|
|
2021-05-16 17:27:10 +01:00
|
|
|
WorkingDirectory = "/tmp";
|
|
|
|
|
|
|
|
CapabilityBoundingSet = [ "" ];
|
|
|
|
DevicePolicy = "closed";
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
ProcSubset = "pid";
|
|
|
|
RemoveIPC = true;
|
|
|
|
RestrictAddressFamilies = [
|
|
|
|
"AF_INET"
|
|
|
|
"AF_INET6"
|
|
|
|
];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = [
|
|
|
|
# 1. allow a reasonable set of syscalls
|
|
|
|
"@system-service"
|
|
|
|
# 2. and deny unreasonable ones
|
|
|
|
"~@privileged @resources"
|
|
|
|
# 3. then allow the required subset within denied groups
|
|
|
|
"@chown"
|
|
|
|
];
|
2020-06-19 20:27:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
# In order to avoid race conditions creating the CA for selfsigned certs,
|
|
|
|
# we have a separate service which will create the necessary files.
|
|
|
|
selfsignCAService = {
|
|
|
|
description = "Generate self-signed certificate authority";
|
|
|
|
|
|
|
|
path = with pkgs; [ minica ];
|
|
|
|
|
|
|
|
unitConfig = {
|
|
|
|
ConditionPathExists = "!/var/lib/acme/.minica/key.pem";
|
2021-11-28 22:48:43 +00:00
|
|
|
StartLimitIntervalSec = 0;
|
2020-06-19 20:27:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
serviceConfig = commonServiceConfig // {
|
|
|
|
StateDirectory = "acme/.minica";
|
|
|
|
BindPaths = "/var/lib/acme/.minica:/tmp/ca";
|
2021-05-05 00:27:19 +01:00
|
|
|
UMask = 0077;
|
2020-06-19 20:27:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
# Working directory will be /tmp
|
|
|
|
script = ''
|
|
|
|
minica \
|
|
|
|
--ca-key ca/key.pem \
|
|
|
|
--ca-cert ca/cert.pem \
|
|
|
|
--domains selfsigned.local
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-12-13 22:19:53 +00:00
|
|
|
# Ensures that directories which are shared across all certs
|
|
|
|
# exist and have the correct user and group, since group
|
|
|
|
# is configurable on a per-cert basis.
|
2020-12-29 15:01:08 +00:00
|
|
|
userMigrationService = let
|
2021-01-09 19:15:03 +00:00
|
|
|
script = with builtins; ''
|
2021-12-04 19:01:18 +00:00
|
|
|
chown -R ${user} .lego/accounts
|
2021-01-09 19:15:03 +00:00
|
|
|
'' + (concatStringsSep "\n" (mapAttrsToList (cert: data: ''
|
2020-12-29 15:01:08 +00:00
|
|
|
for fixpath in ${escapeShellArg cert} .lego/${escapeShellArg cert}; do
|
2020-06-19 20:27:46 +01:00
|
|
|
if [ -d "$fixpath" ]; then
|
2020-10-22 14:04:31 +01:00
|
|
|
chmod -R u=rwX,g=rX,o= "$fixpath"
|
2021-12-04 19:01:18 +00:00
|
|
|
chown -R ${user}:${data.group} "$fixpath"
|
2020-06-19 20:27:46 +01:00
|
|
|
fi
|
|
|
|
done
|
2021-01-09 19:15:03 +00:00
|
|
|
'') certConfigs));
|
2020-12-29 15:01:08 +00:00
|
|
|
in {
|
|
|
|
description = "Fix owner and group of all ACME certificates";
|
2020-06-19 20:27:46 +01:00
|
|
|
|
2020-12-29 15:01:08 +00:00
|
|
|
serviceConfig = commonServiceConfig // {
|
2020-12-13 22:19:53 +00:00
|
|
|
# We don't want this to run every time a renewal happens
|
|
|
|
RemainAfterExit = true;
|
|
|
|
|
|
|
|
# These StateDirectory entries negate the need for tmpfiles
|
2020-12-29 15:01:08 +00:00
|
|
|
StateDirectory = [ "acme" "acme/.lego" "acme/.lego/accounts" ];
|
|
|
|
StateDirectoryMode = 755;
|
|
|
|
WorkingDirectory = "/var/lib/acme";
|
|
|
|
|
|
|
|
# Run the start script as root
|
|
|
|
ExecStart = "+" + (pkgs.writeShellScript "acme-fixperms" script);
|
2020-12-13 22:19:53 +00:00
|
|
|
};
|
2020-06-19 20:27:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
certToConfig = cert: data: let
|
2021-11-28 17:03:31 +00:00
|
|
|
acmeServer = data.server;
|
2020-06-19 20:27:46 +01:00
|
|
|
useDns = data.dnsProvider != null;
|
|
|
|
destPath = "/var/lib/acme/${cert}";
|
2020-09-04 23:39:22 +01:00
|
|
|
selfsignedDeps = optionals (cfg.preliminarySelfsigned) [ "acme-selfsigned-${cert}.service" ];
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
# Minica and lego have a "feature" which replaces * with _. We need
|
|
|
|
# to make this substitution to reference the output files from both programs.
|
|
|
|
# End users never see this since we rename the certs.
|
|
|
|
keyName = builtins.replaceStrings ["*"] ["_"] data.domain;
|
|
|
|
|
|
|
|
# FIXME when mkChangedOptionModule supports submodules, change to that.
|
|
|
|
# This is a workaround
|
|
|
|
extraDomains = data.extraDomainNames ++ (
|
|
|
|
optionals
|
|
|
|
(data.extraDomains != "_mkMergedOptionModule")
|
|
|
|
(builtins.attrNames data.extraDomains)
|
|
|
|
);
|
|
|
|
|
|
|
|
# Create hashes for cert data directories based on configuration
|
2020-09-04 23:39:22 +01:00
|
|
|
# Flags are separated to avoid collisions
|
2020-06-19 20:27:46 +01:00
|
|
|
hashData = with builtins; ''
|
2020-09-04 23:39:22 +01:00
|
|
|
${concatStringsSep " " data.extraLegoFlags} -
|
|
|
|
${concatStringsSep " " data.extraLegoRunFlags} -
|
|
|
|
${concatStringsSep " " data.extraLegoRenewFlags} -
|
2020-06-19 20:27:46 +01:00
|
|
|
${toString acmeServer} ${toString data.dnsProvider}
|
2020-09-04 23:39:22 +01:00
|
|
|
${toString data.ocspMustStaple} ${data.keyType}
|
2020-06-19 20:27:46 +01:00
|
|
|
'';
|
|
|
|
certDir = mkHash hashData;
|
2021-11-27 00:03:35 +00:00
|
|
|
# TODO remove domainHash usage entirely. Waiting on go-acme/lego#1532
|
2020-09-04 23:39:22 +01:00
|
|
|
domainHash = mkHash "${concatStringsSep " " extraDomains} ${data.domain}";
|
2020-12-13 20:22:33 +00:00
|
|
|
accountHash = (mkAccountHash acmeServer data);
|
|
|
|
accountDir = accountDirRoot + accountHash;
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
protocolOpts = if useDns then (
|
|
|
|
[ "--dns" data.dnsProvider ]
|
|
|
|
++ optionals (!data.dnsPropagationCheck) [ "--dns.disable-cp" ]
|
2020-10-07 12:01:08 +01:00
|
|
|
++ optionals (data.dnsResolver != null) [ "--dns.resolvers" data.dnsResolver ]
|
2021-05-21 09:07:24 +01:00
|
|
|
) else if data.listenHTTP != null then [ "--http" "--http.port" data.listenHTTP ]
|
|
|
|
else [ "--http" "--http.webroot" data.webroot ];
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
commonOpts = [
|
|
|
|
"--accept-tos" # Checking the option is covered by the assertions
|
|
|
|
"--path" "."
|
|
|
|
"-d" data.domain
|
|
|
|
"--email" data.email
|
|
|
|
"--key-type" data.keyType
|
|
|
|
] ++ protocolOpts
|
|
|
|
++ optionals (acmeServer != null) [ "--server" acmeServer ]
|
|
|
|
++ concatMap (name: [ "-d" name ]) extraDomains
|
|
|
|
++ data.extraLegoFlags;
|
|
|
|
|
2020-10-06 21:52:49 +01:00
|
|
|
# Although --must-staple is common to both modes, it is not declared as a
|
|
|
|
# mode-agnostic argument in lego and thus must come after the mode.
|
2020-06-19 20:27:46 +01:00
|
|
|
runOpts = escapeShellArgs (
|
|
|
|
commonOpts
|
|
|
|
++ [ "run" ]
|
2020-10-06 21:52:49 +01:00
|
|
|
++ optionals data.ocspMustStaple [ "--must-staple" ]
|
2020-06-19 20:27:46 +01:00
|
|
|
++ data.extraLegoRunFlags
|
|
|
|
);
|
|
|
|
renewOpts = escapeShellArgs (
|
|
|
|
commonOpts
|
2021-05-30 12:12:32 +01:00
|
|
|
++ [ "renew" ]
|
2020-10-06 21:52:49 +01:00
|
|
|
++ optionals data.ocspMustStaple [ "--must-staple" ]
|
2020-06-19 20:27:46 +01:00
|
|
|
++ data.extraLegoRenewFlags
|
|
|
|
);
|
|
|
|
|
2021-09-24 12:09:37 +01:00
|
|
|
# We need to collect all the ACME webroots to grant them write
|
|
|
|
# access in the systemd service.
|
|
|
|
webroots =
|
|
|
|
lib.remove null
|
|
|
|
(lib.unique
|
|
|
|
(builtins.map
|
|
|
|
(certAttrs: certAttrs.webroot)
|
|
|
|
(lib.attrValues config.security.acme.certs)));
|
2020-06-19 20:27:46 +01:00
|
|
|
in {
|
2020-12-13 22:19:53 +00:00
|
|
|
inherit accountHash cert selfsignedDeps;
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
group = data.group;
|
|
|
|
|
|
|
|
renewTimer = {
|
|
|
|
description = "Renew ACME Certificate for ${cert}";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
2021-11-28 17:03:31 +00:00
|
|
|
OnCalendar = data.renewInterval;
|
2020-06-19 20:27:46 +01:00
|
|
|
Unit = "acme-${cert}.service";
|
|
|
|
Persistent = "yes";
|
|
|
|
|
|
|
|
# Allow systemd to pick a convenient time within the day
|
|
|
|
# to run the check.
|
|
|
|
# This allows the coalescing of multiple timer jobs.
|
|
|
|
# We divide by the number of certificates so that if you
|
|
|
|
# have many certificates, the renewals are distributed over
|
|
|
|
# the course of the day to avoid rate limits.
|
|
|
|
AccuracySec = "${toString (_24hSecs / numCerts)}s";
|
|
|
|
|
|
|
|
# Skew randomly within the day, per https://letsencrypt.org/docs/integration-guide/.
|
|
|
|
RandomizedDelaySec = "24h";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
selfsignService = {
|
|
|
|
description = "Generate self-signed certificate for ${cert}";
|
|
|
|
after = [ "acme-selfsigned-ca.service" "acme-fixperms.service" ];
|
2020-09-04 18:48:47 +01:00
|
|
|
requires = [ "acme-selfsigned-ca.service" "acme-fixperms.service" ];
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
path = with pkgs; [ minica ];
|
|
|
|
|
|
|
|
unitConfig = {
|
|
|
|
ConditionPathExists = "!/var/lib/acme/${cert}/key.pem";
|
2021-11-28 22:48:43 +00:00
|
|
|
StartLimitIntervalSec = 0;
|
2020-06-19 20:27:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
serviceConfig = commonServiceConfig // {
|
|
|
|
Group = data.group;
|
2021-05-05 00:27:19 +01:00
|
|
|
UMask = 0027;
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
StateDirectory = "acme/${cert}";
|
|
|
|
|
2020-12-29 15:01:08 +00:00
|
|
|
BindPaths = [
|
|
|
|
"/var/lib/acme/.minica:/tmp/ca"
|
|
|
|
"/var/lib/acme/${cert}:/tmp/${keyName}"
|
|
|
|
];
|
2020-06-19 20:27:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
# Working directory will be /tmp
|
|
|
|
# minica will output to a folder sharing the name of the first domain
|
|
|
|
# in the list, which will be ${data.domain}
|
|
|
|
script = ''
|
|
|
|
minica \
|
|
|
|
--ca-key ca/key.pem \
|
|
|
|
--ca-cert ca/cert.pem \
|
|
|
|
--domains ${escapeShellArg (builtins.concatStringsSep "," ([ data.domain ] ++ extraDomains))}
|
|
|
|
|
|
|
|
# Create files to match directory layout for real certificates
|
|
|
|
cd '${keyName}'
|
|
|
|
cp ../ca/cert.pem chain.pem
|
|
|
|
cat cert.pem chain.pem > fullchain.pem
|
|
|
|
cat key.pem fullchain.pem > full.pem
|
|
|
|
|
|
|
|
# Group might change between runs, re-apply it
|
2021-12-04 19:01:18 +00:00
|
|
|
chown '${user}:${data.group}' *
|
2021-05-05 00:27:19 +01:00
|
|
|
|
|
|
|
# Default permissions make the files unreadable by group + anon
|
|
|
|
# Need to be readable by group
|
|
|
|
chmod 640 *
|
2020-06-19 20:27:46 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
renewService = {
|
|
|
|
description = "Renew ACME certificate for ${cert}";
|
2020-10-06 21:52:55 +01:00
|
|
|
after = [ "network.target" "network-online.target" "acme-fixperms.service" "nss-lookup.target" ] ++ selfsignedDeps;
|
2020-09-04 23:39:22 +01:00
|
|
|
wants = [ "network-online.target" "acme-fixperms.service" ] ++ selfsignedDeps;
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
# https://github.com/NixOS/nixpkgs/pull/81371#issuecomment-605526099
|
|
|
|
wantedBy = optionals (!config.boot.isContainer) [ "multi-user.target" ];
|
|
|
|
|
2021-03-01 10:53:24 +00:00
|
|
|
path = with pkgs; [ lego coreutils diffutils openssl ];
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
serviceConfig = commonServiceConfig // {
|
|
|
|
Group = data.group;
|
|
|
|
|
2020-12-13 22:19:53 +00:00
|
|
|
# Keep in mind that these directories will be deleted if the user runs
|
|
|
|
# systemctl clean --what=state
|
|
|
|
# acme/.lego/${cert} is listed for this reason.
|
2020-12-29 15:01:08 +00:00
|
|
|
StateDirectory = [
|
|
|
|
"acme/${cert}"
|
|
|
|
"acme/.lego/${cert}"
|
|
|
|
"acme/.lego/${cert}/${certDir}"
|
|
|
|
"acme/.lego/accounts/${accountHash}"
|
|
|
|
];
|
2020-06-19 20:27:46 +01:00
|
|
|
|
2021-09-24 12:09:37 +01:00
|
|
|
ReadWritePaths = commonServiceConfig.ReadWritePaths ++ webroots;
|
|
|
|
|
2020-06-19 20:27:46 +01:00
|
|
|
# Needs to be space separated, but can't use a multiline string because that'll include newlines
|
2020-12-29 15:01:08 +00:00
|
|
|
BindPaths = [
|
|
|
|
"${accountDir}:/tmp/accounts"
|
|
|
|
"/var/lib/acme/${cert}:/tmp/out"
|
|
|
|
"/var/lib/acme/.lego/${cert}/${certDir}:/tmp/certificates"
|
|
|
|
];
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
# Only try loading the credentialsFile if the dns challenge is enabled
|
|
|
|
EnvironmentFile = mkIf useDns data.credentialsFile;
|
2020-09-04 18:48:47 +01:00
|
|
|
|
|
|
|
# Run as root (Prefixed with +)
|
|
|
|
ExecStartPost = "+" + (pkgs.writeShellScript "acme-postrun" ''
|
|
|
|
cd /var/lib/acme/${escapeShellArg cert}
|
|
|
|
if [ -e renewed ]; then
|
|
|
|
rm renewed
|
|
|
|
${data.postRun}
|
2021-10-06 10:53:04 +01:00
|
|
|
${optionalString (data.reloadServices != [])
|
|
|
|
"systemctl --no-block try-reload-or-restart ${escapeShellArgs data.reloadServices}"
|
|
|
|
}
|
2020-09-04 18:48:47 +01:00
|
|
|
fi
|
|
|
|
'');
|
2021-05-21 09:07:24 +01:00
|
|
|
} // optionalAttrs (data.listenHTTP != null && toInt (elemAt (splitString ":" data.listenHTTP) 1) < 1024) {
|
2021-12-18 14:52:32 +00:00
|
|
|
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
|
2021-01-09 19:34:54 +00:00
|
|
|
};
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
# Working directory will be /tmp
|
|
|
|
script = ''
|
2021-11-26 12:58:40 +00:00
|
|
|
${optionalString data.enableDebugLogs "set -x"}
|
|
|
|
set -euo pipefail
|
2020-06-19 20:27:46 +01:00
|
|
|
|
2021-03-01 10:53:24 +00:00
|
|
|
# This reimplements the expiration date check, but without querying
|
|
|
|
# the acme server first. By doing this offline, we avoid errors
|
|
|
|
# when the network or DNS are unavailable, which can happen during
|
|
|
|
# nixos-rebuild switch.
|
|
|
|
is_expiration_skippable() {
|
|
|
|
pem=$1
|
|
|
|
|
|
|
|
# This function relies on set -e to exit early if any of the
|
|
|
|
# conditions or programs fail.
|
|
|
|
|
|
|
|
[[ -e $pem ]]
|
|
|
|
|
|
|
|
expiration_line="$(
|
|
|
|
set -euxo pipefail
|
|
|
|
openssl x509 -noout -enddate <$pem \
|
|
|
|
| grep notAfter \
|
|
|
|
| sed -e 's/^notAfter=//'
|
|
|
|
)"
|
|
|
|
[[ -n "$expiration_line" ]]
|
|
|
|
|
|
|
|
expiration_date="$(date -d "$expiration_line" +%s)"
|
|
|
|
now="$(date +%s)"
|
|
|
|
expiration_s=$[expiration_date - now]
|
|
|
|
expiration_days=$[expiration_s / (3600 * 24)] # rounds down
|
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
[[ $expiration_days -gt ${toString data.validMinDays} ]]
|
2021-03-01 10:53:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 19:34:54 +00:00
|
|
|
${optionalString (data.webroot != null) ''
|
2021-03-15 01:33:45 +00:00
|
|
|
# Ensure the webroot exists. Fixing group is required in case configuration was changed between runs.
|
|
|
|
# Lego will fail if the webroot does not exist at all.
|
|
|
|
(
|
|
|
|
mkdir -p '${data.webroot}/.well-known/acme-challenge' \
|
|
|
|
&& chgrp '${data.group}' ${data.webroot}/.well-known/acme-challenge
|
|
|
|
) || (
|
|
|
|
echo 'Please ensure ${data.webroot}/.well-known/acme-challenge exists and is writable by acme:${data.group}' \
|
|
|
|
&& exit 1
|
|
|
|
)
|
2021-01-09 19:34:54 +00:00
|
|
|
''}
|
|
|
|
|
2020-09-04 23:39:22 +01:00
|
|
|
echo '${domainHash}' > domainhash.txt
|
|
|
|
|
2021-11-27 00:03:35 +00:00
|
|
|
# Check if we can renew.
|
|
|
|
# We can only renew if the list of domains has not changed.
|
|
|
|
if cmp -s domainhash.txt certificates/domainhash.txt && [ -e 'certificates/${keyName}.key' -a -e 'certificates/${keyName}.crt' -a -n "$(ls -1 accounts)" ]; then
|
2020-09-04 23:39:22 +01:00
|
|
|
|
2021-11-27 00:03:35 +00:00
|
|
|
# Even if a cert is not expired, it may be revoked by the CA.
|
|
|
|
# Try to renew, and silently fail if the cert is not expired.
|
|
|
|
# Avoids #85794 and resolves #129838
|
2021-11-28 17:03:31 +00:00
|
|
|
if ! lego ${renewOpts} --days ${toString data.validMinDays}; then
|
2021-03-01 10:53:24 +00:00
|
|
|
if is_expiration_skippable out/full.pem; then
|
2021-11-28 17:03:31 +00:00
|
|
|
echo 1>&2 "nixos-acme: Ignoring failed renewal because expiration isn't within the coming ${toString data.validMinDays} days"
|
2021-03-01 10:53:24 +00:00
|
|
|
else
|
2021-11-28 17:03:31 +00:00
|
|
|
# High number to avoid Systemd reserved codes.
|
|
|
|
exit 11
|
2021-03-01 10:53:24 +00:00
|
|
|
fi
|
2020-09-04 23:39:22 +01:00
|
|
|
fi
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
# Otherwise do a full run
|
2021-11-26 21:57:31 +00:00
|
|
|
elif ! lego ${runOpts}; then
|
|
|
|
# Produce a nice error for those doing their first nixos-rebuild with these certs
|
|
|
|
echo Failed to fetch certificates. \
|
|
|
|
This may mean your DNS records are set up incorrectly. \
|
|
|
|
${optionalString (cfg.preliminarySelfsigned) "Selfsigned certs are in place and dependant services will still start."}
|
2021-11-28 17:03:31 +00:00
|
|
|
# Exit 10 so that users can potentially amend SuccessExitStatus to ignore this error.
|
|
|
|
# High number to avoid Systemd reserved codes.
|
|
|
|
exit 10
|
2020-06-19 20:27:46 +01:00
|
|
|
fi
|
|
|
|
|
2020-09-04 23:39:22 +01:00
|
|
|
mv domainhash.txt certificates/
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
# Group might change between runs, re-apply it
|
2021-12-04 19:01:18 +00:00
|
|
|
chown '${user}:${data.group}' certificates/*
|
2020-06-19 20:27:46 +01:00
|
|
|
|
|
|
|
# Copy all certs to the "real" certs directory
|
2021-11-27 00:03:35 +00:00
|
|
|
if ! cmp -s 'certificates/${keyName}.crt' out/fullchain.pem; then
|
2020-09-04 18:48:47 +01:00
|
|
|
touch out/renewed
|
2020-09-03 15:31:06 +01:00
|
|
|
echo Installing new certificate
|
|
|
|
cp -vp 'certificates/${keyName}.crt' out/fullchain.pem
|
|
|
|
cp -vp 'certificates/${keyName}.key' out/key.pem
|
|
|
|
cp -vp 'certificates/${keyName}.issuer.crt' out/chain.pem
|
2020-06-19 20:27:46 +01:00
|
|
|
ln -sf fullchain.pem out/cert.pem
|
|
|
|
cat out/key.pem out/fullchain.pem > out/full.pem
|
|
|
|
fi
|
2021-05-05 00:27:19 +01:00
|
|
|
|
|
|
|
# By default group will have no access to the cert files.
|
|
|
|
# This chmod will fix that.
|
|
|
|
chmod 640 out/*
|
2020-06-19 20:27:46 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
certConfigs = mapAttrs certToConfig cfg.certs;
|
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
# These options can be specified within
|
2021-12-09 21:43:54 +00:00
|
|
|
# security.acme.defaults or security.acme.certs.<name>
|
|
|
|
inheritableModule = isDefaults: { config, ... }: let
|
|
|
|
defaultAndText = name: default: {
|
|
|
|
# When ! isDefaults then this is the option declaration for the
|
|
|
|
# security.acme.certs.<name> path, which has the extra inheritDefaults
|
|
|
|
# option, which if disabled means that we can't inherit it
|
|
|
|
default = if isDefaults || ! config.inheritDefaults then default else cfg.defaults.${name};
|
|
|
|
# The docs however don't need to depend on inheritDefaults, they should
|
|
|
|
# stay constant. Though notably it wouldn't matter much, because to get
|
|
|
|
# the option information, a submodule with name `<name>` is evaluated
|
|
|
|
# without any definitions.
|
|
|
|
defaultText = if isDefaults then default else literalExpression "config.security.acme.defaults.${name}";
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
options = {
|
2021-11-28 17:03:31 +00:00
|
|
|
validMinDays = mkOption {
|
|
|
|
type = types.int;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "validMinDays" 30) default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Minimum remaining validity before renewal in days.";
|
2020-06-19 20:27:46 +01:00
|
|
|
};
|
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
renewInterval = mkOption {
|
|
|
|
type = types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "renewInterval" "daily") default defaultText;
|
2022-08-05 18:39:00 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-11-28 17:03:31 +00:00
|
|
|
Systemd calendar expression when to check for renewal. See
|
2022-08-05 18:39:00 +01:00
|
|
|
{manpage}`systemd.time(7)`.
|
2021-11-28 17:03:31 +00:00
|
|
|
'';
|
2020-06-19 20:27:46 +01:00
|
|
|
};
|
|
|
|
|
2022-08-28 20:18:44 +01:00
|
|
|
enableDebugLogs = mkEnableOption (lib.mdDoc "debug logging for this certificate") // {
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "enableDebugLogs" true) default defaultText;
|
2020-06-19 20:27:46 +01:00
|
|
|
};
|
|
|
|
|
2015-12-06 15:55:09 +00:00
|
|
|
webroot = mkOption {
|
2020-01-15 09:17:11 +00:00
|
|
|
type = types.nullOr types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "webroot" null) default defaultText;
|
2021-01-08 23:33:40 +00:00
|
|
|
example = "/var/lib/acme/acme-challenge";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2015-12-08 18:09:19 +00:00
|
|
|
Where the webroot of the HTTP vhost is located.
|
2022-07-28 22:19:15 +01:00
|
|
|
{file}`.well-known/acme-challenge/` directory
|
2017-06-08 07:46:40 +01:00
|
|
|
will be created below the webroot if it doesn't exist.
|
2022-07-28 22:19:15 +01:00
|
|
|
`http://example.org/.well-known/acme-challenge/` must also
|
2015-12-08 18:09:19 +00:00
|
|
|
be available (notice unencrypted HTTP).
|
|
|
|
'';
|
2015-12-06 15:55:09 +00:00
|
|
|
};
|
|
|
|
|
2019-10-25 23:40:51 +01:00
|
|
|
server = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "server" null) default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-04-21 14:39:46 +01:00
|
|
|
ACME Directory Resource URI. Defaults to Let's Encrypt's
|
2019-10-25 23:40:51 +01:00
|
|
|
production endpoint,
|
2022-07-28 22:19:15 +01:00
|
|
|
<https://acme-v02.api.letsencrypt.org/directory>, if unset.
|
2019-10-25 23:40:51 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-06 15:55:09 +00:00
|
|
|
email = mkOption {
|
2021-11-28 17:03:31 +00:00
|
|
|
type = types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "email" null) default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-11-28 17:03:31 +00:00
|
|
|
Email address for account creation and correspondence from the CA.
|
|
|
|
It is recommended to use the same email for all certs to avoid account
|
|
|
|
creation limits.
|
|
|
|
'';
|
2015-12-06 15:55:09 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 18:09:19 +00:00
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "group" "acme") default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Group running the ACME client.";
|
2015-12-08 18:09:19 +00:00
|
|
|
};
|
|
|
|
|
2021-10-06 10:53:04 +01:00
|
|
|
reloadServices = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "reloadServices" []) default defaultText;
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The list of systemd services to call `systemctl try-reload-or-restart`
|
2021-10-06 10:53:04 +01:00
|
|
|
on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-08 18:09:19 +00:00
|
|
|
postRun = mkOption {
|
|
|
|
type = types.lines;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "postRun" "") default defaultText;
|
2020-06-19 20:27:46 +01:00
|
|
|
example = "cp full.pem backup.pem";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-06-19 20:27:46 +01:00
|
|
|
Commands to run after new certificates go live. Note that
|
2020-09-04 18:48:47 +01:00
|
|
|
these commands run as the root user.
|
2017-11-19 15:41:28 +00:00
|
|
|
|
|
|
|
Executed in the same directory with the new certificate.
|
2015-12-08 18:09:19 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-01-19 18:24:04 +00:00
|
|
|
keyType = mkOption {
|
|
|
|
type = types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "keyType" "ec256") default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-01-19 18:24:04 +00:00
|
|
|
Key type to use for private keys.
|
|
|
|
For an up to date list of supported values check the --key-type option
|
2022-07-28 22:19:15 +01:00
|
|
|
at <https://go-acme.github.io/lego/usage/cli/#usage>.
|
2020-01-19 18:24:04 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-01-12 21:05:57 +00:00
|
|
|
dnsProvider = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "dnsProvider" null) default defaultText;
|
2020-01-15 09:17:11 +00:00
|
|
|
example = "route53";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-01-19 18:24:04 +00:00
|
|
|
DNS Challenge provider. For a list of supported providers, see the "code"
|
2022-07-28 22:19:15 +01:00
|
|
|
field of the DNS providers listed at <https://go-acme.github.io/lego/dns/>.
|
2020-01-19 18:24:04 +00:00
|
|
|
'';
|
2020-01-12 21:05:57 +00:00
|
|
|
};
|
|
|
|
|
2020-10-07 12:01:08 +01:00
|
|
|
dnsResolver = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "dnsResolver" null) default defaultText;
|
2020-10-07 12:01:08 +01:00
|
|
|
example = "1.1.1.1:53";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-10-07 12:01:08 +01:00
|
|
|
Set the resolver to use for performing recursive DNS queries. Supported:
|
|
|
|
host:port. The default is to use the system resolvers, or Google's DNS
|
|
|
|
resolvers if the system's cannot be determined.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-01-12 21:05:57 +00:00
|
|
|
credentialsFile = mkOption {
|
2020-01-19 18:24:04 +00:00
|
|
|
type = types.path;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "credentialsFile" null) default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-01-19 18:24:04 +00:00
|
|
|
Path to an EnvironmentFile for the cert's service containing any required and
|
|
|
|
optional environment variables for your selected dnsProvider.
|
|
|
|
To find out what values you need to set, consult the documentation at
|
2022-07-28 22:19:15 +01:00
|
|
|
<https://go-acme.github.io/lego/dns/> for the corresponding dnsProvider.
|
2020-01-12 21:05:57 +00:00
|
|
|
'';
|
|
|
|
example = "/var/src/secrets/example.org-route53-api-token";
|
|
|
|
};
|
|
|
|
|
|
|
|
dnsPropagationCheck = mkOption {
|
|
|
|
type = types.bool;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "dnsPropagationCheck" true) default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-01-19 18:24:04 +00:00
|
|
|
Toggles lego DNS propagation check, which is used alongside DNS-01
|
|
|
|
challenge to ensure the DNS entries required are available.
|
2020-01-12 21:05:57 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-02-23 01:51:19 +00:00
|
|
|
|
|
|
|
ocspMustStaple = mkOption {
|
|
|
|
type = types.bool;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "ocspMustStaple" false) default defaultText;
|
2022-08-29 22:34:22 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-02-23 01:51:19 +00:00
|
|
|
Turns on the OCSP Must-Staple TLS extension.
|
|
|
|
Make sure you know what you're doing! See:
|
2022-08-29 22:34:22 +01:00
|
|
|
|
|
|
|
- <https://blog.apnic.net/2019/01/15/is-the-web-ready-for-ocsp-must-staple/>
|
|
|
|
- <https://blog.hboeck.de/archives/886-The-Problem-with-OCSP-Stapling-and-Must-Staple-and-why-Certificate-Revocation-is-still-broken.html>
|
2020-02-23 01:51:19 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-02-23 02:02:44 +00:00
|
|
|
|
2020-06-08 01:17:55 +01:00
|
|
|
extraLegoFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "extraLegoFlags" []) default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-06-08 01:17:55 +01:00
|
|
|
Additional global flags to pass to all lego commands.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-02-23 02:02:44 +00:00
|
|
|
extraLegoRenewFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "extraLegoRenewFlags" []) default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-02-23 02:02:44 +00:00
|
|
|
Additional flags to pass to lego renew.
|
|
|
|
'';
|
|
|
|
};
|
2020-06-08 01:18:31 +01:00
|
|
|
|
|
|
|
extraLegoRunFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-12-09 21:43:54 +00:00
|
|
|
inherit (defaultAndText "extraLegoRunFlags" []) default defaultText;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-06-08 01:18:31 +01:00
|
|
|
Additional flags to pass to lego run.
|
|
|
|
'';
|
|
|
|
};
|
2015-12-06 15:55:09 +00:00
|
|
|
};
|
2021-12-09 21:43:54 +00:00
|
|
|
};
|
2019-10-25 23:40:51 +01:00
|
|
|
|
2021-12-04 18:09:43 +00:00
|
|
|
certOpts = { name, config, ... }: {
|
2021-12-09 21:43:54 +00:00
|
|
|
options = {
|
2021-11-28 17:03:31 +00:00
|
|
|
# user option has been removed
|
|
|
|
user = mkOption {
|
|
|
|
visible = false;
|
|
|
|
default = "_mkRemovedOptionModule";
|
|
|
|
};
|
2015-12-06 15:55:09 +00:00
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
# allowKeysForGroup option has been removed
|
|
|
|
allowKeysForGroup = mkOption {
|
|
|
|
visible = false;
|
|
|
|
default = "_mkRemovedOptionModule";
|
|
|
|
};
|
2021-11-26 12:58:40 +00:00
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
# extraDomains was replaced with extraDomainNames
|
|
|
|
extraDomains = mkOption {
|
|
|
|
visible = false;
|
|
|
|
default = "_mkMergedOptionModule";
|
2020-01-12 21:05:57 +00:00
|
|
|
};
|
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
directory = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
readOnly = true;
|
|
|
|
default = "/var/lib/acme/${name}";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Directory where certificate and other state is stored.";
|
2015-12-12 13:21:44 +00:00
|
|
|
};
|
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
domain = mkOption {
|
2015-12-12 13:21:44 +00:00
|
|
|
type = types.str;
|
2021-11-28 17:03:31 +00:00
|
|
|
default = name;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Domain to fetch certificate for (defaults to the entry name).";
|
2021-11-28 17:03:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraDomainNames = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = literalExpression ''
|
|
|
|
[
|
|
|
|
"example.org"
|
|
|
|
"mydomain.org"
|
|
|
|
]
|
|
|
|
'';
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-11-28 17:03:31 +00:00
|
|
|
A list of extra domain names, which are included in the one certificate to be issued.
|
2015-12-12 13:21:44 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
# This setting must be different for each configured certificate, otherwise
|
|
|
|
# two or more renewals may fail to bind to the address. Hence, it is not in
|
|
|
|
# the inheritableOpts.
|
|
|
|
listenHTTP = mkOption {
|
2019-10-25 23:40:51 +01:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2021-11-28 17:03:31 +00:00
|
|
|
example = ":1360";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-11-28 17:03:31 +00:00
|
|
|
Interface and port to listen on to solve HTTP challenges
|
|
|
|
in the form [INTERFACE]:PORT.
|
|
|
|
If you use a port other than 80, you must proxy port 80 to this port.
|
2019-10-25 23:40:51 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
inheritDefaults = mkOption {
|
|
|
|
default = true;
|
|
|
|
example = true;
|
2022-08-14 08:03:30 +01:00
|
|
|
description = lib.mdDoc "Whether to inherit values set in `security.acme.defaults` or not.";
|
2021-11-28 17:03:31 +00:00
|
|
|
type = lib.types.bool;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
security.acme = {
|
2016-06-01 11:39:46 +01:00
|
|
|
preliminarySelfsigned = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-06-01 11:39:46 +01:00
|
|
|
Whether a preliminary self-signed certificate should be generated before
|
|
|
|
doing ACME requests. This can be useful when certificates are required in
|
|
|
|
a webserver, but ACME needs the webserver to make its requests.
|
|
|
|
|
|
|
|
With preliminary self-signed certificate the webserver can be started and
|
|
|
|
can later reload the correct ACME certificates.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-01-12 21:05:57 +00:00
|
|
|
acceptTerms = mkOption {
|
|
|
|
type = types.bool;
|
2020-01-19 18:24:04 +00:00
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-04-21 14:39:46 +01:00
|
|
|
Accept the CA's terms of service. The default provider is Let's Encrypt,
|
2022-07-28 22:19:15 +01:00
|
|
|
you can find their ToS at <https://letsencrypt.org/repository/>.
|
2020-01-12 21:05:57 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-12-04 19:01:18 +00:00
|
|
|
useRoot = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-12-04 19:01:18 +00:00
|
|
|
Whether to use the root user when generating certs. This is not recommended
|
|
|
|
for security + compatiblity reasons. If a service requires root owned certificates
|
|
|
|
consider following the guide on "Using ACME with services demanding root
|
|
|
|
owned certificates" in the NixOS manual, and only using this as a fallback
|
|
|
|
or for testing.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-11-28 17:03:31 +00:00
|
|
|
defaults = mkOption {
|
2021-12-09 21:43:54 +00:00
|
|
|
type = types.submodule (inheritableModule true);
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-11-28 17:03:31 +00:00
|
|
|
Default values inheritable by all configured certs. You can
|
|
|
|
use this to define options shared by all your certs. These defaults
|
|
|
|
can also be ignored on a per-cert basis using the
|
|
|
|
`security.acme.certs.''${cert}.inheritDefaults' option.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-06 15:55:09 +00:00
|
|
|
certs = mkOption {
|
|
|
|
default = { };
|
2021-12-09 21:43:54 +00:00
|
|
|
type = with types; attrsOf (submodule [ (inheritableModule false) certOpts ]);
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-08-29 15:32:59 +01:00
|
|
|
Attribute set of certificates to get signed and renewed. Creates
|
2022-07-28 22:19:15 +01:00
|
|
|
`acme-''${cert}.{service,timer}` systemd units for
|
2019-08-29 15:32:59 +01:00
|
|
|
each certificate defined here. Other services can add dependencies
|
|
|
|
to those units if they rely on the certificates being present,
|
|
|
|
or trigger restarts of the service if certificates get renewed.
|
2015-12-06 15:55:09 +00:00
|
|
|
'';
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''
|
2017-06-08 07:46:40 +01:00
|
|
|
{
|
|
|
|
"example.com" = {
|
2021-01-08 23:33:40 +00:00
|
|
|
webroot = "/var/lib/acme/acme-challenge/";
|
2017-06-08 07:46:40 +01:00
|
|
|
email = "foo@example.com";
|
2020-06-19 20:27:46 +01:00
|
|
|
extraDomainNames = [ "www.example.com" "foo.example.com" ];
|
2017-06-08 07:46:40 +01:00
|
|
|
};
|
|
|
|
"bar.example.com" = {
|
2021-01-08 23:33:40 +00:00
|
|
|
webroot = "/var/lib/acme/acme-challenge/";
|
2017-06-08 07:46:40 +01:00
|
|
|
email = "bar@example.com";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
2015-12-06 15:55:09 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-06-19 20:27:46 +01:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "security" "acme" "production" ] ''
|
|
|
|
Use security.acme.server to define your staging ACME server URL instead.
|
|
|
|
|
|
|
|
To use the let's encrypt staging server, use security.acme.server =
|
|
|
|
"https://acme-staging-v02.api.letsencrypt.org/directory".
|
2021-11-28 17:03:31 +00:00
|
|
|
'')
|
2020-06-19 20:27:46 +01:00
|
|
|
(mkRemovedOptionModule [ "security" "acme" "directory" ] "ACME Directory is now hardcoded to /var/lib/acme and its permisisons are managed by systemd. See https://github.com/NixOS/nixpkgs/issues/53852 for more info.")
|
|
|
|
(mkRemovedOptionModule [ "security" "acme" "preDelay" ] "This option has been removed. If you want to make sure that something executes before certificates are provisioned, add a RequiredBy=acme-\${cert}.service to the service you want to execute before the cert renewal")
|
|
|
|
(mkRemovedOptionModule [ "security" "acme" "activationDelay" ] "This option has been removed. If you want to make sure that something executes before certificates are provisioned, add a RequiredBy=acme-\${cert}.service to the service you want to execute before the cert renewal")
|
2021-11-28 17:03:31 +00:00
|
|
|
(mkChangedOptionModule [ "security" "acme" "validMin" ] [ "security" "acme" "defaults" "validMinDays" ] (config: config.security.acme.validMin / (24 * 3600)))
|
|
|
|
(mkChangedOptionModule [ "security" "acme" "validMinDays" ] [ "security" "acme" "defaults" "validMinDays" ] (config: config.security.acme.validMinDays))
|
|
|
|
(mkChangedOptionModule [ "security" "acme" "renewInterval" ] [ "security" "acme" "defaults" "renewInterval" ] (config: config.security.acme.renewInterval))
|
|
|
|
(mkChangedOptionModule [ "security" "acme" "email" ] [ "security" "acme" "defaults" "email" ] (config: config.security.acme.email))
|
|
|
|
(mkChangedOptionModule [ "security" "acme" "server" ] [ "security" "acme" "defaults" "server" ] (config: config.security.acme.server))
|
|
|
|
(mkChangedOptionModule [ "security" "acme" "enableDebugLogs" ] [ "security" "acme" "defaults" "enableDebugLogs" ] (config: config.security.acme.enableDebugLogs))
|
2020-06-19 20:27:46 +01:00
|
|
|
];
|
|
|
|
|
2015-12-12 15:06:24 +00:00
|
|
|
config = mkMerge [
|
|
|
|
(mkIf (cfg.certs != { }) {
|
|
|
|
|
2020-06-19 20:27:46 +01:00
|
|
|
# FIXME Most of these custom warnings and filters for security.acme.certs.* are required
|
|
|
|
# because using mkRemovedOptionModule/mkChangedOptionModule with attrsets isn't possible.
|
|
|
|
warnings = filter (w: w != "") (mapAttrsToList (cert: data: if data.extraDomains != "_mkMergedOptionModule" then ''
|
|
|
|
The option definition `security.acme.certs.${cert}.extraDomains` has changed
|
|
|
|
to `security.acme.certs.${cert}.extraDomainNames` and is now a list of strings.
|
|
|
|
Setting a custom webroot for extra domains is not possible, instead use separate certs.
|
|
|
|
'' else "") cfg.certs);
|
|
|
|
|
2020-01-12 21:05:57 +00:00
|
|
|
assertions = let
|
2020-06-19 20:27:46 +01:00
|
|
|
certs = attrValues cfg.certs;
|
2020-01-12 21:05:57 +00:00
|
|
|
in [
|
|
|
|
{
|
|
|
|
assertion = cfg.email != null || all (certOpts: certOpts.email != null) certs;
|
|
|
|
message = ''
|
|
|
|
You must define `security.acme.certs.<name>.email` or
|
2020-06-19 20:27:46 +01:00
|
|
|
`security.acme.email` to register with the CA. Note that using
|
|
|
|
many different addresses for certs may trigger account rate limits.
|
2020-01-12 21:05:57 +00:00
|
|
|
'';
|
|
|
|
}
|
2020-01-19 18:24:04 +00:00
|
|
|
{
|
|
|
|
assertion = cfg.acceptTerms;
|
|
|
|
message = ''
|
|
|
|
You must accept the CA's terms of service before using
|
|
|
|
the ACME module by setting `security.acme.acceptTerms`
|
|
|
|
to `true`. For Let's Encrypt's ToS see https://letsencrypt.org/repository/
|
|
|
|
'';
|
|
|
|
}
|
2020-06-19 20:27:46 +01:00
|
|
|
] ++ (builtins.concatLists (mapAttrsToList (cert: data: [
|
|
|
|
{
|
|
|
|
assertion = data.user == "_mkRemovedOptionModule";
|
|
|
|
message = ''
|
|
|
|
The option definition `security.acme.certs.${cert}.user' no longer has any effect; Please remove it.
|
|
|
|
Certificate user is now hard coded to the "acme" user. If you would
|
|
|
|
like another user to have access, consider adding them to the
|
|
|
|
"acme" group or changing security.acme.certs.${cert}.group.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = data.allowKeysForGroup == "_mkRemovedOptionModule";
|
|
|
|
message = ''
|
|
|
|
The option definition `security.acme.certs.${cert}.allowKeysForGroup' no longer has any effect; Please remove it.
|
|
|
|
All certs are readable by the configured group. If this is undesired,
|
|
|
|
consider changing security.acme.certs.${cert}.group to an unused group.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
# * in the cert value breaks building of systemd services, and makes
|
|
|
|
# referencing them as a user quite weird too. Best practice is to use
|
|
|
|
# the domain option.
|
|
|
|
{
|
|
|
|
assertion = ! hasInfix "*" cert;
|
|
|
|
message = ''
|
|
|
|
The cert option path `security.acme.certs.${cert}.dnsProvider`
|
|
|
|
cannot contain a * character.
|
|
|
|
Instead, set `security.acme.certs.${cert}.domain = "${cert}";`
|
|
|
|
and remove the wildcard from the path.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = data.dnsProvider == null || data.webroot == null;
|
|
|
|
message = ''
|
|
|
|
Options `security.acme.certs.${cert}.dnsProvider` and
|
|
|
|
`security.acme.certs.${cert}.webroot` are mutually exclusive.
|
|
|
|
'';
|
|
|
|
}
|
2021-05-21 09:07:24 +01:00
|
|
|
{
|
|
|
|
assertion = data.webroot == null || data.listenHTTP == null;
|
|
|
|
message = ''
|
|
|
|
Options `security.acme.certs.${cert}.webroot` and
|
|
|
|
`security.acme.certs.${cert}.listenHTTP` are mutually exclusive.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = data.listenHTTP == null || data.dnsProvider == null;
|
|
|
|
message = ''
|
|
|
|
Options `security.acme.certs.${cert}.listenHTTP` and
|
|
|
|
`security.acme.certs.${cert}.dnsProvider` are mutually exclusive.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = data.dnsProvider != null || data.webroot != null || data.listenHTTP != null;
|
|
|
|
message = ''
|
|
|
|
One of `security.acme.certs.${cert}.dnsProvider`,
|
|
|
|
`security.acme.certs.${cert}.webroot`, or
|
|
|
|
`security.acme.certs.${cert}.listenHTTP` must be provided.
|
|
|
|
'';
|
|
|
|
}
|
2020-06-19 20:27:46 +01:00
|
|
|
]) cfg.certs));
|
2015-12-12 15:06:24 +00:00
|
|
|
|
2020-06-19 20:27:46 +01:00
|
|
|
users.users.acme = {
|
|
|
|
home = "/var/lib/acme";
|
|
|
|
group = "acme";
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups.acme = {};
|
|
|
|
|
|
|
|
systemd.services = {
|
|
|
|
"acme-fixperms" = userMigrationService;
|
|
|
|
} // (mapAttrs' (cert: conf: nameValuePair "acme-${cert}" conf.renewService) certConfigs)
|
|
|
|
// (optionalAttrs (cfg.preliminarySelfsigned) ({
|
|
|
|
"acme-selfsigned-ca" = selfsignCAService;
|
|
|
|
} // (mapAttrs' (cert: conf: nameValuePair "acme-selfsigned-${cert}" conf.selfsignService) certConfigs)));
|
|
|
|
|
|
|
|
systemd.timers = mapAttrs' (cert: conf: nameValuePair "acme-${cert}" conf.renewTimer) certConfigs;
|
|
|
|
|
2020-12-13 20:22:33 +00:00
|
|
|
systemd.targets = let
|
|
|
|
# Create some targets which can be depended on to be "active" after cert renewals
|
|
|
|
finishedTargets = mapAttrs' (cert: conf: nameValuePair "acme-finished-${cert}" {
|
|
|
|
wantedBy = [ "default.target" ];
|
2021-11-26 21:39:06 +00:00
|
|
|
requires = [ "acme-${cert}.service" ];
|
|
|
|
after = [ "acme-${cert}.service" ];
|
2020-12-13 20:22:33 +00:00
|
|
|
}) certConfigs;
|
|
|
|
|
|
|
|
# Create targets to limit the number of simultaneous account creations
|
2021-01-09 19:15:03 +00:00
|
|
|
# How it works:
|
|
|
|
# - Pick a "leader" cert service, which will be in charge of creating the account,
|
|
|
|
# and run first (requires + after)
|
|
|
|
# - Make all other cert services sharing the same account wait for the leader to
|
|
|
|
# finish before starting (requiredBy + before).
|
|
|
|
# Using a target here is fine - account creation is a one time event. Even if
|
|
|
|
# systemd clean --what=state is used to delete the account, so long as the user
|
|
|
|
# then runs one of the cert services, there won't be any issues.
|
2020-12-13 20:22:33 +00:00
|
|
|
accountTargets = mapAttrs' (hash: confs: let
|
|
|
|
leader = "acme-${(builtins.head confs).cert}.service";
|
|
|
|
dependantServices = map (conf: "acme-${conf.cert}.service") (builtins.tail confs);
|
|
|
|
in nameValuePair "acme-account-${hash}" {
|
|
|
|
requiredBy = dependantServices;
|
|
|
|
before = dependantServices;
|
|
|
|
requires = [ leader ];
|
|
|
|
after = [ leader ];
|
|
|
|
}) (groupBy (conf: conf.accountHash) (attrValues certConfigs));
|
|
|
|
in finishedTargets // accountTargets;
|
2020-06-19 20:27:46 +01:00
|
|
|
})
|
2015-12-12 15:06:24 +00:00
|
|
|
];
|
2015-12-06 15:55:09 +00:00
|
|
|
|
2016-05-09 06:53:27 +01:00
|
|
|
meta = {
|
2020-04-20 01:36:13 +01:00
|
|
|
maintainers = lib.teams.acme.members;
|
2022-01-08 20:05:34 +00:00
|
|
|
doc = ./doc.xml;
|
2016-05-09 06:53:27 +01:00
|
|
|
};
|
2015-12-06 15:55:09 +00:00
|
|
|
}
|