aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/populate/README.md4
-rw-r--r--pkgs/populate/src/flake.rs14
-rw-r--r--pkgs/populate/src/sh.rs64
-rw-r--r--pkgs/populate/tests/scenarios.nix61
4 files changed, 117 insertions, 26 deletions
diff --git a/pkgs/populate/README.md b/pkgs/populate/README.md
index f96ef49..63ec6bd 100644
--- a/pkgs/populate/README.md
+++ b/pkgs/populate/README.md
@@ -559,7 +559,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/src/flake.rs b/pkgs/populate/src/flake.rs
index f992231..22b49e8 100644
--- a/pkgs/populate/src/flake.rs
+++ b/pkgs/populate/src/flake.rs
@@ -203,7 +203,8 @@ struct DerivedState<'a> {
target_kind: &'a str,
/// A line of sha256sum for each patch.
patch_sums: &'a [String],
- /// The hash of each source drawn from, or empty if it's in the store.
+ /// What tells whether each source drawn from changed: git and its
+ /// commit, its hash, or empty if it's in the store.
hashes: &'a [String],
}
@@ -230,10 +231,11 @@ impl<'a> DerivedState<'a> {
}
}
-/// The text whose hash is the key of a derived copy, like the shell
-/// implementation computes it: the original's path, the spec, the patches'
-/// sums, and, for a copy drawing inputs, nix flake lock's arguments and the
-/// hashes of the sources drawn from that aren't in the store.
+/// The text whose hash is the key of a derived copy: the original's path,
+/// the spec, the patches' sums, and, for a copy drawing inputs, nix flake
+/// lock's arguments and what tells whether the sources drawn from that
+/// aren't in the store changed: the commit of a Git work tree that Nix gets
+/// as git+file:, and else a hash of the source.
fn key_text(
original: &str,
spec: &Value,
@@ -535,7 +537,7 @@ impl<'a> Flakes<'a> {
/// the patches, or what inputs get drawn from which sources changes,
/// and others also if the sources they draw inputs from change. The
/// key of a copy, in .populate/derived/<name>/key, is the hash of all
- /// that.
+ /// that, see key_text.
fn derive(&self, name: &str) -> Result<()> {
let settings = &self.sources[name].settings;
let materialize = bool_setting(settings, "materialize", false);
diff --git a/pkgs/populate/src/sh.rs b/pkgs/populate/src/sh.rs
index 70efd3f..9ae920b 100644
--- a/pkgs/populate/src/sh.rs
+++ b/pkgs/populate/src/sh.rs
@@ -759,8 +759,8 @@ nix eval --impure --raw --expr '
/// the definitions of prelude and DEFINITIONS. Populate decides what to
/// do, from what these send back.
pub mod draw {
- /// Defines own_lock DIR, apply_patches PATCH_DIR NAME, and write_key
- /// KEY_FILE KEY.
+ /// Defines own_lock DIR, apply_patches PATCH_DIR NAME, write_key
+ /// KEY_FILE KEY, git_flake PATH, and clean_git_rev PATH.
///
/// Nix rewrites lock files in place, which also changes what else is a
/// hard link to the lock file, e.g. in a backup, or what it is a
@@ -774,6 +774,17 @@ pub mod draw {
///
/// write_key writes KEY to a new file, which then replaces KEY_FILE, so
/// nothing at its place gets written through, e.g. a symlink.
+ ///
+ /// git_flake PATH succeeds if Nix gets the directory at PATH as a
+ /// git+file: flake: if it has a .git directory that 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.
+ ///
+ /// clean_git_rev PATH prints the commit HEAD of the Git work tree at
+ /// PATH points to, if git_flake PATH, 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.
pub const DEFINITIONS: &str = r#"
own_lock() {
if test -e "$1"/flake.lock; then
@@ -796,6 +807,24 @@ write_key() {
printf '%s\n' "$2" > "$key_file"
mv -f "$key_file" "$1"
}
+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
+}
+clean_git_rev() (
+ git_flake "$1" || exit 1
+ cd "$1"
+ git() {
+ command git -c core.hooksPath=/dev/null -c core.fsmonitor=false "$@"
+ }
+ changes=$(git status --porcelain --untracked-files=no 2>/dev/null) || exit 1
+ test -z "$changes" || exit 1
+ git rev-parse --verify --quiet HEAD
+)
"#;
/// use_state SENTINEL NAME...
@@ -805,25 +834,14 @@ use_state "$@"
/// resolve PATH...: prints, for each PATH, what's there and where it
/// resolves to, as two fields, each ending in a null byte. The first
- /// holds d if PATH is a directory, g if it has a .git directory that
- /// git_problems finds nothing wrong with, and that has no hooks, s if
- /// it has .gitmodules, and f if it has flake.nix. Nix runs Git in a
- /// git+file: flake, with the repository's configuration and hooks, so
- /// it only gets such ones.
+ /// holds d if PATH is a directory, g if git_flake PATH, s if it has
+ /// .gitmodules, and f if it has flake.nix.
pub const RESOLVE: &str = r#"
for path; do
resolved=$(readlink -f "$path") || resolved=
flags=
if test -d "$path"; then flags=${flags}d; fi
- if ! test -L "$path"/.git && test -d "$path"/.git; then
- problems=$(git_problems "$path" 2>/dev/null) || problems=failed
- if test -d "$path"/.git/hooks; then
- hooks=$(find "$path"/.git/hooks -mindepth 1 ! -name '*.sample' | head -n 1)
- else
- hooks=
- fi
- if test -z "$problems" && test -z "$hooks"; then flags=${flags}g; fi
- fi
+ if git_flake "$path"; then flags=${flags}g; fi
if test -e "$path"/.gitmodules; then flags=${flags}s; fi
if test -e "$path"/flake.nix; then flags=${flags}f; fi
printf '%s\0%s\0' "$flags" "$resolved"
@@ -870,8 +888,10 @@ nix flake lock "$@" "$url" >&2
/// ending in null bytes: where DIR/original resolves to, the key of the
/// copy, the store path DIR/result links to, if the store has it, link
/// or directory, if TARGET_PATH is one, a line of sha256sum for each
- /// patch in PATCH_DIR, an empty field, and the hash of each DRAWN path,
- /// or nothing if it resolves into the store.
+ /// patch in PATCH_DIR, an empty field, and what tells whether each
+ /// DRAWN path changed: nothing if it resolves into the store, git and
+ /// the commit, if clean_git_rev gives one, or else its hash, which
+ /// takes reading all of it.
pub const DERIVED_STATE: &str = r#"
dir=$1 target_path=$2 patch_dir=$3
shift 3
@@ -905,7 +925,13 @@ for drawn; do
drawn=$(readlink -f "$drawn")
case $drawn in
/nix/store/*) printf '\0' ;;
- *) printf '%s\0' "$(nix-hash --type sha256 "$drawn")" ;;
+ *)
+ if rev=$(clean_git_rev "$drawn") && test -n "$rev"; then
+ printf 'git %s\0' "$rev"
+ else
+ printf '%s\0' "$(nix-hash --type sha256 "$drawn")"
+ fi
+ ;;
esac
done
"#;
diff --git a/pkgs/populate/tests/scenarios.nix b/pkgs/populate/tests/scenarios.nix
index d00ebcb..d50cf19 100644
--- a/pkgs/populate/tests/scenarios.nix
+++ b/pkgs/populate/tests/scenarios.nix
@@ -675,6 +675,67 @@ with import ./builder.nix args;
}
];
}))
+ # 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.