~dblsaiko/nix-extras

9f6228f4b4bf6cbc94fb19240dc55940a00dd02d — Marco Rebhan 5 months ago dc7a6bb
Upstream dovecot extension
2 files changed, 0 insertions(+), 149 deletions(-)

M nixos/ext/default.nix
D nixos/ext/dovecot.nix
M nixos/ext/default.nix => nixos/ext/default.nix +0 -1
@@ 1,6 1,5 @@
{
  imports = [
    ./dovecot.nix
    ./rspamd.nix
  ];
}

D nixos/ext/dovecot.nix => nixos/ext/dovecot.nix +0 -148
@@ 1,148 0,0 @@
# Extra configuration for dovecot
{
  config,
  lib,
  pkgs,
  ...
}: let
  inherit
    (lib)
    concatStringsSep
    flatten
    imap1
    mapAttrsToList
    mkForce
    mkIf
    mkOption
    optional
    optionalString
    ;
  inherit (lib.types) listOf nullOr path str submodule;

  stateDir = "/var/lib/dovecot";

  cfg = config.services.dovecot2;
in {
  options = {
    services.dovecot2 = {
      imapsieve.mailbox = mkOption {
        type = listOf (submodule ({
          options,
          config,
          name,
          ...
        }: {
          options = {
            name = mkOption {
              type = nullOr str;
              default = null;
            };

            from = mkOption {
              type = nullOr str;
              default = null;
            };

            causes = mkOption {
              type = nullOr str;
              default = null;
            };

            before = mkOption {
              type = path;
              default = null;
            };
          };
        }));
      };

      sieve = {
        plugins = mkOption {
          type = listOf str;
          default = [];
          description = "Sieve plugins to load";
        };

        extensions = mkOption {
          type = listOf str;
          default = [];
          description = "Sieve extensions for use in user scripts";
        };

        globalExtensions = mkOption {
          type = listOf str;
          default = [];
          description = "Sieve extensions for use in global scripts";
        };

        pipeBins = mkOption {
          type = listOf path;
          default = [];
          description = "Programs available for use by the vnd.dovecot.pipe extension";
        };
      };
    };
  };

  config = mkIf cfg.enable {
    services.dovecot2 = {
      sieve.plugins =
        optional (cfg.imapsieve.mailbox != []) "sieve_imapsieve"
        ++ optional (cfg.sieve.pipeBins != []) "sieve_extprograms";

      sieve.globalExtensions = optional (cfg.sieve.pipeBins != []) "vnd.dovecot.pipe";

      extraConfig =
        ''
          plugin {
            sieve_plugins = ${concatStringsSep " " cfg.sieve.plugins}
            sieve_extensions = ${concatStringsSep " " (map (el: "+${el}") cfg.sieve.extensions)}
            sieve_global_extensions = ${concatStringsSep " " (map (el: "+${el}") cfg.sieve.globalExtensions)}
        ''
        + optionalString (cfg.imapsieve.mailbox != []) ''
          ${
            concatStringsSep "\n" (flatten (imap1 (
                idx: el:
                  optional (el.name != null) "imapsieve_mailbox${toString idx}_name = ${el.name}"
                  ++ optional (el.from != null) "imapsieve_mailbox${toString idx}_from = ${el.from}"
                  ++ optional (el.causes != null) "imapsieve_mailbox${toString idx}_causes = ${el.causes}"
                  ++ ["imapsieve_mailbox${toString idx}_before = file:${stateDir}/imapsieve/${baseNameOf el.before}"]
              )
              cfg.imapsieve.mailbox))
          }
        ''
        + optionalString (cfg.sieve.pipeBins != []) ''
            sieve_pipe_bin_dir = ${pkgs.linkFarm "sieve-pipe-bins" (map (el: {
              # This breaks with content-addressed derivations, I suppose?
              name = builtins.unsafeDiscardStringContext (baseNameOf el);
              path = el;
            })
            cfg.sieve.pipeBins)}
          }
        '';
    };

    # taken from dovecot in nixpkgs, but adapted for imapsieve
    systemd.services.dovecot2.preStart =
      ''
        rm -rf ${stateDir}/imapsieve
      ''
      + optionalString (cfg.imapsieve.mailbox != []) ''
        mkdir -p ${stateDir}/imapsieve

        ${
          concatStringsSep "\n" (map (el: ''
              cp -p ${el.before} ${stateDir}/imapsieve/${baseNameOf el.before}
              ${pkgs.dovecot_pigeonhole}/bin/sievec '${stateDir}/imapsieve/${baseNameOf el.before}'
            '')
            cfg.imapsieve.mailbox)
        }

        ${
          if cfg.mailUser == null || cfg.mailGroup == null
          then ""
          else "chown -R '${cfg.mailUser}:${cfg.mailGroup}' '${stateDir}/imapsieve'"
        }
      '';
  };
}