Skip to content

Commit 8463ea4

Browse files
committed
wasmer: 6.0.1 -> 7.1.0, add v8 support
1 parent 989409a commit 8463ea4

2 files changed

Lines changed: 149 additions & 10 deletions

File tree

pkgs/by-name/wa/wasmer/package.nix

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,77 @@
22
lib,
33
stdenv,
44
fetchFromGitHub,
5+
fetchurl,
56
rustPlatform,
67
cargo,
78
rustc,
8-
llvmPackages_18,
9+
nix-update,
10+
curl,
11+
writeShellApplication,
12+
llvmPackages_21,
913
libffi,
1014
libxml2,
1115
withLLVM ?
1216
stdenv.hostPlatform.isLinux || (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64),
17+
withV8 ? (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isx86_64),
1318
}:
1419

15-
stdenv.mkDerivation (finalAttrs: {
20+
let
21+
v8Version = "11.9.2";
22+
23+
# Prebuilt V8 from wasmerio's custom builds, only evaluated when withV8 = true.
24+
25+
# Per-platform hashes, auto-updated via the general updateScript
26+
v8Hashes = {
27+
"v8-linux-amd64.tar.xz" = "sha256-nTCVdBKtyVMb7lE+Db4RDsShKkLbG/0r980ejd+EAvo=";
28+
"v8-linux-musl-amd64.tar.xz" = "sha256-XgRs3I46B2PG7Jrv5E+KSeuNfXLhgB7R66cAkA/Bvv8=";
29+
"v8-darwin-arm64.tar.xz" = "sha256-xAG1PcAGw8a0A9k8d78/whTUXnqdfRZBz8yrg/+iz0M=";
30+
};
31+
32+
v8Prebuilt =
33+
let
34+
assetName =
35+
if stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isx86_64 && stdenv.hostPlatform.isMusl then
36+
"v8-linux-musl-amd64.tar.xz"
37+
else if stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isx86_64 then
38+
"v8-linux-amd64.tar.xz"
39+
else if stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64 then
40+
"v8-darwin-arm64.tar.xz"
41+
else
42+
throw "withV8 = true is not supported on ${stdenv.hostPlatform.system}";
43+
in
44+
stdenv.mkDerivation {
45+
name = "wasmer-v8-prebuilt-${v8Version}";
46+
src = fetchurl {
47+
url = "https://github.com/wasmerio/v8-custom-builds/releases/download/${v8Version}/${assetName}";
48+
hash = v8Hashes.${assetName};
49+
};
50+
sourceRoot = ".";
51+
dontBuild = true;
52+
installPhase = ''
53+
cp -r . $out
54+
'';
55+
};
56+
in
57+
58+
stdenv.mkDerivation (finalAttrs: {
1659
pname = "wasmer";
17-
version = "6.0.1";
60+
version = "7.1.0";
61+
62+
__structuredAttrs = true;
63+
strictDeps = true;
1864

1965
src = fetchFromGitHub {
2066
owner = "wasmerio";
2167
repo = "wasmer";
2268
tag = "v${finalAttrs.version}";
23-
hash = "sha256-nw/4hcEDkAabcpatVBRozxvVLzYOKbj3ylrGeQtNzMQ=";
69+
hash = "sha256-A1SkZY+iSR9xlu6R1p9uZYsGFPAOifuYTHtEXaEgves=";
70+
fetchSubmodules = true;
2471
};
2572

2673
cargoDeps = rustPlatform.fetchCargoVendor {
2774
inherit (finalAttrs) pname version src;
28-
hash = "sha256-Nui8KxDk4+sqcmzeoZ6hGRb9Ux71+Nckz8seqq07cdE=";
75+
hash = "sha256-wBEwGKjj9DdZESFlXS8T7B0Xdp7yMe08DYTGr4wnTVI=";
2976
};
3077

3178
nativeBuildInputs = [
@@ -36,28 +83,71 @@ stdenv.mkDerivation (finalAttrs: {
3683
];
3784

3885
buildInputs = lib.optionals withLLVM [
39-
llvmPackages_18.llvm
86+
llvmPackages_21.llvm
4087
libffi
4188
libxml2
4289
];
4390

91+
# In 7.1.0 there is no nice flag to toggle napi/v8 on or off,
92+
# so we manually delete the Makefile entry when we don't want v8
93+
# TODO: v7.2.0 pre-release has a flag, when updating to 7.2.0
94+
# add "ENABLE_NAPI_V8=${if withV8 then "1" else "0"}" to makeFlags
95+
postPatch =
96+
lib.optionalString (!withV8) ''
97+
substituteInPlace Makefile \
98+
--replace-fail ' build_wasmer_extra_features += napi-v8' ""
99+
''
100+
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
101+
substituteInPlace Makefile \
102+
--replace-fail 'install: install-wasmer install-capi-headers install-capi-lib install-pkgconfig install-misc' \
103+
'install: install-wasmer install-capi-headers install-misc'
104+
'';
105+
44106
makeFlags = [
45107
"WASMER_INSTALL_PREFIX=${placeholder "out"}"
46108
"DESTDIR=${placeholder "out"}"
109+
"ENABLE_LLVM=${if withLLVM then "1" else "0"}"
47110
];
48111

112+
# Default all target includes headless C API which doesn't get installed
113+
# by their Makefile and is quite niche to include, so we opt out
49114
buildFlags = [
50-
"ENABLE_LLVM=${if withLLVM then "1" else "0"}"
115+
"build-wasmer"
116+
"build-capi"
51117
];
52118

53-
env = lib.optionalAttrs withLLVM {
54-
LLVM_SYS_180_PREFIX = llvmPackages_18.llvm.dev;
55-
};
119+
env =
120+
lib.optionalAttrs withLLVM {
121+
LLVM_SYS_211_PREFIX = llvmPackages_21.llvm.dev;
122+
}
123+
// lib.optionalAttrs withV8 {
124+
# build.rs skips the download when these are set; see resolve_explicit_v8 in lib/napi/build.rs
125+
NAPI_V8_INCLUDE_DIR = "${v8Prebuilt}/include";
126+
V8_LIB_DIR = "${v8Prebuilt}/lib";
127+
};
128+
129+
postInstall = lib.optionalString stdenv.hostPlatform.isDarwin ''
130+
install -Dm755 target/release/libwasmer.dylib $out/lib/libwasmer.dylib
131+
if pc="$(WASMER_DIR="" target/release/wasmer config --pkg-config 2>/dev/null)"; then
132+
mkdir -p "$out/lib/pkgconfig"
133+
printf '%s\n' "$pc" > "$out/lib/pkgconfig/wasmer.pc"
134+
fi
135+
'';
136+
137+
passthru.updateScript = lib.getExe (writeShellApplication {
138+
name = "update-wasmer";
139+
runtimeInputs = [
140+
nix-update
141+
curl
142+
];
143+
text = builtins.readFile ./update.sh;
144+
});
56145

57146
# Tests are failing due to `Cannot allocate memory` and other reasons
58147
doCheck = false;
59148

60149
meta = {
150+
sourceProvenance = with lib.sourceTypes; [ fromSource ] ++ lib.optional withV8 binaryNativeCode;
61151
description = "Universal WebAssembly Runtime";
62152
mainProgram = "wasmer";
63153
longDescription = ''

pkgs/by-name/wa/wasmer/update.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
old_version=$(grep -oP '^ version = "\K[^"]+(?=";)' pkgs/by-name/wa/wasmer/package.nix)
2+
echo "Current wasmer version: $old_version"
3+
4+
nix-update wasmer "$@"
5+
6+
new_version=$(grep -oP '^ version = "\K[^"]+(?=";)' pkgs/by-name/wa/wasmer/package.nix)
7+
if [ "$old_version" = "$new_version" ]; then
8+
echo "Already at $old_version, nothing to do"
9+
exit 0
10+
fi
11+
echo "Updated wasmer $old_version -> $new_version"
12+
13+
# v8Version is pinned in package.nix to match PREBUILT_V8_VERSION in lib/napi/build.rs.
14+
# lib/napi is a submodule, so resolve its pinned SHA first, then fetch build.rs from that repo.
15+
echo "Fetching PREBUILT_V8_VERSION from lib/napi/build.rs..."
16+
napi_sha=$(curl -fsSL "https://api.github.com/repos/wasmerio/wasmer/contents/lib/napi?ref=v${new_version}" \
17+
| grep -oP '"sha":\s*"\K[^"]+' | head -1)
18+
new_v8=$(curl -fsSL "https://raw.githubusercontent.com/wasmerio/napi/${napi_sha}/build.rs" \
19+
| grep -oP 'PREBUILT_V8_VERSION\s*:\s*&str\s*=\s*"\K[^"]+')
20+
cur_v8=$(grep -oP '^ v8Version = "\K[^"]+(?=";)' pkgs/by-name/wa/wasmer/package.nix)
21+
echo "V8: current=$cur_v8, required=$new_v8"
22+
23+
if [ "$new_v8" = "$cur_v8" ]; then
24+
echo "V8 version unchanged, done"
25+
exit 0
26+
fi
27+
28+
echo "V8 bumped $cur_v8 -> $new_v8, fetching hashes for all platforms..."
29+
sed -i "s|^ v8Version = \"[^\"]*\";$| v8Version = \"$new_v8\";|" \
30+
pkgs/by-name/wa/wasmer/package.nix
31+
32+
base="https://github.com/wasmerio/v8-custom-builds/releases/download/$new_v8"
33+
declare -A assets=(
34+
["v8-linux-amd64.tar.xz"]="$base/v8-linux-amd64.tar.xz"
35+
["v8-linux-musl-amd64.tar.xz"]="$base/v8-linux-musl-amd64.tar.xz"
36+
["v8-darwin-arm64.tar.xz"]="$base/v8-darwin-arm64.tar.xz"
37+
)
38+
39+
for asset in "${!assets[@]}"; do
40+
url="${assets[$asset]}"
41+
echo " Fetching hash for $asset..."
42+
hash=$(nix-prefetch-url --type sha256 "$url" 2>/dev/null \
43+
| xargs nix hash convert --hash-algo sha256 --to sri)
44+
echo " $asset -> $hash"
45+
sed -i "s|\"$asset\" = \"[^\"]*\"|\"$asset\" = \"$hash\"|" \
46+
pkgs/by-name/wa/wasmer/package.nix
47+
done
48+
49+
echo "Done"

0 commit comments

Comments
 (0)