blob: afefb17b654042385c1338bdbd90c35ca63b5f43 [file] [log] [blame]
Skyler Grey5b753722022-08-26 07:53:33 +01001From 519d541fdc71508793ece6e087d2c1a231378558 Mon Sep 17 00:00:00 2001
2From: Skyler Grey <skyler3665@gmail.com>
3Date: Wed, 20 Jul 2022 14:45:45 +0100
4Subject: [PATCH 01/10] Make --expr always include nixpkgs
5
6---
7 src/libcmd/installables.cc | 7 +++++--
8 1 file changed, 5 insertions(+), 2 deletions(-)
9
10diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
11index 59162c4df03..7dc3f7435ee 100644
12--- a/src/libcmd/installables.cc
13+++ b/src/libcmd/installables.cc
14@@ -778,8 +778,11 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
15 } else if (file)
16 state->evalFile(lookupFileArg(*state, *file), *vFile);
17 else {
18- auto e = state->parseExprFromString(*expr, absPath("."));
19- state->eval(e, *vFile);
20+ Strings e = {};
21+ e.push_back("with (builtins.getFlake \"nixpkgs\")");
22+ e.push_back(*expr);
23+ auto parsed = state->parseExprFromString(concatStringsSep(";", e), absPath("."));
24+ state->eval(parsed, *vFile);
25 }
26
27 for (auto & s : ss) {
28
29From 29b83de5671901e5e8755c17b840139a6f8650db Mon Sep 17 00:00:00 2001
30From: Skyler Grey <skyler3665@gmail.com>
31Date: Wed, 20 Jul 2022 14:46:17 +0100
32Subject: [PATCH 02/10] Stop --expr requiring --impure flag
33
34---
35 src/libcmd/installables.cc | 2 +-
36 1 file changed, 1 insertion(+), 1 deletion(-)
37
38diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
39index 7dc3f7435ee..43e759b3e93 100644
40--- a/src/libcmd/installables.cc
41+++ b/src/libcmd/installables.cc
42@@ -767,7 +767,7 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
43 throw UsageError("'--file' and '--expr' are exclusive");
44
45 // FIXME: backward compatibility hack
46- if (file) evalSettings.pureEval = false;
47+ evalSettings.pureEval = false;
48
49 auto state = getEvalState();
50 auto vFile = state->allocValue();
51
52From 9e9d28496a7216a0e2893ac0eb05a8c45d898c95 Mon Sep 17 00:00:00 2001
53From: Skyler Grey <skyler3665@gmail.com>
54Date: Wed, 20 Jul 2022 23:51:16 +0100
55Subject: [PATCH 03/10] Make nix shell take installables as context
56
57---
58 src/libcmd/installables.cc | 46 +++++++++++++++++++++++++++++++++++++-
59 1 file changed, 45 insertions(+), 1 deletion(-)
60
61diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
62index 43e759b3e93..1abad72e333 100644
63--- a/src/libcmd/installables.cc
64+++ b/src/libcmd/installables.cc
65@@ -779,10 +779,54 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
66 state->evalFile(lookupFileArg(*state, *file), *vFile);
67 else {
68 Strings e = {};
69- e.push_back("with (builtins.getFlake \"nixpkgs\")");
70+
71+ for (auto & s : ss) {
72+ std::exception_ptr ex;
73+
74+ if (s.find('/') != std::string::npos) {
75+ try {
76+ result.push_back(std::make_shared<InstallableStorePath>(store, store->followLinksToStorePath(s)));
77+ continue;
78+ } catch (BadStorePath &) {
79+ } catch (...) {
80+ if (!ex)
81+ ex = std::current_exception();
82+ }
83+ }
84+
85+ try {
86+ auto [flakeRef, fragment, outputsSpec] = parseFlakeRefWithFragmentAndOutputsSpec(s, absPath("."));
87+/* result.push_back(std::make_shared<InstallableFlake>(
88+ this,
89+ getEvalState(),
90+ std::move(flakeRef),
91+ fragment,
92+ outputsSpec,
93+ getDefaultFlakeAttrPaths(),
94+ getDefaultFlakeAttrPathPrefixes(),
95+ lockFlags));*/
96+ e.push_back("with (builtins.getFlake \"" + flakeRef.to_string() + "\")" + (fragment != "" ? "." + fragment : ""));
97+ e.push_back("with (builtins.getFlake \"" + flakeRef.to_string() + "\").packages.\"${builtins.currentSystem}\"" + (fragment != "" ? "." + fragment : ""));
98+ e.push_back("with (builtins.getFlake \"" + flakeRef.to_string() + "\").legacyPackages.\"${builtins.currentSystem}\"" + (fragment != "" ? "." + fragment : ""));
99+ continue;
100+ } catch (...) {
101+ ex = std::current_exception();
102+ }
103+
104+ std::rethrow_exception(ex);
105+ }
106+
107 e.push_back(*expr);
108 auto parsed = state->parseExprFromString(concatStringsSep(";", e), absPath("."));
109 state->eval(parsed, *vFile);
110+
111+ auto [prefix, outputsSpec] = parseOutputsSpec(".");
112+ result.push_back(
113+ std::make_shared<InstallableAttrPath>(
114+ state, *this, vFile,
115+ prefix == "." ? "" : prefix,
116+ outputsSpec));
117+ return result;
118 }
119
120 for (auto & s : ss) {
121
122From e3045be11cbac91a5e65ef42e42e320e6689e713 Mon Sep 17 00:00:00 2001
123From: Skyler Grey <skyler3665@gmail.com>
124Date: Wed, 20 Jul 2022 23:56:09 +0100
125Subject: [PATCH 04/10] Update documentation to match nix shell change
126
127---
128 src/nix/nix.md | 9 +++++++--
129 1 file changed, 7 insertions(+), 2 deletions(-)
130
131diff --git a/src/nix/nix.md b/src/nix/nix.md
132index d48682a9432..b5bee708451 100644
133--- a/src/nix/nix.md
134+++ b/src/nix/nix.md
135@@ -139,8 +139,13 @@ the Nix store. Here are the recognised types of installables:
136 * **Nix expressions**: `--expr '(import <nixpkgs> {}).hello.overrideDerivation (prev: { name = "my-hello"; })'`.
137
138 When the `--expr` option is given, all installables are interpreted
139- as Nix expressions. You may need to specify `--impure` if the
140- expression references impure inputs (such as `<nixpkgs>`).
141+ as context for the expression. For example, the following is valid:
142+
143+ ```console
144+ # nix shell nixpkgs#python3 --expr 'withPackages(pyPkgs: with pyPkgs; [ numpy ])'
145+ ```
146+
147+ Using `--expr` implies `--impure`.
148
149 For most commands, if no installable is specified, the default is `.`,
150 i.e. Nix will operate on the default flake output attribute of the
151
152From 4d6c60a8187d9904312d062a19e61ca5c338bc94 Mon Sep 17 00:00:00 2001
153From: Skyler Grey <skyler3665@gmail.com>
154Date: Thu, 21 Jul 2022 21:49:44 +0100
155Subject: [PATCH 05/10] Only build . with expr if it'd be a default installable
156
157---
158 src/libcmd/installables.cc | 16 +++++++++-------
159 1 file changed, 9 insertions(+), 7 deletions(-)
160
161diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
162index 1abad72e333..15679590e39 100644
163--- a/src/libcmd/installables.cc
164+++ b/src/libcmd/installables.cc
165@@ -820,13 +820,15 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
166 auto parsed = state->parseExprFromString(concatStringsSep(";", e), absPath("."));
167 state->eval(parsed, *vFile);
168
169- auto [prefix, outputsSpec] = parseOutputsSpec(".");
170- result.push_back(
171- std::make_shared<InstallableAttrPath>(
172- state, *this, vFile,
173- prefix == "." ? "" : prefix,
174- outputsSpec));
175- return result;
176+ if (!ss.empty()) {
177+ auto [prefix, outputsSpec] = parseOutputsSpec(".");
178+ result.push_back(
179+ std::make_shared<InstallableAttrPath>(
180+ state, *this, vFile,
181+ prefix == "." ? "" : prefix,
182+ outputsSpec));
183+ return result;
184+ }
185 }
186
187 for (auto & s : ss) {
188
189From 14084facbd5654fa9e406ce0391c0544f3a42763 Mon Sep 17 00:00:00 2001
190From: Skyler Grey <skyler3665@gmail.com>
191Date: Fri, 22 Jul 2022 14:28:31 +0100
192Subject: [PATCH 06/10] Do not include implicit installables with expr
193
194---
195 src/libcmd/command.hh | 2 +-
196 src/libcmd/installables.cc | 9 +++++----
197 2 files changed, 6 insertions(+), 5 deletions(-)
198
199diff --git a/src/libcmd/command.hh b/src/libcmd/command.hh
200index 3b4b40981de..ba6ab4197ad 100644
201--- a/src/libcmd/command.hh
202+++ b/src/libcmd/command.hh
203@@ -102,7 +102,7 @@ struct SourceExprCommand : virtual Args, MixFlakeOptions
204 SourceExprCommand(bool supportReadOnlyMode = false);
205
206 std::vector<std::shared_ptr<Installable>> parseInstallables(
207- ref<Store> store, std::vector<std::string> ss);
208+ ref<Store> store, std::vector<std::string> ss, std::optional<std::vector<std::string>> explicitInstallables);
209
210 std::shared_ptr<Installable> parseInstallable(
211 ref<Store> store, const std::string & installable);
212diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
213index 15679590e39..fee419b240a 100644
214--- a/src/libcmd/installables.cc
215+++ b/src/libcmd/installables.cc
216@@ -754,7 +754,7 @@ FlakeRef InstallableFlake::nixpkgsFlakeRef() const
217 }
218
219 std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
220- ref<Store> store, std::vector<std::string> ss)
221+ ref<Store> store, std::vector<std::string> ss, std::optional<std::vector<std::string>> explicitInstallables = std::nullopt)
222 {
223 std::vector<std::shared_ptr<Installable>> result;
224
225@@ -780,7 +780,7 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
226 else {
227 Strings e = {};
228
229- for (auto & s : ss) {
230+ for (auto & s : explicitInstallables.value_or(ss)) {
231 std::exception_ptr ex;
232
233 if (s.find('/') != std::string::npos) {
234@@ -827,8 +827,8 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
235 state, *this, vFile,
236 prefix == "." ? "" : prefix,
237 outputsSpec));
238- return result;
239 }
240+ return result;
241 }
242
243 for (auto & s : ss) {
244@@ -1094,11 +1094,12 @@ void InstallablesCommand::prepare()
245
246 Installables InstallablesCommand::load() {
247 Installables installables;
248+ auto explicitInstallables = _installables;
249 if (_installables.empty() && useDefaultInstallables())
250 // FIXME: commands like "nix profile install" should not have a
251 // default, probably.
252 _installables.push_back(".");
253- return parseInstallables(getStore(), _installables);
254+ return parseInstallables(getStore(), _installables, explicitInstallables);
255 }
256
257 std::vector<std::string> InstallablesCommand::getFlakesForCompletion()
258
259From f82a172cfe18af507fadac565223769af2a39d86 Mon Sep 17 00:00:00 2001
260From: Skyler Grey <skyler3665@gmail.com>
261Date: Fri, 22 Jul 2022 20:13:48 +0100
262Subject: [PATCH 07/10] Allow running --expr purely again
263
264---
265 src/libcmd/installables.cc | 15 ++++++++-------
266 src/nix/nix.md | 2 --
267 2 files changed, 8 insertions(+), 9 deletions(-)
268
269diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
270index fee419b240a..3073b153568 100644
271--- a/src/libcmd/installables.cc
272+++ b/src/libcmd/installables.cc
273@@ -767,7 +767,7 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
274 throw UsageError("'--file' and '--expr' are exclusive");
275
276 // FIXME: backward compatibility hack
277- evalSettings.pureEval = false;
278+ if (file) evalSettings.pureEval = false;
279
280 auto state = getEvalState();
281 auto vFile = state->allocValue();
282@@ -780,7 +780,7 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
283 else {
284 Strings e = {};
285
286- for (auto & s : explicitInstallables.value_or(ss)) {
287+ for (auto & s : explicitInstallables.value_or(std::vector<std::string>())) {
288 std::exception_ptr ex;
289
290 if (s.find('/') != std::string::npos) {
291@@ -796,7 +796,7 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
292
293 try {
294 auto [flakeRef, fragment, outputsSpec] = parseFlakeRefWithFragmentAndOutputsSpec(s, absPath("."));
295-/* result.push_back(std::make_shared<InstallableFlake>(
296+ auto installableFlake = InstallableFlake(
297 this,
298 getEvalState(),
299 std::move(flakeRef),
300@@ -804,10 +804,11 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
301 outputsSpec,
302 getDefaultFlakeAttrPaths(),
303 getDefaultFlakeAttrPathPrefixes(),
304- lockFlags));*/
305- e.push_back("with (builtins.getFlake \"" + flakeRef.to_string() + "\")" + (fragment != "" ? "." + fragment : ""));
306- e.push_back("with (builtins.getFlake \"" + flakeRef.to_string() + "\").packages.\"${builtins.currentSystem}\"" + (fragment != "" ? "." + fragment : ""));
307- e.push_back("with (builtins.getFlake \"" + flakeRef.to_string() + "\").legacyPackages.\"${builtins.currentSystem}\"" + (fragment != "" ? "." + fragment : ""));
308+ lockFlags);
309+ auto lockedFlake = installableFlake.getLockedFlake()->flake.lockedRef;
310+ e.push_back("with (builtins.getFlake \"" + lockedFlake.to_string() + "\")" + (fragment != "" ? "." + fragment : ""));
311+ e.push_back("with (builtins.getFlake \"" + lockedFlake.to_string() + "\").packages.\"${builtins.currentSystem}\"" + (fragment != "" ? "." + fragment : ""));
312+ e.push_back("with (builtins.getFlake \"" + lockedFlake.to_string() + "\").legacyPackages.\"${builtins.currentSystem}\"" + (fragment != "" ? "." + fragment : ""));
313 continue;
314 } catch (...) {
315 ex = std::current_exception();
316diff --git a/src/nix/nix.md b/src/nix/nix.md
317index b5bee708451..33d618d88ad 100644
318--- a/src/nix/nix.md
319+++ b/src/nix/nix.md
320@@ -145,8 +145,6 @@ the Nix store. Here are the recognised types of installables:
321 # nix shell nixpkgs#python3 --expr 'withPackages(pyPkgs: with pyPkgs; [ numpy ])'
322 ```
323
324- Using `--expr` implies `--impure`.
325-
326 For most commands, if no installable is specified, the default is `.`,
327 i.e. Nix will operate on the default flake output attribute of the
328 flake in the current directory.
329
330From 91da7d50e7ba52b846fa50b8e106d723380b08d5 Mon Sep 17 00:00:00 2001
331From: Skyler Grey <skyler3665@gmail.com>
332Date: Sat, 23 Jul 2022 11:46:07 +0100
333Subject: [PATCH 08/10] Fix builtins.currentSystem in pure evaluation
334
335---
336 src/libcmd/installables.cc | 15 ++++++++++++---
337 1 file changed, 12 insertions(+), 3 deletions(-)
338
339diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
340index 3073b153568..d20ce6ed618 100644
341--- a/src/libcmd/installables.cc
342+++ b/src/libcmd/installables.cc
343@@ -806,9 +806,18 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
344 getDefaultFlakeAttrPathPrefixes(),
345 lockFlags);
346 auto lockedFlake = installableFlake.getLockedFlake()->flake.lockedRef;
347- e.push_back("with (builtins.getFlake \"" + lockedFlake.to_string() + "\")" + (fragment != "" ? "." + fragment : ""));
348- e.push_back("with (builtins.getFlake \"" + lockedFlake.to_string() + "\").packages.\"${builtins.currentSystem}\"" + (fragment != "" ? "." + fragment : ""));
349- e.push_back("with (builtins.getFlake \"" + lockedFlake.to_string() + "\").legacyPackages.\"${builtins.currentSystem}\"" + (fragment != "" ? "." + fragment : ""));
350+
351+ auto defaultPathPrefixes = getDefaultFlakeAttrPathPrefixes();
352+ defaultPathPrefixes.push_back("");
353+
354+ for (auto & path : defaultPathPrefixes) {
355+ if (path.length()) {
356+ path.pop_back();
357+ path = "." + path;
358+ }
359+
360+ e.push_back(str(boost::format("with (builtins.getFlake \"%1%\")%2%%3%") % lockedFlake.to_string() % path % (fragment != "" ? "." + fragment : "")));
361+ }
362 continue;
363 } catch (...) {
364 ex = std::current_exception();
365
366From a126a92e4f2c6087bf46dfb8c4acd5c34e5422eb Mon Sep 17 00:00:00 2001
367From: Skyler Grey <skyler3665@gmail.com>
368Date: Sun, 24 Jul 2022 01:30:00 +0100
369Subject: [PATCH 09/10] Gracefully handle missing attributes
370
371---
372 src/libcmd/installables.cc | 2 +-
373 1 file changed, 1 insertion(+), 1 deletion(-)
374
375diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
376index d20ce6ed618..9c8a848d641 100644
377--- a/src/libcmd/installables.cc
378+++ b/src/libcmd/installables.cc
379@@ -816,7 +816,7 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
380 path = "." + path;
381 }
382
383- e.push_back(str(boost::format("with (builtins.getFlake \"%1%\")%2%%3%") % lockedFlake.to_string() % path % (fragment != "" ? "." + fragment : "")));
384+ e.push_back(str(boost::format("with (builtins.getFlake \"%1%\")%2%%3% or {}") % lockedFlake.to_string() % path % (fragment != "" ? "." + fragment : "")));
385 }
386 continue;
387 } catch (...) {
388
389From c7302640120ff9fff6c3a653c22fa678efbeb903 Mon Sep 17 00:00:00 2001
390From: Skyler Grey <skyler3665@gmail.com>
391Date: Sun, 24 Jul 2022 07:11:37 +0100
392Subject: [PATCH 10/10] Fix including a flake without any attrpath
393
394---
395 src/libcmd/installables.cc | 2 +-
396 1 file changed, 1 insertion(+), 1 deletion(-)
397
398diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
399index 9c8a848d641..bdc3830956a 100644
400--- a/src/libcmd/installables.cc
401+++ b/src/libcmd/installables.cc
402@@ -816,7 +816,7 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
403 path = "." + path;
404 }
405
406- e.push_back(str(boost::format("with (builtins.getFlake \"%1%\")%2%%3% or {}") % lockedFlake.to_string() % path % (fragment != "" ? "." + fragment : "")));
407+ e.push_back(str(boost::format("with (builtins.getFlake \"%1%\").outputs%2%%3% or {}") % lockedFlake.to_string() % path % (fragment != "" ? "." + fragment : "")));
408 }
409 continue;
410 } catch (...) {