nix edit: Support non-derivation attributes

E.g.

  $ nix edit .#nixosConfigurations.bla

now works.

(cherry picked from commit d2032edb2f)
This commit is contained in:
Eelco Dolstra 2020-02-07 14:22:01 +01:00
parent c1ca4f0acc
commit 231a8aa2c2
6 changed files with 18 additions and 16 deletions

View file

@ -103,7 +103,7 @@ Pos findDerivationFilename(EvalState & state, Value & v, std::string what)
auto dummyArgs = state.allocBindings(0);
v2 = findAlongAttrPath(state, "meta.position", *dummyArgs, v).first;
} catch (Error &) {
throw Error("package '%s' has no source location information", what);
throw NoPositionInfo("package '%s' has no source location information", what);
}
// FIXME: is it possible to extract the Pos object instead of doing this

View file

@ -8,6 +8,7 @@
namespace nix {
MakeError(AttrPathNotFound, Error);
MakeError(NoPositionInfo, Error);
std::pair<Value *, Pos> findAlongAttrPath(EvalState & state, const string & attrPath,
Bindings & autoArgs, Value & vIn);

View file

@ -3,17 +3,12 @@
#include "args.hh"
#include "common-eval-args.hh"
#include "path.hh"
#include "eval.hh"
namespace nix {
extern std::string programPath;
struct Value;
class Bindings;
class EvalState;
struct Pos;
class Store;
/* A command that requires a Nix store. */
struct StoreCommand : virtual Command
{
@ -48,7 +43,7 @@ struct Installable
Buildable toBuildable();
virtual Value * toValue(EvalState & state)
virtual std::pair<Value *, Pos> toValue(EvalState & state)
{
throw Error("argument '%s' cannot be evaluated", what());
}

View file

@ -29,9 +29,15 @@ struct CmdEdit : InstallableCommand
{
auto state = getEvalState();
auto v = installable->toValue(*state);
auto [v, pos] = installable->toValue(*state);
Pos pos = findDerivationFilename(*state, *v, installable->what());
try {
pos = findDerivationFilename(*state, *v, installable->what());
} catch (NoPositionInfo &) {
}
if (pos == noPos)
throw Error("cannot find position information for '%s", installable->what());
stopProgressBar();

View file

@ -52,7 +52,7 @@ struct CmdEval : MixJSON, InstallableCommand
auto state = getEvalState();
auto v = installable->toValue(*state);
auto v = installable->toValue(*state).first;
PathSet context;
stopProgressBar();

View file

@ -121,7 +121,7 @@ struct InstallableValue : Installable
{
auto state = cmd.getEvalState();
auto v = toValue(*state);
auto v = toValue(*state).first;
Bindings & autoArgs = *cmd.getAutoArgs(*state);
@ -169,11 +169,11 @@ struct InstallableExpr : InstallableValue
std::string what() override { return text; }
Value * toValue(EvalState & state) override
std::pair<Value *, Pos> toValue(EvalState & state) override
{
auto v = state.allocValue();
state.eval(state.parseExprFromString(text, absPath(".")), *v);
return v;
return {v, noPos};
}
};
@ -187,7 +187,7 @@ struct InstallableAttrPath : InstallableValue
std::string what() override { return attrPath; }
Value * toValue(EvalState & state) override
std::pair<Value *, Pos> toValue(EvalState & state) override
{
auto source = cmd.getSourceExpr(state);
@ -196,7 +196,7 @@ struct InstallableAttrPath : InstallableValue
auto v = findAlongAttrPath(state, attrPath, autoArgs, *source).first;
state.forceValue(*v);
return v;
return {v, noPos};
}
};