-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.nix
140 lines (119 loc) · 3.22 KB
/
build.nix
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{ lib
, yamlLib
, stdenv
, dart
, fetchzip
, makeWrapper
, runCommand
}:
{ pname
, version
, specFile
, lockFile
, dartFlags ? [ ]
, buildDir ? "build"
, buildType ? "release"
, src ? null
, srcs ? null
, ...
}@args:
assert builtins.pathExists specFile;
assert builtins.pathExists lockFile;
let
specFile' = yamlLib.readYAML specFile;
lockFile' = yamlLib.readYAML lockFile;
pubCache =
let
step = (state: package:
let
pubCachePathParent = lib.concatStringsSep "/" [
"$out"
package.source
(lib.removePrefix "https://" package.description.url)
];
pubCachePath = lib.concatStringsSep "/" [
pubCachePathParent
"${package.description.name}-${package.version}"
];
nixStorePath = fetchzip {
inherit (package) sha256;
stripRoot = false;
url = lib.concatStringsSep "/" [
package.description.url
"packages"
package.description.name
"versions"
"${package.version}.tar.gz"
];
};
in
state + ''
mkdir -p ${pubCachePathParent}
ln -s ${nixStorePath} ${pubCachePath}
''
);
synthesize =
builtins.foldl' step "" (builtins.attrValues lockFile'.packages);
in
runCommand "${pname}-pub-cache" { } synthesize;
dartOpts = with lib;
concatStringsSep " " ((optional (buildType == "debug") "--enable-asserts")
++ [ "-Dversion=${version}" ] ++ dartFlags);
executables = specFile'.executables or { };
buildSnapshots =
let
inherit (lib) concatStringsSep mapAttrsToList;
buildSnapshot = name: path:
with lib; ''
dart ${dartOpts} --snapshot="${buildDir}/${name}.snapshot" "bin/${path}.dart"
'';
steps = mapAttrsToList buildSnapshot executables;
in
concatStringsSep "\n" steps;
installSnapshots =
let
inherit (builtins) attrNames;
inherit (lib) concatStringsSep mapAttrsToList;
installSnapshot = name: ''
cp "${buildDir}/${name}.snapshot" "$out/lib/dart/${pname}/"
makeWrapper "${dart}/bin/dart" "$out/bin/${name}" \
--argv0 "${name}" \
--add-flags "$out/lib/dart/${pname}/${name}.snapshot"
'';
steps = map installSnapshot (attrNames executables);
in
concatStringsSep "\n" steps;
in
stdenv.mkDerivation ({
PUB_CACHE = "${pubCache}";
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ makeWrapper ];
buildInputs = (args.buildInputs or [ ]) ++ [ dart ];
buildPhase = args.buildPhase or ''
runHook preBuild
mkdir -p "${buildDir}"
# Some tooling still expects this file to exist
touch .packages
(
set -x
pub get --no-precompile --offline
${buildSnapshots}
)
runHook postBuild
'';
installPhase = args.installPhase or ''
runHook preInstall
mkdir -p $out/bin $out/lib/dart/${pname}
${installSnapshots}
runHook postInstall
'';
passthru = (args.passthru or { }) // { inherit pubCache; };
meta = { platforms = dart.meta.platforms; } // (args.meta or { });
}
//
(removeAttrs args [
"buildInputs"
"buildPhase"
"installPhase"
"passthru"
"meta"
]))