Skip to content

rust: bootstrap from source #85542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: staging
Choose a base branch
from

Conversation

progval
Copy link
Member

@progval progval commented Apr 19, 2020

Motivation for this change

Fully building Rust from source instead of starting with a binary download.
Resolves #72606.

Things done
  • Added mrustc, a small compiler written in C++
  • Compile Rust 1.29 with mrustc (this is the latest version that mrustc can compile)
  • Add Rust 1.30.2, to 1.41.0, as each version 1.n is required to build version 1.(n+1)
  • Change the 1.42 derivation to build from the previous 1.41 derivation instead of rustc-binary
Discussion

I am not sure this is something you want in Nixpkgs; as it significantly increases the resources needed to build the latest Rust version.
The initial build of Rust 1.29 (by mrustc) requires 2.5GB in /build and 15GB of RAM and takes about 30 minutes on a modern x86 machine. Each subsequent build of Rust (by rustc) takes about 15 minutes.
Most of the build time (especially the first one) is spent in a single compilation unit, which is src/librustc/lib.rs, so it is not very parallel.

It also requires LLVM 7 and 8 for older Rust version, in addition to LLVM 9 for the current version. (Although all versions should support LLVM 7, if you want to reduce this.)

Tests

I tested building on x86_64 and master. I'm currently building the chain on x86_64 + staging but it's looking good so far (I'll update this text when it's done).
I'm also trying to test on aarch64, but it's taking forever as my test machine only has 4GB of RAM.

Checklist
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

Sorry, something went wrong.

@progval progval requested review from andir, LnL7 and Mic92 as code owners April 19, 2020 10:13
@ofborg ofborg bot added the 6.topic: rust label Apr 19, 2020
Copy link
Member

@andir andir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Great work! 👍

I've attempted this once before but ran into issue building the first "real" rustc with mrustc.

I left a few comments on parts that did stick out to me. We are likely also going to have the whole bootstrap story around rustc. It is a fairly long bootstrap chain. Probably hours on modern computers. That being said: we totally should bootstrap more stuff without needing external binaries.

If we give this a bit more polishing this could probably go in.

aarch64-unknown-linux-gnu = "79ddfb5e2563d0ee09a567fbbe121a2aed3c3bc61255b2787f2dd42183a10f27";
i686-apple-darwin = "628134b3fbaf5c0e7a25bd9a2b8d25f6e68bb256c8b04a3332ec979f5a1cd339";
x86_64-apple-darwin = "b6504003ab70b11f278e0243a43ba9d6bf75e8ad6819b4058a2b6e3991cc8d7a";
bootstrapRustPackages = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC mrustc did not support all the platforms we wanted to support. We should perhaps use binary bootstrapping on all of those platforms that we can't use mrustc on.

}:

let
inherit (stdenv.lib) optionals optional optionalString;
inherit (darwin.apple_sdk.frameworks) Security;

llvmSharedForBuild = pkgsBuildBuild.llvm_9.override { enableSharedLibraries = true; };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this change here is wise. This likely breaks cross-compilation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm indeed, I didn't notice this. Should I replace this with a "selector" function, like pkgs: pkgs.llvm_7?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not entirely sure I can see what you are saying. Can you draft an example here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://gist.github.com/ProgVal/da945eab4ec04f55123411b4063de001

It's a pattern I noticed these files already use (selectRustPackage)

@progval progval force-pushed the rust-source-bootstrap branch from 0fe3747 to 0b9697e Compare April 19, 2020 10:42
@andir
Copy link
Member

andir commented Apr 19, 2020

@progval

I'm also trying to test on aarch64, but it's taking forever as my test machine only has 4GB of RAM.

We have a larger aarch64 community builder that you can use for such purposes: https://github.com/nix-community/aarch64-build-box

@andir andir requested a review from alyssais April 19, 2020 10:44
@andir
Copy link
Member

andir commented Apr 19, 2020

I added @alyssais to reviewers as we had previous conversations about this topic. I expect they might have something to add/own experiences to share.

@progval
Copy link
Member Author

progval commented Apr 19, 2020

I've attempted this once before but ran into issue building the first "real" rustc with mrustc.

At first I tried being more modular; by replacing most of mrustc's makefiles with nix expressions; but it ended up as a complete mess that didn't even work because multiple .so and .rlib paths were conflicting with each other, causing either missing files, wrong rust version or "possibly newer crate" errors. Was this your issue too?

@progval progval force-pushed the rust-source-bootstrap branch from 0b9697e to 5bc3d80 Compare April 19, 2020 10:51
@andir
Copy link
Member

andir commented Apr 19, 2020

I've attempted this once before but ran into issue building the first "real" rustc with mrustc.

At first I tried being more modular; by replacing all of mrustc's makefiles with nix expressions; but it ended up as a complete mess that didn't even work because .so and .rlib paths were conflicting with each other. Was this your issue too?

No, I did the last attempt during December and IIRC this issue contains most of my story: thepowersgang/mrustc#132

Copy link
Member

@alyssais alyssais left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is amazing! I’m so excited for it!

@alyssais
Copy link
Member

My experience was basically the same — I didn’t get very far.

@progval progval force-pushed the rust-source-bootstrap branch from 0d48bb8 to c27a821 Compare April 19, 2020 12:07
@andir
Copy link
Member

andir commented Apr 19, 2020

I just gave this a shot on aarch64 and ran into this:
https://gist.github.com/andir/9ff79f180e1c24d6be474a1c755b542f

@r-burns
Copy link
Contributor

r-burns commented Feb 16, 2021

Would anyone object if I rebased and separately submitted the mrustc init commit? It seems like there are still some open questions here about the bootstrapping, but maybe we can save the mrustc nix expression from bitrot and work on darwin support in the meantime.

@r-burns r-burns mentioned this pull request Feb 20, 2021
10 tasks
@r-burns
Copy link
Contributor

r-burns commented Feb 20, 2021

ping #113817

@KamilaBorowska
Copy link
Member

Worth noting that mrustc was updated to support bootstrapping Rust 1.39: https://github.com/thepowersgang/mrustc/.

@progval
Copy link
Member Author

progval commented Apr 4, 2021

I'm working on a patch on top of #113817 to support it

@siraben
Copy link
Member

siraben commented Jul 26, 2021

Does this PR still need to stay open?

@siraben
Copy link
Member

siraben commented Jul 26, 2021

Also, you might be interested in #123095 which is about bootstrapping the trusted binary seed fully from source (down from an 130 MB uncompressed tarball).

@r-burns
Copy link
Contributor

r-burns commented Sep 1, 2021

I wonder if we can avoid the full stage2 bootstrap for each intermediate rustc binary - i.e. use x.py build instead of x.py dist so that we build each rustcN+1 using a rustcN which was built by rustcN-1. In theory this could cut the build time of each rustc in the bootstrap chain by 50%, because only stage1 would be built. However, rustc documentation hints that there may be ABI issues, so I'd have to test it out to see if it works.

https://rustc-dev-guide.rust-lang.org/building/bootstrapping.html#stages-of-bootstrapping

@Mic92
Copy link
Member

Mic92 commented Sep 2, 2021

Even half the bootstrap chain is still too much in my opinion.

@Ericson2314
Copy link
Member

I still don't see the problem of just adding this as a separate way of building rust, with the goal using it to create the bootstrap binaries. That doesn't wreck rebuild critical path length. What's the problem then?

@stale
Copy link

stale bot commented Apr 18, 2022

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Apr 18, 2022
@06kellyjac
Copy link
Member

Still important.

mrustc got closer to latest but there have been some more releases since.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Apr 18, 2022
@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 2, 2022
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/where-did-you-get-stuck-in-the-nix-ecosystem-tell-me-your-story/31415/6

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Aug 8, 2023
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 19, 2024
@wegank wegank marked this pull request as draft March 20, 2024 15:40
@ghost ghost closed this Dec 21, 2024
@Atemu
Copy link
Member

Atemu commented Dec 21, 2024

That's not a reason to close this. The interesting part of this PR is actually using it to bootstrap rust, not the package itself.

I'm going to mark this as a draft though as this hasn't seen activity in a while and needs broader consensus and/or more confidence in mrustc's ability to keep up with upstream rust so that we don't need to have an insanely long bootstrap chain with dozens of rustcs.

Edit: Was a draft anyways.

@Atemu Atemu reopened this Dec 21, 2024
@github-actions github-actions bot added the 8.has: maintainer-list (update) This PR changes `maintainers/maintainer-list.nix` label Dec 21, 2024
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Dec 21, 2024
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: rust 8.has: maintainer-list (update) This PR changes `maintainers/maintainer-list.nix` 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 501-1000 10.rebuild-darwin: 501+ 10.rebuild-linux: 501+ 10.rebuild-linux: 1001-2500
Projects
Status: WIP
Development

Successfully merging this pull request may close these issues.

None yet