1{ lib
2, callPackage
3, runCommand
4, makeWrapper
5, wrapGAppsHook
6, fetchDartDeps
7, buildDartApplication
8, cacert
9, glib
10, flutter
11}:
12
13# absolutely no mac support for now
14
15{ pubGetScript ? "flutter pub get"
16, flutterBuildFlags ? [ ]
17, extraWrapProgramArgs ? ""
18, ...
19}@args:
20
21(buildDartApplication.override {
22 dart = flutter;
23 fetchDartDeps = fetchDartDeps.override { dart = flutter; };
24}) (args // {
25 sdkSetupScript = ''
26 # Pub needs SSL certificates. Dart normally looks in a hardcoded path.
27 # https://github.com/dart-lang/sdk/blob/3.1.0/runtime/bin/security_context_linux.cc#L48
28 #
29 # Dart does not respect SSL_CERT_FILE...
30 # https://github.com/dart-lang/sdk/issues/48506
31 # ...and Flutter does not support --root-certs-file, so the path cannot be manually set.
32 # https://github.com/flutter/flutter/issues/56607
33 # https://github.com/flutter/flutter/issues/113594
34 #
35 # libredirect is of no use either, as Flutter does not pass any
36 # environment variables (including LD_PRELOAD) to the Pub process.
37 #
38 # Instead, Flutter is patched to allow the path to the Dart binary used for
39 # Pub commands to be overriden.
40 export NIX_FLUTTER_PUB_DART="${runCommand "dart-with-certs" { nativeBuildInputs = [ makeWrapper ]; } ''
41 mkdir -p "$out/bin"
42 makeWrapper ${flutter.dart}/bin/dart "$out/bin/dart" \
43 --add-flags "--root-certs-file=${cacert}/etc/ssl/certs/ca-bundle.crt"
44 ''}/bin/dart"
45
46 export HOME="$NIX_BUILD_TOP"
47 flutter config --no-analytics &>/dev/null # mute first-run
48 flutter config --enable-linux-desktop >/dev/null
49 '';
50
51 nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ wrapGAppsHook ];
52 buildInputs = (args.buildInputs or [ ]) ++ [ glib ];
53
54 dontDartBuild = true;
55 buildPhase = args.buildPhase or ''
56 runHook preBuild
57
58 mkdir -p build/flutter_assets/fonts
59
60 doPubGet flutter pub get --offline -v
61 flutter build linux -v --release --split-debug-info="$debug" ${builtins.concatStringsSep " " (map (flag: "\"${flag}\"") flutterBuildFlags)}
62
63 runHook postBuild
64 '';
65
66 dontDartInstall = true;
67 installPhase = args.installPhase or ''
68 runHook preInstall
69
70 built=build/linux/*/release/bundle
71
72 mkdir -p $out/bin
73 mv $built $out/app
74
75 for f in $(find $out/app -iname "*.desktop" -type f); do
76 install -D $f $out/share/applications/$(basename $f)
77 done
78
79 for f in $(find $out/app -maxdepth 1 -type f); do
80 ln -s $f $out/bin/$(basename $f)
81 done
82
83 # make *.so executable
84 find $out/app -iname "*.so" -type f -exec chmod +x {} +
85
86 # remove stuff like /build/source/packages/ubuntu_desktop_installer/linux/flutter/ephemeral
87 for f in $(find $out/app -executable -type f); do
88 if patchelf --print-rpath "$f" | grep /build; then # this ignores static libs (e,g. libapp.so) also
89 echo "strip RPath of $f"
90 newrp=$(patchelf --print-rpath $f | sed -r "s|/build.*ephemeral:||g" | sed -r "s|/build.*profile:||g")
91 patchelf --set-rpath "$newrp" "$f"
92 fi
93 done
94
95 runHook postInstall
96 '';
97
98 dontWrapGApps = true;
99 extraWrapProgramArgs = ''
100 ''${gappsWrapperArgs[@]} \
101 ${extraWrapProgramArgs}
102 '';
103})