summaryrefslogtreecommitdiffstats
path: root/pkgs/development/tools/electron/common.nix
blob: 5b72914afc4c8096c1dc3c3545e36d6e462d92f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
{
  lib,
  stdenv,
  chromium,
  fetchNpmDeps,
  fetchpatch,

  pkgsBuildHost,
  gclient2nix,
  nodejs,
  npmHooks,
  yarn-berry_4,
  unzip,
  writers,

  libnotify,
  libpulseaudio,
  libsecret,
  pipewire,
  speechd-minimal,

  info,
}:

let
  gclientDeps = gclient2nix.importGclientDeps info.deps;
  yarn-berry = yarn-berry_4;

  # Only apply to old versions after upstream updates to Yarn 4.14
  # https://github.com/electron/electron/blob/main/package.json#L148
  yarnPatch = ./yarn-4.14-support.patch;
in

((chromium.override { upstream-info = info.chromium; }).mkDerivation (base: {
  packageName = "electron";
  inherit (info) version;

  buildTargets = [
    "electron:copy_node_headers"
    "electron:electron_dist_zip"
  ];

  outputs = [
    "out"
    "headers"
  ];

  # don't automatically move the include directory from $headers back into $out
  moveToDev = false;

  nativeBuildInputs = base.nativeBuildInputs ++ [
    gclient2nix.gclientUnpackHook
    nodejs
    npmHooks.npmConfigHook
    yarn-berry
    yarn-berry.yarnBerryConfigHook
    unzip
  ];

  buildInputs = base.buildInputs ++ [ libnotify ];

  npmDeps = fetchNpmDeps rec {
    src = gclientDeps."src".path;
    # Assume that the fetcher always unpack the source,
    # based on update.py
    sourceRoot = "${src.name}/third_party/node";
    hash = info.chromium_npm_hash;
  };

  npmRoot = "third_party/node";

  missingHashes =
    if (info.electron_yarn_data ? "missing_hashes") then
      writers.writeJSON "missing-hashes.json" info.electron_yarn_data.missing_hashes
    else
      null;
  yarnOfflineCache = yarn-berry.fetchYarnBerryDeps {
    src = gclientDeps."src/electron".path;
    patches = [ yarnPatch ];
    hash = info.electron_yarn_data.hash;
    missingHashes =
      if (info.electron_yarn_data ? "missing_hashes") then
        writers.writeJSON "missing-hashes.json" info.electron_yarn_data.missing_hashes
      else
        null;
  };

  dontYarnBerryInstallDeps = true; # we'll run the hook manually

  inherit gclientDeps;
  unpackPhase = null; # prevent chromium's unpackPhase from being used
  sourceRoot = "src";

  env = base.env // {
    # Hydra can fail to build electron due to clang spamming deprecation
    # warnings mid-build, causing the build log to grow beyond the limit
    # of 64mb and then getting killed by Hydra.
    # For some reason, the log size limit appears to only be enforced on
    # aarch64-linux. x86_64-linux happily succeeds to build with ~180mb. To
    # unbreak the build on h.n.o, we simply disable those warnings for now.
    # https://hydra.nixos.org/build/283952243
    NIX_CFLAGS_COMPILE = base.env.NIX_CFLAGS_COMPILE + " -Wno-deprecated";
    # Needed for header generation in electron 35 and above
    ELECTRON_OUT_DIR = "Release";
  };

  src = null;

  patches =
    base.patches
    ++
      # Restore fake libGLESv2.so which is patchelf'd by the chromium derivation
      lib.optional (lib.versionAtLeast info.version "44")
        ./0001-Revert-build-stop-shipping-dummy-ANGLE-libs-in-Linux.patch;

  postPatch = ''
    mkdir -p third_party/jdk/current/bin

    echo 'build_with_chromium = true' >> build/config/gclient_args.gni
    echo 'checkout_google_benchmark = false' >> build/config/gclient_args.gni
    echo 'checkout_android = false' >> build/config/gclient_args.gni
    echo 'checkout_android_prebuilts_build_tools = false' >> build/config/gclient_args.gni
    echo 'checkout_android_native_support = false' >> build/config/gclient_args.gni
    echo 'checkout_ios_webkit = false' >> build/config/gclient_args.gni
    echo 'checkout_nacl = false' >> build/config/gclient_args.gni
    echo 'checkout_openxr = false' >> build/config/gclient_args.gni
    echo 'checkout_rts_model = false' >> build/config/gclient_args.gni
    echo 'checkout_src_internal = false' >> build/config/gclient_args.gni
    echo 'cros_boards = ""' >> build/config/gclient_args.gni
    echo 'cros_boards_with_qemu_images = ""' >> build/config/gclient_args.gni
    echo 'generate_location_tags = true' >> build/config/gclient_args.gni

    echo 'LASTCHANGE=${info.deps."src".args.tag}-refs/heads/master@{#0}' > build/util/LASTCHANGE
    echo "$SOURCE_DATE_EPOCH" > build/util/LASTCHANGE.committime

    cat << EOF > gpu/config/gpu_lists_version.h
    /* Generated by lastchange.py, do not edit.*/
    #ifndef GPU_CONFIG_GPU_LISTS_VERSION_H_
    #define GPU_CONFIG_GPU_LISTS_VERSION_H_
    #define GPU_LISTS_VERSION "${info.deps."src".args.tag}"
    #endif  // GPU_CONFIG_GPU_LISTS_VERSION_H_
    EOF

    cat << EOF > skia/ext/skia_commit_hash.h
    /* Generated by lastchange.py, do not edit.*/
    #ifndef SKIA_EXT_SKIA_COMMIT_HASH_H_
    #define SKIA_EXT_SKIA_COMMIT_HASH_H_
    #define SKIA_COMMIT_HASH "${info.deps."src/third_party/skia".args.rev}-"
    #endif  // SKIA_EXT_SKIA_COMMIT_HASH_H_
    EOF

    echo -n '${info.deps."src/third_party/dawn".args.rev}' > gpu/webgpu/DAWN_VERSION
    cat << EOF > gpu/webgpu/dawn_commit_hash.h
    /* Generated by lastchange.py, do not edit.*/
    #ifndef GPU_WEBGPU_DAWN_COMMIT_HASH_H_
    #define GPU_WEBGPU_DAWN_COMMIT_HASH_H_
    #define DAWN_COMMIT_HASH "${info.deps."src/third_party/dawn".args.rev}"
    #endif  // GPU_WEBGPU_DAWN_COMMIT_HASH_H_
    EOF
    (
      PATH=$PATH:${
        lib.makeBinPath (
          with pkgsBuildHost;
          [
            git
          ]
        )
      }
      cd electron
      git apply ${yarnPatch}
      YARN_ENABLE_SCRIPTS=0 yarnBerryConfigHook
    )
    (
      cd ..
      PATH=$PATH:${
        lib.makeBinPath (
          with pkgsBuildHost;
          [
            jq
            git
          ]
        )
      }
      config=src/electron/patches/config.json
      for entry in $(cat $config | jq -c ".[]")
      do
        patch_dir=$(echo $entry | jq -r ".patch_dir")
        repo=$(echo $entry | jq -r ".repo")
        for patch in $(cat $patch_dir/.patches)
        do
          echo applying in $repo: $patch
          git apply -p1 --directory=$repo --exclude='src/third_party/blink/web_tests/*' --exclude='src/content/test/data/*' $patch_dir/$patch
        done
      done
    )
    echo 'checkout_glic_e2e_tests = false' >> build/config/gclient_args.gni
    echo 'checkout_mutter = false' >> build/config/gclient_args.gni
    echo 'checkout_clusterfuzz_data = false' >> build/config/gclient_args.gni
  ''
  + base.postPatch;

  preConfigure = ''
    (
      cd third_party/node
      grep patch update_npm_deps | sh
    )
  ''
  + (base.preConfigure or "");

  gnFlags = rec {
    # build/args/release.gn
    is_component_build = false;
    is_official_build = true;
    rtc_use_h264 = proprietary_codecs;
    is_component_ffmpeg = true;

    # build/args/all.gn
    is_electron_build = true;
    root_extra_deps = [ "//electron" ];
    node_module_version = lib.toInt info.modules;
    v8_promise_internal_field_count = 1;
    v8_embedder_string = "-electron.0";
    v8_enable_snapshot_native_code_counters = false;
    v8_enable_javascript_promise_hooks = true;
    enable_cdm_host_verification = false;
    proprietary_codecs = true;
    ffmpeg_branding = "Chrome";
    enable_printing = true;
    angle_enable_vulkan_validation_layers = false;
    dawn_enable_vulkan_validation_layers = false;
    enable_pseudolocales = false;
    allow_runtime_configurable_key_storage = true;
    enable_cet_shadow_stack = false;
    is_cfi = false;
    enable_dangling_raw_ptr_checks = false;
    enable_dangling_raw_ptr_feature_flag = false;
    v8_enable_private_mapping_fork_optimization = true;
    v8_expose_public_symbols = true;
    enable_linux_installer = false;
    enable_pdf_save_to_drive = false;
    node_openssl_path = "//third_party/boringssl";
  }
  // lib.optionalAttrs (lib.versionOlder info.version "43") {
    enterprise_cloud_content_analysis = false;
  }
  // {

    # other
    enable_widevine = false;
    override_electron_version = info.version;
  };

  installPhase = ''
    runHook preInstall

    mkdir -p $libExecPath
    unzip -d $libExecPath out/Release/dist.zip

    mkdir -p $headers
    cp -r out/Release/gen/node_headers/* $headers/

    runHook postInstall
  '';

  postFixup =
    let
      libPath = lib.makeLibraryPath [
        libnotify
        pipewire
        stdenv.cc.cc
        libsecret
        libpulseaudio
        speechd-minimal
      ];
    in
    base.postFixup
    + ''
      patchelf \
        --add-rpath "${libPath}" \
        $out/libexec/electron/electron
    '';

  requiredSystemFeatures = [ "big-parallel" ];

  passthru = {
    inherit info;
  };

  meta = {
    description = "Cross platform desktop application shell";
    homepage = "https://github.com/electron/electron";
    platforms = lib.platforms.linux;
    license = lib.licenses.mit;
    teams = [ lib.teams.electron ];
    mainProgram = "electron";
    hydraPlatforms =
      lib.optionals (!(lib.hasInfix "alpha" info.version) && !(lib.hasInfix "beta" info.version))
        [
          "aarch64-linux"
          "x86_64-linux"
        ];
    timeout = 172800; # 48 hours (increased from the Hydra default of 10h)
  };
})).overrideAttrs
  (
    finalAttrs: prevAttrs: {
      # this was the only way I could get the package to properly reference itself
      passthru = prevAttrs.passthru // {
        dist = finalAttrs.finalPackage + "/libexec/electron";
      };
    }
  )