You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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;
}

修改说明

  1. 移除冗余依赖:删除所有Tauri桌面端相关组件(如GTK、WebKit等),Web项目无需这些桌面环境依赖
  2. 更新nixpkgs源:使用nixos-unstable分支确保工具链和包版本处于较新状态,适配Dioxus工具的要求
  3. 调整工具位置:将dioxus-cli和wasm-pack移至nativeBuildInputs,保证构建时能直接调用这些命令行工具
  4. 添加Wasm目标:通过shellHook自动安装wasm32-unknown-unknown,这是构建WebAssembly项目的必需目标平台

操作步骤

  1. 替换项目目录下的shell.nix为上述配置
  2. 重新进入nix环境:nix-shell
  3. 执行构建命令(明确指定Web平台):
dx build --platform web

构建成功后,产物会生成在项目根目录的dist文件夹中。

内容的提问来源于stack exchange,提问作者Eleanor Holley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 10:49:50