Skip to content

Commit b975f71

Browse files
authored
Merge pull request #14595 from NixOS/registry-resolve
Add `nix registry resolve` command
2 parents 72dbd43 + 063cdb5 commit b975f71

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

src/nix/registry-resolve.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
R""(
2+
3+
# Examples
4+
5+
* Resolve the `nixpkgs` and `blender-bin` flakerefs:
6+
7+
```console
8+
# nix registry resolve nixpkgs blender-bin
9+
github:NixOS/nixpkgs/nixpkgs-unstable
10+
github:edolstra/nix-warez?dir=blender
11+
```
12+
13+
* Resolve an indirect flakeref with a branch override:
14+
15+
```console
16+
# nix registry resolve nixpkgs/25.05
17+
github:NixOS/nixpkgs/25.05
18+
```
19+
20+
# Description
21+
22+
This command resolves indirect flakerefs (e.g. `nixpkgs`) to direct flakerefs (e.g. `github:NixOS/nixpkgs`) using the flake registries. It looks up each provided flakeref in all available registries (flag, user, system, and global) and returns the resolved direct flakeref on a separate line on standard output. It does not fetch any flakes.
23+
24+
The resolution process may apply multiple redirections if necessary until a direct flakeref is found. If an indirect flakeref cannot be found in any registry, an error will be thrown.
25+
26+
See the [`nix registry` manual page](./nix3-registry.md) for more details on the registry.
27+
28+
)""

src/nix/registry.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,40 @@ struct CmdRegistryPin : RegistryCommand, EvalCommand
202202
}
203203
};
204204

205+
struct CmdRegistryResolve : StoreCommand
206+
{
207+
std::vector<std::string> urls;
208+
209+
std::string description() override
210+
{
211+
return "resolve flake references using the registry";
212+
}
213+
214+
std::string doc() override
215+
{
216+
return
217+
#include "registry-resolve.md"
218+
;
219+
}
220+
221+
CmdRegistryResolve()
222+
{
223+
expectArgs({
224+
.label = "flake-refs",
225+
.handler = {&urls},
226+
});
227+
}
228+
229+
void run(nix::ref<nix::Store> store) override
230+
{
231+
for (auto & url : urls) {
232+
auto ref = parseFlakeRef(fetchSettings, url);
233+
auto resolved = ref.resolve(fetchSettings, *store);
234+
logger->cout("%s", resolved.to_string());
235+
}
236+
}
237+
};
238+
205239
struct CmdRegistry : NixMultiCommand
206240
{
207241
CmdRegistry()
@@ -212,6 +246,7 @@ struct CmdRegistry : NixMultiCommand
212246
{"add", []() { return make_ref<CmdRegistryAdd>(); }},
213247
{"remove", []() { return make_ref<CmdRegistryRemove>(); }},
214248
{"pin", []() { return make_ref<CmdRegistryPin>(); }},
249+
{"resolve", []() { return make_ref<CmdRegistryResolve>(); }},
215250
})
216251
{
217252
}

tests/functional/flakes/flakes.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,17 @@ nix flake lock "$flake3Dir"
252252
nix flake update --flake "$flake3Dir" --override-flake flake2 nixpkgs
253253
[[ -n $(git -C "$flake3Dir" diff master || echo failed) ]]
254254

255-
# Testing the nix CLI
255+
# Test `nix registry` commands.
256256
nix registry add flake1 flake3
257257
[[ $(nix registry list | wc -l) == 5 ]]
258+
[[ $(nix registry resolve flake1) = "git+file://$percentEncodedFlake3Dir" ]]
258259
nix registry pin flake1
259260
[[ $(nix registry list | wc -l) == 5 ]]
260261
nix registry pin flake1 flake3
261262
[[ $(nix registry list | wc -l) == 5 ]]
262263
nix registry remove flake1
263264
[[ $(nix registry list | wc -l) == 4 ]]
265+
[[ $(nix registry resolve flake1) = "git+file://$flake1Dir" ]]
264266

265267
# Test 'nix registry list' with a disabled global registry.
266268
nix registry add user-flake1 git+file://"$flake1Dir"

0 commit comments

Comments
 (0)