1 { config, lib, pkgs, ... }:
6 cfg = config.services.certmgr;
8 specs = mapAttrsToList (n: v: rec {
10 path = if isAttrs v then pkgs.writeText name (builtins.toJSON v) else v;
13 allSpecs = pkgs.linkFarm "certmgr.d" specs;
15 certmgrYaml = pkgs.writeText "certmgr.yaml" (builtins.toJSON {
17 default_remote = cfg.defaultRemote;
18 svcmgr = cfg.svcManager;
19 before = cfg.validMin;
20 interval = cfg.renewInterval;
21 inherit (cfg) metricsPort metricsAddress;
24 specPaths = map dirOf (concatMap (spec:
26 collect isString (filterAttrsRecursive (n: v: isAttrs v || n == "path") spec)
29 ) (attrValues cfg.specs));
32 ${concatStringsSep " \\\n" (["mkdir -p"] ++ map escapeShellArg specPaths)}
33 ${cfg.package}/bin/certmgr -f ${certmgrYaml} check
37 options.services.certmgr = {
38 enable = mkEnableOption "certmgr";
40 package = mkPackageOption pkgs "certmgr" { };
42 defaultRemote = mkOption {
44 default = "127.0.0.1:8888";
45 description = "The default CA host:port to use.";
51 description = "The interval before a certificate expires to start attempting to renew it.";
54 renewInterval = mkOption {
57 description = "How often to check certificate expirations and how often to update the cert_next_expires metric.";
60 metricsAddress = mkOption {
61 default = "127.0.0.1";
63 description = "The address for the Prometheus HTTP endpoint.";
66 metricsPort = mkOption {
68 type = types.ints.u16;
69 description = "The port for the Prometheus HTTP endpoint.";
74 example = literalExpression ''
78 domain = "example.com";
79 secret = name: "/var/lib/secrets/''${name}.pem";
84 file.path = secret "ca";
93 path = secret "''${domain}-key";
97 hosts = [ "mail.''${domain}" "www.''${domain}" ];
103 O = "Example Organization";
108 otherCert = "/var/certmgr/specs/other-cert.json";
111 type = with types; attrsOf (either path (submodule {
116 description = "The service on which to perform \<action\> after fetching.";
120 type = addCheck str (x: cfg.svcManager == "command" || elem x ["restart" "reload" "nop"]);
122 description = "The action to take after fetching.";
125 # These ought all to be specified according to certmgr spec def.
126 authority = mkOption {
128 description = "certmgr spec authority object.";
131 certificate = mkOption {
133 description = "certmgr spec certificate object.";
136 private_key = mkOption {
138 description = "certmgr spec private_key object.";
143 description = "certmgr spec request object.";
148 Certificate specs as described by:
149 <https://github.com/cloudflare/certmgr#certificate-specs>
150 These will be added to the Nix store, so they will be world readable.
154 svcManager = mkOption {
156 type = types.enum [ "circus" "command" "dummy" "openrc" "systemd" "sysv" ];
158 This specifies the service manager to use for restarting or reloading services.
159 See: <https://github.com/cloudflare/certmgr#certmgryaml>.
160 For how to use the "command" service manager in particular,
161 see: <https://github.com/cloudflare/certmgr#command-svcmgr-and-how-to-use-it>.
167 config = mkIf cfg.enable {
170 assertion = cfg.specs != {};
171 message = "Certmgr specs cannot be empty.";
174 assertion = !any (hasAttrByPath [ "authority" "auth_key" ]) (attrValues cfg.specs);
176 Inline services.certmgr.specs are added to the Nix store rendering them world readable.
177 Specify paths as specs, if you want to use include auth_key - or use the auth_key_file option."
182 systemd.services.certmgr = {
183 description = "certmgr";
184 path = mkIf (cfg.svcManager == "command") [ pkgs.bash ];
185 wants = [ "network-online.target" ];
186 after = [ "network-online.target" ];
187 wantedBy = [ "multi-user.target" ];
193 ExecStart = "${cfg.package}/bin/certmgr -f ${certmgrYaml}";