Skip to content

Instantly share code, notes, and snippets.

@573
Last active December 20, 2024 20:02
Show Gist options
  • Select an option

  • Save 573/885a062ca49d2db355c22004cc395066 to your computer and use it in GitHub Desktop.

Select an option

Save 573/885a062ca49d2db355c22004cc395066 to your computer and use it in GitHub Desktop.
How to build webassembly via rust using just nix
nix-shell -p wasm-pack cargo rustc -I nixpkgs=channel:nixos-unstable
cargo generate-lockfile
crate2nix generate -n '<nixos-unstable>'
nix-build -I nixpkgs=channel:nixos-unstable
wasm-pack build --target web

[INFO]: Checking for the Wasm target...
Error: wasm32-unknown-unknown target not found in sysroot: "/nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0"

Used rustc from the following path: "/nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0/bin/rustc"
It looks like Rustup is not being used. For non-Rustup setups, the wasm32-unknown-unknown target needs to be installed manually. See https://rustwasm.github.io/wasm-pack/book/prerequisites/non-rustup-setups.html on how to do this.

Did exactly that:

$(which rustc) --print sysroot

/nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0

ls -lr /nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0/lib/rustlib/wasm32-unknown-unknown/

drwxr-xr-x 1 me me 4096 Dec 16 17:43 lib

wasm-pack build --target web

[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
Compiling proc-macro2 v1.0.8
Compiling unicode-xid v0.2.0
Compiling syn v1.0.14
Compiling wasm-bindgen-shared v0.2.58
Compiling log v0.4.8
Compiling cfg-if v0.1.10
Compiling bumpalo v3.2.0
Compiling lazy_static v1.4.0
Compiling wasm-bindgen v0.2.58
error[E0514]: found crate core compiled by an incompatible version of rustc
|
= help: please recompile that crate using this compiler (rustc 1.40.0)
= note: the following crate versions were found:
crate core compiled by rustc 1.40.0 (73528e339 2019-12-16): /nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0/lib/rustlib/wasm32-unknown-unknown/lib/libcore-ef54709e300503ed.rlib

error: aborting due to previous error

error: could not compile cfg-if.
warning: build failed, waiting for other jobs to finish...
error: build failed
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute cargo build: exited with exit code: 101

cargo-features = ["edition"]
[package]
edition = "2018"
name = "wasm-pack-examples"
version = "0.1.0"
authors = ["573 <[email protected]>"]
[lib]
crate-type = ["cdylib"]
[profile.release]
lto = true
[dependencies]
wasm-bindgen = "0.2"
# or just nix build -f Cargo.nix rootCrate.build
# ./result/bin/${your_crate_name}
{ pkgs ? import <nixpkgs> { config = {}; } }:
let cargo_nix = pkgs.callPackage ./Cargo.nix {};
in cargo_nix.rootCrate.build
#![allow(non_upper_case_globals)]
# src/lib.rs
use wasm_bindgen::prelude::*;
build:
date
RUSTFLAGS="-C linker=lld" cargo build --release --target wasm32-unknown-unknown
# TODO make work with watch, maybe builtfarm
clean:
cargo clean
# this allows caching of instability.
# making switches less annoying but also less up to date
import (
builtins.fetchGit (
{
url = "https://github.com/NixOS/nixpkgs";
ref = "master";
rev = "b61999e4ad60c351b4da63ae3ff43aae3c0bbdfb";
}
))
{ pkgs, stdenv, rustc }:
stdenv.mkDerivation {
name = "rust-src";
src = rustc.src;
phases = [ "unpackPhase" "installPhase" ];
installPhase = ''
mv src $out
rm -rf $out/{ci,doc,driver,etc,grammar,llvm,rt,rtstartup,rustllvm,test,tools,vendor}
'';
}
with import ./pin-unstable.nix { };
stdenv.mkDerivation {
name = "rust-env";
RUST_SRC_PATH = import ./rust-src.nix {
inherit stdenv;
inherit rustc;
inherit pkgs;
};
RUSTFLAGS="-C linker=lld";
nativeBuildInputs = [
(rustc.override {
stdenv = stdenv.override{
targetPlatform= {
parsed = {
cpu = { name = "wasm32"; };
vendor = {name = "unknown";};
kernel = {name = "unknown";};
abi = {name = "unknown";};
};
};
};
}) cargo wasm-pack llvmPackages_9.lld
##inotify-tools
];
# Set Environment Variables
RUST_BACKTRACE = 1;
}
@GuillaumeDesforges
Copy link

Still working even without lockfile, many thanks

@xixiaofinland
Copy link

Still works today, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment