2023-10-25 13:29:28 +02:00
|
|
|
{ lib
|
2023-10-28 16:16:08 +02:00
|
|
|
, callPackage
|
2023-10-25 13:29:28 +02:00
|
|
|
, fetchurl
|
|
|
|
, fetchgit
|
|
|
|
, runCommand
|
|
|
|
}:
|
|
|
|
|
|
|
|
{
|
|
|
|
# The source directory of the package.
|
|
|
|
src
|
|
|
|
|
|
|
|
# The package subdirectory within src.
|
|
|
|
# Useful if the package references sibling packages with relative paths.
|
|
|
|
, packageRoot ? "."
|
|
|
|
|
|
|
|
# The pubspec.lock file, in attribute set form.
|
|
|
|
, pubspecLock
|
|
|
|
|
|
|
|
# Hashes for Git dependencies.
|
|
|
|
# Pub does not record these itself, so they must be manually provided.
|
|
|
|
, gitHashes ? { }
|
|
|
|
|
|
|
|
# Functions to generate SDK package sources.
|
|
|
|
# The function names should match the SDK names, and the package name is given as an argument.
|
|
|
|
, sdkSourceBuilders ? { }
|
2023-10-28 16:16:08 +02:00
|
|
|
|
|
|
|
# Functions that create custom package source derivations.
|
|
|
|
#
|
|
|
|
# The function names should match the package names, and the package version,
|
|
|
|
# source, and source files are given in an attribute set argument.
|
|
|
|
#
|
|
|
|
# The passthru of the source derivation should be propagated.
|
|
|
|
, customSourceBuilders ? { }
|
2023-10-25 13:29:28 +02:00
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
dependencyVersions = builtins.mapAttrs (name: details: details.version) pubspecLock.packages;
|
|
|
|
|
|
|
|
dependencyTypes = {
|
|
|
|
"direct main" = "main";
|
|
|
|
"direct dev" = "dev";
|
|
|
|
"direct overridden" = "overridden";
|
|
|
|
"transitive" = "transitive";
|
|
|
|
};
|
|
|
|
|
|
|
|
dependencies = lib.foldlAttrs
|
|
|
|
(dependencies: name: details: dependencies // { ${dependencyTypes.${details.dependency}} = dependencies.${dependencyTypes.${details.dependency}} ++ [ name ]; })
|
|
|
|
(lib.genAttrs (builtins.attrValues dependencyTypes) (dependencyType: [ ]))
|
|
|
|
pubspecLock.packages;
|
|
|
|
|
2023-12-26 20:54:30 +01:00
|
|
|
# fetchTarball fails with "tarball contains an unexpected number of top-level files". This is a workaround.
|
|
|
|
# https://discourse.nixos.org/t/fetchtarball-with-multiple-top-level-directories-fails/20556
|
2023-10-25 13:29:28 +02:00
|
|
|
mkHostedDependencySource = name: details:
|
|
|
|
let
|
|
|
|
archive = fetchurl {
|
|
|
|
name = "pub-${name}-${details.version}.tar.gz";
|
|
|
|
url = "${details.description.url}/packages/${details.description.name}/versions/${details.version}.tar.gz";
|
|
|
|
sha256 = details.description.sha256;
|
|
|
|
};
|
|
|
|
in
|
2023-10-25 15:34:58 +02:00
|
|
|
runCommand "pub-${name}-${details.version}" { passthru.packageRoot = "."; } ''
|
2023-10-25 13:29:28 +02:00
|
|
|
mkdir -p "$out"
|
|
|
|
tar xf '${archive}' -C "$out"
|
|
|
|
'';
|
|
|
|
|
2023-10-25 15:34:58 +02:00
|
|
|
mkGitDependencySource = name: details: (fetchgit {
|
2023-10-25 13:29:28 +02:00
|
|
|
name = "pub-${name}-${details.version}";
|
|
|
|
url = details.description.url;
|
|
|
|
rev = details.description.resolved-ref;
|
|
|
|
hash = gitHashes.${name} or (throw "A Git hash is required for ${name}! Set to an empty string to obtain it.");
|
2023-10-25 15:34:58 +02:00
|
|
|
}).overrideAttrs ({ passthru ? { }, ... }: {
|
|
|
|
passthru = passthru // {
|
|
|
|
packageRoot = details.description.path;
|
|
|
|
};
|
|
|
|
});
|
2023-10-25 13:29:28 +02:00
|
|
|
|
|
|
|
mkPathDependencySource = name: details:
|
2023-10-25 15:34:58 +02:00
|
|
|
assert lib.assertMsg details.description.relative "Only relative paths are supported - ${name} has an absolue path!";
|
|
|
|
(if lib.isDerivation src then src else (runCommand "pub-${name}-${details.version}" { } ''cp -r '${src}' "$out"'')).overrideAttrs ({ passthru ? { }, ... }: {
|
|
|
|
passthru = passthru // {
|
|
|
|
packageRoot = "${packageRoot}/${details.description.path}";
|
|
|
|
};
|
|
|
|
});
|
2023-10-25 13:29:28 +02:00
|
|
|
|
|
|
|
mkSdkDependencySource = name: details:
|
|
|
|
(sdkSourceBuilders.${details.description} or (throw "No SDK source builder has been given for ${details.description}!")) name;
|
|
|
|
|
2023-10-28 13:30:12 +02:00
|
|
|
addDependencySourceUtils = dependencySource: details: dependencySource.overrideAttrs ({ passthru, ... }: {
|
2023-10-26 07:28:54 +02:00
|
|
|
passthru = passthru // {
|
2023-10-28 13:30:12 +02:00
|
|
|
inherit (details) version;
|
2023-10-26 07:28:54 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-10-28 16:16:08 +02:00
|
|
|
sourceBuilders = callPackage ../../../development/compilers/dart/package-source-builders { } // customSourceBuilders;
|
|
|
|
|
2023-10-25 13:29:28 +02:00
|
|
|
dependencySources = lib.filterAttrs (name: src: src != null) (builtins.mapAttrs
|
2023-10-28 13:30:12 +02:00
|
|
|
(name: details:
|
2023-10-28 16:16:08 +02:00
|
|
|
(sourceBuilders.${name} or ({ src, ... }: src)) {
|
|
|
|
inherit (details) version source;
|
|
|
|
src = ((addDependencySourceUtils (({
|
2023-10-28 13:30:12 +02:00
|
|
|
"hosted" = mkHostedDependencySource;
|
|
|
|
"git" = mkGitDependencySource;
|
|
|
|
"path" = mkPathDependencySource;
|
|
|
|
"sdk" = mkSdkDependencySource;
|
2023-10-28 16:16:08 +02:00
|
|
|
}.${details.source} name) details)) details);
|
|
|
|
})
|
2023-10-25 13:29:28 +02:00
|
|
|
pubspecLock.packages);
|
|
|
|
in
|
|
|
|
{
|
|
|
|
inherit
|
|
|
|
# An attribute set of dependency categories to package name lists.
|
|
|
|
dependencies
|
|
|
|
|
|
|
|
# An attribute set of package names to their versions.
|
|
|
|
dependencyVersions
|
|
|
|
|
|
|
|
# An attribute set of package names to their sources.
|
|
|
|
dependencySources;
|
|
|
|
}
|