@@ -0,0 +1,202 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+
7+ cfg = config.security.acme;
8+
9+ certOpts = { ... }: {
10+ options = {
11+ webroot = mkOption {
12+ type = types.str;
13+ description = ''
14+ Where the webroot of the HTTP vhost is located.
15+ <filename>.well-known/acme-challenge/</filename> directory
16+ will be created automatically if it doesn't exist.
17+ <literal>http://example.org/.well-known/acme-challenge/</literal> must also
18+ be available (notice unencrypted HTTP).
19+ '';
20+ };
21+
22+ email = mkOption {
23+ type = types.nullOr types.str;
24+ default = null;
25+ description = "Contact email address for the CA to be able to reach you.";
26+ };
27+
28+ user = mkOption {
29+ type = types.str;
30+ default = "root";
31+ description = "User running the ACME client.";
32+ };
33+
34+ group = mkOption {
35+ type = types.str;
36+ default = "root";
37+ description = "Group running the ACME client.";
38+ };
39+
40+ postRun = mkOption {
41+ type = types.lines;
42+ default = "";
43+ example = "systemctl reload nginx.service";
44+ description = ''
45+ Commands to run after certificates are re-issued. Typically
46+ the web server and other servers using certificates need to
47+ be reloaded.
48+ '';
49+ };
50+
51+ plugins = mkOption {
52+ type = types.listOf (types.enum [
53+ "cert.der" "cert.pem" "chain.der" "chain.pem" "external_pem.sh"
54+ "fullchain.der" "fullchain.pem" "key.der" "key.pem" "account_key.json"
55+ ]);
56+ default = [ "fullchain.pem" "key.pem" "account_key.json" ];
57+ description = ''
58+ Plugins to enable. With default settings simp_le will
59+ store public certificate bundle in <filename>fullchain.pem</filename>
60+ and private key in <filename>key.pem</filename> in its state directory.
61+ '';
62+ };
63+
64+ extraDomains = mkOption {
65+ type = types.attrsOf (types.nullOr types.str);
66+ default = {};
67+ example = {
68+ "example.org" = "/srv/http/nginx";
69+ "mydomain.org" = null;
70+ };
71+ description = ''
72+ Extra domain names for which certificates are to be issued, with their
73+ own server roots if needed.
74+ '';
75+ };
76+ };
77+ };
78+
79+in
80+
81+{
82+
83+ ###### interface
84+
85+ options = {
86+ security.acme = {
87+ directory = mkOption {
88+ default = "/var/lib/acme";
89+ type = types.str;
90+ description = ''
91+ Directory where certs and other state will be stored by default.
92+ '';
93+ };
94+
95+ validMin = mkOption {
96+ type = types.int;
97+ default = 30 * 24 * 3600;
98+ description = "Minimum remaining validity before renewal in seconds.";
99+ };
100+
101+ renewInterval = mkOption {
102+ type = types.str;
103+ default = "weekly";
104+ description = ''
105+ Systemd calendar expression when to check for renewal. See
106+ <citerefentry><refentrytitle>systemd.time</refentrytitle>
107+ <manvolnum>5</manvolnum></citerefentry>.
108+ '';
109+ };
110+
111+ certs = mkOption {
112+ default = { };
113+ type = types.loaOf types.optionSet;
114+ description = ''
115+ Attribute set of certificates to get signed and renewed.
116+ '';
117+ options = [ certOpts ];
118+ example = {
119+ "example.com" = {
120+ webroot = "/var/www/challenges/";
121+ email = "foo@example.com";
122+ extraDomains = { "www.example.com" = null; "foo.example.com" = "/var/www/foo/"; };
123+ };
124+ "bar.example.com" = {
125+ webroot = "/var/www/challenges/";
126+ email = "bar@example.com";
127+ };
128+ };
129+ };
130+ };
131+ };
132+
133+ ###### implementation
134+ config = mkMerge [
135+ (mkIf (cfg.certs != { }) {
136+
137+ systemd.services = flip mapAttrs' cfg.certs (cert: data:
138+ let
139+ cpath = "${cfg.directory}/${cert}";
140+ cmdline = [ "-v" "-d" cert "--default_root" data.webroot "--valid_min" cfg.validMin ]
141+ ++ optionals (data.email != null) [ "--email" data.email ]
142+ ++ concatMap (p: [ "-f" p ]) data.plugins
143+ ++ concatLists (mapAttrsToList (name: root: [ "-d" (if root == null then name else "${name}:${root}")]) data.extraDomains);
144+
145+ in nameValuePair
146+ ("acme-${cert}")
147+ ({
148+ description = "ACME cert renewal for ${cert} using simp_le";
149+ after = [ "network.target" ];
150+ serviceConfig = {
151+ Type = "oneshot";
152+ SuccessExitStatus = [ "0" "1" ];
153+ PermissionsStartOnly = true;
154+ User = data.user;
155+ Group = data.group;
156+ PrivateTmp = true;
157+ };
158+ path = [ pkgs.simp_le ];
159+ preStart = ''
160+ mkdir -p '${cfg.directory}'
161+ if [ ! -d '${cpath}' ]; then
162+ mkdir -m 700 '${cpath}'
163+ chown '${data.user}:${data.group}' '${cpath}'
164+ fi
165+ '';
166+ script = ''
167+ cd '${cpath}'
168+ set +e
169+ simp_le ${concatMapStringsSep " " (arg: escapeShellArg (toString arg)) cmdline}
170+ EXITCODE=$?
171+ set -e
172+ echo "$EXITCODE" > /tmp/lastExitCode
173+ exit "$EXITCODE"
174+ '';
175+ postStop = ''
176+ if [ -e /tmp/lastExitCode ] && [ "$(cat /tmp/lastExitCode)" = "0" ]; then
177+ echo "Executing postRun hook..."
178+ ${data.postRun}
179+ fi
180+ '';
181+ })
182+ );
183+
184+ systemd.timers = flip mapAttrs' cfg.certs (cert: data: nameValuePair
185+ ("acme-${cert}")
186+ ({
187+ description = "timer for ACME cert renewal of ${cert}";
188+ wantedBy = [ "timers.target" ];
189+ timerConfig = {
190+ OnCalendar = cfg.renewInterval;
191+ Unit = "acme-${cert}.service";
192+ };
193+ })
194+ );
195+ })
196+
197+ { meta.maintainers = with lib.maintainers; [ abbradar fpletz globin ];
198+ meta.doc = ./acme.xml;
199+ }
200+ ];
201+
202+}