-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
166 lines (144 loc) · 5.71 KB
/
flake.nix
File metadata and controls
166 lines (144 loc) · 5.71 KB
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
{
description = "curl-lean - Lean 4 bindings for libcurl with WebSocket support";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
zlog-lean.url = "github:marcellop71/zlog-lean";
};
outputs = { self, nixpkgs, flake-utils, zlog-lean }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
# Platform-specific Lean 4 binary
leanVersion = "4.27.0";
leanPlatform = if pkgs.stdenv.isDarwin then "darwin" else "linux";
leanArch = if pkgs.stdenv.isDarwin then "darwin" else "linux";
leanSha256 = if pkgs.stdenv.isDarwin
then "sha256-5MpUHYaIHDVJfLbmwaITWPA6Syz7Lo1OFOWNwqCoBa4="
else "sha256-BW4tyFZPwGSoAeafPrGMBEubVGvIsOWiwAJH+KHLjOY=";
lean4Bin = pkgs.stdenv.mkDerivation {
pname = "lean4";
version = leanVersion;
src = pkgs.fetchurl {
url = "https://github.com/leanprover/lean4/releases/download/v${leanVersion}/lean-${leanVersion}-${leanPlatform}.tar.zst";
sha256 = leanSha256;
};
nativeBuildInputs = [ pkgs.zstd ]
++ pkgs.lib.optionals pkgs.stdenv.isLinux [ pkgs.autoPatchelfHook ];
buildInputs = pkgs.lib.optionals pkgs.stdenv.isLinux [ pkgs.stdenv.cc.cc.lib pkgs.zlib ];
unpackPhase = ''
tar --zstd -xf $src
'';
installPhase = ''
mkdir -p $out
cp -r lean-${leanVersion}-${leanArch}/* $out/
'';
};
# Library path variable name (different on Darwin vs Linux)
libPathVar = if pkgs.stdenv.isDarwin then "DYLD_LIBRARY_PATH" else "LD_LIBRARY_PATH";
leanBin = lean4Bin;
lakeBin = lean4Bin;
zlogDeps = zlog-lean.lib.${system};
# Native dependencies for building
nativeDeps = [
zlogDeps.zlog
pkgs.curl
pkgs.gmp
];
# Development shell with all dependencies
devShell = pkgs.mkShell {
buildInputs = nativeDeps ++ [
leanBin
lakeBin
pkgs.clang
pkgs.lld
];
"${libPathVar}" = pkgs.lib.makeLibraryPath nativeDeps;
LIBRARY_PATH = pkgs.lib.makeLibraryPath nativeDeps;
C_INCLUDE_PATH = pkgs.lib.makeSearchPath "include" [ zlogDeps.zlog pkgs.curl.dev ];
CURL_INCLUDE_DIR = "${pkgs.curl.dev}/include";
shellHook = ''
echo "curl-lean development environment"
echo "Lean version: $(lean --version 2>/dev/null || echo 'Lean not found')"
echo "libcurl available at: ${pkgs.curl}"
'';
};
# Build the Lean package
curlLeanPackage = pkgs.stdenv.mkDerivation {
pname = "curl-lean";
version = "0.1.0";
src = ./.;
nativeBuildInputs = [ leanBin lakeBin pkgs.clang pkgs.lld pkgs.makeWrapper ];
buildInputs = nativeDeps ++ [ zlogDeps.zlogLeanPackage ];
patchPhase = ''
# Remove require statements from lakefile.lean for Nix build
# Dependencies are provided via LEAN_PATH instead
sed -i '/^require LSpec from git/,+1d' lakefile.lean
'';
configurePhase = ''
export HOME=$TMPDIR
# Set LEAN_PATH to include dependencies
export LEAN_PATH="${zlogDeps.zlogLeanPackage}/lib/lean:$LEAN_PATH"
'';
buildPhase = ''
# Set up library paths for FFI compilation
export ${libPathVar}="${pkgs.lib.makeLibraryPath nativeDeps}"
export LIBRARY_PATH="${pkgs.lib.makeLibraryPath nativeDeps}"
export C_INCLUDE_PATH="${pkgs.lib.makeSearchPath "include" [ zlogDeps.zlog pkgs.curl.dev ]}"
export CURL_INCLUDE_DIR="${pkgs.curl.dev}/include"
export LEAN_PATH="${zlogDeps.zlogLeanPackage}/lib/lean:$LEAN_PATH"
# Build the package
ZLOG_LEAN_DIR="$TMPDIR/zlog-lean"
cp -r "${zlog-lean}" "$ZLOG_LEAN_DIR"
chmod -R u+w "$ZLOG_LEAN_DIR"
cat > $TMPDIR/lake-packages.json <<EOF
{
"version": "1.1.0",
"packages": [
{
"type": "path",
"scope": "",
"name": "zlogLean",
"manifestFile": "lake-manifest.json",
"inherited": false,
"dir": "$ZLOG_LEAN_DIR",
"configFile": "lakefile.lean"
}
]
}
EOF
lake build --packages $TMPDIR/lake-packages.json
'';
installPhase = ''
mkdir -p $out/bin $out/lib
# Copy executables if they exist
if [ -d .lake/build/bin ]; then
for bin in .lake/build/bin/*; do
if [ -f "$bin" ]; then
cp "$bin" $out/bin/
wrapProgram "$out/bin/$(basename "$bin")" \
--prefix ${libPathVar} : "${pkgs.lib.makeLibraryPath nativeDeps}"
fi
done
fi
# Copy libraries
if [ -d .lake/build/lib ]; then
cp -r .lake/build/lib/* $out/lib/
fi
# Copy Lake package metadata for downstream consumers
mkdir -p $out/share/lean
cp -r .lake/build/ir $out/share/lean/ || true
cp lakefile.lean $out/share/lean/
cp lean-toolchain $out/share/lean/
'';
};
in {
devShells.default = devShell;
packages.default = curlLeanPackage;
lib = {
inherit nativeDeps curlLeanPackage;
curl = pkgs.curl;
};
}
);
}