#
# Fixes an issue where pcsclite doesn't 
#
# vim: et:ts=2:sw=2:
#
{ ... }:
let
  overlay = final': prev': {
    pcsclite = prev'.pcsclite.overrideAttrs (
      final: prev: {

        postInstall = ''
          # pcsc-spy is a debugging utility and it drags python into the closure
          moveToOutput bin/pcsc-spy "$dev"

          # libpcsclite loads its delegate (libpcsclite_real) dynamically.
          # To avoid downstream wrapping/patching, we add libpcsclite_real to the lib
          # Relevant code: https://salsa.debian.org/rousseau/PCSC/-/blob/773be65d160da07de2fc34616af475e06dbaa343/src/libredirect.c#L128
          patchelf $lib/lib/libpcsclite.so.1 \
            --add-needed libpcsclite_real.so.1
        '';

      }
    );
  };
in
{
  nixpkgs.overlays = [ overlay ];
}
