feat: add COMMIT_HASH build time env var

This commit is contained in:
Tamipes 2025-11-25 16:27:43 +01:00
parent e7d06a45f6
commit 3b7e976deb
3 changed files with 22 additions and 0 deletions

17
build.rs Normal file
View file

@ -0,0 +1,17 @@
use std::process::Command;
fn main() {
let commit_hash = match std::env::var("COMMIT_HASH") {
Ok(commit_hash_string) => commit_hash_string,
Err(_) => {
match Command::new("git")
.args(vec!["rev-parse", "--short", "HEAD"])
.output()
{
Ok(x) => String::from_utf8_lossy(x.stdout.trim_ascii_end()).into_owned(),
Err(_) => "no hash :(".to_string(),
}
}
};
println!("cargo::rustc-env=COMMIT_HASH=\"{}\"", commit_hash);
}

View file

@ -44,6 +44,9 @@
# Additional environment variables can be set directly
# MY_CUSTOM_VAR = "some value";
COMMIT_HASH = builtins.substring 0 7 (
if self ? rev then self.rev else "nixDirt"
);
};
craneLibLLvmTools = craneLib.overrideToolchain

View file

@ -14,6 +14,8 @@ async fn main() {
tracing_subscriber::fmt::init();
let cache = KubeCache::Cache::create().unwrap();
let arcCache = Arc::new(cache);
let commit_hash: &'static str = env!("COMMIT_HASH");
println!("COMMIT_HASH: {}", commit_hash);
let mut listener = TcpListener::bind("0.0.0.0:25565").await.unwrap();