1 { lib, config, pkgs, ... }:
3 inherit (lib) escapeShellArg stringAfter;
4 mkActvationScript = name: cert-cfg:
6 pem-path = "${cert-cfg.dir}/${name}.pem";
7 key-path = "${cert-cfg.dir}/${name}.key";
9 name = "make-cert-${name}";
10 value = stringAfter [ "users" ] (''
11 if [[ ! -e ${escapeShellArg pem-path} ]];then
12 ${pkgs.coreutils}/bin/mkdir -p ${escapeShellArg cert-cfg.dir}
13 ${pkgs.openssl}/bin/openssl req -batch -x509 -newkey rsa:4096 \
14 -keyout ${escapeShellArg key-path} \
15 -out ${escapeShellArg pem-path} \
16 -days ${escapeShellArg cert-cfg.lifetime} \
18 ${pkgs.coreutils}/bin/chown ${escapeShellArg cert-cfg.user} ${
19 escapeShellArg key-path
22 '' + lib.optionalString cert-cfg.print ''
23 echo Public certificate for ${escapeShellArg name}: >&2
24 ${pkgs.coreutils}/bin/cat ${escapeShellArg pem-path} >&2
29 chkno.make-certs = lib.mkOption {
30 description = "Certificates to generate.";
32 send-email.user = "stunnel";
33 send-print.user = "stunnel";
35 type = lib.types.attrsOf (lib.types.submodule {
39 description = "Where to put the certificate and key.";
42 lifetime = lib.mkOption {
44 description = "Lifetime of the generated certificate (in days).";
45 # This doesn't yet include any notion of certificate rotation,
46 # so just make really long-lived certificates for now.
49 print = lib.mkOption {
50 type = lib.types.bool;
51 description = "If set, print the certificate (public key) during activation.";
56 description = "The username that owns (can read) the secret key.";
63 system.activationScripts =
64 lib.mapAttrs' mkActvationScript config.chkno.make-certs;