1# Schema:
2# ${flutterVersion}.${targetPlatform}.${hostPlatform}
3#
4# aarch64-darwin as a host is not yet supported.
5# https://github.com/flutter/flutter/issues/60118
6{
7 lib,
8 runCommand,
9 xorg,
10 cacert,
11 unzip,
12
13 flutterPlatform,
14 systemPlatform,
15 flutter,
16 hash,
17}:
18
19let
20 flutterPlatforms = [
21 "android"
22 "ios"
23 "web"
24 "linux"
25 "windows"
26 "macos"
27 "fuchsia"
28 "universal"
29 ];
30
31 flutter' = flutter.override {
32 # Use a version of Flutter with just enough capabilities to download
33 # artifacts.
34 supportedTargetFlutterPlatforms = [ ];
35
36 # Modify flutter-tool's system platform in order to get the desired platform's hashes.
37 flutter = flutter.unwrapped.override {
38 flutterTools = flutter.unwrapped.tools.override {
39 inherit systemPlatform;
40 };
41 };
42 };
43in
44runCommand "flutter-artifacts-${flutterPlatform}-${systemPlatform}"
45 {
46 nativeBuildInputs = [
47 xorg.lndir
48 flutter'
49 unzip
50 ];
51
52 NIX_FLUTTER_TOOLS_VM_OPTIONS = "--root-certs-file=${cacert}/etc/ssl/certs/ca-bundle.crt";
53 NIX_FLUTTER_OPERATING_SYSTEM =
54 {
55 "x86_64-linux" = "linux";
56 "aarch64-linux" = "linux";
57 "x86_64-darwin" = "macos";
58 "aarch64-darwin" = "macos";
59 }
60 .${systemPlatform};
61
62 outputHash = hash;
63 outputHashMode = "recursive";
64 outputHashAlgo = "sha256";
65
66 passthru = {
67 inherit flutterPlatform;
68 };
69 }
70 (
71 ''
72 export FLUTTER_ROOT="$NIX_BUILD_TOP"
73 lndir -silent '${flutter'}' "$FLUTTER_ROOT"
74 rm -rf "$FLUTTER_ROOT/bin/cache"
75 mkdir "$FLUTTER_ROOT/bin/cache"
76 ''
77 + lib.optionalString (lib.versionAtLeast flutter'.version "3.26") ''
78 mkdir "$FLUTTER_ROOT/bin/cache/dart-sdk"
79 lndir -silent '${flutter'}/bin/cache/dart-sdk' "$FLUTTER_ROOT/bin/cache/dart-sdk"
80 ''
81 # Could not determine engine revision
82 + lib.optionalString (lib.versionAtLeast flutter'.version "3.32") ''
83 cp '${flutter'}/bin/internal/engine.version' "$FLUTTER_ROOT/bin/cache/engine.stamp"
84 ''
85 + ''
86
87 HOME="$(mktemp -d)" flutter precache ${
88 lib.optionalString (
89 flutter ? engine && flutter.engine.meta.available
90 ) "--local-engine ${flutter.engine.outName}"
91 } \
92 -v '--${flutterPlatform}' ${
93 builtins.concatStringsSep " " (map (p: "'--no-${p}'") (lib.remove flutterPlatform flutterPlatforms))
94 }
95
96 rm -rf "$FLUTTER_ROOT/bin/cache/lockfile"
97 ''
98 + lib.optionalString (lib.versionAtLeast flutter'.version "3.26") ''
99 rm -rf "$FLUTTER_ROOT/bin/cache/dart-sdk"
100 ''
101 + ''
102 find "$FLUTTER_ROOT" -type l -lname '${flutter'}/*' -delete
103
104 cp -r bin/cache "$out"
105 ''
106 )