2020-06-23 14:56:20 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.programs.mu;
|
|
|
|
|
|
|
|
# Used to generate command line arguments that mu can operate with.
|
|
|
|
genCmdMaildir = path: "--maildir=" + path;
|
|
|
|
|
2024-11-18 17:35:56 +01:00
|
|
|
# Sorted list of personal email addresses to register
|
|
|
|
sortedAddresses = let
|
2022-02-22 04:24:27 +01:00
|
|
|
# Set of email account sets where mu.enable = true.
|
2020-06-23 14:56:20 +02:00
|
|
|
muAccounts =
|
|
|
|
filter (a: a.mu.enable) (attrValues config.accounts.email.accounts);
|
|
|
|
addrs = map (a: a.address) muAccounts;
|
2022-02-22 04:24:27 +01:00
|
|
|
# Construct list of lists containing email aliases, and flatten
|
|
|
|
aliases = flatten (map (a: a.aliases) muAccounts);
|
2024-11-18 17:35:56 +01:00
|
|
|
# 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;
|
2020-06-23 14:56:20 +02:00
|
|
|
in concatStringsSep " " addMyAddress;
|
|
|
|
|
|
|
|
in {
|
|
|
|
meta.maintainers = [ maintainers.KarlJoad ];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
programs.mu = {
|
|
|
|
enable = mkEnableOption "mu, a maildir indexer and searcher";
|
|
|
|
|
2023-08-09 18:40:43 +02:00
|
|
|
package = mkPackageOption pkgs "mu" { };
|
|
|
|
|
2020-06-23 14:56:20 +02:00
|
|
|
# No options/config file present for mu, and program author will not be
|
|
|
|
# adding one soon. See https://github.com/djcb/mu/issues/882 for more
|
|
|
|
# information about this.
|
|
|
|
};
|
|
|
|
|
|
|
|
accounts.email.accounts = mkOption {
|
|
|
|
type = with types;
|
|
|
|
attrsOf
|
|
|
|
(submodule { options.mu.enable = mkEnableOption "mu indexing"; });
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2023-08-09 18:40:43 +02:00
|
|
|
home.packages = [ cfg.package ];
|
2020-06-23 14:56:20 +02:00
|
|
|
|
|
|
|
home.activation.runMuInit = let
|
|
|
|
maildirOption = genCmdMaildir config.accounts.email.maildirBasePath;
|
|
|
|
dbLocation = config.xdg.cacheHome + "/mu";
|
2024-11-18 17:35:56 +01:00
|
|
|
muExe = getExe cfg.package;
|
2024-12-17 17:28:24 +01:00
|
|
|
gawkExe = getExe pkgs.gawk;
|
2020-06-23 14:56:20 +02:00
|
|
|
in hm.dag.entryAfter [ "writeBoundary" ] ''
|
2024-11-18 17:35:56 +01:00
|
|
|
# If the database directory exists and registered personal addresses remain the same,
|
|
|
|
# then `mu init` should NOT be run.
|
2020-06-23 14:56:20 +02:00
|
|
|
# In theory, mu is the only thing that creates that directory, and it is
|
|
|
|
# only created during the initial index.
|
2024-12-17 17:28:24 +01:00
|
|
|
MU_SORTED_ADDRS=$((${muExe} info store | ${gawkExe} '/personal-address/{print $4}' | LC_ALL=C sort | paste -sd ' ') || exit 0)
|
2024-11-18 17:35:56 +01:00
|
|
|
if [[ ! -d "${dbLocation}" || ! "$MU_SORTED_ADDRS" = "${
|
|
|
|
concatStringsSep " " sortedAddresses
|
|
|
|
}" ]]; then
|
|
|
|
run ${muExe} init ${maildirOption} ${myAddresses} $VERBOSE_ARG;
|
2020-06-23 14:56:20 +02:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|