about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2016-01-05 15:32:59 +0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2016-01-05 17:21:48 +0100
commit81e530a7499e3f06bec13b6ec974d0f1e3a4ab84 (patch)
treee36db6e22b2ce16e5b3bdd9a3788a782d1c4704d
parent2b5ed580993b7276f21f55a5d31f8edf0fc155a1 (diff)
downloadnixlib-81e530a7499e3f06bec13b6ec974d0f1e3a4ab84.tar
nixlib-81e530a7499e3f06bec13b6ec974d0f1e3a4ab84.tar.gz
nixlib-81e530a7499e3f06bec13b6ec974d0f1e3a4ab84.tar.bz2
nixlib-81e530a7499e3f06bec13b6ec974d0f1e3a4ab84.tar.lz
nixlib-81e530a7499e3f06bec13b6ec974d0f1e3a4ab84.tar.xz
nixlib-81e530a7499e3f06bec13b6ec974d0f1e3a4ab84.tar.zst
nixlib-81e530a7499e3f06bec13b6ec974d0f1e3a4ab84.zip
Set SOURCE_DATE_EPOCH to latest source file
This provides a timestamp that's more useful than 1970-01-01 yet still
deterministic.
-rw-r--r--pkgs/build-support/setup-hooks/set-source-date-epoch-to-latest.sh37
-rw-r--r--pkgs/stdenv/generic/default.nix1
2 files changed, 38 insertions, 0 deletions
diff --git a/pkgs/build-support/setup-hooks/set-source-date-epoch-to-latest.sh b/pkgs/build-support/setup-hooks/set-source-date-epoch-to-latest.sh
new file mode 100644
index 000000000000..e57848cff558
--- /dev/null
+++ b/pkgs/build-support/setup-hooks/set-source-date-epoch-to-latest.sh
@@ -0,0 +1,37 @@
+updateSourceDateEpoch() {
+    local path="$1"
+
+    # FIXME: Support Darwin.
+    if ! [[ $system =~ linux ]]; then
+        exit 1
+        return
+    fi
+
+    # Get the last modification time of all regular files, sort them,
+    # and get the most recent. Maybe we should use
+    # https://github.com/0-wiz-0/findnewest here.
+    local -a res=($(find "$path" -type f -print0 | xargs -0 -r stat -c '%Y %n' | sort -n | tail -n1))
+    local time="${res[0]}"
+    local newestFile="${res[1]}"
+
+    # Update $SOURCE_DATE_EPOCH if the most recent file we found is newer.
+    if [ "$time" -gt "$SOURCE_DATE_EPOCH" ]; then
+        echo "setting SOURCE_DATE_EPOCH to timestamp $time of file $newestFile"
+        export SOURCE_DATE_EPOCH="$time"
+
+        # Warn if the new timestamp is too close to the present. This
+        # may indicate that we were being applied to a file generated
+        # during the build, or that an unpacker didn't restore
+        # timestamps properly.
+        local now="$(date +%s)"
+        if [ "$time" -gt $((now - 60)) ]; then
+            echo "warning: file $newestFile may be generated; SOURCE_DATE_EPOCH may be non-deterministic"
+        fi
+    fi
+}
+
+postUnpackHooks+=(_updateSourceDateEpochFromSourceRoot)
+
+_updateSourceDateEpochFromSourceRoot() {
+    updateSourceDateEpoch "$sourceRoot"
+}
diff --git a/pkgs/stdenv/generic/default.nix b/pkgs/stdenv/generic/default.nix
index 2fb1f8b39ae4..6dc2553db386 100644
--- a/pkgs/stdenv/generic/default.nix
+++ b/pkgs/stdenv/generic/default.nix
@@ -84,6 +84,7 @@ let
       ../../build-support/setup-hooks/patch-shebangs.sh
       ../../build-support/setup-hooks/move-sbin.sh
       ../../build-support/setup-hooks/move-lib64.sh
+      ../../build-support/setup-hooks/set-source-date-epoch-to-latest.sh
       cc
     ];