aboutsummaryrefslogtreecommitdiff
path: root/modules/private/certificates.nix
blob: 43f6a2343afafeba86880779f6aa66afc75c6447 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{ lib, pkgs, config,  ... }:
{
  options.services.myCertificates = {
    certConfig = lib.mkOption {
      default = {
        webroot = "${config.security.acme.directory}/acme-challenge";
        email = "ismael@bouya.org";
        postRun = ''
          systemctl reload httpdTools.service httpdInte.service httpdProd.service
        '';
        plugins = [ "cert.pem" "chain.pem" "fullchain.pem" "full.pem" "key.pem" "account_key.json" ];
      };
      description = "Default configuration for certificates";
    };
  };

  config = {
    services.websitesCerts = config.services.myCertificates.certConfig;
    myServices.databasesCerts = config.services.myCertificates.certConfig;
    myServices.ircCerts = config.services.myCertificates.certConfig;

    security.acme.preliminarySelfsigned = true;

    security.acme.certs = {
      "eldiron" = config.services.myCertificates.certConfig // {
        domain = "eldiron.immae.eu";
      };
    };

    systemd.services = lib.attrsets.mapAttrs' (k: v:
      lib.attrsets.nameValuePair "acme-selfsigned-${k}" (lib.mkBefore { script =
        (lib.optionalString (builtins.elem "cert.pem" v.plugins) ''
        cp $workdir/server.crt ${config.security.acme.directory}/${k}/cert.pem
        chown '${v.user}:${v.group}' ${config.security.acme.directory}/${k}/cert.pem
        chmod ${if v.allowKeysForGroup then "750" else "700"} ${config.security.acme.directory}/${k}/cert.pem
        '') +
        (lib.optionalString (builtins.elem "chain.pem" v.plugins) ''
        cp $workdir/ca.crt ${config.security.acme.directory}/${k}/chain.pem
        chown '${v.user}:${v.group}' ${config.security.acme.directory}/${k}/chain.pem
        chmod ${if v.allowKeysForGroup then "750" else "700"} ${config.security.acme.directory}/${k}/chain.pem
        '')
      ; })
    ) config.security.acme.certs // {
      httpdProd.after = [ "acme-selfsigned-certificates.target" ];
      httpdProd.wants = [ "acme-selfsigned-certificates.target" ];
      httpdTools.after = [ "acme-selfsigned-certificates.target" ];
      httpdTools.wants = [ "acme-selfsigned-certificates.target" ];
      httpdInte.after = [ "acme-selfsigned-certificates.target" ];
      httpdInte.wants = [ "acme-selfsigned-certificates.target" ];
    };
  };
}