Skip to content

Commit 06f29fa

Browse files
committed
nix-prefetch-url: Support prefetching from a Nix expression
For example, $ nix-prefetch-url -A hello.src will prefetch the file specified by the fetchurl call in the attribute ‘hello.src’ from the Nix expression in the current directory. This differs from ‘nix-build -A hello.src’ in that it doesn't verify the hash. You can also specify a path to the Nix expression: $ nix-prefetch-url ~/Dev/nixpkgs -A hello.src List elements (typically used in ‘patches’ attributes) also work: $ nix-prefetch-url -A portmidi.patches.0
1 parent a3c4eb0 commit 06f29fa

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎doc/manual/command-ref/nix-prefetch-url.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<cmdsynopsis>
2121
<command>nix-prefetch-url</command>
2222
<arg><option>--type</option> <replaceable>hashAlgo</replaceable></arg>
23+
<arg><option>--print-path</option></arg>
2324
<arg choice='plain'><replaceable>url</replaceable></arg>
2425
<arg><replaceable>hash</replaceable></arg>
2526
</cmdsynopsis>

‎src/nix-prefetch-url/nix-prefetch-url.cc‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "eval.hh"
66
#include "eval-inline.hh"
77
#include "common-opts.hh"
8+
#include "attr-path.hh"
89

910
#include <iostream>
1011

@@ -49,6 +50,9 @@ int main(int argc, char * * argv)
4950
std::vector<string> args;
5051
Strings searchPath;
5152
bool printPath = getEnv("PRINT_PATH") != "";
53+
bool fromExpr = false;
54+
string attrPath;
55+
std::map<string, string> autoArgs_;
5256

5357
parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
5458
if (*arg == "--help")
@@ -63,6 +67,12 @@ int main(int argc, char * * argv)
6367
}
6468
else if (*arg == "--print-path")
6569
printPath = true;
70+
else if (*arg == "--attr" || *arg == "-A") {
71+
fromExpr = true;
72+
attrPath = getArg(*arg, arg, end);
73+
}
74+
else if (parseAutoArgs(arg, end, autoArgs_))
75+
;
6676
else if (parseSearchPathArg(arg, end, searchPath))
6777
;
6878
else if (*arg != "" && arg->at(0) == '-')
@@ -72,15 +82,37 @@ int main(int argc, char * * argv)
7282
return true;
7383
});
7484

75-
if (args.size() < 1 || args.size() > 2)
76-
throw UsageError("nix-prefetch-url expects one argument");
85+
if (args.size() > 2)
86+
throw UsageError("too many arguments");
7787

7888
store = openStore();
79-
8089
EvalState state(searchPath);
8190

91+
Bindings & autoArgs(*evalAutoArgs(state, autoArgs_));
92+
93+
/* If -A is given, get the URI from the specified Nix
94+
expression. */
95+
string uri;
96+
if (!fromExpr) {
97+
if (args.empty())
98+
throw UsageError("you must specify a URI");
99+
uri = args[0];
100+
} else {
101+
Path path = resolveExprPath(lookupFileArg(state, args.empty() ? "." : args[0]));
102+
Value vRoot;
103+
state.evalFile(path, vRoot);
104+
Value & v(*findAlongAttrPath(state, attrPath, autoArgs, vRoot));
105+
state.forceAttrs(v);
106+
auto urls = v.attrs->find(state.symbols.create("urls"));
107+
if (urls == v.attrs->end())
108+
throw Error("attribute set does not contain a ‘urls’ attribute");
109+
state.forceList(*urls->value);
110+
if (urls->value->listSize() < 1)
111+
throw Error("‘urls’ list is empty");
112+
uri = state.forceString(*urls->value->listElems()[0]);
113+
}
114+
82115
/* Figure out a name in the Nix store. */
83-
auto uri = args[0];
84116
auto name = baseNameOf(uri);
85117
if (name.empty())
86118
throw Error(format("cannot figure out file name for ‘%1%’") % uri);

0 commit comments

Comments
 (0)