News for hackers. Stuff that matters.
Re:Rust is actually portable by hazz99 on Thursday July 28, 2022 @03:36AM (#32261470)
I’m using Bazel to build my rust project (Using the rules_rust rules) and it’a become quite a pain to use in concert with docker.

This is not a complaint about Bazel specifically, its fantastic, and easily my favourite build system bar none.

However it cannot cross compile Rust. This means if I’m developing on my MacBook, and I want to compile a Rust binary and put it in an Ubuntu docker container, I can’t do it on my host machine. I need to copy the source into the container and build it there, using multistage builds.

But this is -extremely slow- because it cannot take advantage of Rusts build caching. I’m talking 10-15 minutes for my small Rust project.

Has anyone run into this? How do you work around it?

I’ve considered running a Bazel remote execution server on a local Ubuntu VM, but this feels like so much extra complexity just to use Rust, Bazel and containers.

Re:Rust is actually portable by jmillikin on Thursday July 28, 2022 @05:21AM (#32262110)
Bazel can cross-compile Rust, but you need a linker that can produce target executables. Cargo has the same limitation.

Apple ld doesn't support Linux as an output target, so you need to use GNU ld or LLVM lld instead.

Code examples at https://john-millikin.com/notes-on-cross-compiling-rust#baze...

Re:Rust is actually portable by tinco on Thursday July 28, 2022 @03:44AM (#32261507)
Why not just mount the code in the container instead of copying it over? Rust deals very cleanly with build artefacts from different architectures so there's no risk of corruption.
Re:Rust is actually portable by fnord123 on Thursday July 28, 2022 @06:06AM (#32262453)
This is a good idea but there is tension between this straightforward approach and the fact that, on macos, docker is running in a VM so it's inherently slower.
Re:Rust is actually portable by arein3 on Thursday July 28, 2022 @09:30AM (#32264021)
Isn't docker trying to use virtualization from the OS to run linux on "bare metal"? If so it should acheive similar speeds to native linux
Re:Rust is actually portable by tinco on Thursday July 28, 2022 @03:12PM (#32268881)
The virtualized storage layer could actually be a bottleneck here. Though I think they've improved the performance of it to the point where it might no longer be a bottleneck but I'm not sure.
Re:Rust is actually portable by andrewaylett on Thursday July 28, 2022 @12:44PM (#32266828)
You might want to try using Docker's cache mounts? https://docs.docker.com/engine/reference/builder/#run---moun...

I've been using them locally of late, and they're excellent for storing state that's not obviously identical across invocations of the same layer.

Re:Rust is actually portable by lijogdfljk on Thursday July 28, 2022 @10:55AM (#32265198)
You can cache the Rust target and make it use Rust's cache properly.

There are two common ways i'm familiar with. Cargo Chef and Manually. Manually can be a bit involved, but you basically just build with empty source and prune your fake-lib files from the cache dir. Cargo Chef is very easy.

My docker images build as fast as my local images. Though if you have many crates in a workspace it can start to become difficult.

Re:Rust is actually portable by amardeep on Thursday July 28, 2022 @05:08AM (#32261990)
I haven't used it yet but https://github.com/nadirizr/dazel might help. It runs bazel inside a docker container via a seamless proxy.
Re:Rust is actually portable by bdavis__ on Thursday July 28, 2022 @08:05AM (#32263196)
"MacBook, Rust, Bazel and containers." what is the essence of what you are trying to accomplish. focus on that.