{
  stdenv,
  fetchurl,
  fetchzip,
  electron_33,
  _7zz,
  asar,
  writeScriptBin,
  bash,
  libgcc
}:
let
  better-sqlite3-version = "11.4.0";
  electron-version = "130";

  notion-app-unwrapped = stdenv.mkDerivation rec {
    name = "notion-app-unwrapped";
    version = "4.4.0";

    src = fetchurl {
      url = "https://desktop-release.notion-static.com/Notion-${version}.dmg";
      hash = "sha256-FHPru5Byub8DKAmtqr8EYqGiikslEJBS/zV4PVfFiE8=";
    };

    betterSqlite3 = fetchzip {
      url = "https://github.com/WiseLibs/better-sqlite3/releases/download/v${better-sqlite3-version}/better-sqlite3-v${better-sqlite3-version}-electron-v${electron-version}-linux-x64.tar.gz";
      hash = "sha256-NMFE8lgXSz5vzHa37/ioaL8iY2SW8vUGA3oEgv/OISk=";
    };

    nativeBuildInputs = [
      _7zz
      asar
    ];

    unpackPhase = ''
      7zz x $src || true
      mv "Notion Installer" Notion
    '';

    buildPhase = ''
			ls -lah **/*

      asar e "Notion/Notion.app/Contents/Resources/app.asar" asar_patched

      # replace the native dependencies with linux versions
      cp "$betterSqlite3/Release/better_sqlite3.node" "asar_patched/node_modules/better-sqlite3/build/Release/"

      # fully disabling auto updates
      sed -i 's/if("darwin"===process.platform){const e=s.systemPreferences?.getUserDefault(E,"boolean"),t=_.Store.getState().app.preferences?.isAutoUpdaterDisabled;return Boolean(e||t)}return!1/return!0/g' "asar_patched/.webpack/main/index.js"

      # fix tray icon and right click menu
      mkdir -p asar_patched/.webpack/main
      cp ${./notion.png} asar_patched/.webpack/main/trayIcon.png
      sed -i 's|this.tray.on("click",(()=>{this.onClick()}))|this.tray.setContextMenu(this.trayMenu),this.tray.on("click",(()=>{this.onClick()}))|g' "asar_patched/.webpack/main/index.js"
      sed -i 's|getIcon(){[^}]*}|getIcon(){return s.default.join(__dirname, "trayIcon.png");}|g' "asar_patched/.webpack/main/index.js"

      # avoid running duplicated instances, fixes url opening
      sed -i 's|o.app.on("open-url",w.handleOpenUrl)):"win32"===process.platform|o.app.on("open-url",w.handleOpenUrl)):"linux"===process.platform|g' "asar_patched/.webpack/main/index.js"

      # fake the useragent as windows to fix the spellchecker languages selector and other issues
      sed -i 's|e.setUserAgent(`''${e.getUserAgent()} WantsServiceWorker`),|e.setUserAgent(`''${e.getUserAgent().replace("Linux", "Windows")} WantsServiceWorker`),|g' "asar_patched/.webpack/main/index.js"

      # re-pack the asar
      asar p asar_patched app.asar --unpack "*.node"
    '';

    installPhase = ''
      mkdir -p $out/bin
      mkdir -p $out/lib/notion-app

      cp -r asar_patched $out/lib/notion-app/app
    '';
  };
in
stdenv.mkDerivation rec {
  pname = "notion-app";
  version = notion-app-unwrapped.version;

  dontUnpack = true;
  dontConfigure = true;
  dontBuild = true;

  src = ./notion.png;

  runScript = writeScriptBin "notion-app" ''
    #!${bash}/bin/bash

     export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${libgcc.lib}/lib

     # Enable Wayland when appropriate.
     [[ $NIXOS_OZONE_WL -eq 1 ]] && WL_ARGS="--enable-features=UseOzonePlatform --ozone-platform=wayland" || WL_ARGS=""

     # Launch
     cd ${notion-app-unwrapped}/lib/notion-app/app
     exec ${electron_33}/bin/electron . $WL_ARGS "$@"
  '';

  desktopFile = ''
    [Desktop Entry]
    Version=1.0
    Type=Application
    Name=Notion
    GenericName=Online Document Editor
    Comment=Your connected workspace for wiki, docs & projects
    Exec=${placeholder "out"}/bin/notion-app %U
    Icon=${src}
    Categories=Office;
    MimeType=x-scheme-handler/notion;
    StartupNotify=false
  '';

  installPhase = ''
    mkdir -p $out/bin
    mkdir -p $out/share/applications
    mkdir -p $out/share/icons/hicolor/256x256/apps

    cp $runScript/bin/notion-app $out/bin/notion-app
    cp $src $out/share/icons/hicolor/256x256/apps

    echo "$desktopFile" > $out/share/applications/notion.desktop
  '';
}
