about summary refs log tree commit diff
path: root/pkgs/build-support/dart/build-dart-application/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/build-support/dart/build-dart-application/default.nix')
-rw-r--r--pkgs/build-support/dart/build-dart-application/default.nix111
1 files changed, 70 insertions, 41 deletions
diff --git a/pkgs/build-support/dart/build-dart-application/default.nix b/pkgs/build-support/dart/build-dart-application/default.nix
index 2cb193ac6f161..c9b03b28dd465 100644
--- a/pkgs/build-support/dart/build-dart-application/default.nix
+++ b/pkgs/build-support/dart/build-dart-application/default.nix
@@ -1,6 +1,23 @@
-{ lib, stdenv, callPackage, fetchDartDeps, runCommand, symlinkJoin, writeText, dartHooks, makeWrapper, dart, cacert, nodejs, darwin, jq }:
+{ lib
+, stdenv
+, callPackage
+, writeText
+, pub2nix
+, dartHooks
+, makeWrapper
+, dart
+, nodejs
+, darwin
+, jq
+}:
 
-{ sdkSetupScript ? ""
+{ src
+, sourceRoot ? "source"
+, packageRoot ? (lib.removePrefix "/" (lib.removePrefix "source" sourceRoot))
+, gitHashes ? { }
+, sdkSourceBuilders ? { }
+
+, sdkSetupScript ? ""
 , pubGetScript ? "dart pub get"
 
   # Output type to produce. Can be any kind supported by dart
@@ -29,33 +46,42 @@
 , customPackageOverrides ? { }
 , autoDepsList ? false
 , depsListFile ? null
-, pubspecLockFile ? null
-, vendorHash ? ""
+, pubspecLock
 , ...
 }@args:
 
 let
-  dartDeps = (fetchDartDeps.override {
-    dart = symlinkJoin {
-      name = "dart-sdk-fod";
-      paths = [
-        (runCommand "dart-fod" { nativeBuildInputs = [ makeWrapper ]; } ''
-          mkdir -p "$out/bin"
-          makeWrapper "${dart}/bin/dart" "$out/bin/dart" \
-            --add-flags "--root-certs-file=${cacert}/etc/ssl/certs/ca-bundle.crt"
-        '')
-        dart
-      ];
-    };
-  }) {
-    buildDrvArgs = args;
-    inherit sdkSetupScript pubGetScript vendorHash pubspecLockFile;
+  generators = callPackage ./generators.nix { inherit dart; } { inherit sdkSetupScript; buildDrvArgs = args; };
+
+  depsList = if depsListFile == null then null else lib.importJSON depsListFile;
+  generatedDepsList = generators.mkDepsList { inherit pubspecLockFile pubspecLockData packageConfig; };
+
+  pubspecLockFile = builtins.toJSON pubspecLock;
+  pubspecLockData = pub2nix.readPubspecLock { inherit src packageRoot pubspecLock gitHashes sdkSourceBuilders; };
+  packageConfig = pub2nix.generatePackageConfig {
+    pname = if args.pname != null then "${args.pname}-${args.version}" else null;
+
+    dependencies =
+      # Ideally, we'd only include the main dependencies and their transitive
+      # dependencies.
+      #
+      # The pubspec.lock file does not contain information about where
+      # transitive dependencies come from, though, and it would be weird to
+      # include the transitive dependencies of dev and override dependencies
+      # without including the dev and override dependencies themselves.
+      builtins.concatLists (builtins.attrValues pubspecLockData.dependencies);
+
+    inherit (pubspecLockData) dependencySources;
   };
+
   inherit (dartHooks.override { inherit dart; }) dartConfigHook dartBuildHook dartInstallHook dartFixupHook;
 
-  baseDerivation = stdenv.mkDerivation (finalAttrs: args // {
-    inherit sdkSetupScript pubGetScript dartCompileCommand dartOutputType
-      dartRuntimeCommand dartCompileFlags dartJitFlags runtimeDependencies;
+  baseDerivation = stdenv.mkDerivation (finalAttrs: (builtins.removeAttrs args [ "gitHashes" "sdkSourceBuilders" "pubspecLock" ]) // {
+    inherit pubspecLockFile packageConfig sdkSetupScript pubGetScript
+      dartCompileCommand dartOutputType dartRuntimeCommand dartCompileFlags
+      dartJitFlags runtimeDependencies;
+
+    outputs = args.outputs or [ ] ++ [ "out" "pubcache" ];
 
     dartEntryPoints =
       if (dartEntryPoints != null)
@@ -66,7 +92,6 @@ let
 
     nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
       dart
-      dartDeps
       dartConfigHook
       dartBuildHook
       dartInstallHook
@@ -77,25 +102,32 @@ let
       darwin.sigtool
     ];
 
-    preUnpack = ''
-      ${lib.optionalString (!autoDepsList) ''
-        if ! { [ '${lib.boolToString (depsListFile != null)}' = 'true' ] ${lib.optionalString (depsListFile != null) "&& cmp -s <(jq -Sc . '${depsListFile}') <(jq -Sc . '${finalAttrs.passthru.dartDeps.depsListFile}')"}; }; then
-          echo 1>&2 -e '\nThe dependency list file was either not given or differs from the expected result.' \
-                      '\nPlease choose one of the following solutions:' \
-                      '\n - Duplicate the following file and pass it to the depsListFile argument.' \
-                      '\n   ${finalAttrs.passthru.dartDeps.depsListFile}' \
-                      '\n - Set autoDepsList to true (not supported by Hydra or permitted in Nixpkgs)'.
-          exit 1
-        fi
-      ''}
-      ${args.preUnpack or ""}
+    preConfigure = args.preConfigure or "" + ''
+      ln -sf "$pubspecLockFilePath" pubspec.lock
+    '';
+
+    preBuild = args.preBuild or "" + lib.optionalString (!autoDepsList) ''
+      if ! { [ '${lib.boolToString (depsListFile != null)}' = 'true' ] ${lib.optionalString (depsListFile != null) "&& cmp -s <(jq -Sc . '${depsListFile}') <(jq -Sc . deps.json)"}; }; then
+        echo 1>&2 -e '\nThe dependency list file was either not given or differs from the expected result.' \
+                     '\nPlease choose one of the following solutions:' \
+                     '\n - Duplicate the following file and pass it to the depsListFile argument.' \
+                     '\n   ${generatedDepsList}' \
+                     '\n - Set autoDepsList to true (not supported by Hydra or permitted in Nixpkgs)'.
+        exit 1
+      fi
     '';
 
     # When stripping, it seems some ELF information is lost and the dart VM cli
     # runs instead of the expected program. Don't strip if it's an exe output.
     dontStrip = args.dontStrip or (dartOutputType == "exe");
 
-    passthru = { inherit dartDeps; } // (args.passthru or { });
+    passAsFile = [ "pubspecLockFile" ];
+
+    passthru = {
+      pubspecLock = pubspecLockData;
+      depsList = generatedDepsList;
+      generatePubspecLock = generators.generatePubspecLock { inherit pubGetScript; };
+    } // (args.passthru or { });
 
     meta = (args.meta or { }) // { platforms = args.meta.platforms or dart.meta.platforms; };
   });
@@ -103,11 +135,8 @@ let
   packageOverrideRepository = (callPackage ../../../development/compilers/dart/package-overrides { }) // customPackageOverrides;
   productPackages = builtins.filter (package: package.kind != "dev")
     (if autoDepsList
-    then lib.importJSON dartDeps.depsListFile
-    else
-      if depsListFile == null
-      then [ ]
-      else lib.importJSON depsListFile);
+    then lib.importJSON generatedDepsList
+    else if depsList == null then [ ] else depsList);
 in
 assert !(builtins.isString dartOutputType && dartOutputType != "") ->
 throw "dartOutputType must be a non-empty string";