nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 callPackage,
4 fetchurl,
5 bash,
6 symlinkJoin,
7 darwin,
8 clang,
9 tools ? callPackage ./tools.nix {
10 inherit (stdenv)
11 hostPlatform
12 buildPlatform
13 ;
14 },
15 stdenv,
16 dart,
17 fetchgit,
18 llvmPackages,
19 patchelf,
20 gn,
21 openbox,
22 xorg-server,
23 libxxf86vm,
24 libxrender,
25 libxrandr,
26 libxi,
27 libxinerama,
28 libxfixes,
29 libxext,
30 libxcursor,
31 libx11,
32 xorgproto,
33 libxcb,
34 libglvnd,
35 libepoxy,
36 wayland,
37 freetype,
38 pango,
39 glib,
40 harfbuzz,
41 cairo,
42 gdk-pixbuf,
43 at-spi2-atk,
44 zlib,
45 gtk3,
46 pkg-config,
47 ninja,
48 python312,
49 gitMinimal,
50 version,
51 flutterVersion,
52 dartSdkVersion,
53 swiftshaderHash,
54 swiftshaderRev,
55 hashes,
56 patches,
57 url,
58 runtimeMode ? "release",
59 isOptimized ? runtimeMode != "debug",
60}:
61let
62 constants = callPackage ./constants.nix { platform = stdenv.targetPlatform; };
63
64 python3 = python312;
65
66 src = callPackage ./source.nix {
67 inherit
68 flutterVersion
69 version
70 hashes
71 url
72 ;
73 inherit (stdenv)
74 hostPlatform
75 buildPlatform
76 targetPlatform
77 ;
78 };
79
80 swiftshader = fetchgit {
81 url = "https://swiftshader.googlesource.com/SwiftShader.git";
82 hash = swiftshaderHash;
83 rev = swiftshaderRev;
84
85 postFetch = ''
86 rm --recursive --force $out/third_party/llvm-project
87 '';
88 };
89
90 llvm = symlinkJoin {
91 name = "llvm";
92 paths = [
93 clang
94 llvmPackages.llvm
95 ];
96 };
97
98 outName = "host_${runtimeMode}${lib.optionalString (!isOptimized) "_unopt"}";
99
100 dartPath = "flutter/third_party/dart";
101in
102stdenv.mkDerivation (finalAttrs: {
103 pname = "flutter-engine-${runtimeMode}${lib.optionalString (!isOptimized) "-unopt"}";
104 inherit version src patches;
105
106 setOutputFlags = false;
107 doStrip = isOptimized;
108
109 toolchain = symlinkJoin {
110 name = "flutter-engine-toolchain-${version}";
111
112 paths =
113 lib.flatten (
114 map (dep: lib.optionals (lib.isDerivation dep) ([ dep ] ++ map (output: dep.${output}) dep.outputs))
115 (
116 lib.optionals (stdenv.hostPlatform.isLinux) [
117 gtk3
118 wayland
119 libepoxy
120 libglvnd
121 freetype
122 at-spi2-atk
123 glib
124 gdk-pixbuf
125 harfbuzz
126 pango
127 cairo
128 libxcb
129 libx11
130 libxcursor
131 libxrandr
132 libxrender
133 libxinerama
134 libxi
135 libxext
136 libxfixes
137 libxxf86vm
138 xorgproto
139 zlib
140 ]
141 ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
142 clang
143 llvm
144 ]
145 )
146 )
147 ++ [
148 stdenv.cc.libc_dev
149 stdenv.cc.libc_lib
150 ];
151
152 # Needed due to Flutter expecting everything to be relative to $out
153 # and not true absolute path (ie relative to "/").
154 postBuild = ''
155 mkdir --parents $(dirname $(dirname "$out/$out"))
156 ln --symbolic $(dirname "$out") $out/$(dirname "$out")
157 '';
158 };
159
160 NIX_CFLAGS_COMPILE = [
161 "-I${finalAttrs.toolchain}/include"
162 "-O2"
163 "-Wno-error"
164 "-Wno-absolute-value"
165 "-Wno-implicit-float-conversion"
166 ]
167 ++ lib.optional (!isOptimized) "-U_FORTIFY_SOURCE";
168
169 nativeCheckInputs = lib.optionals stdenv.hostPlatform.isLinux [
170 xorg-server
171 openbox
172 ];
173
174 nativeBuildInputs = [
175 (python3.withPackages (
176 ps: with ps; [
177 pyyaml
178 ]
179 ))
180 (tools.vpython python3)
181 gitMinimal
182 pkg-config
183 ninja
184 dart
185 ]
186 ++ lib.optionals (stdenv.hostPlatform.isLinux) [ patchelf ]
187 ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
188 darwin.system_cmds
189 darwin.xcode
190 tools.xcode-select
191 ]
192 ++ lib.optionals (stdenv.cc.libc ? bin) [ stdenv.cc.libc.bin ];
193
194 buildInputs = [
195 gtk3
196 libepoxy
197 ]
198 ++ lib.optionals (lib.versionAtLeast flutterVersion "3.41") [
199 at-spi2-atk
200 glib
201 ];
202
203 dontPatch = true;
204
205 env.patchgit = toString [
206 dartPath
207 "flutter"
208 "."
209 "flutter/third_party/skia"
210 ];
211
212 postUnpack =
213 lib.optionalString (lib.versionAtLeast flutterVersion "3.38") ''
214 chmod +w .
215 mkdir --parents bin/internal
216 echo '#!${lib.getExe bash}' > bin/internal/content_aware_hash.sh
217 echo 'echo 1111111111111111111111111111111111111111' >> bin/internal/content_aware_hash.sh
218 chmod +x bin/internal/content_aware_hash.sh
219 ''
220 + ''
221 pushd ${finalAttrs.src.name}
222
223 cp ${
224 fetchurl {
225 url = "https://raw.githubusercontent.com/chromium/chromium/631a813125b886a52274653144019fd1681a0e97/build/config/linux/pkg-config.py";
226 hash = "sha256-9coRpgCewlkFXSGrMVkudaZUll0IFc9jDRBP+2PloOI=";
227 }
228 } src/build/config/linux/pkg-config.py
229
230 cp --preserve=mode,ownership,timestamps --recursive --reflink=auto ${swiftshader} src/flutter/third_party/swiftshader
231 chmod --recursive u+w -- src/flutter/third_party/swiftshader
232
233 ln --symbolic ${llvmPackages.llvm.monorepoSrc} src/flutter/third_party/swiftshader/third_party/llvm-project
234
235 mkdir --parents src/flutter/buildtools/${constants.alt-platform}
236 ln --symbolic ${llvm} src/flutter/buildtools/${constants.alt-platform}/clang
237
238 mkdir --parents src/buildtools/${constants.alt-platform}
239 ln --symbolic ${llvm} src/buildtools/${constants.alt-platform}/clang
240
241 mkdir --parents src/${dartPath}/tools/sdks
242 ln --symbolic ${dart} src/${dartPath}/tools/sdks/dart-sdk
243
244 mkdir --parents src/flutter/third_party/gn/
245 ln --symbolic --force ${lib.getExe gn} src/flutter/third_party/gn/gn
246
247 for dir in ''${patchgit[@]}; do
248 pushd src/$dir
249 rm --recursive --force .git
250 git init
251 git add .
252 git config user.name "nobody"
253 git config user.email "nobody@local.host"
254 git commit --all --message="$dir" --quiet
255 popd
256 done
257
258 dart src/${dartPath}/tools/generate_package_config.dart
259 echo "${dartSdkVersion}" >src/${dartPath}/sdk/version
260 python3 src/flutter/third_party/dart/tools/generate_sdk_version_file.py
261 rm --recursive --force src/third_party/angle/.git
262 python3 src/flutter/tools/pub_get_offline.py
263
264 pushd src/flutter
265
266 for p in ''${patches[@]}; do
267 patch -p1 -i $p
268 done
269
270 popd
271 ''
272 # error: 'close_range' is missing exception specification 'noexcept(true)'
273 + lib.optionalString (lib.versionAtLeast flutterVersion "3.35") ''
274 substituteInPlace src/flutter/third_party/dart/runtime/bin/process_linux.cc \
275 --replace-fail "(unsigned int first, unsigned int last, int flags)" "(unsigned int first, unsigned int last, int flags) noexcept(true)"
276 ''
277 # src/flutter/third_party/libcxx/include/__type_traits/is_referenceable.h:33:1: error: templates must have C++ linkage
278 + lib.optionalString (lib.versionAtLeast flutterVersion "3.41") ''
279 substituteInPlace src/flutter/shell/platform/linux/fl_view_accessible.cc \
280 --replace-fail "// Workaround missing C code compatibility in ATK header." "#include <glib.h>"
281 ''
282 + ''
283 popd
284 '';
285
286 configureFlags = [
287 "--no-prebuilt-dart-sdk"
288 "--embedder-for-target"
289 "--no-goma"
290 "--no-dart-version-git-info"
291 ]
292 ++ lib.optionals (stdenv.targetPlatform.isx86_64 == false) [
293 "--linux"
294 "--linux-cpu ${constants.alt-arch}"
295 ]
296 ++ lib.optional (!isOptimized) "--unoptimized"
297 ++ lib.optional (runtimeMode == "debug") "--no-stripped"
298 ++ lib.optional finalAttrs.finalPackage.doCheck "--enable-unittests"
299 ++ lib.optional (!finalAttrs.finalPackage.doCheck) "--no-enable-unittests";
300
301 configurePhase = ''
302 runHook preConfigure
303
304 export PYTHONPATH=$src/src/build
305 ''
306 + lib.optionalString stdenv.hostPlatform.isDarwin ''
307 export PATH=${darwin.xcode}/Contents/Developer/usr/bin/:$PATH
308 ''
309 + ''
310 python3 ./src/flutter/tools/gn $configureFlags \
311 --runtime-mode ${runtimeMode} \
312 --out-dir $out \
313 --target-sysroot ${finalAttrs.toolchain} \
314 --target-dir ${outName} \
315 --target-triple ${stdenv.targetPlatform.config} \
316 --enable-fontconfig
317
318 runHook postConfigure
319 '';
320
321 buildPhase = ''
322 runHook preBuild
323
324 export TERM=dumb
325 ''
326 # ValueError: ZIP does not support timestamps before 1980
327 + ''
328 substituteInPlace src/flutter/build/zip.py \
329 --replace-fail "zipfile.ZipFile(args.output, 'w', zipfile.ZIP_DEFLATED)" "zipfile.ZipFile(args.output, 'w', zipfile.ZIP_DEFLATED, strict_timestamps=False)"
330 ''
331 + ''
332 ninja -C $out/out/${outName} -j$NIX_BUILD_CORES
333
334 runHook postBuild
335 '';
336
337 # Tests are broken
338 doCheck = false;
339 checkPhase = ''
340 runHook preCheck
341
342 ln --symbolic $out/out src/out
343 touch src/out/run_tests.log
344 sh src/flutter/testing/run_tests.sh ${outName}
345 rm src/out/run_tests.log
346
347 runHook postCheck
348 '';
349
350 installPhase = ''
351 runHook preInstall
352
353 rm --recursive --force $out/out/${outName}/{obj,exe.unstripped,lib.unstripped,zip_archives}
354 rm $out/out/${outName}/{args.gn,build.ninja,build.ninja.d,compile_commands.json,toolchain.ninja}
355 find $out/out/${outName} -name '*_unittests' -delete
356 find $out/out/${outName} -name '*_benchmarks' -delete
357 ''
358 + lib.optionalString (finalAttrs.finalPackage.doCheck) ''
359 rm $out/out/${outName}/{display_list_rendertests,flutter_tester}
360 ''
361 + ''
362 runHook postInstall
363 '';
364
365 passthru = {
366 inherit
367 dartSdkVersion
368 isOptimized
369 runtimeMode
370 outName
371 swiftshader
372 ;
373 dart = callPackage ./dart.nix { engine = finalAttrs.finalPackage; };
374 };
375
376 meta = {
377 # Very broken on Darwin
378 broken = stdenv.hostPlatform.isDarwin;
379 description = "Flutter engine";
380 homepage = "https://flutter.dev";
381 maintainers = with lib.maintainers; [ RossComputerGuy ];
382 license = lib.licenses.bsd3;
383 platforms = [
384 "x86_64-linux"
385 "aarch64-linux"
386 "x86_64-darwin"
387 "aarch64-darwin"
388 ];
389 };
390})