Skip to content

Allow supplying of prebuilt bootloader to avoid nested Cargo builds #583

Open

Description

@debater-coder

Hi there,

Thanks for the wonderful crate.

I've been trying to build my Rust kernel using Nix (it makes building my userspace and ramdisk a lot easier).
Nix runs builds in a sandbox without any network access unless the output of the build is guaranteed to match a hash (which it provides utilities for doing from a Cargo lock file). Basically this works best if Nix is what invokes Cargo and it has access to the lock file.

Basically, bootloader's invocation of Cargo from its build script causes problems here, since it builds the bootloader-x86_64-uefi crate (I use UEFI only but I believe something similar happens for BIOS builds), and though it runs --locked, there isn't really a good way to get Nix to "reach" that lock file.

What I would like is a way to supply a prebuilt .efi file, made by a Cargo build running separately, to the build script (by say an environment variable holding a path to the built EFI).

bootloader/build.rs

Lines 70 to 91 in 9ea5b3d

let mut cmd = Command::new(cargo);
cmd.arg("install").arg("bootloader-x86_64-uefi");
if Path::new("uefi").exists() {
// local build
cmd.arg("--path").arg("uefi");
println!("cargo:rerun-if-changed=uefi");
println!("cargo:rerun-if-changed=common");
cmd.arg("--target-dir").arg("target/uefi_target");
} else {
cmd.arg("--version").arg(BOOTLOADER_VERSION);
}
cmd.arg("--locked");
cmd.arg("--target").arg("x86_64-unknown-uefi");
cmd.arg("-Zbuild-std=core")
.arg("-Zbuild-std-features=compiler-builtins-mem");
cmd.arg("--root").arg(&out_dir);
cmd.arg("-vv");
cmd.env_remove("RUSTFLAGS");
cmd.env_remove("CARGO_ENCODED_RUSTFLAGS");
let status = cmd
.status()
.expect("failed to run cargo install for uefi bootloader");

So instead of building the EFI in the build script, it could be done outside and copied into that output directory. If this sounds sensible, I would be willing to open a PR to do this. I would believe similar options would need to be made for each of the BIOS stages for consistency.


Alternatively, since this crate requires a nightly compiler anyway, if this crate used artifact binary dependencies to build each of the bootloader stages and used those built versions in the compiler since that means the same lockfile is used for those builds. This would solve the problem completely, I have tested that Nix can build Rust packages with artifact dependencies just fine with no extra configurations. If this is feasible it would likely be preferable.

Activity

  1. Freax13 commented on Nov 5, 2026

    @Freax13
    Member

    That's an interesting problem.

    I suppose we could allow overriding the binary using an environment variable. That said if we want to do so, I think we should document very clearly that the bootloader's build process is subject to change and may be changed without a breaking release. I don't think we'll want to lock down how to bootloader is built internally.

    Alternatively. you could set up your nix derivation to patch the bootloader crate to pass in the binaries like you described.

    IIRC there are some issues with using artifact dependencies and -Zbuild-std. While we could probably build the UEFI bootloader without -Zbuild-std this is not viable for the BIOS bootloader.

  2. debater-coder commented on Nov 7, 2026

    @debater-coder
    Author

    Ah, it is this right? rust-lang/cargo#10444

  3. Freax13 commented on Nov 7, 2026

    @Freax13
    Member

    I think so, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions