Unverified Commit d86f9637 authored by Maximilian Bosch's avatar Maximilian Bosch Committed by GitHub
Browse files

Merge pull request #329657 from flyingcircusio/mailpit-module

nixos/mailpit: init
parents 67d0a620 f07601ce
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -664,6 +664,7 @@
  ./services/mail/mailcatcher.nix
  ./services/mail/mailhog.nix
  ./services/mail/mailman.nix
  ./services/mail/mailpit.nix
  ./services/mail/mlmmj.nix
  ./services/mail/nullmailer.nix
  ./services/mail/offlineimap.nix
+106 −0
Original line number Diff line number Diff line
{
  config,
  lib,
  pkgs,
  ...
}:

let
  inherit (config.services.mailpit) instances;
  inherit (lib)
    cli
    concatStringsSep
    const
    filterAttrs
    getExe
    mapAttrs'
    mkIf
    mkOption
    nameValuePair
    types
    ;

  isNonNull = v: v != null;
  genCliFlags =
    settings: concatStringsSep " " (cli.toGNUCommandLine { } (filterAttrs (const isNonNull) settings));
in
{
  options.services.mailpit.instances = mkOption {
    default = { };
    type = types.attrsOf (
      types.submodule {
        freeformType = types.attrsOf (
          types.oneOf [
            types.str
            types.int
            types.bool
          ]
        );
        options = {
          database = mkOption {
            type = types.nullOr types.str;
            default = null;
            example = "mailpit.db";
            description = ''
              Specify the local database filename to store persistent data.
              If `null`, a temporary file will be created that will be removed when the application stops.
              It's recommended to specify a relative path. The database will be written into the service's
              state directory then.
            '';
          };
          max = mkOption {
            type = types.ints.unsigned;
            default = 500;
            description = ''
              Maximum number of emails to keep. If the number is exceeded, old emails
              will be deleted.

              Set to `0` to never prune old emails.
            '';
          };
          listen = mkOption {
            default = "127.0.0.1:8025";
            type = types.str;
            description = ''
              HTTP bind interface and port for UI.
            '';
          };
          smtp = mkOption {
            default = "127.0.0.1:1025";
            type = types.str;
            description = ''
              SMTP bind interface and port.
            '';
          };
        };
      }
    );
    description = ''
      Configure mailpit instances. The attribute-set values are
      CLI flags passed to the `mailpit` CLI.

      See [upstream docs](https://mailpit.axllent.org/docs/configuration/runtime-options/)
      for all available options.
    '';
  };

  config = mkIf (instances != { }) {
    systemd.services = mapAttrs' (
      name: cfg:
      nameValuePair "mailpit-${name}" {
        wantedBy = [ "multi-user.target" ];
        after = [ "network-online.target" ];
        wants = [ "network-online.target" ];
        serviceConfig = {
          DynamicUser = true;
          StateDirectory = "mailpit";
          WorkingDirectory = "%S/mailpit";
          ExecStart = "${getExe pkgs.mailpit} ${genCliFlags cfg}";
          Restart = "on-failure";
        };
      }
    ) instances;
  };

  meta.maintainers = lib.teams.flyingcircus.members;
}
+1 −0
Original line number Diff line number Diff line
@@ -552,6 +552,7 @@ in {
  magnetico = handleTest ./magnetico.nix {};
  mailcatcher = handleTest ./mailcatcher.nix {};
  mailhog = handleTest ./mailhog.nix {};
  mailpit = handleTest ./mailpit.nix {};
  mailman = handleTest ./mailman.nix {};
  man = handleTest ./man.nix {};
  mariadb-galera = handleTest ./mysql/mariadb-galera.nix {};
+35 −0
Original line number Diff line number Diff line
import ./make-test-python.nix (
  { lib, ... }:
  {
    name = "mailpit";
    meta.maintainers = lib.teams.flyingcircus.members;

    nodes.machine =
      { pkgs, ... }:
      {
        services.mailpit.instances.default = { };

        environment.systemPackages = with pkgs; [ swaks ];
      };

    testScript = ''
      start_all()

      from json import loads

      machine.wait_for_unit("mailpit-default.service")
      machine.wait_for_open_port(1025)
      machine.wait_for_open_port(8025)
      machine.succeed(
          'echo "this is the body of the email" | swaks --to root@example.org --body - --server localhost:1025'
      )

      received = loads(machine.succeed("curl http://localhost:8025/api/v1/messages"))
      assert received['total'] == 1
      message = received["messages"][0]
      assert len(message['To']) == 1
      assert message['To'][0]['Address'] == 'root@example.org'
      assert "this is the body of the email" in message['Snippet']
    '';
  }
)
+7 −3
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
  fetchNpmDeps,
  testers,
  mailpit,
  nixosTests,
}:

let
@@ -78,10 +79,13 @@ buildGoModule {
    cp -r ${ui} server/ui/dist
  '';

  passthru.tests.version = testers.testVersion {
  passthru.tests = {
    inherit (nixosTests) mailpit;
    version = testers.testVersion {
      package = mailpit;
      command = "mailpit version";
    };
  };

  passthru.updateScript = {
    supportedFeatures = [ "commit" ];