about summary refs log tree commit diff
path: root/nixos/modules/services/mail
diff options
context:
space:
mode:
authorJonas Heinrich <onny@project-insanity.org>2023-02-16 22:47:11 +0100
committerYt <happysalada@proton.me>2023-02-17 17:42:27 -0500
commit13ff144c70351811141067d6e77b599e146e44b2 (patch)
tree62992c56dc0bead932f2cd6b67164c20f36c122a /nixos/modules/services/mail
parentd79f5d4516cbfe17a6471a157cb1d7d02ee124e3 (diff)
downloadnixlib-13ff144c70351811141067d6e77b599e146e44b2.tar
nixlib-13ff144c70351811141067d6e77b599e146e44b2.tar.gz
nixlib-13ff144c70351811141067d6e77b599e146e44b2.tar.bz2
nixlib-13ff144c70351811141067d6e77b599e146e44b2.tar.lz
nixlib-13ff144c70351811141067d6e77b599e146e44b2.tar.xz
nixlib-13ff144c70351811141067d6e77b599e146e44b2.tar.zst
nixlib-13ff144c70351811141067d6e77b599e146e44b2.zip
nixos/maddy: Add option ensureAccounts
Diffstat (limited to 'nixos/modules/services/mail')
-rw-r--r--nixos/modules/services/mail/maddy.nix51
1 files changed, 44 insertions, 7 deletions
diff --git a/nixos/modules/services/mail/maddy.nix b/nixos/modules/services/mail/maddy.nix
index eeb113e204c6..5f3a9b56292d 100644
--- a/nixos/modules/services/mail/maddy.nix
+++ b/nixos/modules/services/mail/maddy.nix
@@ -223,22 +223,59 @@ in {
         '';
       };
 
+      ensureAccounts = mkOption {
+        type = types.listOf types.str;
+        default = [];
+        description = lib.mdDoc ''
+          List of IMAP accounts which get automatically created. Note that for
+          a complete setup, user credentials for these accounts are required too
+          and can be created using the command `maddyctl creds`.
+          This option does not delete accounts which are not (anymore) listed.
+        '';
+        example = [
+          "user1@localhost"
+          "user2@localhost"
+        ];
+      };
+
     };
   };
 
   config = mkIf cfg.enable {
 
     systemd = {
+
       packages = [ pkgs.maddy ];
-      services.maddy = {
-        serviceConfig = {
-          User = cfg.user;
-          Group = cfg.group;
-          StateDirectory = [ "maddy" ];
+      services = {
+        maddy = {
+          serviceConfig = {
+            User = cfg.user;
+            Group = cfg.group;
+            StateDirectory = [ "maddy" ];
+          };
+          restartTriggers = [ config.environment.etc."maddy/maddy.conf".source ];
+          wantedBy = [ "multi-user.target" ];
+        };
+        maddy-ensure-accounts = {
+          script = ''
+            ${optionalString (cfg.ensureAccounts != []) ''
+              ${concatMapStrings (account: ''
+                if ! ${pkgs.maddy}/bin/maddyctl imap-acct list | grep "${account}"; then
+                  ${pkgs.maddy}/bin/maddyctl imap-acct create ${account}
+                fi
+              '') cfg.ensureAccounts}
+            ''}
+          '';
+          serviceConfig = {
+            Type = "oneshot";
+            User= "maddy";
+          };
+          after = [ "maddy.service" ];
+          wantedBy = [ "multi-user.target" ];
         };
-        restartTriggers = [ config.environment.etc."maddy/maddy.conf".source ];
-        wantedBy = [ "multi-user.target" ];
+
       };
+
     };
 
     environment.etc."maddy/maddy.conf" = {