1{ lib
2, stdenv
3, callPackage
4, flutter
5, supportsLinuxDesktop ? stdenv.hostPlatform.isLinux
6, supportsAndroid ? stdenv.hostPlatform.isx86_64
7, includedEngineArtifacts ? null
8, extraPkgConfigPackages ? [ ]
9, extraLibraries ? [ ]
10, extraIncludes ? [ ]
11, extraCxxFlags ? [ ]
12, extraCFlags ? [ ]
13, extraLinkerFlags ? [ ]
14, makeWrapper
15, runCommandLocal
16, writeShellScript
17, git
18, which
19, pkg-config
20, atk
21, cairo
22, gdk-pixbuf
23, glib
24, gtk3
25, harfbuzz
26, libepoxy
27, pango
28, libX11
29, xorgproto
30, libdeflate
31, zlib
32, cmake
33, ninja
34, clang
35}:
36
37let
38 flutterWithArtifacts = flutter.override {
39 includedEngineArtifacts = if includedEngineArtifacts != null then includedEngineArtifacts else {
40 common = [
41 "flutter_patched_sdk"
42 "flutter_patched_sdk_product"
43 ];
44 platform = {
45 android = lib.optionalAttrs supportsAndroid
46 ((lib.genAttrs [ "arm" "arm64" "x64" ] (architecture: [ "profile" "release" ])) // { x86 = [ "jit-release" ]; });
47 linux = lib.optionalAttrs supportsLinuxDesktop
48 (lib.genAttrs ((lib.optional stdenv.hostPlatform.isx86_64 "x64") ++ (lib.optional stdenv.hostPlatform.isAarch64 "arm64"))
49 (architecture: [ "debug" "profile" "release" ]));
50 };
51 };
52 };
53
54 # By default, Flutter stores downloaded files (such as the Pub cache) in the SDK directory.
55 # Wrap it to ensure that it does not do that, preferring home directories instead.
56 immutableFlutter = writeShellScript "flutter_immutable" ''
57 export PUB_CACHE=''${PUB_CACHE:-"$HOME/.pub-cache"}
58 ${flutterWithArtifacts}/bin/flutter "$@"
59 '';
60
61 # Tools that the Flutter tool depends on.
62 tools = [ git which ];
63
64 # Libraries that Flutter apps depend on at runtime.
65 appRuntimeDeps = lib.optionals supportsLinuxDesktop [
66 atk
67 cairo
68 gdk-pixbuf
69 glib
70 gtk3
71 harfbuzz
72 libepoxy
73 pango
74 libX11
75 libdeflate
76 ];
77
78 # Development packages required for compilation.
79 appBuildDeps =
80 let
81 # https://discourse.nixos.org/t/handling-transitive-c-dependencies/5942/3
82 deps = pkg: builtins.filter lib.isDerivation ((pkg.buildInputs or [ ]) ++ (pkg.propagatedBuildInputs or [ ]));
83 collect = pkg: lib.unique ([ pkg ] ++ deps pkg ++ builtins.concatMap collect (deps pkg));
84 in
85 builtins.concatMap collect appRuntimeDeps;
86
87 # Some header files and libraries are not properly located by the Flutter SDK.
88 # They must be manually included.
89 appStaticBuildDeps = (lib.optionals supportsLinuxDesktop [ libX11 xorgproto zlib ]) ++ extraLibraries;
90
91 # Tools used by the Flutter SDK to compile applications.
92 buildTools = lib.optionals supportsLinuxDesktop [
93 pkg-config
94 cmake
95 ninja
96 clang
97 ];
98
99 # Nix-specific compiler configuration.
100 pkgConfigPackages = map (lib.getOutput "dev") (appBuildDeps ++ extraPkgConfigPackages);
101 includeFlags = map (pkg: "-isystem ${lib.getOutput "dev" pkg}/include") (appStaticBuildDeps ++ extraIncludes);
102 linkerFlags = (map (pkg: "-rpath,${lib.getOutput "lib" pkg}/lib") appRuntimeDeps) ++ extraLinkerFlags;
103in
104(callPackage ./sdk-symlink.nix { }) (runCommandLocal "flutter-wrapped"
105{
106 nativeBuildInputs = [ makeWrapper ];
107
108 passthru = flutterWithArtifacts.passthru // {
109 inherit (flutterWithArtifacts) version;
110 unwrapped = flutterWithArtifacts;
111 };
112
113 inherit (flutterWithArtifacts) meta;
114} ''
115 for path in ${builtins.concatStringsSep " " (builtins.foldl' (paths: pkg: paths ++ (map (directory: "'${pkg}/${directory}/pkgconfig'") ["lib" "share"])) [ ] pkgConfigPackages)}; do
116 addToSearchPath FLUTTER_PKG_CONFIG_PATH "$path"
117 done
118
119 mkdir -p $out/bin
120 makeWrapper '${immutableFlutter}' $out/bin/flutter \
121 --set-default ANDROID_EMULATOR_USE_SYSTEM_LIBS 1 \
122 --suffix PATH : '${lib.makeBinPath (tools ++ buildTools)}' \
123 --suffix PKG_CONFIG_PATH : "$FLUTTER_PKG_CONFIG_PATH" \
124 --suffix LIBRARY_PATH : '${lib.makeLibraryPath appStaticBuildDeps}' \
125 --prefix CXXFLAGS "''\t" '${builtins.concatStringsSep " " (includeFlags ++ extraCxxFlags)}' \
126 --prefix CFLAGS "''\t" '${builtins.concatStringsSep " " (includeFlags ++ extraCFlags)}' \
127 --prefix LDFLAGS "''\t" '${builtins.concatStringsSep " " (map (flag: "-Wl,${flag}") linkerFlags)}'
128'')