nixos/mailhog: add setSendmail option (#339283)

This commit is contained in:
lassulus
2024-12-17 21:37:08 +01:00
committed by GitHub
3 changed files with 49 additions and 17 deletions
+6
View File
@@ -19566,6 +19566,12 @@
githubId = 61306;
name = "Rene Treffer";
};
RTUnreal = {
email = "unreal+nixpkgs@rtinf.net";
github = "RTUnreal";
githubId = 22859658;
name = "RTUnreal";
};
rubenhoenle = {
email = "git@hoenle.xyz";
github = "rubenhoenle";
+36 -8
View File
@@ -1,4 +1,9 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.mailhog;
@@ -8,17 +13,24 @@ let
"-smtp-bind-addr :${toString cfg.smtpPort}"
"-ui-bind-addr :${toString cfg.uiPort}"
"-storage ${cfg.storage}"
] ++ lib.optional (cfg.storage == "maildir")
"-maildir-path $STATE_DIRECTORY"
]
++ lib.optional (cfg.storage == "maildir") "-maildir-path $STATE_DIRECTORY"
++ cfg.extraArgs
);
mhsendmail = pkgs.writeShellScriptBin "mailhog-sendmail" ''
exec ${lib.getExe pkgs.mailhog} sendmail $@
'';
in
{
###### interface
imports = [
(lib.mkRemovedOptionModule [ "services" "mailhog" "user" ] "")
(lib.mkRemovedOptionModule [
"services"
"mailhog"
"user"
] "")
];
options = {
@@ -26,8 +38,15 @@ in
services.mailhog = {
enable = lib.mkEnableOption "MailHog, web and API based SMTP testing";
setSendmail = lib.mkEnableOption "set the system sendmail to mailhogs's" // {
default = true;
};
storage = lib.mkOption {
type = lib.types.enum [ "maildir" "memory" ];
type = lib.types.enum [
"maildir"
"memory"
];
default = "memory";
description = "Store mails on disk or in memory.";
};
@@ -52,13 +71,12 @@ in
extraArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
default = [ ];
description = "List of additional arguments to pass to the MailHog process.";
};
};
};
###### implementation
config = lib.mkIf cfg.enable {
@@ -69,11 +87,21 @@ in
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "exec";
ExecStart = "${pkgs.mailhog}/bin/MailHog ${args}";
ExecStart = "${lib.getExe pkgs.mailhog} ${args}";
DynamicUser = true;
Restart = "on-failure";
StateDirectory = "mailhog";
};
};
services.mail.sendmailSetuidWrapper = lib.mkIf cfg.setSendmail {
program = "sendmail";
source = lib.getExe mhsendmail;
# Communication happens through the network, no data is written to disk
owner = "nobody";
group = "nogroup";
};
};
meta.maintainers = with lib.maintainers; [RTUnreal];
}
+7 -9
View File
@@ -1,11 +1,9 @@
import ./make-test-python.nix ({ lib, ... }: {
import ./make-test-python.nix ({lib, ...}: {
name = "mailhog";
meta.maintainers = with lib.maintainers; [ jojosch ];
meta.maintainers = with lib.maintainers; [jojosch RTUnreal];
nodes.machine = { pkgs, ... }: {
nodes.machine = {pkgs, ...}: {
services.mailhog.enable = true;
environment.systemPackages = with pkgs; [ swaks ];
};
testScript = ''
@@ -14,11 +12,11 @@ import ./make-test-python.nix ({ lib, ... }: {
machine.wait_for_unit("mailhog.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'
)
assert "this is the body of the email" in machine.succeed(
# Test sendmail wrapper (this uses smtp, which tests the connection)
machine.succeed('printf "To: root@example.com\r\n\r\nthis is the body of the email" | sendmail -t -i -f sender@example.com')
res = machine.succeed(
"curl --fail http://localhost:8025/api/v2/messages"
)
assert all(msg in res for msg in ["this is the body of the email", "sender@example.com", "root@example.com"])
'';
})