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.
This commit is contained in:
Andrew Fontaine 2021-02-28 10:06:11 -05:00 committed by GitHub
parent 3327cbe1f9
commit aa479b0124
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 14 deletions

View file

@ -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.<name>.msmtp.enable' is true.
'';
}
];
};
}

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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"

View file

@ -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"
'';
};
}