Repository navigation
Description
Describe the bug
I discovered this looking into these two Nixpkgs PRs:
- stdenv: ignore structured attrs for source epoch nixpkgs#586202
- tzdata: fails to build on or after 2026-11-01 nixpkgs#582038
Before the builder starts, Nix writes a set of input files into the build directory:
.attrs.jsonand.attrs.sh(structured attrs).attr-<hash>(passAsFile)exportReferencesGraphoutputs
Since #13769 these all go through DesugaredEnv::extraFiles into UnixDerivationBuilderImpl::writeBuilderFile. Each file keeps the wall-clock time of the write as its mtime.
On sandboxed builds, startBuilder calls initEnv() (which writes these files) before prepareSandbox() creates the chroot root, and the child later adds /dev, /proc, /sys and so on under it. So inside the sandbox, every one of these files has an mtime that is current but slightly older than /.
This causes two problems:
-
Builds can see wall-clock time. Anything in the build that reads these files' mtimes (archiving the build directory, make-style freshness checks, timestamp heuristics) gets a value that differs on every run.
-
It breaks nixpkgs'
SOURCE_DATE_EPOCHdetection.set-source-date-epoch-to-latest.shsetsSOURCE_DATE_EPOCHto the mtime of the newest file in the source root. It usesfind -not -newer "$NIX_BUILD_TOP/.."(that is, the sandbox/) to skip files generated during the build. Since the builder input files always look older than/, a package withsourceRoot = "."getsSOURCE_DATE_EPOCHset to the build time instead of the newest source file.
This broke tzdata in nixpkgs (NixOS/nixpkgs#582038); @me-and has a PR working around it at NixOS/nixpkgs#586202.
Steps To Reproduce
-
Save as
repro.nix:{ nixpkgs ? <nixpkgs>, structured ? false, }: with import nixpkgs { }; stdenvNoCC.mkDerivation { name = "builder-file-mtimes"; __structuredAttrs = structured; someText = "hello"; passAsFile = [ "someText" ]; buildCommand = '' cd "$NIX_BUILD_TOP" stat -c '%.9Y %n' / "$NIX_BUILD_TOP" .attr* env-vars echo "files that look like they predate the build (find -not -newer /):" find . -maxdepth 1 -type f -not -newer / -printf ' %p\n' touch $out ''; } -
Run
nix-build repro.nixandnix-build repro.nix --arg structured trueon Linux withsandbox = true. -
Observe:
1794416389.452199627 / 1794416389.473503281 /build 1794416389.440502823 .attr-050v6i1hs2ggbikhjj4pckq4zm5ki7npjn4061rdm0i9iqvm8rqk 1794416389.475181265 env-vars files that look like they predate the build (find -not -newer /): ./.attr-050v6i1hs2ggbikhjj4pckq4zm5ki7npjn4061rdm0i9iqvm8rqk1794416390.506517621 / 1794416390.525517885 /build 1794416390.493517441 .attrs.json 1794416390.493517441 .attrs.sh 1794416390.527392381 env-vars files that look like they predate the build (find -not -newer /): ./.attrs.sh ./.attrs.jsonThe Nix-written files are about 12 ms older than
/. For comparison,env-varsis written by stdenv once the builder is running, so it is correctly newer than/.
Expected behavior
Files that Nix writes for the builder should have a fixed mtime, just like store paths, which are canonicalised to mtimeStore = 1. I think that a single futimens(fd, {mtimeStore, mtimeStore}) in writeBuilderFile, right after chownToBuilder, would cover every case listed above on all platforms.
With an mtime of 1, these files can never be the newest file, so the nixpkgs hook on all versions will work as intended.
My other sense is that it would be a mistake to normalise the mtime of the sandbox root itself. The hook above treats it as the build-start marker. Setting / to a fixed old timestamp would make it skip every unpacked source file and silently reset SOURCE_DATE_EPOCH to 1980 for nearly every package. Maybe that's OK, but I think it's tangential to this issue.
Metadata
nix-env (Nix) 2.35.5
Additional context
-
Lix has the same ordering, so it has the same weakness.
-
Non-chroot builds (Darwin,
sandbox = false) mostly don't trigger the nixpkgs symptom. The build directory's parent already exists and gets its mtime bumped when the build directory is created, before the files are written. They are still exposed through coarse kernel timestamps and concurrent builds bumping the parent. And the wall-clock leak (problem 1 above) applies everywhere.
Checklist
- checked latest Nix manual (source)
- checked open bug issues and pull requests for possible duplicates
- reviewed the contributing guide
Add 👍 to issues you find important.
Activity
me-and commentedon Nov 11, 2026
Minor correction:
The thing that broke tzdata is the fact that the system date is exposed in the sandbox through commands like
date, and the tzdata build would fail if the current date was after 2026-11-01T00:00:00 UTC.I attempted to fix this in NixOS/nixpkgs#586207 by setting the "current time" as being the
SOURCE_DATE_EPOCHvalue set as part of stdenv builds, and found that was being set to the time the build started due to the mtime on files like.attrs.sh. The tzdata build failure has already been fixed in NixOS/nixpkgs#582577, however, which instead uses a timestamp calculated from the contents of a specific source file.The workaround I've proposed in NixOS/nixpkgs#586202 doesn't fix any user-visible problem I'm aware of, but it would mean the
SOURCE_DATE_EPOCHvalue is meaningful for any other build with a similar setup to tzdata.