diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 2809e83a..dc7159da 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 4b2912e6..cbac449a 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 45aface8..d5a6b4ee 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 00000000..ded862bd --- /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 00000000..e76d3187 --- /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" + ''; + }; +}