Exposing Git Information in Rust Binaries Built with Nix
12 days ago
- #Rust
- #Nix
- #Git
- Git information in Rust binaries helps track versions and commits, useful for debugging.
- shadow-rs crate is used to embed git info (like commit hash, branch) at build time for CLI tools.
- Nix builds exclude .git directory, preventing shadow-rs from accessing git info.
- Solution: Use Nix flake's self.rev to pass commit hash as a build-time environment variable.
- Modified build.rs to handle both cargo and Nix builds, ensuring version info is available in both.
- For Nix builds, commit hash is taken from flake's self.rev; branch name remains unavailable.
- Final output shows version info includes commit hash for Nix builds and branch name for cargo builds.
- Acknowledgments to contributors for guidance and solution.
- Author invites feedback via social media and offers early access to komorebi for Mac via GitHub sponsorship.