Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mu: run initialization command when personal addresses change #6100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions modules/programs/mu.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ let
# Used to generate command line arguments that mu can operate with.
genCmdMaildir = path: "--maildir=" + path;

# Takes the list of accounts with mu.enable = true, and generates a
# command-line flag for initializing the mu database.
myAddresses = let
# Sorted list of personal email addresses to register
sortedAddresses = let
# Set of email account sets where mu.enable = true.
muAccounts =
filter (a: a.mu.enable) (attrValues config.accounts.email.accounts);
addrs = map (a: a.address) muAccounts;
# Construct list of lists containing email aliases, and flatten
aliases = flatten (map (a: a.aliases) muAccounts);
# Prefix --my-address= to each account's address AND all defined aliases
addMyAddress = map (addr: "--my-address=" + addr) (addrs ++ aliases);
# Sort the list
in sort lessThan (addrs ++ aliases);

# Takes the list of accounts with mu.enable = true, and generates a
# command-line flag for initializing the mu database.
myAddresses = let
# Prefix --my-address= to each account's address and all defined aliases
addMyAddress = map (addr: "--my-address=" + addr) sortedAddresses;
in concatStringsSep " " addMyAddress;

in {
Expand Down Expand Up @@ -49,14 +54,19 @@ in {
home.activation.runMuInit = let
maildirOption = genCmdMaildir config.accounts.email.maildirBasePath;
dbLocation = config.xdg.cacheHome + "/mu";
muExe = getExe cfg.package;
in hm.dag.entryAfter [ "writeBoundary" ] ''
# If the database directory exists, then `mu init` should NOT be run.
# If the database directory exists and registered personal addresses remain the same,
# then `mu init` should NOT be run.
# In theory, mu is the only thing that creates that directory, and it is
# only created during the initial index.
if [[ ! -d "${dbLocation}" ]]; then
run ${
getExe cfg.package
} init ${maildirOption} ${myAddresses} $VERBOSE_ARG;
MU_SORTED_ADDRS=$(${muExe} info store | ${
getExe pkgs.gawk
} '/personal-address/{print $4}' | LC_ALL=C sort | paste -sd ' ')
if [[ ! -d "${dbLocation}" || ! "$MU_SORTED_ADDRS" = "${
concatStringsSep " " sortedAddresses
}" ]]; then
run ${muExe} init ${maildirOption} ${myAddresses} $VERBOSE_ARG;
fi
'';
};
Expand Down
4 changes: 2 additions & 2 deletions tests/modules/programs/mu/basic-configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

nmt.script = ''
assertFileContains activate \
'if [[ ! -d "/home/hm-user/.cache/mu" ]]; then'
'if [[ ! -d "/home/hm-user/.cache/mu" || ! "$MU_SORTED_ADDRS" = "foo@example.com hm@example.com" ]]; then'

assertFileContains activate \
'run @mu@/bin/mu init --maildir=/home/hm-user/Mail --my-address=hm@example.com --my-address=foo@example.com $VERBOSE_ARG;'
'run @mu@/bin/mu init --maildir=/home/hm-user/Mail --my-address=foo@example.com --my-address=hm@example.com $VERBOSE_ARG;'
'';
}
Loading