How to correctly package perl module in nix

I want to try jmap mail protocol on NixOS. Sadly there’s no package cyrus-imapd in nixpkgs now but a pending PR .

I found that the reason for hindering this PR is the management script cyradm of cyrus-imapd. The script is in the perl language. I tried to fix the PERL5LIB. The current build code is as follows.

{
  stdenv,
  lib,
  fetchurl,
  pkg-config,
  bison,
  cyrus_sasl,
  icu,
  jansson,
  flex,
  libbsd,
  libtool,
  libuuid,
  openssl,
  cunit,
  coreutils,
  perl,
  # CalDAV, CardDAV
  withHttp ? true,
  brotli,
  libical,
  libxml2,
  nghttp2,
  shapelib,
  zlib,
  withJMAP ? true,
  libchardet,
  wslay,
  withXapian ? true,
  xapian,
  withCalalarmd ? true,
  withMySQL ? false,
  libmysqlclient,
  withPgSQL ? false,
  postgresql,
  withSQLite ? true,
  sqlite,
  makeWrapper,
  perlPackages,
  installShellFiles,
}:
stdenv.mkDerivation rec {
  pname = "cyrus-imapd";
  version = "3.8.2";

  src = fetchurl {
    url = "https://github.com/cyrusimap/cyrus-imapd/releases/download/cyrus-imapd-${version}/cyrus-imapd-${version}.tar.gz";
    hash = "sha256-Z7FyF+BhCW7VPbuQBcMxgtA96vThUCWuoCn+amKoH/Y=";
  };

  nativeBuildInputs = [bison flex pkg-config perl makeWrapper installShellFiles];
  buildInputs =
    [perlPackages.perl cyrus_sasl.dev icu jansson libbsd libtool libuuid openssl]
    ++ lib.optionals (withHttp || withCalalarmd || withJMAP) [brotli.dev libical libxml2 nghttp2 shapelib zlib]
    ++ lib.optionals withJMAP [libchardet wslay]
    ++ lib.optional withXapian xapian
    ++ lib.optional withMySQL libmysqlclient
    ++ lib.optional withPgSQL postgresql

    ++ lib.optional withSQLite sqlite;

  enableParallelBuilding = true;

  postPatch = ''
    patchShebangs cunit/*.pl
    patchShebangs imap/promdatagen
    patchShebangs tools/*

    sed -i \
      -e 's!/usr/bin/touch!${coreutils}/bin/touch!g' \
      -e 's!/bin/echo!${coreutils}/bin/echo!g' \
      -e 's!/bin/sh!${stdenv.shell}!g' \
      -e 's!/usr/bin/tr!${coreutils}/bin/tr!g' \
      cunit/command.testc

    # failing test case in 3.4.3    sed -i 's!TESTCASE("re: no re: foobar", "nore:foobar");!!' cunit/conversations.testc
  '';

  postFixup = ''
    wrapProgram $out/bin/cyradm \      --set PERL5LIB $(find $out/lib/perl5 -type d | tr "\\n" ":")
  '';

  configureFlags =
    [
      "--enable-autocreate"
      "--enable-idled"
      "--enable-murder"
      "--enable-replication"

      "--enable-unit-tests"
      "--with-pidfile=/run/cyrus-master.pid"
    ]
    ++ lib.optional (withHttp || withCalalarmd || withJMAP) "--enable-http"
    ++ lib.optional withJMAP "--with-jmap"
    ++ lib.optional withXapian "--with-xapian"
    ++ lib.optional withCalalarmd "--enable-calalarmd"
    ++ lib.optional withMySQL "--with-mysql"
    ++ lib.optional withPgSQL "--with-pgsql"
    ++ lib.optional withSQLite "--with-sqlite";

  checkInputs = [cunit];
  doCheck = true;

  meta = with lib; {
    homepage = "https://www.cyrusimap.org";
    description = "Cyrus IMAP is an email, contacts and calendar server";
    license = with licenses; [bsdOriginal];
    maintainers = with maintainers; [pingiun];
    platforms = platforms.unix;
  };
}

But the problem still exists. A library that cyradm relies on reports an error, but through ldd, it is found that the dependent libraries exist and the version is probably correct.

Can't load '/nix/store/57fk19q0dil96nkgmjzvrqp669flp7cc-cyrus-imapd-3.8.2/lib/perl5/site_perl/5.34.1/x86_64-linux-thread-multi/auto/Cyrus/IMAP/IMAP.so' for module Cyrus::IMAP: /nix/store/57fk19q0dil96nkgmjzvrqp669flp7cc-cyrus-imapd-3.8.2/lib/perl5/site_perl/5.34.1/x86_64-linux-thread-multi/auto/Cyrus/IMAP/IMAP.so: undefined symbol: inflateEnd at /nix/store/2l02m7b3p0f8fk45qiipmdpzc2myl09q-perl-5.38.2/lib/perl5/5.38.2/x86_64-linux-thread-multi/DynaLoader.pm line 206.
 at /nix/store/57fk19q0dil96nkgmjzvrqp669flp7cc-cyrus-imapd-3.8.2/lib/perl5/site_perl/5.34.1/x86_64-linux-thread-multi/Cyrus/IMAP/Admin.pm line 43.
Compilation failed in require at /nix/store/57fk19q0dil96nkgmjzvrqp669flp7cc-cyrus-imapd-3.8.2/lib/perl5/site_perl/5.34.1/x86_64-linux-thread-multi/Cyrus/IMAP/Admin.pm line 43.
BEGIN failed--compilation aborted at /nix/store/57fk19q0dil96nkgmjzvrqp669flp7cc-cyrus-imapd-3.8.2/lib/perl5/site_perl/5.34.1/x86_64-linux-thread-multi/Cyrus/IMAP/Admin.pm line 43.
Compilation failed in require at /nix/store/57fk19q0dil96nkgmjzvrqp669flp7cc-cyrus-imapd-3.8.2/lib/perl5/site_perl/5.34.1/x86_64-linux-thread-multi/Cyrus/IMAP/Shell.pm line 59.
BEGIN failed--compilation aborted at /nix/store/57fk19q0dil96nkgmjzvrqp669flp7cc-cyrus-imapd-3.8.2/lib/perl5/site_perl/5.34.1/x86_64-linux-thread-multi/Cyrus/IMAP/Shell.pm line 59.
Compilation failed in require at /nix/store/57fk19q0dil96nkgmjzvrqp669flp7cc-cyrus-imapd-3.8.2/bin/.cyradm-wrapped line 66.
BEGIN failed--compilation aborted at /nix/store/57fk19q0dil96nkgmjzvrqp669flp7cc-cyrus-imapd-3.8.2/bin/.cyradm-wrapped line 66.

The ldd info:

linux-vdso.so.1 (0x00007ffc2b672000)
libssl.so.1.1 => /nix/store/v8vpzh3slc5hm4d9id5bim4dsb4d2ndh-openssl-1.1.1n/lib/libssl.so.1.1 (0x00007fc236f6f000)
libcrypto.so.1.1 => /nix/store/v8vpzh3slc5hm4d9id5bim4dsb4d2ndh-openssl-1.1.1n/lib/libcrypto.so.1.1 (0x00007fc236c00000)
libc.so.6 => /nix/store/ayrsyv7npr0lcbann4k9lxr19x813f0z-glibc-2.34-115/lib/libc.so.6 (0x00007fc236a01000)
libdl.so.2 => /nix/store/ayrsyv7npr0lcbann4k9lxr19x813f0z-glibc-2.34-115/lib/libdl.so.2 (0x00007fc236f6a000)
libpthread.so.0 => /nix/store/ayrsyv7npr0lcbann4k9lxr19x813f0z-glibc-2.34-115/lib/libpthread.so.0 (0x00007fc236f65000)
/nix/store/ksk3rnb0ljx8gngzk19jlmbjyvac4hw6-glibc-2.38-44/lib64/ld-linux-x86-64.so.2 (0x00007fc237093000)