Skip to content

Commit

Permalink
nix: send the Git date to build.rs
Browse files Browse the repository at this point in the history
Thanks to @thoughtpolice for providing the original version of the nix code; I
edited it a bit.
  • Loading branch information
ilyagr committed Mar 26, 2024
1 parent fc184e0 commit ca13d2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
19 changes: 13 additions & 6 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,20 @@ fn timestamp_to_date(ts_str: &str) -> Option<DateTime<Utc>> {
.and_then(|ts| DateTime::<Utc>::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<DateTime<Utc>>)> {
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<DateTime<Utc>>) {
Expand Down
10 changes: 9 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ca13d2b

Please sign in to comment.