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 + ''
82
83 HOME="$(mktemp -d)" flutter precache ${
84 lib.optionalString (
85 flutter ? engine && flutter.engine.meta.available
86 ) "--local-engine ${flutter.engine.outName}"
87 } \
88 -v '--${flutterPlatform}' ${
89 builtins.concatStringsSep " " (map (p: "'--no-${p}'") (lib.remove flutterPlatform flutterPlatforms))
90 }
91
92 rm -rf "$FLUTTER_ROOT/bin/cache/lockfile"
93 ''
94 + lib.optionalString (lib.versionAtLeast flutter'.version "3.26") ''
95 rm -rf "$FLUTTER_ROOT/bin/cache/dart-sdk"
96 ''
97 + ''
98 find "$FLUTTER_ROOT" -type l -lname '${flutter'}/*' -delete
99
100 cp -r bin/cache "$out"
101 ''
102 )