1 { config, lib, pkgs, ... }:
4 cfg = config.services.opendkim;
6 defaultSock = "local:/run/opendkim/opendkim.sock";
8 keyFile = "${cfg.keyPath}/${cfg.selector}.private";
15 ] ++ lib.optionals (cfg.configFile != null) [ "-x" cfg.configFile ];
19 (lib.mkRenamedOptionModule [ "services" "opendkim" "keyFile" ] [ "services" "opendkim" "keyPath" ])
28 enable = lib.mkOption {
29 type = lib.types.bool;
31 description = "Whether to enable the OpenDKIM sender authentication system.";
34 socket = lib.mkOption {
36 default = defaultSock;
37 description = "Socket which is used for communication with OpenDKIM.";
43 description = "User for the daemon.";
46 group = lib.mkOption {
49 description = "Group for the daemon.";
52 domains = lib.mkOption {
54 default = "csl:${config.networking.hostName}";
55 defaultText = lib.literalExpression ''"csl:''${config.networking.hostName}"'';
56 example = "csl:example.com,mydomain.net";
58 Local domains set (see `opendkim(8)` for more information on datasets).
59 Messages from them are signed, not verified.
63 keyPath = lib.mkOption {
64 type = lib.types.path;
66 The path that opendkim should put its generated private keys into.
67 The DNS settings will be found in this directory with the name selector.txt.
69 default = "/var/lib/opendkim/keys";
72 selector = lib.mkOption {
74 description = "Selector to use when signing.";
77 configFile = lib.mkOption {
78 type = lib.types.nullOr lib.types.path;
80 description = "Additional opendkim configuration.";
90 config = lib.mkIf cfg.enable {
92 users.users = lib.optionalAttrs (cfg.user == "opendkim") {
95 uid = config.ids.uids.opendkim;
99 users.groups = lib.optionalAttrs (cfg.group == "opendkim") {
100 opendkim.gid = config.ids.gids.opendkim;
103 environment.systemPackages = [ pkgs.opendkim ];
105 systemd.tmpfiles.rules = [
106 "d '${cfg.keyPath}' - ${cfg.user} ${cfg.group} - -"
109 systemd.services.opendkim = {
110 description = "OpenDKIM signing and verification daemon";
111 after = [ "network.target" ];
112 wantedBy = [ "multi-user.target" ];
116 if ! test -f ${cfg.selector}.private; then
117 ${pkgs.opendkim}/bin/opendkim-genkey -s ${cfg.selector} -d all-domains-generic-key
118 echo "Generated OpenDKIM key! Please update your DNS settings:\n"
119 echo "-------------------------------------------------------------"
120 cat ${cfg.selector}.txt
121 echo "-------------------------------------------------------------"
126 ExecStart = "${pkgs.opendkim}/bin/opendkim ${lib.escapeShellArgs args}";
129 RuntimeDirectory = lib.optional (cfg.socket == defaultSock) "opendkim";
130 StateDirectory = "opendkim";
131 StateDirectoryMode = "0700";
132 ReadWritePaths = [ cfg.keyPath ];
134 AmbientCapabilities = [];
135 CapabilityBoundingSet = "";
136 DevicePolicy = "closed";
137 LockPersonality = true;
138 MemoryDenyWriteExecute = true;
139 NoNewPrivileges = true;
140 PrivateDevices = true;
141 PrivateMounts = true;
145 ProtectControlGroups = true;
147 ProtectHostname = true;
148 ProtectKernelLogs = true;
149 ProtectKernelModules = true;
150 ProtectKernelTunables = true;
151 ProtectSystem = "strict";
153 RestrictAddressFamilies = [ "AF_INET" "AF_INET6 AF_UNIX" ];
154 RestrictNamespaces = true;
155 RestrictRealtime = true;
156 RestrictSUIDSGID = true;
157 SystemCallArchitectures = "native";
158 SystemCallFilter = [ "@system-service" "~@privileged @resources" ];