aboutsummaryrefslogtreecommitdiff
path: root/modules/private/certificates.nix
blob: 337a7fcf4e5e05361efd093f8b3388e307c5df65 (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
53
54
55
56
{ lib, pkgs, config,  ... }:
{
  options.myServices.certificates = {
    enable = lib.mkEnableOption "enable certificates";
    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 = lib.mkIf config.myServices.certificates.enable {
    services.duplyBackup.profiles.system.excludeFile = ''
      + ${config.security.acme.directory}
      '';
    services.websites.certs = config.myServices.certificates.certConfig;
    myServices.databasesCerts = config.myServices.certificates.certConfig;
    myServices.ircCerts = config.myServices.certificates.certConfig;

    security.acme.preliminarySelfsigned = true;

    security.acme.certs = {
      "eldiron" = config.myServices.certificates.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" ];
    };
  };
}