8000 Builder input files get build-time mtimes, leaking wall-clock time into builds · Issue #16799 · NixOS/nix · GitHub
[go: up one dir, main page]

Skip to content

Builder input files get build-time mtimes, leaking wall-clock time into builds #16799

Open

Description

@philiptaron

Describe the bug

I discovered this looking into these two Nixpkgs PRs:

Before the builder starts, Nix writes a set of input files into the build directory:

  • .attrs.json and .attrs.sh (structured attrs)
  • .attr-<hash> (passAsFile)
  • exportReferencesGraph outputs

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:

  1. 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.

  2. It breaks nixpkgs' SOURCE_DATE_EPOCH detection. set-source-date-epoch-to-latest.sh sets SOURCE_DATE_EPOCH to the mtime of the newest file in the source root. It uses find -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 with sourceRoot = "." gets SOURCE_DATE_EPOCH set 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

  1. 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
      '';
    }
    
  2. Run nix-build repro.nix and nix-build repro.nix --arg structured true on Linux with sandbox = true.

  3. 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-050v6i1hs2ggbikhjj4pckq4zm5ki7npjn4061rdm0i9iqvm8rqk
    
    1794416390.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.json
    

    The Nix-written files are about 12 ms older than /. For comparison, env-vars is 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


Add 👍 to issues you find important.

Activity

  1. me-and commented on Nov 11, 2026

    @me-and
    Member

    Minor correction:

    This broke tzdata in nixpkgs (NixOS/nixpkgs#582038); @me-and has a PR working around it at NixOS/nixpkgs#586202.

    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_EPOCH value 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_EPOCH value is meaningful for any other build with a similar setup to tzdata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0