From aa479b0124393e0a828f97cfa75a418b4c6c20f8 Mon Sep 17 00:00:00 2001 From: Andrew Fontaine Date: Sun, 28 Feb 2021 10:06:11 -0500 Subject: [PATCH] git: rely on msmtp for smtp if msmtp is enabled (#1829) If a user using msmtp to send all their email, it would be preferred if git used it as well. The only settings necessary are to set the smtp server to the msmtp binary and set envelop sender to true, which makes git call msmtp with the -f flag to set the from address from the email. --- modules/misc/news.nix | 9 ++++ modules/programs/git.nix | 33 ++++++++------ tests/modules/programs/git/default.nix | 1 + .../programs/git/git-with-msmtp-expected.conf | 14 ++++++ tests/modules/programs/git/git-with-msmtp.nix | 45 +++++++++++++++++++ 5 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 tests/modules/programs/git/git-with-msmtp-expected.conf create mode 100644 tests/modules/programs/git/git-with-msmtp.nix diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 2809e83ae..dc7159da7 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1863,6 +1863,15 @@ in lists to polybar-style 'foo-0, foo-1, ...' lists. ''; } + { + time = "2021-02-25T22:36:43+00:00"; + condition = config.programs.git.enable + && any config.accounts.email.accounts (account: account.msmtp.enable); + message = '' + Git will now defer to msmtp for sending emails if + 'accounts.email.accounts..msmtp.enable' is true. + ''; + } ]; }; } diff --git a/modules/programs/git.nix b/modules/programs/git.nix index 4b2912e65..cbac449a5 100644 --- a/modules/programs/git.nix +++ b/modules/programs/git.nix @@ -276,21 +276,26 @@ in { genIdentity = name: account: with account; - nameValuePair "sendemail.${name}" ({ - smtpEncryption = if smtp.tls.enable then - (if smtp.tls.useStartTls - || versionOlder config.home.stateVersion "20.09" then - "tls" - else - "ssl") - else - ""; - smtpServer = smtp.host; - smtpUser = userName; + nameValuePair "sendemail.${name}" (if account.msmtp.enable then { + smtpServer = "${pkgs.msmtp}/bin/msmtp"; + envelopeSender = true; from = address; - } // optionalAttrs (smtp.port != null) { - smtpServerPort = smtp.port; - }); + } else + { + smtpEncryption = if smtp.tls.enable then + (if smtp.tls.useStartTls + || versionOlder config.home.stateVersion "20.09" then + "tls" + else + "ssl") + else + ""; + smtpServer = smtp.host; + smtpUser = userName; + from = address; + } // optionalAttrs (smtp.port != null) { + smtpServerPort = smtp.port; + }); in mapAttrs' genIdentity (filterAttrs hasSmtp config.accounts.email.accounts); } diff --git a/tests/modules/programs/git/default.nix b/tests/modules/programs/git/default.nix index 45aface8d..d5a6b4ee1 100644 --- a/tests/modules/programs/git/default.nix +++ b/tests/modules/programs/git/default.nix @@ -1,5 +1,6 @@ { git-with-email = ./git-with-email.nix; git-with-most-options = ./git.nix; + git-with-msmtp = ./git-with-msmtp.nix; git-with-str-extra-config = ./git-with-str-extra-config.nix; } diff --git a/tests/modules/programs/git/git-with-msmtp-expected.conf b/tests/modules/programs/git/git-with-msmtp-expected.conf new file mode 100644 index 000000000..ded862bd8 --- /dev/null +++ b/tests/modules/programs/git/git-with-msmtp-expected.conf @@ -0,0 +1,14 @@ +[sendemail "hm-account"] + from = "hm@example.org" + smtpEncryption = "tls" + smtpServer = "smtp.example.org" + smtpUser = "home.manager.jr" + +[sendemail "hm@example.com"] + envelopeSender = true + from = "hm@example.com" + smtpServer = "@msmtp@/bin/msmtp" + +[user] + email = "hm@example.com" + name = "H. M. Test" diff --git a/tests/modules/programs/git/git-with-msmtp.nix b/tests/modules/programs/git/git-with-msmtp.nix new file mode 100644 index 000000000..e76d3187f --- /dev/null +++ b/tests/modules/programs/git/git-with-msmtp.nix @@ -0,0 +1,45 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + imports = [ ../../accounts/email-test-accounts.nix ]; + + config = { + accounts.email.accounts."hm@example.com".msmtp.enable = true; + programs.git = { + enable = true; + package = pkgs.gitMinimal; + userEmail = "hm@example.com"; + userName = "H. M. Test"; + }; + + home.stateVersion = "20.09"; + + nmt.script = '' + function assertGitConfig() { + local value + value=$(${pkgs.gitMinimal}/bin/git config \ + --file $TESTED/home-files/.config/git/config \ + --get $1) + if [[ $value != $2 ]]; then + fail "Expected option '$1' to have value '$2' but it was '$value'" + fi + } + + assertFileExists home-files/.config/git/config + assertFileContent home-files/.config/git/config \ + ${ + pkgs.substituteAll { + inherit (pkgs) msmtp; + src = ./git-with-msmtp-expected.conf; + } + } + + assertGitConfig "sendemail.hm@example.com.from" "hm@example.com" + assertGitConfig "sendemail.hm-account.from" "hm@example.org" + assertGitConfig "sendemail.hm@example.com.smtpServer" "${pkgs.msmtp}/bin/msmtp" + assertGitConfig "sendemail.hm@example.com.envelopeSender" "true" + ''; + }; +}