WASM build instructions for Nix
Hi there, I was trying to build this package for a mixed wasm/non-wasm build and landed on adding the following for ensuring clang is available but only used for the wasm build:
export CC_wasm32_unknown_unknown="$(nix build --no-link --print-out-paths nixpkgs#clang)/bin/clang"
export AR_wasm32_unknown_unknown="$(nix build --no-link --print-out-paths nixpkgs#llvmPackages.bintools-unwrapped)/bin/llvm-ar"
This works for me on macOS when using nix
Perhaps this can be added to the wiki, I couldn't find a way to submit an edit
To add to this, if the resulting wasm package has errors that look like "Can't resolve 'env' in ../pkg' or similar, the wasm.js tries to "import 'env'", you must disable zerousdcallregs and stackprotector, i.e
hardeningDisable = [ "zerocallusedregs" "stackprotector" ];
My full arguments:
CC_wasm32_unknown_unknown = "${llvmPackages.libcxxClang}/bin/clang";
AR_wasm32_unknown_unknown = "${llvmPackages.bintools-unwrapped}/bin/llvm-ar";
hardeningDisable = [ "zerocallusedregs" "stackprotector" ];
ensure libcxxClang is used, as that provides some standard C headers that zstd needs to compile. otherwise you will run into compile errors like "limits.h" not found. Of course, zstd must be in the buildInputs section.
Right, I'm actually using this now:
export CC_wasm32_unknown_unknown="$(nix build --no-link --print-out-paths nixpkgs#llvmPackages_19.clang-unwrapped)/bin/clang"
export AR_wasm32_unknown_unknown="$(nix build --no-link --print-out-paths nixpkgs#llvmPackages_19.bintools-unwrapped)/bin/llvm-ar"
export CFLAGS_wasm32_unknown_unknown="-I $(nix build --no-link --print-out-paths nixpkgs#llvmPackages_19.clang-unwrapped.lib)/lib/clang/19/include"
This ensures the stdlib is available and the hardening flags that are normally set on the stdenv are not used.