Make rust reproducible!

Rust is not clean programming language:

[kreyren@leonid:~]$ cat shell.nix
# NixOS shell configuration to bootstrap the required dependencies
# NOTE(Krey): Uses mozilla's overlay, because nix's upstream is slow

let
	moz_overlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz);
	nixpkgs = import <nixpkgs> { overlays = [ moz_overlay ]; };
in
	with nixpkgs;
	stdenv.mkDerivation {
		name = "moz_overlay_shell";
		buildInputs = [
			# NOTE(Krey): Using nightly for 2021 edition
			nixpkgs.latest.rustChannels.nightly.rust
		];
	}

[nix-shell:/tmp]$ cd dirty-rust/

[nix-shell:/tmp/dirty-rust]$ for file in hello1.rs hello2.rs; do printf '%s\n' 'fn main() { println!("Hello, World!"); }' > "$file" && rustc "$file"; done

[nix-shell:/tmp/dirty-rust]$ sha256sum hello{1,2}
469e4a490b1e51356e8c23a3d3bfe72e9feb5cbef895c21f7add5b38ab95efcb  hello1
df0f08665258fc13c21e8b2ae28481d1c98aada9fc5920bb2d321bd307a9f3de  hello
[kreyren@leonid:/tmp/dirty-rust]$ nix-shell -p diffoscope --run "diffoscope hello{1,2}"
https://gist.githubusercontent.com/Kreyren/66b7acf1332a54960e1b3c1e1ce41b72/raw/d8924bb1197cc9d8922e01b9b17d28b8725995e2/gistfile1.txt

Which makes it unsuitable for mission critical development.

Context

I am trying to define a framework for modular software development on rustlang by defining Kconfig-like handling for feature-gates that are reading a source-independent file governed by a written standard, but the rustc is making it impossible to develop a test for bit-by-bit cleaness to ensure that the feature gates are not included in assuming it being designed for mission-critical environment (aviation) to avoid random software failure by the feature gate influencing the runtime e.g. airspeed indicator to cause a low altitude aerodynamic stall in the worst case scenario in fly-by-wire aircraft.

Relevant: Creates unexpected dirty binaries · Issue #4 · reitermarkus/cargo-eval · GitHub

2 Likes

You can get reproducible builds by making the source filenames match and by using the --remap-path-prefix option to make the source paths map to the same thing. For example, this command:

for DIR in hello1 hello2; do \
    mkdir -p "$DIR"; \
    rm -f "$DIR/hello.rs"; \
    echo 'fn main() { println!("hello") }' >"$DIR/hello.rs"; \
    rustc --remap-path-prefix "$DIR=." "$DIR/hello.rs" -o "$DIR/hello"; \
    md5sum "$DIR/hello"; \
done

Produces this output (using rustc 1.54.0 on Linux x86_64):

37dd3b6b9db6724d458ec8e74b90ea26  hello1/hello
37dd3b6b9db6724d458ec8e74b90ea26  hello2/hello

For more information, see:

12 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.