Add module for aerc (#3070)

This adds support for configuring email accounts, with automatic smtp, imap,
sendmail (msmpt) and maildir (mbsync, offlineimap) setup in aerc,
via `accounts.email`.
This commit is contained in:
Lukas Nagel 2022-08-11 23:08:28 +02:00 committed by GitHub
parent c1addfdad3
commit 324fedcf9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 681 additions and 0 deletions

4
.github/CODEOWNERS vendored
View file

@ -56,6 +56,10 @@ Makefile @thiagokokada
/tests/modules/misc/xdg/desktop-full-expected.desktop @cwyc
/tests/modules/misc/xdg/desktop-min-expected.desktop @cwyc
/modules/programs/aerc.nix @lukasngl
/modules/programs/aerc-accounts.nix @lukasngl
/tests/modules/programs/aerc @lukasngl
/modules/programs/aria2.nix @JustinLovinger
/modules/programs/autojump.nix @evanjs

View file

@ -299,4 +299,10 @@
github = "mtoohey31";
githubId = 36740602;
};
lukasngl = {
name = "Lukas Nagel";
email = "69244516+lukasngl@users.noreply.github.com";
github = "lukasngl";
githubId = 69244516;
};
}

View file

@ -43,6 +43,7 @@ let
./misc/xdg-user-dirs.nix
./misc/xdg.nix
./programs/abook.nix
./programs/aerc.nix
./programs/afew.nix
./programs/alacritty.nix
./programs/alot.nix

View file

@ -0,0 +1,131 @@
{ config, lib, pkgs, confSections, confSection, ... }:
with lib;
let
mapAttrNames = f: attr:
with builtins;
listToAttrs (attrValues (mapAttrs (k: v: {
name = f k;
value = v;
}) attr));
addAccountName = name: k: "${k}:account=${name}";
in {
type = mkOption {
type = types.attrsOf (types.submodule {
options.aerc = {
enable = mkEnableOption "aerc";
extraAccounts = mkOption {
type = confSection;
default = { };
example =
literalExpression ''{ source = "maildir://~/Maildir/example"; }'';
description = ''
Extra config added to the configuration of this account in
<filename>$HOME/.config/aerc/accounts.conf</filename>.
See aerc-config(5).
'';
};
extraBinds = mkOption {
type = confSections;
default = { };
example = literalExpression
''{ messages = { d = ":move ''${folder.trash}<Enter>"; }; }'';
description = ''
Extra bindings specific to this account, added to
<filename>$HOME/.config/aerc/accounts.conf</filename>.
See aerc-config(5).
'';
};
extraConfig = mkOption {
type = confSections;
default = { };
example = literalExpression "{ ui = { sidebar-width = 42; }; }";
description = ''
Extra config specific to this account, added to
<filename>$HOME/.config/aerc/aerc.conf</filename>.
See aerc-config(5).
'';
};
smtpAuth = mkOption {
type = with types; nullOr (enum [ "none" "plain" "login" ]);
default = "plain";
example = "auth";
description = ''
Sets the authentication mechanism if smtp is used as the outgoing
method.
See aerc-smtp(5).
'';
};
};
});
};
mkAccount = name: account:
let
nullOrMap = f: v: if v == null then v else f v;
optPort = port: if port != null then ":${toString port}" else "";
optAttr = k: v:
if v != null && v != [ ] && v != "" then { ${k} = v; } else { };
optPwCmd = k: p:
optAttr "${k}-cred-cmd" (nullOrMap (builtins.concatStringsSep " ") p);
mkConfig = {
maildir = cfg: {
source =
"maildir://${config.accounts.email.maildirBasePath}/${cfg.maildir.path}";
};
imap = { userName, imap, passwordCommand, aerc, ... }@cfg:
let
protocol = if imap.tls.enable then
if imap.tls.useStartTls then "imap" else "imaps"
else
"imap+insecure";
port' = optPort imap.port;
in {
source = "${protocol}://${userName}@${imap.host}${port'}";
} // optPwCmd "source" passwordCommand;
smtp = { userName, smtp, passwordCommand, ... }@cfg:
let
loginMethod' =
if cfg.aerc.smtpAuth != null then "+${cfg.aerc.smtpAuth}" else "";
protocol = if smtp.tls.enable && !smtp.tls.useStartTls then
"smtps${loginMethod'}"
else
"smtp${loginMethod'}";
port' = optPort smtp.port;
smtp-starttls =
if smtp.tls.enable && smtp.tls.useStartTls then "yes" else null;
in {
outgoing = "${protocol}://${userName}@${smtp.host}${port'}";
} // optPwCmd "outgoing" passwordCommand
// optAttr "smtp-starttls" smtp-starttls;
msmtp = cfg: {
outgoing = "msmtpq --read-envelope-from --read-recipients";
};
};
basicCfg = account:
{
from = "${account.realName} <${account.address}>";
} // (optAttr "copy-to" account.folders.sent)
// (optAttr "default" account.folders.inbox)
// (optAttr "postpone" account.folders.drafts)
// (optAttr "aliases" account.aliases) // account.aerc.extraAccounts;
sourceCfg = account:
if account.mbsync.enable || account.offlineimap.enable then
mkConfig.maildir account
else if account.imap != null then
mkConfig.imap account
else
{ };
outgoingCfg = account:
if account.msmtp.enable then
mkConfig.msmtp account
else if account.smtp != null then
mkConfig.smtp account
else
{ };
in (basicCfg account) // (sourceCfg account) // (outgoingCfg account);
mkAccountConfig = name: account:
mapAttrNames (addAccountName name) account.aerc.extraConfig;
mkAccountBinds = name: account:
mapAttrNames (addAccountName name) account.aerc.extraBinds;
}

165
modules/programs/aerc.nix Normal file
View file

@ -0,0 +1,165 @@
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.aerc;
primitive = with types;
((type: either type (listOf type)) (nullOr (oneOf [ str int bool float ])))
// {
description =
"values (null, bool, int, string of float) or a list of values, that will be joined with a comma";
};
confSection = types.attrsOf primitive;
confSections = types.attrsOf confSection;
sectionsOrLines = types.either types.lines confSections;
accounts = import ./aerc-accounts.nix {
inherit config pkgs lib confSection confSections;
};
aerc-accounts =
attrsets.filterAttrs (_: v: v.aerc.enable) config.accounts.email.accounts;
in {
meta.maintainers = with lib.hm.maintainers; [ lukasngl ];
options.accounts.email.accounts = accounts.type;
options.programs.aerc = {
enable = mkEnableOption "aerc";
extraAccounts = mkOption {
type = sectionsOrLines;
default = { };
example = literalExpression
''{ Work = { source = "maildir://~/Maildir/work"; }; }'';
description = ''
Extra lines added to <filename>$HOME/.config/aerc/accounts.conf</filename>.
See aerc-config(5).
'';
};
extraBinds = mkOption {
type = sectionsOrLines;
default = { };
example = literalExpression ''{ messages = { q = ":quit<Enter>"; }; }'';
description = ''
Extra lines added to <filename>$HOME/.config/aerc/binds.conf</filename>.
Global keybindings can be set in the `global` section.
See aerc-config(5).
'';
};
extraConfig = mkOption {
type = sectionsOrLines;
default = { };
example = literalExpression ''{ ui = { sort = "-r date"; }; }'';
description = ''
Extra lines added to <filename>$HOME/.config/aerc/aerc.conf</filename>.
See aerc-config(5).
'';
};
stylesets = mkOption {
type = with types; attrsOf (either confSection lines);
default = { };
example = literalExpression ''
{ default = { ui = { "tab.selected.reverse" = toggle; }; }; };
'';
description = ''
Stylesets added to <filename>$HOME/.config/aerc/stylesets/</filename>.
See aerc-stylesets(7).
'';
};
templates = mkOption {
type = with types; attrsOf lines;
default = { };
example = literalExpression ''
{ new_message = "Hello!"; };
'';
description = ''
Templates added to <filename>$HOME/.config/aerc/templates/</filename>.
See aerc-templates(7).
'';
};
};
config = let
joinCfg = cfgs:
with builtins;
concatStringsSep "\n" (filter (v: v != "") cfgs);
toINI = conf: # quirk: global section is prepended w/o section heading
let
global = conf.global or { };
local = removeAttrs conf [ "global" ];
optNewLine = if global != { } && local != { } then "\n" else "";
mkValueString = v:
with builtins;
if isList v then # join with comma
concatStringsSep "," (map (generators.mkValueStringDefault { }) v)
else
generators.mkValueStringDefault { } v;
mkKeyValue =
generators.mkKeyValueDefault { inherit mkValueString; } " = ";
in joinCfg [
(generators.toKeyValue { inherit mkKeyValue; } global)
(generators.toINI { inherit mkKeyValue; } local)
];
mkINI = conf: if builtins.isString conf then conf else toINI conf;
mkStyleset = attrsets.mapAttrs' (k: v:
let value = if builtins.isString v then v else toINI { global = v; };
in {
name = "aerc/stylesets/${k}";
value.text = joinCfg [ header value ];
});
mkTemplates = attrsets.mapAttrs' (k: v: {
name = "aerc/templates/${k}";
value.text = v;
});
accountsExtraAccounts = builtins.mapAttrs accounts.mkAccount aerc-accounts;
accountsExtraConfig =
builtins.mapAttrs accounts.mkAccountConfig aerc-accounts;
accountsExtraBinds =
builtins.mapAttrs accounts.mkAccountBinds aerc-accounts;
joinContextual = contextual:
with builtins;
joinCfg (map mkINI (attrValues contextual));
header = ''
# Generated by Home Manager.
'';
in mkIf cfg.enable {
warnings = if ((cfg.extraAccounts != "" && cfg.extraAccounts != { })
|| accountsExtraAccounts != { })
&& (cfg.extraConfig.general.unsafe-accounts-conf or false) == false then [''
aerc: An email account was configured, but `extraConfig.general.unsafe-accounts-conf` is set to false or unset.
This will prevent aerc from starting, see `unsafe-accounts-conf` in aerc-config(5) for details.
Consider setting the option `extraConfig.general.unsafe-accounts-conf` to true.
''] else
[ ];
home.packages = [ pkgs.aerc ];
xdg.configFile = {
"aerc/accounts.conf" = mkIf
((cfg.extraAccounts != "" && cfg.extraAccounts != { })
|| accountsExtraAccounts != { }) {
text = joinCfg [
header
(mkINI cfg.extraAccounts)
(mkINI accountsExtraAccounts)
];
};
"aerc/aerc.conf" =
mkIf (cfg.extraConfig != "" && cfg.extraConfig != { }) {
text = joinCfg [
header
(mkINI cfg.extraConfig)
(joinContextual accountsExtraConfig)
];
};
"aerc/binds.conf" = mkIf ((cfg.extraBinds != "" && cfg.extraBinds != { })
|| accountsExtraBinds != { }) {
text = joinCfg [
header
(mkINI cfg.extraBinds)
(joinContextual accountsExtraBinds)
];
};
} // (mkStyleset cfg.stylesets) // (mkTemplates cfg.templates);
};
}

View file

@ -53,6 +53,7 @@ import nmt {
./modules/misc/fontconfig
./modules/misc/nix
./modules/misc/specialization
./modules/programs/aerc
./modules/programs/alacritty
./modules/programs/alot
./modules/programs/aria2

View file

@ -0,0 +1,4 @@
{
aerc-noSettings = ./noSettings.nix;
aerc-settings = ./settings.nix;
}

View file

@ -0,0 +1,76 @@
# Generated by Home Manager.
[Test1]
enable-folders-sort = true
folders = INBOX,SENT,JUNK
source = maildir:///dev/null
[Test2]
pgp-key-id = 42
[a_imap-nopasscmd-tls-starttls-folders]
copy-to = aercSent
default = aercInbox
from = Foo Bar <addr@mail.invalid>
postpone = aercDrafts
source = imap://foobar@imap.host.invalid:1337
[b_imap-passcmd-tls-nostarttls-extraAccounts]
connection-timeout = 42s
from = Foo Bar <addr@mail.invalid>
source = imaps://foobar@imap.host.invalid:1337
source-cred-cmd = echo PaSsWorD!
[c_imap-passcmd-notls-nostarttls-extraConfig]
from = Foo Bar <addr@mail.invalid>
source = imap+insecure://foobar@imap.host.invalid:1337
source-cred-cmd = echo PaSsWorD!
[d_imap-passcmd-notls-starttls-extraBinds]
from = Foo Bar <addr@mail.invalid>
source = imap+insecure://foobar@imap.host.invalid:1337
source-cred-cmd = echo PaSsWorD!
[e_smtp-nopasscmd-tls-starttls]
from = Foo Bar <addr@mail.invalid>
outgoing = smtp+plain://foobar@smtp.host.invalid:42
smtp-starttls = yes
[f_smtp-passcmd-tls-nostarttls]
from = Foo Bar <addr@mail.invalid>
outgoing = smtps+plain://foobar@smtp.host.invalid:42
outgoing-cred-cmd = echo PaSsWorD!
[g_smtp-passcmd-notls-nostarttls]
from = Foo Bar <addr@mail.invalid>
outgoing = smtp+plain://foobar@smtp.host.invalid:42
outgoing-cred-cmd = echo PaSsWorD!
[h_smtp-passcmd-notls-starttls]
from = Foo Bar <addr@mail.invalid>
outgoing = smtp+plain://foobar@smtp.host.invalid:42
outgoing-cred-cmd = echo PaSsWorD!
[i_maildir-mbsync]
from = Foo Bar <addr@mail.invalid>
source = maildir:///home/hm-user/Maildir/i_maildir-mbsync
[j_maildir-offlineimap]
from = Foo Bar <addr@mail.invalid>
source = maildir:///home/hm-user/Maildir/j_maildir-offlineimap
[l_smpt-auth-none]
from = Foo Bar <addr@mail.invalid>
outgoing = smtps+none://foobar@smtp.host.invalid:42
[m_smpt-auth-plain]
from = Foo Bar <addr@mail.invalid>
outgoing = smtps+plain://foobar@smtp.host.invalid:42
[n_smpt-auth-login]
from = Foo Bar <addr@mail.invalid>
outgoing = smtps+login://foobar@smtp.host.invalid:42
[o_msmtp]
from = Foo Bar <addr@mail.invalid>
outgoing = msmtpq --read-envelope-from --read-recipients

View file

@ -0,0 +1,17 @@
# Generated by Home Manager.
<C-n> = :next-tab<Enter>
<C-p> = :prev-tab<Enter>
<C-t> = :term<Enter>
[compose::editor]
$ex = <C-x>
$noinherit = true
<C-k> = :prev-field<Enter>
[messages]
j = :next<Enter>
q = :quit<Enter>
[messages:account=d_imap-passcmd-notls-starttls-extraBinds]
d = :move Trash<Enter>

View file

@ -0,0 +1,18 @@
# Generated by Home Manager.
[general]
unsafe-accounts-conf = true
[ui]
index-format = null
mouse-enabled = false
sidebar-width = 42
sort = -r date
spinner = true,2,3.400000,5
test-float = 1337.420000
[ui:account=Test]
sidebar-width = 1337
[ui:account=c_imap-passcmd-notls-nostarttls-extraConfig]
index-format = %42.1337n

View file

@ -0,0 +1,18 @@
{ config, lib, pkgs, ... }:
with lib;
{
config = {
nmt.script = let dir = "home-files/.config/aerc";
in ''
assertPathNotExists ${dir}/accounts.conf
assertPathNotExists ${dir}/aerc.conf
assertPathNotExists ${dir}/binds.conf
assertPathNotExists ${dir}/stylesets
'';
programs.aerc.enable = true;
test.stubs.aerc = { };
};
}

View file

@ -0,0 +1,229 @@
{ config, lib, pkgs, ... }:
with lib;
{
config = {
nmt.script = let dir = "home-files/.config/aerc";
in ''
assertFileContent ${dir}/accounts.conf ${./extraAccounts.expected}
assertFileContent ${dir}/binds.conf ${./extraBinds.expected}
assertFileContent ${dir}/aerc.conf ${./extraConfig.expected}
assertFileContent ${dir}/templates/bar ${./templates.expected}
assertFileContent ${dir}/templates/foo ${./templates.expected}
assertFileContent ${dir}/stylesets/default ${./stylesets.expected}
assertFileContent ${dir}/stylesets/asLines ${./stylesets.expected}
'';
test.stubs.aerc = { };
programs.aerc = {
enable = true;
extraAccounts = {
Test1 = {
source = "maildir:///dev/null";
enable-folders-sort = true;
folders = [ "INBOX" "SENT" "JUNK" ];
};
Test2 = { pgp-key-id = 42; };
};
extraBinds = {
global = {
"<C-p>" = ":prev-tab<Enter>";
"<C-n>" = ":next-tab<Enter>";
"<C-t>" = ":term<Enter>";
};
messages = {
q = ":quit<Enter>";
j = ":next<Enter>";
};
"compose::editor" = {
"$noinherit" = "true";
"$ex" = "<C-x>";
"<C-k>" = ":prev-field<Enter>";
};
};
extraConfig = {
general.unsafe-accounts-conf = true;
ui = {
index-format = null;
sort = "-r date";
spinner = [ true 2 3.4 "5" ];
sidebar-width = 42;
mouse-enabled = false;
test-float = 1337.42;
};
"ui:account=Test" = { sidebar-width = 1337; };
};
stylesets = {
asLines = ''
*.default = true
*.selected.reverse = toggle
*error.bold = true
error.fg = red
header.bold = true
title.reverse = true
'';
default = {
"*.default" = "true";
"*error.bold" = "true";
"error.fg" = "red";
"header.bold" = "true";
"*.selected.reverse" = "toggle";
"title.reverse" = "true";
};
};
templates = rec {
foo = ''
X-Mailer: aerc {{version}}
Just a test.
'';
bar = foo;
};
};
accounts.email.accounts = let
basics = {
aerc = { enable = true; };
realName = "Foo Bar";
userName = "foobar";
address = "addr@mail.invalid";
folders = {
drafts = "";
inbox = "";
sent = "";
trash = "";
};
};
in {
a_imap-nopasscmd-tls-starttls-folders = basics // {
primary = true;
imap = {
host = "imap.host.invalid";
port = 1337;
tls.enable = true;
tls.useStartTls = true;
};
folders = {
drafts = "aercDrafts";
inbox = "aercInbox";
sent = "aercSent";
};
};
b_imap-passcmd-tls-nostarttls-extraAccounts = basics // {
passwordCommand = "echo PaSsWorD!";
imap = {
host = "imap.host.invalid";
port = 1337;
tls.enable = true;
tls.useStartTls = false;
};
aerc = {
enable = true;
extraAccounts = { connection-timeout = "42s"; };
};
};
c_imap-passcmd-notls-nostarttls-extraConfig = basics // {
passwordCommand = "echo PaSsWorD!";
aerc = {
enable = true;
extraConfig = { ui.index-format = "%42.1337n"; };
};
imap = {
host = "imap.host.invalid";
port = 1337;
tls.enable = false;
tls.useStartTls = false;
};
};
d_imap-passcmd-notls-starttls-extraBinds = basics // {
passwordCommand = "echo PaSsWorD!";
imap = {
host = "imap.host.invalid";
port = 1337;
tls.enable = false;
tls.useStartTls = true;
};
aerc = {
enable = true;
extraBinds = { messages = { d = ":move Trash<Enter>"; }; };
};
};
e_smtp-nopasscmd-tls-starttls = basics // {
smtp = {
host = "smtp.host.invalid";
port = 42;
tls.enable = true;
tls.useStartTls = true;
};
};
f_smtp-passcmd-tls-nostarttls = basics // {
passwordCommand = "echo PaSsWorD!";
smtp = {
host = "smtp.host.invalid";
port = 42;
tls.enable = true;
tls.useStartTls = false;
};
};
g_smtp-passcmd-notls-nostarttls = basics // {
passwordCommand = "echo PaSsWorD!";
smtp = {
host = "smtp.host.invalid";
port = 42;
tls.enable = false;
tls.useStartTls = false;
};
};
h_smtp-passcmd-notls-starttls = basics // {
passwordCommand = "echo PaSsWorD!";
smtp = {
host = "smtp.host.invalid";
port = 42;
tls.enable = false;
tls.useStartTls = true;
};
};
i_maildir-mbsync = basics // { mbsync.enable = true; };
j_maildir-offlineimap = basics // { offlineimap.enable = true; };
k_notEnabled = basics // { aerc.enable = false; };
l_smpt-auth-none = basics // {
smtp = {
host = "smtp.host.invalid";
port = 42;
};
aerc = {
enable = true;
smtpAuth = "none";
};
};
m_smpt-auth-plain = basics // {
smtp = {
host = "smtp.host.invalid";
port = 42;
};
aerc = {
enable = true;
smtpAuth = "plain";
};
};
n_smpt-auth-login = basics // {
smtp = {
host = "smtp.host.invalid";
port = 42;
};
aerc = {
enable = true;
smtpAuth = "login";
};
};
o_msmtp = basics // { msmtp = { enable = true; }; };
};
};
}

View file

@ -0,0 +1,8 @@
# Generated by Home Manager.
*.default = true
*.selected.reverse = toggle
*error.bold = true
error.fg = red
header.bold = true
title.reverse = true

View file

@ -0,0 +1,3 @@
X-Mailer: aerc {{version}}
Just a test.