about summary refs log tree commit diff
path: root/pkgs/build-support/dart/pub2nix/pubspec-lock.nix
blob: fac096de31f7772c442e7a05980271122e4687a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{ lib
, callPackage
, 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 ? { }

  # 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 ? { }
}:

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;

  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
    runCommand "pub-${name}-${details.version}" { passthru.packageRoot = "."; } ''
      mkdir -p "$out"
      tar xf '${archive}' -C "$out"
    '';

  mkGitDependencySource = name: details: (fetchgit {
    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.");
  }).overrideAttrs ({ passthru ? { }, ... }: {
    passthru = passthru // {
      packageRoot = details.description.path;
    };
  });

  mkPathDependencySource = name: details:
    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}";
      };
    });

  mkSdkDependencySource = name: details:
    (sdkSourceBuilders.${details.description} or (throw "No SDK source builder has been given for ${details.description}!")) name;

  addDependencySourceUtils = dependencySource: details: dependencySource.overrideAttrs ({ passthru, ... }: {
    passthru = passthru // {
      inherit (details) version;
    };
  });

  sourceBuilders = callPackage ../../../development/compilers/dart/package-source-builders { } // customSourceBuilders;

  dependencySources = lib.filterAttrs (name: src: src != null) (builtins.mapAttrs
    (name: details:
      (sourceBuilders.${name} or ({ src, ... }: src)) {
        inherit (details) version source;
        src = ((addDependencySourceUtils (({
          "hosted" = mkHostedDependencySource;
          "git" = mkGitDependencySource;
          "path" = mkPathDependencySource;
          "sdk" = mkSdkDependencySource;
        }.${details.source} name) details)) details);
      })
    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;
}