From f46bb7f2a5dbdcd0098904654f7d8742dc111c01 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 14 Nov 2026 01:49:56 +0000 Subject: populate: tell whether a Git work tree drawn from changed by its commit Whether a derived copy of a flake or flake-input source needs to be made again depends on the sources it draws inputs from. For one that isn't in the store, the key had its hash, so every run read all of it, e.g. all of a nixpkgs work tree, .git included, which takes long even when nothing changed. For a Git work tree that Nix gets as git+file:, whose commit and tracked files are all Nix uses, the key has its commit instead, if no tracked file differs from it, which Git tells quickly. git_flake decides what Nix gets as git+file:, for flake_url as well. Keys of copies drawing from such work trees change once, so those copies get made again on the first run. The keys are the same as krops 2's. rewrite-cache-git checks that such a copy gets made again for a new commit, but not for redeploying, nor for an untracked file. --- CHANGELOG.md | 8 +++++++ README.md | 4 +++- pkgs/populate/default.nix | 53 +++++++++++++++++++++++++++++++++------- tests/scenarios.nix | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c167ce7..774c818 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changes +## Unreleased + +- Whether a copy of a `flake` or `flake-input` source drawing inputs from + a Git work tree that Nix gets as `git+file:` needs to be made again + depends on its commit, if no tracked file differs from it, instead of a + hash of all of the work tree, `.git` included, which populate read on + every run. Such copies get made again once. + ## 1.31.1 - Drawing an input from a source that isn't a directory stops populate, diff --git a/README.md b/README.md index 8661a21..97f2557 100644 --- a/README.md +++ b/README.md @@ -713,7 +713,9 @@ see [The target directory](#the-target-directory), and has no hooks. Lock files get rewritten or generated on every run, so they match the sources. Copies of `flake` and `flake-input` sources get made again only if the delivered store path, the patches, or the sources they draw inputs from -change. Inputs that get drawn from other sources don't get delivered with +change. For a source in a Git work tree that Nix gets as `git+file:`, +that's its commit and tracked files, which populate checks with Git; +other sources get read entirely, to hash them. Inputs that get drawn from other sources don't get delivered with a `flake` or `flake-input` source, and neither does what they pin. Generated flakes and copies get removed once their sources no longer need them. This requires Nix with flake support (Nix 2.4 or later) on the diff --git a/pkgs/populate/default.nix b/pkgs/populate/default.nix index 80cbcd3..e69d64b 100644 --- a/pkgs/populate/default.nix +++ b/pkgs/populate/default.nix @@ -460,9 +460,18 @@ let then "${flakesDir}/${source}" else "${target.path}/${source}" )}) + # By the commit of a Git work tree that Nix gets as + # git+file:, which Git tells quickly, and else by a hash, + # which takes reading all of the source. case $drawn in /nix/store/*) ;; - *) nix-hash --type sha256 "$drawn" ;; + *) + if rev=$(clean_git_rev "$drawn") && test -n "$rev"; then + printf 'git %s\n' "$rev" + else + nix-hash --type sha256 "$drawn" + fi + ;; esac '') (unique (drawnFrom name))} ''} @@ -608,20 +617,46 @@ let } ${gitProblems} - # Prints the flake URL of the source named $1: git+file: if it has a - # .git directory git_problems finds nothing wrong with, and that has - # no hooks, else path:. Nix runs Git in a git+file: flake, with the - # repository's configuration and hooks. + # Succeeds if Nix gets the directory $1 as a git+file: flake: if it + # has a .git directory git_problems finds nothing wrong with, and + # that has no hooks. Nix runs Git in a git+file: flake, with the + # repository's configuration and hooks, so it only gets such ones. + git_flake() { + ! test -L "$1"/.git && test -d "$1"/.git || return 1 + git_flake_problems=$(git_problems "$1" 2>/dev/null) || return 1 + test -z "$git_flake_problems" || return 1 + if test -d "$1"/.git/hooks; then + test -z "$(find "$1"/.git/hooks -mindepth 1 ! -name '*.sample' | head -n 1)" + fi + } + # Prints the commit HEAD of the Git work tree $1 points to, if + # git_flake $1, and none of its tracked files differ from that + # commit, which is all of it Nix uses then. Git runs without hooks + # and file system monitors. + clean_git_rev() ( + git_flake "$1" || exit 1 + cd "$1" + git() { + command git -c core.hooksPath=/dev/null -c core.fsmonitor=false "$@" + } + # Without writing the index: in a copy of a work tree, e.g. in a file + # source, that makes it differ from the original's, so the next run + # copies it again, unless the original's changed in the same second, + # keeping its size, which rsync doesn't notice. Then the work tree + # differs from its index, which Nix refuses to lock. + changes=$(git --no-optional-locks status --porcelain --untracked-files=no 2>/dev/null) || exit 1 + test -z "$changes" || exit 1 + git rev-parse --verify --quiet HEAD + ) + # Prints the flake URL of the source named $1: git+file: if + # git_flake, else path:. flake_url() { path=$(readlink -f ${quote target.path}/"$1") if ! test -d "$path"; then >&2 printf 'error: source %s is not a directory\n' "$1" exit 1 fi - if ! test -L "$path"/.git && test -d "$path"/.git && - problems=$(git_problems "$path" 2>/dev/null) && - test -z "$problems" && - test -z "$(find "$path"/.git/hooks -mindepth 1 ! -name '*.sample' 2>/dev/null | head -n 1)"; then + if git_flake "$path"; then if test -e "$path"/.gitmodules; then printf 'git+file:%s?submodules=1' "$(url_path "$path")" else diff --git a/tests/scenarios.nix b/tests/scenarios.nix index 2a092aa..3426b92 100644 --- a/tests/scenarios.nix +++ b/tests/scenarios.nix @@ -1010,6 +1010,67 @@ assert !(krops.isLocalTarget (targets.remote "/")); } ]; })) + # A copy with a rewritten lock file that draws an input from a Git work + # tree, which Nix gets as git+file:, gets made again only when the + # commit or the tracked files change, not for an untracked file, which + # Nix doesn't see. The source is a file source keeping its .git. + (s "rewrite-cache-git" (let + sources = { + x.flake-input = { + url = "/fixtures/top"; name = "dep"; inherit fetchOnTarget; + inputs.inner.follows = "a"; + }; + a.file = "/tmp/rewrite-cache-git"; + }; + copyKept = '' + expect_eq "store path" "$(readlink "$T"/x)" "$(cat "$T".first)" + expect_eq "copy made at" "$(stat -c %y "$T"/.populate/derived/x/key)" "$(cat "$T".key)" + ''; + in { + prepareDeployer = '' + rm -fR /tmp/rewrite-cache-git + mkdir /tmp/rewrite-cache-git + cd /tmp/rewrite-cache-git + echo '{ outputs = _: { greeting = "altgit"; }; }' > flake.nix + git init -q + git add flake.nix + git -c user.name=krops -c user.email=spam@krebsco.de commit -q -m altgit + ''; + phases = [ + { + inherit sources; + check = checkFlake { attr = "greeting"; expected = "dep+altgit"; }; + mutate = '' + readlink "$T"/x > "$T".first + stat -c %y "$T"/.populate/derived/x/key > "$T".key + ''; + } + { + inherit sources; + check = copyKept; + mutateDeployer = '' + echo untracked > /tmp/rewrite-cache-git/untracked + ''; + } + { + inherit sources; + check = copyKept + '' + test -e "$T"/a/untracked || fail "a/untracked missing" + ''; + mutateDeployer = '' + cd /tmp/rewrite-cache-git + echo '{ outputs = _: { greeting = "altgit2"; }; }' > flake.nix + git -c user.name=krops -c user.email=spam@krebsco.de commit -q -a -m altgit2 + ''; + } + { + inherit sources; + check = checkFlake { attr = "greeting"; expected = "dep+altgit2"; } + '' + test "$(readlink "$T"/x)" != "$(cat "$T".first)" || fail "store path unchanged" + ''; + } + ]; + })) # A materialized input is a writable copy, patched and with a rewritten # lock file, which gets made again only when what it derives from # changes, and becomes a link again when not materialized anymore. -- cgit v1.3.1