Skip to content

add tests for nix registry pin #13229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libfetchers/include/nix/fetchers/registry.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
8 changes: 5 additions & 3 deletions src/libfetchers/registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}

Expand Down Expand Up @@ -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<Registry> getGlobalRegistry(const Settings & settings, ref<Store> store)
Expand Down
4 changes: 2 additions & 2 deletions src/nix/registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
};
Expand Down Expand Up @@ -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());
}
};
Expand Down
1 change: 1 addition & 0 deletions tests/functional/flakes/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ suites += {
'deps': [],
'tests': [
'flakes.sh',
'registry-pin.sh',
'develop.sh',
'edit.sh',
'run.sh',
Expand Down
58 changes: 58 additions & 0 deletions tests/functional/flakes/registry-pin.sh
Original file line number Diff line number Diff line change
@@ -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
Loading