{ 
    lib, stdenv, fetchFromGitHub, 
    makeWrapper, makeDesktopItem, mkYarnPackage, 
    electron, element-desktop, schildichat-web, 
    callPackage, Security, AppKit, CoreServices, 
    useWayland ? false, cleanSource, 
    schildichat-desktop-src ? ../. 
}:

let
  packageJSON = schildichat-desktop-src + "/element-desktop/package.json";
  yarnLock = schildichat-desktop-src + "/element-desktop/yarn.lock";
  version = builtins.fromJSON (builtins.readFile packageJSON).version;
  electron_exec = if stdenv.isDarwin then "${electron}/Applications/Electron.app/Contents/MacOS/Electron" else "${electron}/bin/electron";
in mkYarnPackage {
  pname = "schildichat-desktop";
  inherit version packageJSON;
  src = cleanSource (schildichat-desktop-src + "/element-desktop");
  nativeBuildInputs = [ makeWrapper ];
  inherit (element-desktop) seshat keytar;

  buildPhase = ''
    export HOME=$(mktemp -d)
    pushd deps/schildichat-desktop/
    npx tsc
    yarn run i18n
    node ./scripts/copy-res.js
    popd
    rm -rf node_modules/matrix-seshat node_modules/keytar
    ln -s $keytar node_modules/keytar
    ln -s $seshat node_modules/matrix-seshat
  '';

  installPhase = ''
    mkdir -p "$out/share/element"
    ln -s '${schildichat-web}' "$out/share/element/webapp"
    cp -r './deps/schildichat-desktop' "$out/share/element/electron"
    cp -r './deps/schildichat-desktop/res/img' "$out/share/element"
    rm "$out/share/element/electron/node_modules"
    cp -r './node_modules' "$out/share/element/electron"
    cp $out/share/element/electron/lib/i18n/strings/en_EN.json $out/share/element/electron/lib/i18n/strings/en-us.json
    ln -s $out/share/element/electron/lib/i18n/strings/en{-us,}.json

    for icon in $out/share/element/electron/build/icons/*.png; do
      mkdir -p "$out/share/icons/hicolor/$(basename $icon .png)/apps"
      ln -s "$icon" "$out/share/icons/hicolor/$(basename $icon .png)/apps/element.png"
    done

    mkdir -p "$out/share"
    ln -s "${desktopItem}/share/applications" "$out/share/applications"

    makeWrapper '${electron_exec}' "$out/bin/schildichat-desktop" \
      --add-flags "$out/share/element/electron${lib.optionalString useWayland " --enable-features=UseOzonePlatform --ozone-platform=wayland"}"
  '';

  distPhase = "true";
  desktopItem = makeDesktopItem {
    name = "schildichat-desktop";
    exec = "schildichat-desktop %u";
    icon = "schildichat";
    desktopName = "SchildiChat";
    genericName = "Matrix Client";
    categories = "Network;InstantMessaging;Chat;";
    extraEntries = "StartupWMClass=schildichat\nMimeType=x-scheme-handler/element;";
  };
}