{ stdenv, pkgs, fetchurl, lib, unzip, autoPatchelfHook, makeWrapper, ... }:

stdenv.mkDerivation rec {
  pname = "tal-u-no-lx";
  version = "latest";
  src = fetchurl {
    url = "https://tal-software.com/downloads/plugins/TAL-U-NO-LX-V2_64_linux.zip";
    sha256 = "sha256-1xcThCE3C3RRobEdVESHK6S1QMRg5yT8sF082LJW8Vs=";
  };
  nativeBuildInputs = [ makeWrapper unzip autoPatchelfHook ];
  buildInputs = with pkgs; [
    freetype
    fontconfig
    alsa-lib
    pipewire
    stdenv.cc.cc.lib
  ];

  unpackPhase = ''
    unzip $src
  '';

  installPhase = ''
    mkdir -p $out/lib/vst $out/lib/vst3 $out/lib/clap
    cp -r TAL-U-NO-LX-V2/TAL-U-NO-LX-V2.clap $out/lib/clap
    cp -r TAL-U-NO-LX-V2/libTAL-U-NO-LX-V2.so $out/lib/vst
    cp -r TAL-U-NO-LX-V2/TAL-U-NO-LX-V2.vst3 $out/lib/vst3
  '';

  postFixup = ''
    for file in \
      $out/lib/clap/TAL-U-NO-LX-V2.clap \
      $out/lib/vst/libTAL-U-NO-LX-V2.so \
      $out/lib/vst3/TAL-U-NO-LX-V2.vst3/Contents/x86_64-linux/TAL-U-NO-LX-V2.so
    do
      patchelf --set-rpath "${lib.makeLibraryPath buildInputs}" $file
    done
  '';

  meta = with lib; {
    homepage = "https://tal-software.com/products/tal-u-no-lx";
    description = "TAL-U-NO-LX";
    platforms = platforms.linux;
  };
}
