nix: align Rust toolchain with rust-toolchain.toml in flake (#1798)

- derive Rust components from kernel/rust-toolchain.toml
- auto-add required components (cargo/rustc/rust-std) when missing
- emit trace warning when required components are absent in toml
- prioritize ${rust-toolchain}/bin in nix run yolo and devShell shellHook
- document fixed-output sha256 update workflow
- keep fixed-output hash strategy for reproducibility

This avoids cargo/rustc drift caused by PATH/toolchain mixing across Nix entrypoints.
This commit is contained in:
Vitus 2026-02-28 16:30:53 +08:00 committed by GitHub
parent 99ba1af9d4
commit f14b39e1e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 10 deletions

2
.envrc
View File

@ -1 +1 @@
use flake ./tools/nix-dev-shell
use flake

View File

@ -66,15 +66,34 @@
null
else
builtins.elemAt rust-channel-match 2;
rust-components = rust-toolchain-toml.toolchain.components or [ ];
rust-components-from-toml = rust-toolchain-toml.toolchain.components or [ ];
rust-required-components = [
"cargo"
"rustc"
"rust-std"
];
rust-missing-required = builtins.filter (
component: !(builtins.elem component rust-components-from-toml)
) rust-required-components;
rust-components-effective = lib.unique (rust-components-from-toml ++ rust-required-components);
rust-components-selected =
if rust-missing-required == [ ] then
rust-components-effective
else
builtins.trace "Warning: kernel/rust-toolchain.toml is missing required Rust components: ${lib.concatStringsSep ", " rust-missing-required}. DragonOS Nix build will auto-add them. Recommended: add them to kernel/rust-toolchain.toml to keep one explicit source of truth." rust-components-effective;
rust-dist-root = "https://rsproxy.cn/dist";
rust-toolchain-base = fenix.packages.${system}.toolchainOf {
root = rust-dist-root;
channel = rust-channel;
date = rust-date;
sha256 = "sha256-3JA9u08FrvsLdi5dGIsUeQZq3Tpn9RvWdkLus2+5cHs=";
# Fixed-output hash for Rust channel metadata.
# Update this when rust-toolchain channel/date changes:
# 1) run nix develop / nix run
# 2) copy the "got: sha256-..." from mismatch error to this field
# 3) re-run nix develop and verify rustc/cargo come from rust-toolchain-wrapped/bin
sha256 = "sha256-ggvRZZFjlAlrZVjqul/f/UpU5CEhDbdKZU0OCR8Uzbc=";
};
rust-toolchain-raw = rust-toolchain-base.withComponents rust-components;
rust-toolchain-raw = rust-toolchain-base.withComponents rust-components-selected;
rust-toolchain = pkgs.symlinkJoin {
name = "rust-toolchain-wrapped";
paths = [ rust-toolchain-raw ];
@ -156,6 +175,8 @@
runApp = pkgs.writeScriptBin "dragonos-yolo" ''
#!${pkgs.runtimeShell}
set -e
export USING_DRAGONOS_NIX_ENV=1
export PATH=${rust-toolchain}/bin:$PATH
echo "==> Step 1: Building kernel with make kernel..."
${pkgs.gnumake}/bin/make kernel
@ -260,6 +281,7 @@
# Shell启动脚本
shellHook = ''
export PATH=${rust-toolchain}/bin:$PATH
# 自动创建 GC root避免 nix store gc 清理开发环境
if [ -z "''${DRAGONOS_NIX_GC_ROOT:-}" ]; then
if [ ! -e ".nix-gc-root" ]; then
@ -269,8 +291,14 @@
fi
echo " DragonOS Nix !"
echo ""
echo " Rust toolchain:"
echo " rustc: $(rustc -V)"
echo " cargo: $(cargo -V)"
echo " path : ${rust-toolchain}/bin (prepended)"
echo ""
echo " DragonOSrootfsQEMU"
echo ""
echo " 沿 shell Rust/Cargo "
echo " : nix run .#yolo-x86_64"
echo " : make kernel"
echo " rootfs: nix run .#rootfs-x86_64"

View File

@ -271,10 +271,7 @@ let
inherit arch;
isNographic = if arch == "riscv64" then true else baseConfig.nographic;
qemuBin =
if preferSystemQemu then
"qemu-system-${arch}"
else
"${pkgs.qemu_kvm}/bin/qemu-system-${arch}";
if preferSystemQemu then "qemu-system-${arch}" else "${pkgs.qemu_kvm}/bin/qemu-system-${arch}";
}
);
in

View File

@ -67,8 +67,8 @@ in
cross.glibc
# Simple C utilities
(static.callPackage ./about {})
(static.callPackage ./c_unitest {})
(static.callPackage ./about { })
(static.callPackage ./c_unitest { })
]
++ lib.optionals (target == "x86_64" && testOpt.syscall.enable) [