{ lib
, fetchurl
, makeWrapper
, appimageTools
, libappindicator-gtk3
, pkgs
}:
let
  pname = "filen-desktop";
  version = "latest";

  src = pkgs.fetchurl {
    url =
      "https://cdn.filen.io/@filen/desktop/release/latest/Filen_linux_x86_64.AppImage";
    sha256 = "sha256-keaD5PUjkoFrFTCuap4DvmYq5X3Tjnq+njtiLgAZ9W8=";
  };

  appimageContents = appimageTools.extractType2 { inherit pname version src; };
  name = "filendesktop";
  # Needed for the tray to function
  libPath = lib.makeLibraryPath [ libappindicator-gtk3 ];
in
appimageTools.wrapType2 rec {
  inherit pname version src name;

  extraInstallCommands = ''
     source "${pkgs.makeWrapper}/nix-support/setup-hook"
    wrapProgram $out/bin/${pname} \
      --add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform=wayland --enable-features=WaylandWindowDecorations}}" \
      --prefix LD_LIBRARY_PATH : ${libPath}

    install -m 444 -D ${appimageContents}/@${name}.desktop $out/share/applications/${pname}.desktop
    substituteInPlace $out/share/applications/${pname}.desktop \
      --replace 'Exec=AppRun' 'Exec=filen-desktop'
    cp -r ${appimageContents}/usr/share/icons $out/share
  '';

  meta = with lib; {
    description = "Desktop client for Filen.io";
    homepage = "https://filen.io/";
    license = licenses.agpl3Plus;
    platforms = [ "x86_64-linux" ];
  };
}
