From 4b42b45679b774356486c2c0a9fd3909a5043746 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Fri, 11 Aug 2023 20:20:12 -0700 Subject: [PATCH] nix: send the Git date to `build.rs` Thanks to @thoughtpolice for providing the original version of the nix code; I edited it a bit. --- cli/build.rs | 19 +++++++++++++------ flake.nix | 10 +++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index ec5119d762..942c616981 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -69,13 +69,20 @@ fn timestamp_to_date(ts_str: &str) -> Option> { .and_then(|ts| DateTime::::from_timestamp(ts, 0)) } -/// Return the git hash and the committer timestamp +/// Return the UTC committer date (maybe) and the git hash fn get_git_timestamp_and_hash() -> Option<(String, Option>)> { - if let Some(nix_hash) = std::env::var("NIX_JJ_GIT_HASH") - .ok() - .filter(|s| !s.is_empty()) - { - return Some((nix_hash, None)); + if let (Ok(nix_hash), maybe_nix_timestamp) = ( + std::env::var("NIX_JJ_GIT_HASH"), + std::env::var("NIX_JJ_GIT_TIMESTAMP"), + ) { + if !nix_hash.is_empty() { + return Some(( + nix_hash, + maybe_nix_timestamp + .ok() + .and_then(|ts_str| timestamp_to_date(&ts_str)), + )); + } } fn parse_timestamp_vbar_hash(bytes: &[u8]) -> (String, Option>) { diff --git a/flake.nix b/flake.nix index 5a0544268f..39e4acf1c3 100644 --- a/flake.nix +++ b/flake.nix @@ -18,6 +18,11 @@ } // (flake-utils.lib.eachDefaultSystem (system: let + # TODO(aseipp): Use dirtyRev and dirtyShortRev to record dirty checkout + # when we update `build.rs` to understand dirty checkouts. + gitRev = self.rev or ""; + gitTimestamp = toString self.lastModified; + pkgs = import nixpkgs { inherit system; overlays = [ @@ -100,7 +105,8 @@ ZSTD_SYS_USE_PKG_CONFIG = "1"; LIBSSH2_SYS_USE_PKG_CONFIG = "1"; RUSTFLAGS = pkgs.lib.optionalString useMoldLinker "-C link-arg=-fuse-ld=mold"; - NIX_JJ_GIT_HASH = self.rev or ""; + NIX_JJ_GIT_HASH = gitRev; + NIX_JJ_GIT_TIMESTAMP = gitTimestamp; CARGO_INCREMENTAL = "0"; preCheck = '' @@ -188,6 +194,8 @@ export RUST_BACKTRACE=1 export ZSTD_SYS_USE_PKG_CONFIG=1 export LIBSSH2_SYS_USE_PKG_CONFIG=1 + export NIX_JJ_GIT_HASH="${gitRev}" + export NIX_JJ_GIT_TIMESTAMP="${gitTimestamp}"; '' + pkgs.lib.optionalString useMoldLinker '' export RUSTFLAGS="-C link-arg=-fuse-ld=mold" '' + darwinNextestHack;