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