Login
You're viewing the discuss.systems public feed.
  • Mar 19, 2026, 3:50 AM

    i just built the same binary twice from the same source code using the same version of rustc, once with crate2nix and once with `cargo build --release`, and the cargo one is a megabyte and a half *bigger*. wtf????

    #nix #crate2nix #duckduckfedi #rust

    💬 1🔄 0⭐ 0

Replies

  • 💬 0🔄 0⭐ 0
  • Mar 19, 2026, 3:55 AM

    wtf? when i run both binaries through strip they're still different sizes. the cargo one is still 800KB bigger. WTF??????

    is it missing a whole goddamn crate?

    💬 0🔄 0⭐ 0
  • Mar 19, 2026, 5:04 AM

    @AVincentInSpace
    What settings does crate2nix change? Even with stripping and high optimization I can think of a lot of things that could change binary size:

    • using thin/fat LTO or not (disabled by default in Cargo as it leads to catastrophic link times for large projects, but can significantly reduce binary sizes with a wholistic optimization and dead code elimination)
    • parallel codegen reduces optimization opportunities (Cargo codegen-units defaults to 16 to speed up compilation, 1 significantly improves optimization)
    • the libstd used by Nix is built with LTO or different optimization levels than the default one provided with rustc
    • even with debuginfo stripped, limited location information is still kept in the binary to make panic backtraces more useful, although that can't be disabled without a nightly compiler
    💬 1🔄 0⭐ 0
  • Mar 19, 2026, 5:21 AM

    @nytpu Thanks for your help. I read the source code of buildRustCrate, which crate2nix uses internally (god I wish literally anything in the Nix ecosystem was documented) and determined that it defaults to 1 codegen unit. I instructed Cargo to use this as well, and it's better, but the Cargo-built binary is still 35KB bigger after stripping.

    That discrepancy is too small to be explained by LTO across the whole crate, and none of the flags buildRustCrate passes to rustc mention it at all.

    I'm not sure whether Nix uses its own build of libstd different from the rustup one. If it was compiled with LTO and rustup's wasn't for some reason, that might explain it, although I don't know how to find out.

    I would really like Cargo and crate2nix to be able to generate sha256 equivalent binaries. I'm pretty sure I saw another project a while ago that had done it but I don't remember which one. I'm gonna go try and find some of those.

    💬 0🔄 0⭐ 0
  • Mar 19, 2026, 3:16 PM

    @AVincentInSpace nix strips the debug info. But my first thought was - who cares about half a meg if binaries are gig+. Or at least they are here :)

    💬 1🔄 0⭐ 0
  • Mar 19, 2026, 4:49 PM

    @manpacket First of all, no it doesn't. Running both files through strip reduced both by a meg+. Second of all, the filesizes were still 800KB different after that, and still 35KB different after I told Cargo to use codegen_units=1. Third of all, the binaries are less than six megs unstripped, but lastly and most importantly, I want my builds to be reproducible, and if possible I want Cargo and crate2nix to produce identical files so I can know for sure that if something weird happens and I get a bug report, the method the person used to build it isn't at fault.

    💬 1🔄 0⭐ 0
  • Mar 19, 2026, 5:27 PM

    @AVincentInSpace Hmm... I think nix strips them by default and you have to disable it, at least that's how it behaves here.

    One other source of the discrepancy might be a feature unification or lack of it in crate2nix. But I would expect this will make the resulting binary bigger, not smaller.

    Are you linking in any external libraries? This can be a source of the difference. I'd run `cargo build -vvv` or something similar and compare all the rustc invocations, possibly with a script.

    💬 0🔄 0⭐ 0