I want to set up a dev shell for running wasm-pack
, and I have this flake right now:
{
description = "wasm-pack setup";
inputs = {
nixpkgs = { url = "github:nixos/nixpkgs/nixos-unstable"; };
rust-overlay = { url = "github:oxalica/rust-overlay"; };
};
outputs = { nixpkgs, rust-overlay, ... }:
let system = "x86_64-linux";
in {
devShell.${system} = let
overlay = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlay; };
in (({ pkgs, ... }:
pkgs.mkShell {
buildInputs = with pkgs; [
rustc
cargo
nodejs
wasm-pack
];
shellHook = "";
}) { pkgs = pkgs; });
};
}
Then I run wasm-pack new foo
, cd foo
, wasm-pack build
, and I got this error:
Error: wasm32-unknown-unknown target not found in sysroot: "/nix/store/sw7kr9clh3y3rpghgh50j023nbnwqz9i-rustc-1.55.0"
Used rustc from the following path: "/nix/store/sw7kr9clh3y3rpghgh50j023nbnwqz9i-rustc-1.55.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.
I have found someone with similar issue in this gist, but I cannot find an obvious solution yet:
Cargo.toml
cargo-features = ["edition"]
[package]
edition = "2018"
name = "wasm-pack-examples"
version = "0.1.0"
authors = ["573 <me@gmail.com>"]
[lib]
crate-type = ["cdylib"]
This file has been truncated. show original
default.nix
# 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
lib.rs
#![allow(non_upper_case_globals)]
# src/lib.rs
use wasm_bindgen::prelude::*;
There are more than three files. show original
So how can I set the target correctly?
1 Like
efx
October 8, 2021, 8:50pm
2
I skimmed this quickly and it looks like you’ll need to manually add the target.
Someone using oxalica’s overlay demonstrates how you can override the Rust toolchain and add a target:
opened 09:10AM - 07 Oct 21 UTC
```
# flake.nix
{
description = "chainlink-terra";
inputs = {
n… ixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs = inputs@{ self, nixpkgs, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; overlays = [ rust-overlay.overlay ]; };
in rec {
devShell = pkgs.callPackage ./shell.nix {};
});
}
```
```
# shell.nix
{ stdenv, pkgs, lib }:
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
(rust-bin.stable.latest.default.override {
extensions = ["rust-src"];
targets = [
"x86_64-unknown-linux-gnu"
"wasm32-unknown-unknown"
];
})
cargo-generate
cargo-tarpaulin
];
RUST_BACKTRACE = "1";
}
```
When running `nix develop -c cargo clippy --all-targets -- -D warnings` it fails during proc macro checking:
```
Checking thiserror v1.0.29
error: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by /home/runner/work/foo/target/debug/deps/libthiserror_impl-22080d5093a0fbd6.so)
--> /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/thiserror-1.0.29/src/lib.rs:213:9
|
213 | pub use thiserror_impl::*;
| ^^^^^^^^^^^^^^
```
However compiling, running tests, `nix develop -c cargo fmt` or `nix develop -c cargo build --release --target wasm32-unknown-unknown` all works fine.
Possibly related: https://internals.rust-lang.org/t/bootstrap-fails-with-linker-error-while-running-on-non-nixos-nix-environment-wsl2-ubuntu/15066/2
2 Likes
Thanks! I am able to use that example, add that target, and it worked!
{
description = "wasm-pack setup";
inputs = {
nixpkgs = { url = "github:nixos/nixpkgs/nixos-unstable"; };
rust-overlay = { url = "github:oxalica/rust-overlay"; };
};
outputs = { nixpkgs, rust-overlay, ... }:
let system = "x86_64-linux";
in {
devShell.${system} = let
pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlay ];
};
in (({ pkgs, ... }:
pkgs.mkShell {
buildInputs = with pkgs; [
cargo
nodejs
wasm-pack
(rust-bin.stable.latest.default.override {
extensions = [ "rust-src" ];
targets = [ "wasm32-unknown-unknown" ];
})
];
shellHook = "";
}) { pkgs = pkgs; });
};
}
2 Likes