2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.certmgr;
|
|
|
|
|
|
|
|
specs = mapAttrsToList (n: v: rec {
|
|
|
|
name = n + ".json";
|
|
|
|
path = if isAttrs v then pkgs.writeText name (builtins.toJSON v) else v;
|
|
|
|
}) cfg.specs;
|
|
|
|
|
|
|
|
allSpecs = pkgs.linkFarm "certmgr.d" specs;
|
|
|
|
|
|
|
|
certmgrYaml = pkgs.writeText "certmgr.yaml" (builtins.toJSON {
|
|
|
|
dir = allSpecs;
|
|
|
|
default_remote = cfg.defaultRemote;
|
|
|
|
svcmgr = cfg.svcManager;
|
|
|
|
before = cfg.validMin;
|
|
|
|
interval = cfg.renewInterval;
|
|
|
|
inherit (cfg) metricsPort metricsAddress;
|
|
|
|
});
|
|
|
|
|
|
|
|
specPaths = map dirOf (concatMap (spec:
|
|
|
|
if isAttrs spec then
|
|
|
|
collect isString (filterAttrsRecursive (n: v: isAttrs v || n == "path") spec)
|
|
|
|
else
|
|
|
|
[ spec ]
|
|
|
|
) (attrValues cfg.specs));
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
${concatStringsSep " \\\n" (["mkdir -p"] ++ map escapeShellArg specPaths)}
|
|
|
|
${cfg.package}/bin/certmgr -f ${certmgrYaml} check
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.certmgr = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "certmgr");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = mkPackageOption pkgs "certmgr" { };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
defaultRemote = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1:8888";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The default CA host:port to use.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
validMin = mkOption {
|
|
|
|
default = "72h";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The interval before a certificate expires to start attempting to renew it.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
renewInterval = mkOption {
|
|
|
|
default = "30m";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "How often to check certificate expirations and how often to update the cert_next_expires metric.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
metricsAddress = mkOption {
|
|
|
|
default = "127.0.0.1";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The address for the Prometheus HTTP endpoint.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
metricsPort = mkOption {
|
|
|
|
default = 9488;
|
|
|
|
type = types.ints.u16;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The port for the Prometheus HTTP endpoint.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
specs = mkOption {
|
|
|
|
default = {};
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
{
|
|
|
|
exampleCert =
|
|
|
|
let
|
|
|
|
domain = "example.com";
|
|
|
|
secret = name: "/var/lib/secrets/''${name}.pem";
|
|
|
|
in {
|
|
|
|
service = "nginx";
|
|
|
|
action = "reload";
|
|
|
|
authority = {
|
|
|
|
file.path = secret "ca";
|
|
|
|
};
|
|
|
|
certificate = {
|
|
|
|
path = secret domain;
|
|
|
|
};
|
|
|
|
private_key = {
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
mode = "0600";
|
|
|
|
path = secret "''${domain}-key";
|
|
|
|
};
|
|
|
|
request = {
|
|
|
|
CN = domain;
|
|
|
|
hosts = [ "mail.''${domain}" "www.''${domain}" ];
|
|
|
|
key = {
|
|
|
|
algo = "rsa";
|
|
|
|
size = 2048;
|
|
|
|
};
|
|
|
|
names = {
|
|
|
|
O = "Example Organization";
|
|
|
|
C = "USA";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
otherCert = "/var/certmgr/specs/other-cert.json";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
type = with types; attrsOf (either path (submodule {
|
|
|
|
options = {
|
|
|
|
service = mkOption {
|
|
|
|
type = nullOr str;
|
|
|
|
default = null;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc "The service on which to perform \<action\> after fetching.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
action = mkOption {
|
|
|
|
type = addCheck str (x: cfg.svcManager == "command" || elem x ["restart" "reload" "nop"]);
|
|
|
|
default = "nop";
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc "The action to take after fetching.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# These ought all to be specified according to certmgr spec def.
|
|
|
|
authority = mkOption {
|
|
|
|
type = attrs;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc "certmgr spec authority object.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
certificate = mkOption {
|
|
|
|
type = nullOr attrs;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc "certmgr spec certificate object.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private_key = mkOption {
|
|
|
|
type = nullOr attrs;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc "certmgr spec private_key object.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
request = mkOption {
|
|
|
|
type = nullOr attrs;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc "certmgr spec request object.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Certificate specs as described by:
|
2022-08-12 12:06:08 +00:00
|
|
|
<https://github.com/cloudflare/certmgr#certificate-specs>
|
2020-04-24 23:36:52 +00:00
|
|
|
These will be added to the Nix store, so they will be world readable.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
svcManager = mkOption {
|
|
|
|
default = "systemd";
|
|
|
|
type = types.enum [ "circus" "command" "dummy" "openrc" "systemd" "sysv" ];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
This specifies the service manager to use for restarting or reloading services.
|
2022-08-12 12:06:08 +00:00
|
|
|
See: <https://github.com/cloudflare/certmgr#certmgryaml>.
|
2020-04-24 23:36:52 +00:00
|
|
|
For how to use the "command" service manager in particular,
|
2022-08-12 12:06:08 +00:00
|
|
|
see: <https://github.com/cloudflare/certmgr#command-svcmgr-and-how-to-use-it>.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.specs != {};
|
|
|
|
message = "Certmgr specs cannot be empty.";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = !any (hasAttrByPath [ "authority" "auth_key" ]) (attrValues cfg.specs);
|
|
|
|
message = ''
|
|
|
|
Inline services.certmgr.specs are added to the Nix store rendering them world readable.
|
|
|
|
Specify paths as specs, if you want to use include auth_key - or use the auth_key_file option."
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.certmgr = {
|
|
|
|
description = "certmgr";
|
|
|
|
path = mkIf (cfg.svcManager == "command") [ pkgs.bash ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
inherit preStart;
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "10s";
|
|
|
|
ExecStart = "${cfg.package}/bin/certmgr -f ${certmgrYaml}";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|