nutrx
January 28, 2025, 10:09am
1
In my case it’s about cargo-component
to create WebAssembly components, typically installed via cargo install component
and then used like this
cargo component build --target wasm32-wasip2 --release
This also works via
nix-shell --pure -p rustup cargo-component
cargo-component build --target wasm32-wasip2 --release
but it will fail in Nix builds because rustup doesn’t work:
my-build.nix
let
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-24.11.tar.gz") { };
in
pkgs.stdenv.mkDerivation {
name = "my-wasm-component";
src = ./my-wasm-component;
buildInputs = [
pkgs.rustup
pkgs.cargo-component
];
buildPhase = ''
cargo-component build --target wasm32-wasip2 --release
'';
installPhase = ''
cp target/wasm32-wasip2/release/my-wasm-component.wasm $out
'';
}
➜ nix-build my-build.nix
this derivation will be built:
/nix/store/m772j16rganmnikgzr2drvrk9p7vzl7c-my-wasm-component.drv
building '/nix/store/m772j16rganmnikgzr2drvrk9p7vzl7c-my-wasm-component.drv'...
Running phase: unpackPhase
unpacking source archive /nix/store/fx6igfd7pgr0g4vjhz4p42l11i5cxqag-my-wasm-component
source root is my-wasm-component
Running phase: patchPhase
Running phase: updateAutotoolsGnuConfigScriptsPhase
Running phase: configurePhase
no configure script, doing nothing
Running phase: buildPhase
Error: failed to load cargo metadata
Caused by:
`cargo metadata` exited with an error: error: could not create home directory: '/homeless-shelter/.rustup': Permission denied (os error 13)
Without rustup
it will run into other erros. Is there a general way to use cargo apps in Nix builds?
export HOME=$(mktemp -d)
in the Nix build might help a bit, but the main thing you should worry about is the fact you don’t have internet connection in Nix builds. I am not experienced with actual Rust development, but I had an interesting experience solving this issue:
opened 09:35PM - 27 Nov 24 UTC
closed 08:27PM - 31 Dec 24 UTC
build-system
topic:distros
Hello,
On NixOS, when we build packages, we disallow the usage of the interne… t. Since dependencies of Rust packages such as this and of course the source code itself of packages must be fetched from the internet, we have a mechanism to download in advance all sources required, and use a hash computed from the contents of the fetched sources, to verify these don't change.
So for example the rust dependencies of this package, are all downloaded to a cargo cache, and get a hash. Then this cache is used when we build Taskwarrior (offline, just like any other package).
Weirdly, we get this issue when using the above mechanism (which works well for an uncountable number of packages):
```
error: download of config.json failed
Caused by:
failed to download from `https://index.crates.io/config.json`
Caused by:
[6] Could not resolve hostname (Could not resolve host: index.crates.io)
make[2]: *** [src/taskchampion-cpp/CMakeFiles/cxxbridge_v1.0.128.dir/build.make:74: corrosion/cxxbridge_v1.0.128/bin/cxxbridge] Error 101
make[1]: *** [CMakeFiles/Makefile2:617: src/taskchampion-cpp/CMakeFiles/cxxbridge_v1.0.128.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
```
Do you have an idea what exactly that CMake file is trying to download? We should be able to fix our build if CMake will be able to detect that nothing should be downloaded, as the files are already there - because we'll put them there ourselves.
Which was basically solved in the end with:
postUnpack = ''
export CARGO_HOME=$PWD/.cargo
'';
While you use rustPlatform.cargoSetupHook
and a cargoDeps = rustPlatform.fetchCargoTarball { ... };
. With this you should be able to run any cargo install
command before any other build /install phase.
Hope this helps!
1 Like
573
January 28, 2025, 3:25pm
3
1 Like