-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathflake.nix
336 lines (296 loc) · 11.3 KB
/
flake.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};
outputs = { self, nixpkgs, nixpkgs-unstable, flake-utils, fenix, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
clang = pkgs.llvmPackages_17.clang;
pythonEnv = pkgs.python39.withPackages (ps: []);
toolchain = with fenix.packages.${system}; combine [
complete.cargo
complete.rustc
complete.rust-std
complete.rustfmt
complete.rust-analyzer
targets.wasm32-unknown-unknown.latest.rust-std
targets.x86_64-unknown-linux-musl.latest.rust-std
];
version = (builtins.fromTOML (builtins.readFile ./engine/Cargo.toml)).workspace.package.version;
appleDeps = with pkgs.darwin.apple_sdk.frameworks; [
CoreServices
System
SystemConfiguration
pkgs.libiconv-darwin
];
rustPlatform = pkgs.makeRustPlatform {
inherit (fenix.packages.${system}.minimal) cargo rustc;
inherit (fenix.packages.${system}.latest) rust-std;
};
buildInputs = (with pkgs; [
cmake
git
openssl
pkg-config
lld_17
pythonEnv
ruby
ruby.devEnv
maturin
pnpm
vsce # VSCode extension packaging tool
toolchain
nodejs
nodePackages.typescript
pkgs-unstable.uv
pkgs-unstable.flatbuffers
wasm-pack
pkgs.gcc
napi-rs-cli
wasm-bindgen-cli
# For building the typescript client.
pixman
cairo
pango
libjpeg
giflib
librsvg
]) ++ (if pkgs.stdenv.isDarwin then appleDeps else []);
nativeBuildInputs = [
pkgs.cmake
pkgs.openssl
pkgs.pkg-config
pkgs.ruby
pythonEnv
pkgs.maturin
pkgs.perl
pkgs.lld_17
pkgs.gcc
];
wheelName = "baml_py-${version}-cp38-abi3-linux_x86_64.whl";
bamlRustPackage = {
pname,
buildPhase ? null,
installPhase ? null,
nativeBuildInputsExtra ? [],
buildType,
extraAttrs ? {},
}:
rustPlatform.buildRustPackage ({
inherit pname version;
src = ./engine;
LIBCLANG_PATH = pkgs.libclang.lib + "/lib/";
BINDGEN_EXTRA_CLANG_ARGS = if pkgs.stdenv.isDarwin then
"-I${pkgs.llvmPackages_17.libclang.lib}/lib/clang/17/headers "
else
"-isystem ${pkgs.llvmPackages_17.libclang.lib}/lib/clang/17/include -isystem ${pkgs.llvmPackages_17.libclang.lib}/include -isystem ${pkgs.glibc.dev}/include";
RUSTFLAGS = if pkgs.stdenv.isDarwin
then "--cfg tracing_unstable -C linker=lld"
else "--cfg tracing_unstable -Zlinker-features=+lld -C linker=gcc";
OPENSSL_STATIC = "1";
OPENSSL_DIR = "${pkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include";
inherit buildInputs;
nativeBuildInputs = nativeBuildInputs ++ nativeBuildInputsExtra;
doCheck = false;
inherit buildType;
cargoLock = { lockFile = ./engine/Cargo.lock; outputHashes = {}; };
SKIP_BAML_VALIDATION = "1";
}
// (if buildPhase != null then { inherit buildPhase; } else {})
// (if installPhase != null then { inherit installPhase; } else {})
// extraAttrs);
in
rec {
packages.default = bamlRustPackage {
pname = "baml-cli";
buildType = "debug";
installPhase = ''
runHook preInstall
echo "Listing baml binaries in target/debug:"
find target/debug -type f -name "baml*"
mkdir -p $out/bin
BINARY_NAME=$(find target/debug -type f -executable -name "baml*" | head -n1)
echo "Found binary: $BINARY_NAME"
cp "$BINARY_NAME" $out/bin/baml-cli
runHook postInstall
'';
extraAttrs = {
PYTHON_SYS_EXECUTABLE = "${pythonEnv}/bin/python3";
LD_LIBRARY_PATH = "${pythonEnv}/lib";
PYTHONPATH = "${pythonEnv}/${pythonEnv.sitePackages}";
# CC="${clang}/bin/clang"; # Temporarily commented out for linux testing.
};
};
packages.pyLib = bamlRustPackage {
pname = "baml-cli";
buildType = "debug";
nativeBuildInputsExtra = [ pkgs.maturin pythonEnv ];
buildPhase = ''
cargo build
cd language_client_python
maturin build --offline --target-dir ../target
'';
installPhase = ''
mkdir -p $out/lib
ls ../target/wheels
cp ../target/wheels/${wheelName} $out/lib/
touch $out/results.txt
ls -la $out >> $out/results.txt
ls -la $out/lib >> $out/results.txt
ls -la $out/lib/${wheelName} >> $out/results.txt
'';
};
packages.baml-py = pkgs.python39Packages.buildPythonPackage {
pname = "baml-py";
inherit version;
format = "wheel";
src = "${packages.pyLib}/lib/${wheelName}";
propagatedBuildInputs = with pkgs.python39.pkgs; [
pydantic
typing-extensions
];
pythonImportsCheck = [ "baml_py" ];
doCheck = false;
meta = with pkgs.lib; {
description = "Python bindings for BAML";
homepage = "https://github.com/boundaryml/baml";
license = licenses.mit;
platforms = platforms.linux;
};
};
packages.tsLib = bamlRustPackage {
pname = "baml-ts";
buildType = "debug";
nativeBuildInputsExtra = [ pkgs.nodejs pkgs.napi-rs-cli pkgs.pnpm ];
buildPhase = ''
# Build the CLI
echo "Building the CLI"
cargo build -p baml-cli
# Build specifically the typescript FFI crate
echo "Building the typescript FFI crate"
cargo build -p baml-typescript-ffi
cd language_client_typescript
echo "Listing current directory contents:"
ls -la
# Copy the built library to where napi expects it
echo "Copying the built library to where napi expects it"
mkdir -p target/debug
find ../target -name "*.so" -o -name "*.dylib" -o -name "*.dll"
cp ../target/debug/libbaml.so target/debug/libbaml_typescript_ffi.so
# Build the native module directly with release flag
napi build --platform --js ./native.js --dts ./native.d.ts
# Compile TypeScript files using the Nix-provided TypeScript
${pkgs.nodePackages.typescript}/bin/tsc ./typescript_src/*.ts --outDir ./dist --module commonjs --allowJs --declaration true || true
# Copy any pre-existing JavaScript files that might be needed
cp *.js dist/ || true
# Copy TypeScript declarations
cp *.d.ts dist/ || true
# Copy the native modules
cp *.node dist/
# Create minimal package.json and package-lock.json
cat > dist/package.json << EOF
{
"name": "@boundaryml/baml",
"version": "${version}",
"bin": {
"baml-cli": "./bin/baml-cli"
},
"files": [
"*.js",
"*.ts",
"*.node",
"bin/baml-cli"
],
"dependencies": {},
"os": ["linux"],
"cpu": ["x64"]
}
EOF
cat > dist/package-lock.json << EOF
{
"name": "@boundaryml/baml",
"version": "${version}",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@boundaryml/baml",
"version": "${version}",
"dependencies": {},
"bin": {
"baml-cli": "bin/baml-cli"
}
}
}
}
EOF
# Copy the CLI binary
mkdir -p dist/bin
cp ../target/debug/baml-cli dist/bin/baml-cli
'';
installPhase = ''
mkdir -p $out/lib
cp -r dist/* $out/lib/
'';
extraAttrs = {
SKIP_BAML_VALIDATION = "1";
cargoLock = { lockFile = ./engine/Cargo.lock; };
};
};
packages.baml-ts = let
# Create a source with files in the correct location
npmSource = pkgs.runCommand "baml-ts-${version}-source" {} ''
mkdir -p $out
cp -r ${packages.tsLib}/lib/* $out/
'';
in pkgs.buildNpmPackage {
pname = "baml";
inherit version;
src = npmSource;
npmDepsHash = "sha256-p7AxgJSqngcwHwKsjF6u+fS0E27KY6/ulGIIRlZLsFU=";
forceEmptyCache = true;
buildInputs = [ pkgs.nodejs ];
# Configure npm to use temporary directories
NPM_CONFIG_CACHE = "./tmp/npm";
NPM_CONFIG_TMP = "./tmp/npm";
NPM_CONFIG_PREFIX = "./tmp/npm";
buildPhase = ''
# Ensure temp directories exist
mkdir -p tmp/npm
npm pack
'';
installPhase = ''
mkdir -p $out/lib
touch $out/results.txt
ls -lha
ls -la >> $out/results.txt
cp boundaryml-baml-${version}.tgz $out/lib/
'';
};
devShell = pkgs.mkShell rec {
inherit buildInputs;
PATH="${clang}/bin:$PATH";
RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc;
LIBCLANG_PATH = pkgs.libclang.lib + "/lib/";
BINDGEN_EXTRA_CLANG_ARGS = if pkgs.stdenv.isDarwin then
"" # Rely on default includes provided by stdenv.cc + libclang
else
"-isystem ${pkgs.llvmPackages_17.libclang.lib}/lib/clang/17/include -isystem ${pkgs.llvmPackages_17.libclang.lib}/include -isystem ${pkgs.glibc.dev}/include";
};
}
);
}