From 5e83e20cb78837d75c2c65624b9ca19c2ed55c79 Mon Sep 17 00:00:00 2001 From: RTUnreal Date: Wed, 28 Aug 2024 14:22:48 +0200 Subject: [PATCH] nixos/mailhog: add setSendmail option for sendmail setuid wrapper. This might be used in dev environments, where `sendmail` is the prefered mail transport. --- maintainers/maintainer-list.nix | 6 ++++ nixos/modules/services/mail/mailhog.nix | 44 ++++++++++++++++++++----- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/maintainers/maintainer-list.nix b/maintainers/maintainer-list.nix index 141f114898ca..66ca092d2f50 100644 --- a/maintainers/maintainer-list.nix +++ b/maintainers/maintainer-list.nix @@ -18077,6 +18077,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"; diff --git a/nixos/modules/services/mail/mailhog.nix b/nixos/modules/services/mail/mailhog.nix index 93400167a209..0ec289298f38 100644 --- a/nixos/modules/services/mail/mailhog.nix +++ b/nixos/modules/services/mail/mailhog.nix @@ -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]; }