diff --git a/pkgs/by-name/el/element-desktop/element-desktop-pin.nix b/pkgs/by-name/el/element-desktop/element-desktop-pin.nix index e4b777e34ead..5c2cee48db3b 100644 --- a/pkgs/by-name/el/element-desktop/element-desktop-pin.nix +++ b/pkgs/by-name/el/element-desktop/element-desktop-pin.nix @@ -2,6 +2,6 @@ "version" = "1.12.7"; "hashes" = { "desktopSrcHash" = "sha256-VnZyNrylYbKhd9YyuoUSJWbTEphOsTivbbYIUeEZv2U="; - "desktopYarnHash" = "sha256-SAIoJRX06oE4j2Gjb/0MSiUa0UGYkQmuyuV90VRbFtk="; + "desktopYarnHash" = "sha256-Gnd/ouRI/OxFwsvR5arfi/FcGld3XjtW9tzuwyX8IRg="; }; } diff --git a/pkgs/by-name/el/element-desktop/package.nix b/pkgs/by-name/el/element-desktop/package.nix index 28b3c267ba46..945c5177e171 100644 --- a/pkgs/by-name/el/element-desktop/package.nix +++ b/pkgs/by-name/el/element-desktop/package.nix @@ -4,17 +4,20 @@ fetchFromGitHub, makeWrapper, makeDesktopItem, - yarn, nodejs, - jq, electron_38, element-web, sqlcipher, callPackage, - desktopToDarwinBundle, typescript, # command line arguments which are always set commandLineArgs ? "", + yarnConfigHook, + yarnBuildHook, + fetchYarnDeps, + asar, + copyDesktopItems, + darwin, }: let @@ -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,90 +67,101 @@ 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:" + + # 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" ''; - # Workaround for darwin sandbox build failure: "Error: listen EPERM: operation not permitted ..tsx..." - preBuild = lib.optionalString stdenv.hostPlatform.isDarwin '' - export TMPDIR="$(mktemp -d)" + 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 ''; - buildPhase = '' - runHook preBuild + postBuild = '' + # relative path to app.asar differs on Linux and MacOS + packed=$(find ./dist -name app.asar) + asar extract "$packed" tmp-app - 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 + # 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 - ln -s $seshat 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 + # icon, used in makeDesktopItem 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" + 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 { - name = "element-desktop"; - exec = "${executableName} %u"; - icon = "element"; - desktopName = "Element"; - genericName = "Matrix Client"; - comment = finalAttrs.meta.description; - categories = [ - "Network" - "InstantMessaging" - "Chat" - ]; - startupWMClass = "Element"; - mimeTypes = [ - "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 - ''; + desktopItems = [ + (makeDesktopItem { + name = "element-desktop"; + exec = "${executableName} %u"; + icon = "element"; + desktopName = "Element"; + genericName = "Matrix Client"; + comment = finalAttrs.meta.description; + categories = [ + "Network" + "InstantMessaging" + "Chat" + ]; + startupWMClass = "Element"; + mimeTypes = [ + "x-scheme-handler/element" + "x-scheme-handler/io.element.desktop" + ]; + }) + ]; passthru = { # run with: nix-shell ./maintainers/scripts/update.nix --argstr package element-desktop diff --git a/pkgs/by-name/el/element-desktop/yarn.nix b/pkgs/by-name/el/element-desktop/yarn.nix deleted file mode 100644 index df839ddbc981..000000000000 --- a/pkgs/by-name/el/element-desktop/yarn.nix +++ /dev/null @@ -1,45 +0,0 @@ -{ - 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"; -}