huggingface / candle

Minimalist ML framework for Rust
Apache License 2.0
15.48k stars 917 forks source link

candle-kernels fails to build in Nix due to read-only sources #2453

Open gmacon opened 1 month ago

gmacon commented 1 month ago

In Nix, builds are isolated and have limited network access. The built artifacts are written into a read-only location known as the "store". It's common to build Rust applications using Crane which invokes cargo build with a cargo cache directory filled with symlinks into the store. This means that when the build script is run, the sources are read-only. The current build.rs in candle-kernels specifies that src/lib.rs, which is read-only in my application, be overwritten by bindgen_cuda. This behavior is discouraged by the Cargo book. They instead recommend a pattern where the generated code is written to the build script output directory ($OUT_DIR) and then include!d into the library. I think you should implement this pattern.

I also opened https://github.com/Narsil/bindgen_cuda/issues/10 requesting that bindgen_cuda change their documentation to avoid having future consumers fall into this same anti-pattern.

ahirner commented 3 weeks ago

Can you share your (crane) nix expression?

gmacon commented 3 weeks ago

This isn't necessarily the smallest possible reproducer, but it's a reasonably small diff of non-generated files from the Crane quick-start-simple template: https://github.com/gmacon/candle-build-failure-reproducer.

To reproduce: nix build. The failing derivation is /nix/store/hccbv08gly4xpc00a0fbkwx98yqm8ygs-quick-start-simple-deps-0.1.0.drv.