Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
298 lines (260 loc) · 9.04 KB
/
Copy pathflake.nix
File metadata and controls
298 lines (260 loc) · 9.04 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
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
{
description = "headless browser designed for AI and automation";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-26.05";
zigPkgs = {
url = "github:silversquirl/zig-flake";
inputs.nixpkgs.follows = "nixpkgs";
};
zlsPkg = {
url = "github:zigtools/zls/0.16.0";
inputs.zig-flake.follows = "zigPkgs";
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{
self,
nixpkgs,
zigPkgs,
zlsPkg,
fenix,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
overlays = [
(final: prev: {
zls = zlsPkg.packages.${prev.system}.default;
})
];
pkgs = import nixpkgs {
inherit system overlays;
};
zig = pkgs.zig_0_16;
zls = pkgs.zls;
rustToolchain = fenix.packages.${system}.stable.toolchain;
# V8 versions — kept in sync with .github/actions/install/action.yml
v8Version = "14.9.207.35";
zigV8Tag = "v0.5.2";
# Map Nix system to the V8 archive naming convention used by the
# zig-v8-fork releases: libc_v8_<v8>_<os>_<arch>.a
v8SystemMap = {
x86_64-linux = {
os = "linux";
arch = "x86_64";
};
aarch64-linux = {
os = "linux";
arch = "aarch64";
};
x86_64-darwin = {
os = "macos";
arch = "x86_64";
};
aarch64-darwin = {
os = "macos";
arch = "aarch64";
};
};
v8ArchiveName =
let
s = v8SystemMap.${system};
in
"libc_v8_${v8Version}_${s.os}_${s.arch}.a";
# Prebuilt V8 hashes per platform.
v8Hashes = {
x86_64-linux = "sha256-K3iZPeU5bwaeaXhFA+fxwz+X4jXUHgHbh5Q5PDv06lk=";
aarch64-linux = pkgs.lib.fakeHash;
x86_64-darwin = pkgs.lib.fakeHash;
aarch64-darwin = pkgs.lib.fakeHash;
};
# Prebuilt V8 library — fetched instead of building from source.
prebuiltV8 = pkgs.fetchurl {
name = v8ArchiveName;
url = "https://github.com/lightpanda-io/zig-v8-fork/releases/download/${zigV8Tag}/${v8ArchiveName}";
hash = v8Hashes.${system};
};
# Pre-fetched zig dependencies via linkFarm.
# Generated from build.zig.zon by zon2nix, then post-processed to
# replace expiring release-asset URLs and ?ref= git URLs with stable
# canonical URLs (see the header comment in deps.nix).
# To regenerate:
# nix run nixpkgs#zon2nix -- build.zig.zon > deps.nix
# # then run the post-processing step documented in deps.nix
zigDeps = pkgs.callPackage ./deps.nix { };
# Pre-fetch Rust crate dependencies for the html5ever component.
cargoDeps =
pkgs.runCommand "lightpanda-cargo-deps"
{
nativeBuildInputs = [ rustToolchain ];
src = ./.;
outputHashMode = "recursive";
outputHash = "sha256-LHsM5pXlR8Zv2B1fRHm8/MPV42fEJ2TQWF9C8EPFVXo=";
}
''
export CARGO_HOME=$(mktemp -d)
cd "$src"/src/html5ever
cargo fetch --locked
mv "$CARGO_HOME" "$out"
'';
# Zig-compatible libc directory.
# Replaces the FHS env: zig uses ZIG_LIBC to find crt files,
# headers, and shared libraries at standard FHS-like paths.
zigLibcDir = pkgs.symlinkJoin {
name = "zig-libc";
paths = [
pkgs.glibc
pkgs.glibc.dev
];
postBuild = ''
# GCC crt objects (crtbeginS.o, crtendS.o, etc.)
mkdir -p $out/lib
cp -r ${pkgs.gcc.cc}/lib/gcc $out/lib/gcc
# Also copy crt files directly into lib/ for easier discovery
find ${pkgs.gcc.cc}/lib/gcc -name 'crt*.o' -exec cp {} $out/lib/ \; 2>/dev/null || true
# libstdc++ shared library
for f in ${pkgs.gcc.cc.lib}/lib/libstdc++*; do
ln -sf "$f" $out/lib/ 2>/dev/null || true
done
'';
};
# Zig 0.16+ expects ZIG_LIBC to point to a libc paths FILE, not a dir.
zigLibc = pkgs.writeText "zig-libc-paths" ''
include_dir=${zigLibcDir}/include
sys_include_dir=${zigLibcDir}/include
crt_dir=${zigLibcDir}/lib
msvc_lib_dir=
kernel32_lib_dir=
gcc_dir=
'';
lightpanda = pkgs.stdenv.mkDerivation {
name = "lightpanda-${v8Version}";
src = ./.;
nativeBuildInputs = [ zig pkgs.cacert rustToolchain ];
ZIG_LIBC = "${zigLibc}";
dontUseCmakeConfigure = true;
# Pre-fetched cargo cache for the html5ever Rust component.
preBuild = ''
export CARGO_HOME=$(mktemp -d)
ln -s ${cargoDeps}/registry "$CARGO_HOME"/registry
'';
buildPhase = ''
runHook preBuild
# Symlink the prebuilt V8 archive where zig-v8-fork expects it.
mkdir -p v8
ln -sf ${prebuiltV8} v8/libc_v8.a
# Populate the zig global cache with pre-fetched deps.
# Zig 0.16+ stores deps as <hash>.tar.gz, not extracted dirs.
export ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
mkdir -p "$ZIG_GLOBAL_CACHE_DIR"/p
for _d in "${zigDeps}"/*; do
name=$(basename "$_d")
src=$(readlink "$_d")
tar -czf "$ZIG_GLOBAL_CACHE_DIR/p/''${name}.tar.gz" \
--mtime=@0 --owner=0 --group=0 --numeric-owner \
--hard-dereference \
-C "$src" .
done
echo "=== Building V8 snapshot (release fast) ==="
zig build -Doptimize=ReleaseFast \
-Dprebuilt_v8_path=v8/libc_v8.a \
snapshot_creator -- src/snapshot.bin
echo "=== Building lightpanda (release fast) ==="
zig build -Doptimize=ReleaseFast \
-Dsnapshot_path=../../snapshot.bin \
-Dprebuilt_v8_path=v8/libc_v8.a
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out/bin
cp zig-out/bin/lightpanda $out/bin/
runHook postInstall
'';
meta = {
description = "Headless browser designed for AI and automation";
homepage = "https://lightpanda.io";
license = pkgs.lib.licenses.agpl3Only;
mainProgram = "lightpanda";
platforms = builtins.attrNames v8SystemMap;
};
};
in
{
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
zig
zls
rustToolchain
python3
python3Packages.playwright
pkg-config
cmake
gperf
];
buildInputs = with pkgs; [
glibc.dev
stdenv.cc.cc
stdenv.cc.cc.lib
gcc.cc.lib
expat.dev
glib.dev
zlib
];
ZIG_LIBC = "${zigLibc}";
shellHook = ''
export ZIG_GLOBAL_CACHE_DIR="$PWD/.zig-cache/global"
export CARGO_HOME="$PWD/.cargo-home"
export NIX_CFLAGS_COMPILE="-Wno-error=cpp"
mkdir -p "$ZIG_GLOBAL_CACHE_DIR" "$CARGO_HOME"
mkdir -p .lp-cache/prebuilt-v8
if [ ! -f ".lp-cache/prebuilt-v8/${v8ArchiveName}" ]; then
ln -sf ${prebuiltV8} ".lp-cache/prebuilt-v8/${v8ArchiveName}"
fi
mkdir -p v8
if [ ! -f v8/libc_v8.a ]; then
ln -sf ${prebuiltV8} v8/libc_v8.a
fi
echo "=== Lightpanda dev shell ==="
echo " zig : $(zig version)"
echo " v8 : ${v8Version} (prebuilt)"
echo " cache : $ZIG_GLOBAL_CACHE_DIR"
echo ""
echo "Quick start:"
echo " zig build -Dprebuilt_v8_path=v8/libc_v8.a # debug build"
echo " zig build -Dprebuilt_v8_path=v8/libc_v8.a test # run tests"
echo " zig build -Doptimize=ReleaseFast -Dprebuilt_v8_path=v8/libc_v8.a # release"
echo " make build-dev # or use make"
'';
};
packages.zig-deps = zigDeps;
packages.cargo-deps = cargoDeps;
packages.default = lightpanda;
}
)
// {
nixosModules.default =
{
config,
lib,
pkgs,
...
}:
{
nixpkgs.overlays = [
(final: prev: {
lightpanda = self.packages.${pkgs.system}.default;
})
];
imports = [ ./modules/nixos/lightpanda.nix ];
};
};
}