From 4bbfae1fa8b97598259579914175c6c9893fc88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Sun, 21 Apr 2024 11:19:30 +0200 Subject: [PATCH] neverest: init at 1.0.0-beta --- modules/modules.nix | 1 + modules/programs/neverest.nix | 165 ++++++++++++++++++ tests/default.nix | 1 + .../programs/neverest/basic-expected.toml | 14 ++ tests/modules/programs/neverest/basic.nix | 34 ++++ tests/modules/programs/neverest/default.nix | 1 + 6 files changed, 216 insertions(+) create mode 100644 modules/programs/neverest.nix create mode 100644 tests/modules/programs/neverest/basic-expected.toml create mode 100644 tests/modules/programs/neverest/basic.nix create mode 100644 tests/modules/programs/neverest/default.nix diff --git a/modules/modules.nix b/modules/modules.nix index ff48b21a5..ccff9edeb 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -162,6 +162,7 @@ let ./programs/ne.nix ./programs/neomutt.nix ./programs/neovim.nix + ./programs/neverest.nix ./programs/newsboat.nix ./programs/nheko.nix ./programs/nix-index.nix diff --git a/modules/programs/neverest.nix b/modules/programs/neverest.nix new file mode 100644 index 000000000..906578e24 --- /dev/null +++ b/modules/programs/neverest.nix @@ -0,0 +1,165 @@ +{ config, lib, pkgs, ... }: + +let + inherit (config.programs) neverest; + + toml = pkgs.formats.toml { }; + + backendType = lib.types.enum [ "imap" "maildir" "notmuch" ]; + + mkEncryptionConfig = tls: + if tls.useStartTls then + "start-tls" + else if tls.enable then + "tls" + else + "none"; + + mkBackendConfig = account: + let + hmAccount = config.accounts.email.accounts.${account.name}; + notmuchEnabled = account.backend == "notmuch" + && hmAccount.notmuch.enable or false; + imapEnabled = account.backend == "imap" && !isNull hmAccount.imap + && !notmuchEnabled; + maildirEnabled = account.backend == "maildir" && !isNull hmAccount.maildir + && !imapEnabled && !notmuchEnabled; + + in lib.optionalAttrs (imapEnabled) { + type = "imap"; + host = hmAccount.imap.host; + port = hmAccount.imap.port; + encryption = mkEncryptionConfig hmAccount.imap.tls; + login = hmAccount.userName; + passwd.cmd = builtins.concatStringsSep " " hmAccount.passwordCommand; + + } // lib.optionalAttrs (maildirEnabled) { + type = "maildir"; + root-dir = hmAccount.maildir.absPath; + + } // lib.optionalAttrs (notmuchEnabled) { + type = "notmuch"; + database-path = config.accounts.email.maildirBasePath; + }; + + mkAccountConfig = accountName: account: + let + leftBackend = { backend = mkBackendConfig account.left; }; + rightBackend = { backend = mkBackendConfig account.right; }; + + in lib.recursiveUpdate account.settings { + left = lib.recursiveUpdate leftBackend account.left.settings; + right = lib.recursiveUpdate rightBackend account.right.settings; + }; + +in { + meta.maintainers = with lib.hm.maintainers; [ soywod ]; + + options = { + programs.neverest = { + enable = lib.mkEnableOption "the email synchronizer Neverest CLI"; + + package = lib.mkPackageOption pkgs "neverest" { }; + + accounts = lib.mkOption { + description = '' + Accounts configuration. + ''; + type = lib.types.attrsOf (lib.types.submodule { + options = { + left = lib.mkOption { + description = '' + Account configuration of the left backend. + See for supported values. + ''; + type = lib.types.submodule { + options = { + backend = lib.mkOption { + type = backendType; + description = '' + The type of the left backend. + ''; + }; + + name = lib.mkOption { + type = lib.types.enum + (builtins.attrNames config.accounts.email.accounts); + description = '' + The name of the Home Manager email account to use + as the left side of the current Neverest account. + ''; + }; + + settings = lib.mkOption { + type = lib.types.submodule { freeformType = toml.type; }; + default = { }; + description = '' + Additional account configuration of the left backend. + See for supported values. + ''; + }; + }; + }; + }; + + right = lib.mkOption { + description = '' + Account configuration of the right backend. + See for supported values. + ''; + type = lib.types.submodule { + options = { + backend = lib.mkOption { + type = backendType; + description = '' + The type of the right backend. + ''; + }; + + name = lib.mkOption { + type = lib.types.enum + (builtins.attrNames config.accounts.email.accounts); + description = '' + The name of the Home Manager email account to use + as the right side of the current Neverest account. + ''; + }; + + settings = lib.mkOption { + type = lib.types.submodule { freeformType = toml.type; }; + default = { }; + description = '' + Additional account configuration of the right backend. + See for supported values. + ''; + }; + }; + }; + + }; + + settings = lib.mkOption { + type = lib.types.submodule { freeformType = toml.type; }; + default = { }; + description = '' + Additional account configuration. + See for supported values. + ''; + }; + }; + }); + }; + }; + }; + + config = lib.mkIf neverest.enable { + home.packages = [ neverest.package ]; + + xdg = { + configFile."neverest/config.toml".source = + toml.generate "neverest-config.toml" { + accounts = lib.mapAttrs mkAccountConfig neverest.accounts; + }; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 625389661..2ec526095 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -110,6 +110,7 @@ in import nmtSrc { ./modules/programs/ne ./modules/programs/neomutt ./modules/programs/neovim + ./modules/programs/neverest ./modules/programs/newsboat ./modules/programs/nheko ./modules/programs/nix-index diff --git a/tests/modules/programs/neverest/basic-expected.toml b/tests/modules/programs/neverest/basic-expected.toml new file mode 100644 index 000000000..45c640155 --- /dev/null +++ b/tests/modules/programs/neverest/basic-expected.toml @@ -0,0 +1,14 @@ +[accounts.imap-maildir] +[accounts.imap-maildir.left.backend] +encryption = "tls" +host = "imap.example.com" +login = "home.manager" +port = 993 +type = "imap" + +[accounts.imap-maildir.left.backend.passwd] +cmd = "password-command" + +[accounts.imap-maildir.right.backend] +root-dir = "/home/hm-user/Mail/hm@example.com" +type = "maildir" diff --git a/tests/modules/programs/neverest/basic.nix b/tests/modules/programs/neverest/basic.nix new file mode 100644 index 000000000..97b929886 --- /dev/null +++ b/tests/modules/programs/neverest/basic.nix @@ -0,0 +1,34 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + imports = [ ../../accounts/email-test-accounts.nix ]; + + programs.neverest = { + enable = true; + accounts = { + "imap-maildir" = { + left = { + backend = "imap"; + name = "hm@example.com"; + settings = { backend.port = 993; }; + }; + right = { + backend = "maildir"; + name = "hm@example.com"; + }; + settings = { left.backend.port = 143; }; + }; + }; + }; + + test.stubs.neverest = { }; + + nmt.script = '' + assertFileExists home-files/.config/neverest/config.toml + assertFileContent home-files/.config/neverest/config.toml ${ + ./basic-expected.toml + } + ''; +} diff --git a/tests/modules/programs/neverest/default.nix b/tests/modules/programs/neverest/default.nix new file mode 100644 index 000000000..480d982d5 --- /dev/null +++ b/tests/modules/programs/neverest/default.nix @@ -0,0 +1 @@ +{ neverest-basic = ./basic.nix; }