Unverified Commit 65987e97 authored by Sandro Jäckel's avatar Sandro Jäckel Committed by GitHub
Browse files

element-desktop: use electron-builder, update to electron_39, update license (#474644)

parents dc351d25 4b9156c6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2,6 +2,6 @@
  "version" = "1.12.7";
  "hashes" = {
    "desktopSrcHash" = "sha256-VnZyNrylYbKhd9YyuoUSJWbTEphOsTivbbYIUeEZv2U=";
    "desktopYarnHash" = "sha256-SAIoJRX06oE4j2Gjb/0MSiUa0UGYkQmuyuV90VRbFtk=";
    "desktopYarnHash" = "sha256-Gnd/ouRI/OxFwsvR5arfi/FcGld3XjtW9tzuwyX8IRg=";
  };
}
+89 −74
Original line number Diff line number Diff line
@@ -4,24 +4,27 @@
  fetchFromGitHub,
  makeWrapper,
  makeDesktopItem,
  yarn,
  nodejs,
  jq,
  electron_38,
  electron_39,
  element-web,
  sqlcipher,
  callPackage,
  desktopToDarwinBundle,
  typescript,
  # command line arguments which are always set
  commandLineArgs ? "",
  yarnConfigHook,
  yarnBuildHook,
  fetchYarnDeps,
  asar,
  copyDesktopItems,
  darwin,
}:

let
  pinData = import ./element-desktop-pin.nix;
  inherit (pinData.hashes) desktopSrcHash desktopYarnHash;
  executableName = "element-desktop";
  electron = electron_38;
  electron = electron_39;
  seshat = callPackage ./seshat { };
in
stdenv.mkDerivation (
@@ -37,24 +40,25 @@ stdenv.mkDerivation (
      hash = desktopSrcHash;
    };

    # TODO: fetchYarnDeps currently does not deal properly with a dependency
    # declared as a pin to a commit in a specific git repository.
    # While it does download everything correctly, `yarn install --offline`
    # always wants to `git ls-remote` to the repository, ignoring the local
    # cached tarball.
    offlineCache = callPackage ./yarn.nix {
      inherit (finalAttrs) version src;
    offlineCache = fetchYarnDeps {
      yarnLock = finalAttrs.src + "/yarn.lock";
      hash = desktopYarnHash;
    };

    env.ELECTRON_SKIP_BINARY_DOWNLOAD = "1";

    nativeBuildInputs = [
      asar
      copyDesktopItems
      nodejs
      makeWrapper
      jq
      yarn
      typescript
      yarnConfigHook
      yarnBuildHook
    ]
    ++ lib.optionals stdenv.hostPlatform.isDarwin [ desktopToDarwinBundle ];
    ++ lib.optionals stdenv.hostPlatform.isDarwin [
      darwin.autoSignDarwinBinariesHook
    ];

    inherit seshat;

@@ -63,69 +67,83 @@ stdenv.mkDerivation (
    # this shouldn't be in the closure just for unused scripts.
    dontPatchShebangs = true;

    configurePhase = ''
      runHook preConfigure
    postPatch = ''
      cp -r ${electron.dist} electron-dist
      chmod -R u+w electron-dist

      mkdir -p node_modules/
      cp -r $offlineCache/node_modules/* node_modules/
      substituteInPlace package.json --replace-fail "tsx " "node node_modules/tsx/dist/cli.mjs "
      substituteInPlace package.json \
        --replace-fail \
        ' electron-builder",' \
        ' electron-builder --dir -c.electronDist=electron-dist -c.electronVersion=${electron.version} -c.mac.identity=null",'

      runHook postConfigure
    '';
      # `@electron/fuses` tries to run `codesign` and fails. Disable and use autoSignDarwinBinariesHook instead
      substituteInPlace ./electron-builder.ts \
        --replace-fail "resetAdHocDarwinSignature:" "// resetAdHocDarwinSignature:"

    # Workaround for darwin sandbox build failure: "Error: listen EPERM: operation not permitted ..tsx..."
    preBuild = lib.optionalString stdenv.hostPlatform.isDarwin ''
      export TMPDIR="$(mktemp -d)"
      # Need to disable asar integrity check to copy in native seshat files, see postBuild phase
      substituteInPlace ./electron-builder.ts \
        --replace-fail "enableEmbeddedAsarIntegrityValidation: true" "enableEmbeddedAsarIntegrityValidation: false"
    '';

    buildPhase = ''
      runHook preBuild
    preBuild = ''
      # Apply upstream patch
      # Can be removed if upstream removes patches/@types+auto-launch+5.0.5.patch introduced in
      # https://github.com/element-hq/element-desktop/commit/5e882f8e08d58bf9663c8e3ab33885bf7b3709de
      node ./node_modules/patch-package/index.js
    '';

      yarn --offline run build:ts
      node node_modules/matrix-web-i18n/scripts/gen-i18n.js
      yarn --offline run i18n:sort
      yarn --offline run build:res
    postBuild = ''
      # relative path to app.asar differs on Linux and MacOS
      packed=$(find ./dist -name app.asar)
      asar extract "$packed" tmp-app

      ln -s $seshat node_modules/matrix-seshat
      # linking here leads to Error: tmp-app/node_modules/matrix-seshat: file ... links out of the package
      cp -r $seshat tmp-app/node_modules/matrix-seshat

      runHook postBuild
      asar pack tmp-app "$packed"
    '';

    installPhase = ''
      runHook preInstall
    ''
    + lib.optionalString stdenv.hostPlatform.isDarwin ''
      mkdir -p "$out/Applications" "$out/bin"
      mv dist/mac*/Element.app "$out/Applications"

      ln -s '${element-web}' "$out/Applications/Element.app/Contents/Resources/webapp"

      wrapProgram "$out/Applications/Element.app/Contents/MacOS/Element" \
        --add-flags ${lib.escapeShellArg commandLineArgs}

      makeWrapper "$out/Applications/Element.app/Contents/MacOS/Element" "$out/bin/${executableName}"
    ''
    + lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
      mkdir -p "$out/bin" "$out/share"

      cp -a dist/*-unpacked/resources $out/share/element

      # resources
      mkdir -p "$out/share/element"
      ln -s '${element-web}' "$out/share/element/webapp"
      cp -r '.' "$out/share/element/electron"
      chmod -R "a+w" "$out/share/element/electron/node_modules"
      rm -rf "$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

      # icon
      mkdir -p "$out/share/icons/hicolor/512x512/apps"
      ln -s "$out/share/element/electron/build/icon.png" "$out/share/icons/hicolor/512x512/apps/element.png"

      # desktop item
      mkdir -p "$out/share"
      ln -s "${finalAttrs.desktopItem}/share/applications" "$out/share/applications"
      # icon, used in makeDesktopItem
      mkdir -p "$out/share/icons/hicolor/512x512/apps"
      ln -s "$out/share/element/build/icon.png" "$out/share/icons/hicolor/512x512/apps/element.png"

      # executable wrapper
      # LD_PRELOAD workaround for sqlcipher not found: https://github.com/matrix-org/seshat/issues/102
      makeWrapper '${electron}/bin/electron' "$out/bin/${executableName}" \
      makeWrapper '${lib.getExe electron}' "$out/bin/${executableName}" \
        --set LD_PRELOAD ${sqlcipher}/lib/libsqlcipher.so \
        --add-flags "$out/share/element/electron" \
        --add-flags "$out/share/element/app.asar" \
        --add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}" \
        --add-flags ${lib.escapeShellArg commandLineArgs}

    ''
    + ''
      runHook postInstall
    '';

    # The desktop item properties should be kept in sync with data from upstream:
    # https://github.com/element-hq/element-desktop/blob/develop/package.json
    desktopItem = makeDesktopItem {
    desktopItems = [
      (makeDesktopItem {
        name = "element-desktop";
        exec = "${executableName} %u";
        icon = "element";
@@ -142,11 +160,8 @@ stdenv.mkDerivation (
          "x-scheme-handler/element"
          "x-scheme-handler/io.element.desktop"
        ];
    };

    postFixup = lib.optionalString stdenv.hostPlatform.isDarwin ''
      cp build/icon.icns $out/Applications/Element.app/Contents/Resources/element.icns
    '';
      })
    ];

    passthru = {
      # run with: nix-shell ./maintainers/scripts/update.nix --argstr package element-desktop
@@ -157,7 +172,7 @@ stdenv.mkDerivation (
      description = "Feature-rich client for Matrix.org";
      homepage = "https://element.io/";
      changelog = "https://github.com/element-hq/element-desktop/blob/v${finalAttrs.version}/CHANGELOG.md";
      license = lib.licenses.asl20;
      license = lib.licenses.agpl3Plus;
      teams = [ lib.teams.matrix ];
      platforms = electron.meta.platforms ++ lib.platforms.darwin;
      mainProgram = "element-desktop";
+0 −45
Original line number Diff line number Diff line
{
  stdenvNoCC,
  nodejs,
  yarn,
  cacert,
  git,
  version,
  src,
  hash,
}:
stdenvNoCC.mkDerivation {
  pname = "element-desktop-yarn-deps";
  inherit version src;

  nativeBuildInputs = [
    cacert
    nodejs
    yarn
    git
  ];

  dontInstall = true;

  NODE_EXTRA_CA_CERTS = "${cacert}/etc/ssl/certs/ca-bundle.crt";

  buildPhase = ''
    export HOME=$(mktemp -d)
    export YARN_ENABLE_TELEMETRY=0

    yarn install --frozen-lockfile --ignore-platform --skip-integrity-check --ignore-scripts --no-progress --non-interactive
    # Apply upstream patch
    # Can be removed if upstream removes patches/@types+auto-launch+5.0.5.patch introduced in
    # https://github.com/element-hq/element-desktop/commit/5e882f8e08d58bf9663c8e3ab33885bf7b3709de
    node ./node_modules/patch-package/index.js

    mkdir -p $out/node_modules
    cp -r node_modules/* $out/node_modules/
  '';

  dontPatchShebangs = true;

  outputHash = hash;
  outputHashMode = "recursive";
  outputHashAlgo = "sha256";
}
+1 −1
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ stdenv.mkDerivation (
      homepage = "https://element.io/";
      changelog = "https://github.com/element-hq/element-web/blob/v${finalAttrs.version}/CHANGELOG.md";
      teams = [ lib.teams.matrix ];
      license = lib.licenses.asl20;
      license = lib.licenses.agpl3Plus;
      platforms = lib.platforms.all;
    };
  }