From 030ce4707f93be0a426e595a30d8cc72d80a51f9 Mon Sep 17 00:00:00 2001
From: Julien Moutinho <julm@autogeree.net>
Date: Tue, 25 Dec 2018 22:35:23 +0000
Subject: [PATCH] postfix: fix LDAP SASL

---
 install/overlays.nix                      |  2 +-
 install/overlays/servers/mail/postfix.nix | 49 ++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/install/overlays.nix b/install/overlays.nix
index 9172acb..c399349 100644
--- a/install/overlays.nix
+++ b/install/overlays.nix
@@ -1,5 +1,5 @@
 map import
-[ #overlays/servers/mail/postfix.nix
+[ overlays/servers/mail/postfix.nix
   overlays/servers/mail/dovecot.nix
    # TODO: remove when using a nixpkgs including the fix
    # https://github.com/NixOS/nixpkgs/pull/46859
diff --git a/install/overlays/servers/mail/postfix.nix b/install/overlays/servers/mail/postfix.nix
index f356ccf..b23a92a 100644
--- a/install/overlays/servers/mail/postfix.nix
+++ b/install/overlays/servers/mail/postfix.nix
@@ -1,3 +1,48 @@
-self: super:  {
-  withLDAP = true;
+# DESCRIPTION: add -DUSE_LDAP_SASL
+# TODO: remove when using nixpkgs with https://github.com/NixOS/nixpkgs/pull/46883
+self: super:
+let
+   lib = super.lib;
+   ccargs = lib.concatStringsSep " " ([
+    "-DUSE_TLS" "-DUSE_SASL_AUTH" "-DUSE_CYRUS_SASL" "-I${self.cyrus_sasl.dev}/include/sasl"
+    "-DHAS_DB_BYPASS_MAKEDEFS_CHECK"
+   ] ++ lib.optional  self.withPgSQL "-DHAS_PGSQL"
+     ++ lib.optionals self.withMySQL [ "-DHAS_MYSQL" "-I${self.mysql.connector-c}/include/mysql" "-L${self.mysql.connector-c}/lib/mysql" ]
+     ++ lib.optional  self.withSQLite "-DHAS_SQLITE"
+     ++ lib.optional  self.withLDAP "-DHAS_LDAP -DUSE_LDAP_SASL");
+   auxlibs = lib.concatStringsSep " " ([
+     "-ldb" "-lnsl" "-lresolv" "-lsasl2" "-lcrypto" "-lssl"
+   ] ++ lib.optional self.withPgSQL "-lpq"
+     ++ lib.optional self.withMySQL "-lmysqlclient"
+     ++ lib.optional self.withSQLite "-lsqlite3"
+     ++ lib.optional self.withLDAP "-lldap");
+in {
+  withLDAP   = true;
+  withPgSQL  = false;
+  withMySQL  = false;
+  withSQLite = false;
+  postfix = super.postfix.overrideAttrs (old: {
+    preBuild = ''
+      sed -e '/^PATH=/d' -i postfix-install
+      sed -e "s|@PACKAGE@|$out|" -i conf/post-install
+
+      # post-install need skip permissions check/set on all symlinks following to /nix/store
+      sed -e "s|@NIX_STORE@|$NIX_STORE|" -i conf/post-install
+
+      export command_directory=$out/sbin
+      export config_directory=/etc/postfix
+      export meta_directory=$out/etc/postfix
+      export daemon_directory=$out/libexec/postfix
+      export data_directory=/var/lib/postfix/data
+      export html_directory=$out/share/postfix/doc/html
+      export mailq_path=$out/bin/mailq
+      export manpage_directory=$out/share/man
+      export newaliases_path=$out/bin/newaliases
+      export queue_directory=/var/lib/postfix/queue
+      export readme_directory=$out/share/postfix/doc
+      export sendmail_path=$out/bin/sendmail
+
+      make makefiles CCARGS='${ccargs}' AUXLIBS='${auxlibs}'
+  '';
+  });
 }
-- 
2.47.2