aboutsummaryrefslogtreecommitdiff
path: root/flakes/opendmarc/flake.nix
blob: 277fd259c8bbc39800ff09a9ee69cc349505b8f6 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
  description = "Open source ARC implementation";

  inputs.myuids = {
    url = "https://git.immae.eu/perso/Immae/Config/Nix.git";
    type = "git";
    dir = "flakes/myuids";
  };
  inputs.flake-utils.url = "github:numtide/flake-utils";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs";

  outputs = { self, myuids, flake-utils, nixpkgs }: flake-utils.lib.eachSystem ["aarch64-linux" "i686-linux" "x86_64-linux"] (system:
    let
      pkgs = import nixpkgs { inherit system; overlays = []; };
    in rec {
      packages.opendmarc = pkgs.callPackage ./. {};
      defaultPackage = packages.opendmarc;
      legacyPackages.opendmarc = packages.opendmarc;
      apps.opendmarc = flake-utils.lib.mkApp { drv = packages.opendmarc; };
      defaultApp = apps.opendmarc;
      hydraJobs = checks;
      checks = {
        build = defaultPackage;
      } // pkgs.lib.optionalAttrs (builtins.elem system pkgs.lib.systems.doubles.linux) {
        test =
          let testing = import (nixpkgs + "/nixos/lib/testing-python.nix") { inherit system; };
          in testing.makeTest {
            nodes = {
              server = { pkgs, ... }: {
                imports = [ self.nixosModule ];
                config.services.opendmarc.enable = true;
              };
            };
            testScript = ''
              start_all()
              server.wait_for_unit("opendmarc.service")
              server.succeed("[ -S /run/opendmarc/opendmarc.sock ]")
            '';
          };
        };
    }) // rec {
     overlays = {
       opendmarc = final: prev: {
         opendmarc = self.defaultPackage."${final.system}";
       };
     };
     overlay = overlays.opendmarc;
     nixosModule = { config, lib, pkgs, ... }:
       let
         cfg = config.services.opendmarc;
         defaultSock = "/run/opendmarc/opendmarc.sock";
         args = [ "-f" "-l" "-p" "local:${cfg.socket}" ] ++ lib.optionals (cfg.configFile != null) [ "-c" cfg.configFile ];
       in {
         options = {
           services.opendmarc = {
             enable = lib.mkOption {
               type = lib.types.bool;
               default = false;
               description = "Whether to enable the OpenDMARC sender authentication system.";
             };

             socket = lib.mkOption {
               type = lib.types.str;
               default = defaultSock;
               description = "Socket which is used for communication with OpenDMARC.";
             };

             user = lib.mkOption {
               type = lib.types.str;
               default = "opendmarc";
               description = "User for the daemon.";
             };

             group = lib.mkOption {
               type = lib.types.str;
               default = "opendmarc";
               description = "Group for the daemon.";
             };

             configFile = lib.mkOption {
               type = lib.types.nullOr lib.types.path;
               default = null;
               description = "Additional OpenDMARC configuration.";
             };

           };
         };

         config = lib.mkIf cfg.enable {
           nixpkgs.overlays = [ self.overlay ];
           users.users = lib.optionalAttrs (cfg.user == "opendmarc") {
             opendmarc = {
               group = cfg.group;
               uid = myuids.lib.uids.opendmarc;
             };
           };

           users.groups = lib.optionalAttrs (cfg.group == "opendmarc") {
             opendmarc.gid = myuids.lib.gids.opendmarc;
           };

           environment.systemPackages = [ pkgs.opendmarc ];

           systemd.services.opendmarc = {
             description = "OpenDMARC daemon";
             after = [ "network.target" ];
             wantedBy = [ "multi-user.target" ];

             serviceConfig = {
               ExecStart = "${self.defaultApp."${pkgs.system}".program} ${lib.escapeShellArgs args}";
               User = cfg.user;
               Group = cfg.group;
               RuntimeDirectory = lib.optional (cfg.socket == defaultSock) "opendmarc";
               PermissionsStartOnly = true;
             };
           };
         };
       };
    };
  }