summary refs log tree commit diff
path: root/pkgs/development/compilers/flutter/wrapper.nix
diff options
context:
space:
mode:
authorFlafyDev <flafyarazi@gmail.com>2023-06-30 15:00:56 +0300
committerFlafyDev <flafyarazi@gmail.com>2023-06-30 15:23:34 +0300
commit570f3efd1d447597fd1d468188d785dfe1207b44 (patch)
tree0b84675a4fdaef6e98ea37609b7408b9d50eda4d /pkgs/development/compilers/flutter/wrapper.nix
parent4ac061369b96d09030aa16725260671ac76f5ff7 (diff)
downloadnixpkgs-570f3efd1d447597fd1d468188d785dfe1207b44.tar
nixpkgs-570f3efd1d447597fd1d468188d785dfe1207b44.tar.gz
nixpkgs-570f3efd1d447597fd1d468188d785dfe1207b44.tar.bz2
nixpkgs-570f3efd1d447597fd1d468188d785dfe1207b44.tar.lz
nixpkgs-570f3efd1d447597fd1d468188d785dfe1207b44.tar.xz
nixpkgs-570f3efd1d447597fd1d468188d785dfe1207b44.tar.zst
nixpkgs-570f3efd1d447597fd1d468188d785dfe1207b44.zip
flutter: Separate cache and unwrapped derivations
flutter-unwrapped will now not come with engine artifacts in its cache directory(`$out/bin/cache`).

To specify a different cache directory, set FLUTTER_CACHE_DIR.

Flutter's wrapper now sets FLUTTER_CACHE_DIR to set engine artifacts.

The sh file `$out/bin/internal/shared.sh` runs when launching Flutter and calls `"$FLUTTER_ROOT/bin/cache/` instead of our environment variable `FLUTTER_CACHE_DIR`.
I decided not to patch it since the script doesn't require engine artifacts(which are the only thing not added by the unwrapped derivation), so it shouldn't fail, and patching it will just be harder to maintain.
Diffstat (limited to 'pkgs/development/compilers/flutter/wrapper.nix')
-rw-r--r--pkgs/development/compilers/flutter/wrapper.nix95
1 files changed, 75 insertions, 20 deletions
diff --git a/pkgs/development/compilers/flutter/wrapper.nix b/pkgs/development/compilers/flutter/wrapper.nix
index 81e5e6e4fdc..50c522fd0e0 100644
--- a/pkgs/development/compilers/flutter/wrapper.nix
+++ b/pkgs/development/compilers/flutter/wrapper.nix
@@ -4,7 +4,19 @@
 , flutter
 , supportsLinuxDesktop ? stdenv.hostPlatform.isLinux
 , supportsAndroid ? stdenv.hostPlatform.isx86_64
-, includedEngineArtifacts ? null
+, includedEngineArtifacts ? {
+    common = [
+      "flutter_patched_sdk"
+      "flutter_patched_sdk_product"
+    ];
+    platform = {
+      android = lib.optionalAttrs supportsAndroid
+        ((lib.genAttrs [ "arm" "arm64" "x64" ] (architecture: [ "profile" "release" ])) // { x86 = [ "jit-release" ]; });
+      linux = lib.optionalAttrs supportsLinuxDesktop
+        (lib.genAttrs ((lib.optional stdenv.hostPlatform.isx86_64 "x64") ++ (lib.optional stdenv.hostPlatform.isAarch64 "arm64"))
+          (architecture: [ "debug" "profile" "release" ]));
+    };
+  }
 , extraPkgConfigPackages ? [ ]
 , extraLibraries ? [ ]
 , extraIncludes ? [ ]
@@ -32,30 +44,73 @@
 , cmake
 , ninja
 , clang
+, lndir
+, symlinkJoin
 }:
 
 let
-  flutterWithArtifacts = flutter.override {
-    includedEngineArtifacts = if includedEngineArtifacts != null then includedEngineArtifacts else {
-      common = [
-        "flutter_patched_sdk"
-        "flutter_patched_sdk_product"
-      ];
-      platform = {
-        android = lib.optionalAttrs supportsAndroid
-          ((lib.genAttrs [ "arm" "arm64" "x64" ] (architecture: [ "profile" "release" ])) // { x86 = [ "jit-release" ]; });
-        linux = lib.optionalAttrs supportsLinuxDesktop
-          (lib.genAttrs ((lib.optional stdenv.hostPlatform.isx86_64 "x64") ++ (lib.optional stdenv.hostPlatform.isAarch64 "arm64"))
-            (architecture: [ "debug" "profile" "release" ]));
-      };
-    };
+  engineArtifacts = callPackage ./engine-artifacts { inherit (flutter) engineVersion; };
+  mkCommonArtifactLinkCommand = { artifact }:
+    ''
+      mkdir -p $out/artifacts/engine/common
+      lndir -silent ${artifact} $out/artifacts/engine/common
+    '';
+  mkPlatformArtifactLinkCommand = { artifact, os, architecture, variant ? null }:
+    let
+      artifactDirectory = "${os}-${architecture}${lib.optionalString (variant != null) "-${variant}"}";
+    in
+    ''
+      mkdir -p $out/artifacts/engine/${artifactDirectory}
+      lndir -silent ${artifact} $out/artifacts/engine/${artifactDirectory}
+    '';
+  engineArtifactDirectory =
+    runCommandLocal "flutter-engine-artifacts-${flutter.version}" { nativeBuildInputs = [ lndir ]; }
+      (
+        builtins.concatStringsSep "\n"
+          ((map
+            (name: mkCommonArtifactLinkCommand {
+              artifact = engineArtifacts.common.${name};
+            })
+            (includedEngineArtifacts.common or [ ])) ++
+          (builtins.foldl'
+            (commands: os: commands ++
+              (builtins.foldl'
+                (commands: architecture: commands ++
+                  (builtins.foldl'
+                    (commands: variant: commands ++
+                      (map
+                        (artifact: mkPlatformArtifactLinkCommand {
+                          inherit artifact os architecture variant;
+                        })
+                        engineArtifacts.platform.${os}.${architecture}.variants.${variant}))
+                    (map
+                      (artifact: mkPlatformArtifactLinkCommand {
+                        inherit artifact os architecture;
+                      })
+                      engineArtifacts.platform.${os}.${architecture}.base)
+                    includedEngineArtifacts.platform.${os}.${architecture}))
+                [ ]
+                (builtins.attrNames includedEngineArtifacts.platform.${os})))
+            [ ]
+            (builtins.attrNames (includedEngineArtifacts.platform or { }))))
+      );
+
+  cacheDir = symlinkJoin {
+    name = "flutter-cache-dir";
+    paths = [
+      engineArtifactDirectory
+      "${flutter}/bin/cache"
+    ];
   };
 
   # By default, Flutter stores downloaded files (such as the Pub cache) in the SDK directory.
   # Wrap it to ensure that it does not do that, preferring home directories instead.
+  # The sh file `$out/bin/internal/shared.sh` runs when launching Flutter and calls `"$FLUTTER_ROOT/bin/cache/` instead of our environment variable `FLUTTER_CACHE_DIR`.
+  # We do not patch it since the script doesn't require engine artifacts(which are the only thing not added by the unwrapped derivation), so it shouldn't fail, and patching it will just be harder to maintain.
   immutableFlutter = writeShellScript "flutter_immutable" ''
     export PUB_CACHE=''${PUB_CACHE:-"$HOME/.pub-cache"}
-    ${flutterWithArtifacts}/bin/flutter "$@"
+    export FLUTTER_CACHE_DIR=${cacheDir}
+    ${flutter}/bin/flutter "$@"
   '';
 
   # Tools that the Flutter tool depends on.
@@ -105,12 +160,12 @@ in
 {
   nativeBuildInputs = [ makeWrapper ];
 
-  passthru = flutterWithArtifacts.passthru // {
-    inherit (flutterWithArtifacts) version;
-    unwrapped = flutterWithArtifacts;
+  passthru = flutter.passthru // {
+    inherit (flutter) version;
+    unwrapped = flutter;
   };
 
-  inherit (flutterWithArtifacts) meta;
+  inherit (flutter) meta;
 } ''
   for path in ${builtins.concatStringsSep " " (builtins.foldl' (paths: pkg: paths ++ (map (directory: "'${pkg}/${directory}/pkgconfig'") ["lib" "share"])) [ ] pkgConfigPackages)}; do
     addToSearchPath FLUTTER_PKG_CONFIG_PATH "$path"