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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
{
lib,
callPackage,
writeText,
symlinkJoin,
darwin,
clang,
llvm,
tools ? callPackage ./tools.nix {
inherit (stdenv)
hostPlatform
buildPlatform
;
},
stdenv,
stdenvNoCC,
dart,
fetchgit,
runCommand,
llvmPackages,
llvmPackages_15,
patchelf,
openbox,
xorg,
libglvnd,
libepoxy,
wayland,
freetype,
pango,
glib,
harfbuzz,
cairo,
gdk-pixbuf,
at-spi2-atk,
zlib,
gtk3,
pkg-config,
ninja,
python312,
python39,
gitMinimal,
version,
flutterVersion,
dartSdkVersion,
swiftshaderHash,
swiftshaderRev,
hashes,
patches,
url,
runtimeMode ? "release",
isOptimized ? runtimeMode != "debug",
}:
let
expandSingleDep =
dep: lib.optionals (lib.isDerivation dep) ([ dep ] ++ map (output: dep.${output}) dep.outputs);
expandDeps = deps: lib.flatten (map expandSingleDep deps);
constants = callPackage ./constants.nix { platform = stdenv.targetPlatform; };
python3 = if lib.versionAtLeast flutterVersion "3.20" then python312 else python39;
src = callPackage ./source.nix {
inherit
tools
flutterVersion
version
hashes
url
;
inherit (stdenv)
hostPlatform
buildPlatform
targetPlatform
;
};
swiftshader = fetchgit {
url = "https://swiftshader.googlesource.com/SwiftShader.git";
hash = swiftshaderHash;
rev = swiftshaderRev;
postFetch = ''
rm -rf $out/third_party/llvm-project
'';
};
llvm = symlinkJoin {
name = "llvm";
paths = with llvmPackages; [
clang
llvmPackages.llvm
];
};
outName = "host_${runtimeMode}${lib.optionalString (!isOptimized) "_unopt"}";
dartPath = "${
if (lib.versionAtLeast flutterVersion "3.23") then "flutter/third_party" else "third_party"
}/dart";
in
stdenv.mkDerivation (finalAttrs: {
pname = "flutter-engine-${runtimeMode}${lib.optionalString (!isOptimized) "-unopt"}";
inherit
version
runtimeMode
patches
isOptimized
dartSdkVersion
src
outName
swiftshader
;
setOutputFlags = false;
doStrip = isOptimized;
toolchain = symlinkJoin {
name = "flutter-engine-toolchain-${version}";
paths =
expandDeps (
lib.optionals (stdenv.hostPlatform.isLinux) [
gtk3
wayland
libepoxy
libglvnd
freetype
at-spi2-atk
glib
gdk-pixbuf
harfbuzz
pango
cairo
xorg.libxcb
xorg.libX11
xorg.libXcursor
xorg.libXrandr
xorg.libXrender
xorg.libXinerama
xorg.libXi
xorg.libXext
xorg.libXfixes
xorg.libXxf86vm
xorg.xorgproto
zlib
]
++ lib.optionals (stdenv.hostPlatform.isDarwin) [
clang
llvm
]
)
++ [
stdenv.cc.libc_dev
stdenv.cc.libc_lib
];
# Needed due to Flutter expecting everything to be relative to $out
# and not true absolute path (ie relative to "/").
postBuild = ''
mkdir -p $(dirname $(dirname "$out/$out"))
ln -s $(dirname "$out") $out/$(dirname "$out")
'';
};
NIX_CFLAGS_COMPILE = [
"-I${finalAttrs.toolchain}/include"
] ++ lib.optional (!isOptimized) "-U_FORTIFY_SOURCE";
nativeCheckInputs = lib.optionals stdenv.hostPlatform.isLinux [
xorg.xorgserver
openbox
];
nativeBuildInputs =
[
(python3.withPackages (
ps: with ps; [
pyyaml
]
))
(tools.vpython python3)
gitMinimal
pkg-config
ninja
dart
]
++ lib.optionals (stdenv.hostPlatform.isLinux) [ patchelf ]
++ lib.optionals (stdenv.hostPlatform.isDarwin) [
darwin.system_cmds
darwin.xcode
tools.xcode-select
]
++ lib.optionals (stdenv.cc.libc ? bin) [ stdenv.cc.libc.bin ];
buildInputs = [ gtk3 ];
patchtools = [ "flutter/third_party/gn/gn" ];
dontPatch = true;
patchgit = [
dartPath
"flutter"
"."
] ++ lib.optional (lib.versionAtLeast flutterVersion "3.21") "flutter/third_party/skia";
postUnpack = ''
pushd ${src.name}
cp ${./pkg-config.py} src/build/config/linux/pkg-config.py
cp -pr --reflink=auto $swiftshader src/flutter/third_party/swiftshader
chmod -R u+w -- src/flutter/third_party/swiftshader
ln -s ${llvmPackages_15.llvm.monorepoSrc} src/flutter/third_party/swiftshader/third_party/llvm-project
mkdir -p src/flutter/buildtools/${constants.alt-platform}
ln -s ${llvm} src/flutter/buildtools/${constants.alt-platform}/clang
mkdir -p src/buildtools/${constants.alt-platform}
ln -s ${llvm} src/buildtools/${constants.alt-platform}/clang
mkdir -p src/${dartPath}/tools/sdks
ln -s ${dart} src/${dartPath}/tools/sdks/dart-sdk
${lib.optionalString (stdenv.hostPlatform.isLinux) ''
for patchtool in ''${patchtools[@]}; do
patchelf src/$patchtool --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker)
done
''}
for dir in ''${patchgit[@]}; do
pushd src/$dir
rm -rf .git
git init
git add .
git config user.name "nobody"
git config user.email "nobody@local.host"
git commit -a -m "$dir" --quiet
popd
done
dart src/${dartPath}/tools/generate_package_config.dart
echo "${dartSdkVersion}" >src/${dartPath}/sdk/version
rm -rf src/third_party/angle/.git
python3 src/flutter/tools/pub_get_offline.py
pushd src/flutter
for p in ''${patches[@]}; do
patch -p1 -i $p
done
popd
popd
'';
configureFlags =
[
"--no-prebuilt-dart-sdk"
"--embedder-for-target"
"--no-goma"
]
++ lib.optionals (stdenv.targetPlatform.isx86_64 == false) [
"--linux"
"--linux-cpu ${constants.alt-arch}"
]
++ lib.optional (!isOptimized) "--unoptimized"
++ lib.optional (runtimeMode == "debug") "--no-stripped"
++ lib.optional finalAttrs.finalPackage.doCheck "--enable-unittests"
++ lib.optional (!finalAttrs.finalPackage.doCheck) "--no-enable-unittests";
# NOTE: Once https://github.com/flutter/flutter/issues/127606 is fixed, use "--no-prebuilt-dart-sdk"
configurePhase =
''
runHook preConfigure
export PYTHONPATH=$src/src/build
''
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
export PATH=${darwin.xcode}/Contents/Developer/usr/bin/:$PATH
''
+ ''
python3 ./src/flutter/tools/gn $configureFlags \
--runtime-mode $runtimeMode \
--out-dir $out \
--target-sysroot $toolchain \
--target-dir $outName \
--target-triple ${stdenv.targetPlatform.config} \
--enable-fontconfig
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
export TERM=dumb
${lib.optionalString (lib.versionAtLeast flutterVersion "3.29") ''
# ValueError: ZIP does not support timestamps before 1980
substituteInPlace src/flutter/build/zip.py \
--replace-fail "zipfile.ZipFile(args.output, 'w', zipfile.ZIP_DEFLATED)" "zipfile.ZipFile(args.output, 'w', zipfile.ZIP_DEFLATED, strict_timestamps=False)"
''}
ninja -C $out/out/$outName -j$NIX_BUILD_CORES
runHook postBuild
'';
# Tests are broken
doCheck = false;
checkPhase = ''
ln -s $out/out src/out
touch src/out/run_tests.log
sh src/flutter/testing/run_tests.sh $outName
rm src/out/run_tests.log
'';
installPhase =
''
runHook preInstall
rm -rf $out/out/$outName/{obj,exe.unstripped,lib.unstripped,zip_archives}
rm $out/out/$outName/{args.gn,build.ninja,build.ninja.d,compile_commands.json,toolchain.ninja}
find $out/out/$outName -name '*_unittests' -delete
find $out/out/$outName -name '*_benchmarks' -delete
''
+ lib.optionalString (finalAttrs.finalPackage.doCheck) ''
rm $out/out/$outName/{display_list_rendertests,flutter_tester}
''
+ ''
runHook postInstall
'';
passthru = {
dart = callPackage ./dart.nix { engine = finalAttrs.finalPackage; };
};
meta =
with lib;
{
# Very broken on Darwin
broken = stdenv.hostPlatform.isDarwin;
description = "The Flutter engine";
homepage = "https://flutter.dev";
maintainers = with maintainers; [ RossComputerGuy ];
license = licenses.bsd3;
platforms = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
}
// lib.optionalAttrs (lib.versionOlder flutterVersion "3.22") { hydraPlatforms = [ ]; };
})
|