在NixOS中搭建基础Dioxus项目遇构建错误,求可行shell配置
Dioxus Web项目在NixOS下的构建问题解决
问题背景
在NixOS上基于Dioxus开发Web项目,参考Tauri文档编写shell.nix并添加dioxus-cli和wasm-pack,执行dx new hot_dog创建Web项目(拒绝全栈、路由、Tailwind选项),cargo build执行正常,但dx build报错:
[nix-shell:~/workbench/hot_dog]$ dx build 0. 0s INFO Building project... 0.243s ERROR Failed to verify tooling: I/O Error: No such file or directory (os error 2) dx will proceed, but you might run into errors later. 0.321s ERROR Cargo build failed - no output location 0.332s ERROR err=Other(Build did not return an executable ...(栈跟踪信息省略)
原shell.nix配置:
let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/archive/fa35a3c8e17a3de613240fea68f876e5b4896aec.tar.gz"; pkgs = import nixpkgs { config = { }; overlays = []; }; in pkgs.mkShell { buildInputs = with pkgs;[ at-spi2-atk atkmm cairo gdk-pixbuf glib gtk3 harfbuzz librsvg libsoup_3 pango webkitgtk_4_1 openssl ]; nativeBuildInputs = with pkgs; [ pkg-config gobject-introspection cargo cargo-tauri nodejs ]; packages = with pkgs; [ dioxus-cli emacs git wasm-pack ]; RUST_BACKTRACE = 1; RUSTFLAGS = "-Awarnings"; }
解决方案
使用以下精简适配的shell.nix配置:
let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz"; pkgs = import nixpkgs { config = { }; overlays = []; }; in pkgs.mkShell { nativeBuildInputs = with pkgs; [ cargo pkg-config dioxus-cli wasm-pack rustup ]; # 自动安装WebAssembly目标平台 shellHook = '' rustup target add wasm32-unknown-unknown ''; RUST_BACKTRACE = 1; }
修改说明
- 移除冗余依赖:删除所有Tauri桌面端相关组件(如GTK、WebKit等),Web项目无需这些桌面环境依赖
- 更新nixpkgs源:使用
nixos-unstable分支确保工具链和包版本处于较新状态,适配Dioxus工具的要求 - 调整工具位置:将
dioxus-cli和wasm-pack移至nativeBuildInputs,保证构建时能直接调用这些命令行工具 - 添加Wasm目标:通过
shellHook自动安装wasm32-unknown-unknown,这是构建WebAssembly项目的必需目标平台
操作步骤
- 替换项目目录下的
shell.nix为上述配置 - 重新进入nix环境:
nix-shell - 执行构建命令(明确指定Web平台):
dx build --platform web
构建成功后,产物会生成在项目根目录的dist文件夹中。
内容的提问来源于stack exchange,提问作者Eleanor Holley
相关产品推荐
相关产品推荐

