From 3b7e976deb3bfedcb3b31685ae9c8a332e651562 Mon Sep 17 00:00:00 2001 From: Tamipes Date: Tue, 25 Nov 2025 16:27:43 +0100 Subject: [PATCH] feat: add COMMIT_HASH build time env var --- build.rs | 17 +++++++++++++++++ flake.nix | 3 +++ src/main.rs | 2 ++ 3 files changed, 22 insertions(+) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..11f8d76 --- /dev/null +++ b/build.rs @@ -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); +} diff --git a/flake.nix b/flake.nix index 5640d0f..6cb17c6 100644 --- a/flake.nix +++ b/flake.nix @@ -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 diff --git a/src/main.rs b/src/main.rs index 0011090..bdb6c6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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();