#
# Fix a bug that makes signal not start on wayland.
#
# vim: et:ts=2:sw=2:
#
{ ... }:
let
  overlay = final': prev': {
    signal-desktop = prev'.signal-desktop.overrideAttrs (
      final: prev: rec {

        # We'll need to patch the ASAR in order to apply our fix.
        nativeBuildInputs = prev.nativeBuildInputs ++ [
          prev'.asar
        ];

        # Fix things to the version that our patch applies to.
        version = "7.18.0";
        src = prev'.fetchurl {
          url = "https://updates.signal.org/desktop/apt/pool/s/signal-desktop/signal-desktop_${version}_amd64.deb";
          hash = "sha256-xI3GCs9ZekENktuSf9NNxoOOGuYtKrOV8Ng3eFy493M=";
        };

        # Do terrible, terrible things.
        preFixup =
          prev.preFixup
          + ''
            # Fix up the main.js in the app to actually show the window.
            pushd $out/lib/Signal/resources/

            asar extract app.asar app-unpacked

            pushd app-unpacked
            patch -p1 < ${./01-show-window.patch}
            popd

            asar pack app-unpacked app.asar
            popd
          '';

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