From 3aed9c8dd4922ba60c27a097324708d2e47afedf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 19 May 2025 09:32:14 +0200 Subject: [PATCH 1/2] add tests for nix registry pin --- tests/functional/flakes/meson.build | 1 + tests/functional/flakes/registry-pin.sh | 58 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100755 tests/functional/flakes/registry-pin.sh diff --git a/tests/functional/flakes/meson.build b/tests/functional/flakes/meson.build index 213c388a6d9..1e22d66b905 100644 --- a/tests/functional/flakes/meson.build +++ b/tests/functional/flakes/meson.build @@ -3,6 +3,7 @@ suites += { 'deps': [], 'tests': [ 'flakes.sh', + 'registry-pin.sh', 'develop.sh', 'edit.sh', 'run.sh', diff --git a/tests/functional/flakes/registry-pin.sh b/tests/functional/flakes/registry-pin.sh new file mode 100755 index 00000000000..cd96c6cbab5 --- /dev/null +++ b/tests/functional/flakes/registry-pin.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +source ./common.sh + +TODO_NixOS + +requireGit + +clearStore +rm -rf "$TEST_HOME/.cache" "$TEST_HOME/.config" + +createFlake1 + +# Add a branch in flake1. +git -C "$flake1Dir" checkout -b branch1 +echo > "$flake1Dir/some-file" +git -C "$flake1Dir" add "$flake1Dir/some-file" +git -C "$flake1Dir" commit -m 'Some change' +git -C "$flake1Dir" checkout master + +nix registry add --registry "$registry" flake1 "git+file://$flake1Dir" + +commit=$(nix flake metadata "git+file://$flake1Dir" --json | jq -r '.revision') +commit2=$(nix flake metadata "git+file://$flake1Dir?ref=refs/heads/branch1" --json | jq -r '.revision') +[[ "$commit" != "$commit2" ]] + +nix registry list | grepQuiet '^global' # global flake1 + +commit=$(nix flake metadata flake1 --json | jq -r '.revision') +commit2=$(nix flake metadata flake1/branch1 --json | jq -r '.revision') +nix build -o "$TEST_ROOT/result" flake1#root +find "$TEST_ROOT/result/" | grepInverse some-file +nix build -o "$TEST_ROOT/result" flake1/branch1#root +find "$TEST_ROOT/result/" | grepQuiet some-file +[[ "$commit" != "$commit2" ]] + + +nix registry pin flake1 +# new output something like: +# user flake:flake1 git+file:///tmp/nix-test/flakes/registry-pin/flake1?ref=refs/heads/master&rev=c55c61f18fa23762b1dc700af6f33af012ec6772 +# global flake:flake1 git+file:///tmp/nix-test/flakes/registry-pin/flake1 +nix registry list | grepQuiet '^global' # global flake1 +nix registry list | grepQuiet '^user' # user flake1 +nix registry remove --registry "$registry" flake1 + +nix build -o "$TEST_ROOT/result" flake1#root +find "$TEST_ROOT/result/" | grepInverse some-file +nix build -o "$TEST_ROOT/result" flake1/branch1#root +find "$TEST_ROOT/result/" | grepQuiet some-file + +commit=$(nix flake metadata flake1 --json | jq -r '.revision') +# overriding the branch does still work +commit2=$(nix flake metadata flake1/branch1 --json | jq -r '.revision') +[[ "$commit" != "$commit2" ]] + +nix registry remove flake1 +nix registry list | grepInverse '^user' # no more pinned flake1 +nix registry list | grepQuiet '^global' # global flake1 is still there From e3300d4cde0455305410a270b00b819c78cd1394 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 28 Feb 2025 14:39:14 +0100 Subject: [PATCH 2/2] nix flake pin: Mark the resulting registry entry as exact Since the resulting target flakeref contains attributes like "lastModified" or "narHash", it's not suitable for overriding. This led to errors like $ nix build nixpkgs/24.05#hello error: 'lastModified' attribute mismatch in input 'github:NixOS/nixpkgs/63dacb46bf939521bdc93981b4cbb7ecb58427a0', expected 1728538411, got 1717179513 after doing `nix registry pin nixpkgs`. --- src/libfetchers/include/nix/fetchers/registry.hh | 3 ++- src/libfetchers/registry.cc | 8 +++++--- src/nix/registry.cc | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/libfetchers/include/nix/fetchers/registry.hh b/src/libfetchers/include/nix/fetchers/registry.hh index efbfe07c849..d7706bd75b2 100644 --- a/src/libfetchers/include/nix/fetchers/registry.hh +++ b/src/libfetchers/include/nix/fetchers/registry.hh @@ -45,7 +45,8 @@ struct Registry void add( const Input & from, const Input & to, - const Attrs & extraAttrs); + const Attrs & extraAttrs, + bool exact); void remove(const Input & input); }; diff --git a/src/libfetchers/registry.cc b/src/libfetchers/registry.cc index 335935f53af..54bde49c621 100644 --- a/src/libfetchers/registry.cc +++ b/src/libfetchers/registry.cc @@ -84,13 +84,15 @@ void Registry::write(const Path & path) void Registry::add( const Input & from, const Input & to, - const Attrs & extraAttrs) + const Attrs & extraAttrs, + bool exact) { entries.emplace_back( Entry { .from = from, .to = to, - .extraAttrs = extraAttrs + .extraAttrs = extraAttrs, + .exact = exact }); } @@ -144,7 +146,7 @@ void overrideRegistry( const Input & to, const Attrs & extraAttrs) { - getFlagRegistry(*from.settings)->add(from, to, extraAttrs); + getFlagRegistry(*from.settings)->add(from, to, extraAttrs, false); } static std::shared_ptr getGlobalRegistry(const Settings & settings, ref store) diff --git a/src/nix/registry.cc b/src/nix/registry.cc index 340d10ec42e..7de79592253 100644 --- a/src/nix/registry.cc +++ b/src/nix/registry.cc @@ -115,7 +115,7 @@ struct CmdRegistryAdd : MixEvalArgs, Command, RegistryCommand fetchers::Attrs extraAttrs; if (toRef.subdir != "") extraAttrs["dir"] = toRef.subdir; registry->remove(fromRef.input); - registry->add(fromRef.input, toRef.input, extraAttrs); + registry->add(fromRef.input, toRef.input, extraAttrs, false); registry->write(getRegistryPath()); } }; @@ -193,7 +193,7 @@ struct CmdRegistryPin : RegistryCommand, EvalCommand warn("flake '%s' is not locked", resolved.to_string()); fetchers::Attrs extraAttrs; if (ref.subdir != "") extraAttrs["dir"] = ref.subdir; - registry->add(ref.input, resolved, extraAttrs); + registry->add(ref.input, resolved, extraAttrs, true); registry->write(getRegistryPath()); } };