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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# buildLakePackage: build Lean 4 projects that use the Lake build system.
#
# Dependencies can be provided in two ways:
# - `leanDeps`: nix-packaged Lean libraries, injected via
# `lake --packages` and propagated transitively via LEAN_PATH.
# - `lakeHash`: SRI hash for a fetchLakeDeps FOD that clones git
# dependencies listed in lake-manifest.json (like buildGoModule's
# vendorHash). Not needed when all deps are in `leanDeps`.
{
lib,
stdenv,
lean4,
gitMinimal,
cacert,
jq,
writeText,
stdenvNoCC,
}:
let
fetchLakeDeps = import ./fetch-lake-deps.nix {
inherit
lib
stdenvNoCC
gitMinimal
cacert
jq
;
};
in
lib.extendMkDerivation {
constructDrv = stdenv.mkDerivation;
excludeDrvArgNames = [
"lakeHash"
"lakeDeps"
"leanDeps"
"buildTargets"
"isLibrary"
"leanPackageName"
"overrideLakeDepsAttrs"
];
extendDrvArgs =
finalAttrs:
{
pname,
version,
nativeBuildInputs ? [ ],
passthru ? { },
# SRI hash for the Lake dependencies FOD (null = all deps nix-managed).
lakeHash ? null,
# Pre-built Lake dependencies derivation (overrides lakeHash).
lakeDeps ? null,
# Nix-packaged Lean libraries, injected via lake --packages.
leanDeps ? [ ],
# Lake package name as declared in lakefile (defaults to pname).
leanPackageName ? finalAttrs.pname,
# Lake build targets (empty = default targets).
buildTargets ? [ ],
# Library (install .olean tree) or executable (install binaries only).
isLibrary ? true,
# Override the FOD derivation attrs.
overrideLakeDepsAttrs ? (finalAttrs: previousAttrs: { }),
meta ? { },
...
}@args:
let
leanPackageName = args.leanPackageName or finalAttrs.pname;
allLeanDeps = lib.unique (
builtins.concatMap (dep: [ dep ] ++ (dep.passthru.allLeanDeps or [ ])) leanDeps
);
computedLakeDeps =
if lakeDeps != null then
lakeDeps
else if lakeHash == null then
null
else
(fetchLakeDeps {
inherit (finalAttrs) src pname version;
hash = lakeHash;
sourceRoot = finalAttrs.sourceRoot or "";
patches = finalAttrs.patches or [ ];
prePatch = finalAttrs.prePatch or "";
postPatch = finalAttrs.postPatch or "";
excludePackages = builtins.map (dep: dep.passthru.lakePackageName or dep.pname) allLeanDeps;
}).overrideAttrs
(lib.toExtension overrideLakeDepsAttrs);
# Nix-managed dep overrides, generated at eval time.
# --packages takes precedence over .lake/package-overrides.json.
overridesFile = writeText "lake-overrides.json" (
builtins.toJSON {
schemaVersion = "1.2.0";
packages = map (dep: {
type = "path";
name = dep.passthru.lakePackageName or dep.pname;
inherited = false;
dir = "${dep}";
}) allLeanDeps;
}
);
in
{
strictDeps = true;
__structuredAttrs = true;
nativeBuildInputs = nativeBuildInputs ++ [
lean4
gitMinimal
jq
];
propagatedBuildInputs = lib.optionals isLibrary leanDeps;
buildInputs = lib.optionals (!isLibrary) leanDeps;
configurePhase =
args.configurePhase or ''
runHook preConfigure
export HOME="$TMPDIR"
# Disable cloud caching and Reservoir lookups.
export LAKE_NO_CACHE=1
export RESERVOIR_API_URL=""
export LEAN_CC="${stdenv.cc}/bin/cc"
if [ -n "''${LEAN_PATH:-}" ]; then
echo "buildLakePackage: LEAN_PATH=$LEAN_PATH"
fi
${lib.optionalString (computedLakeDeps != null) ''
mkdir -p .lake/packages
for dep in ${computedLakeDeps}/*; do
depName="$(basename "$dep")"
cp -r "$dep" ".lake/packages/$depName"
chmod -R u+w ".lake/packages/$depName"
done
# FOD deps use package-overrides.json (the on-disk mechanism).
# Nix-managed deps use --packages (the CLI mechanism, takes precedence).
jq -n --argjson pkgs "$(
for dep in .lake/packages/*/; do
[ -d "$dep" ] || continue
depName="$(basename "$dep")"
jq -n --arg name "$depName" --arg dir ".lake/packages/$depName" \
'{type: "path", name: $name, inherited: false, dir: $dir}'
done | jq -s '.'
)" '{schemaVersion: "1.2.0", packages: $pkgs}' > .lake/package-overrides.json
''}
runHook postConfigure
'';
buildPhase =
args.buildPhase or ''
runHook preBuild
local targets="${lib.concatStringsSep " " buildTargets}"
echo "buildLakePackage: building ''${targets:-default targets}"
lake build --no-ansi --packages=${overridesFile} $targets
runHook postBuild
'';
installPhase =
args.installPhase or (
if isLibrary then
''
runHook preInstall
# Install the complete Lake package tree.
cp -rT . "$out"
# Remove build-time artifacts.
rm -rf "$out/.lake/packages"
rm -f "$out/.lake/package-overrides.json"
# Reconcile config trace directory naming.
if [ -d "$out/.lake/config/[anonymous]" ]; then
mv "$out/.lake/config/[anonymous]" "$out/.lake/config/${leanPackageName}"
fi
if [ -d "$out/.lake/build/ir" ]; then
find "$out/.lake/build/ir" -name '*.setup.json' -delete
fi
rm -rf "$out/.lake/config"
# Setup hook propagates LEAN_PATH to downstream packages.
mkdir -p "$out/nix-support"
cp ${./setup-hook.sh} "$out/nix-support/setup-hook"
if [ -d "$out/.lake/build/bin" ]; then
mkdir -p "$out/bin"
for exe in "$out/.lake/build/bin"/*; do
if [ -f "$exe" ] && [ -x "$exe" ]; then
ln -s "../.lake/build/bin/$(basename "$exe")" "$out/bin/$(basename "$exe")"
fi
done
fi
runHook postInstall
''
else
''
runHook preInstall
if [ -d .lake/build/bin ]; then
mkdir -p "$out/bin"
find .lake/build/bin -type f -executable \
-exec install -Dm755 {} "$out/bin/" \;
fi
runHook postInstall
''
);
passthru = passthru // {
inherit computedLakeDeps lean4 allLeanDeps;
lakePackageName = leanPackageName;
overrideLakeDepsAttrs = lib.toExtension overrideLakeDepsAttrs;
};
meta = meta // {
# Note: This conflates the platforms that the Lean compiler can run on (a package build system) and the platforms the Lean compiler
# can target (build host)
platforms = meta.platforms or lean4.meta.platforms;
};
};
}
|