1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-29 14:53:16 +02:00

Merge remote-tracking branch 'origin/master' into lazy-trees

This commit is contained in:
Eelco Dolstra 2022-05-26 14:11:46 +02:00
commit dfbb9a997f
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
46 changed files with 1339 additions and 391 deletions

View file

@ -15,7 +15,7 @@ jobs:
fetch-depth: 0 fetch-depth: 0
- name: Create backport PRs - name: Create backport PRs
# should be kept in sync with `version` # should be kept in sync with `version`
uses: zeebe-io/backport-action@v0.0.7 uses: zeebe-io/backport-action@v0.0.8
with: with:
# Config README: https://github.com/zeebe-io/backport-action#backport-action # Config README: https://github.com/zeebe-io/backport-action#backport-action
github_token: ${{ secrets.GITHUB_TOKEN }} github_token: ${{ secrets.GITHUB_TOKEN }}

View file

@ -297,6 +297,17 @@ esac
AC_ARG_WITH(sandbox-shell, AS_HELP_STRING([--with-sandbox-shell=PATH],[path of a statically-linked shell to use as /bin/sh in sandboxes]), AC_ARG_WITH(sandbox-shell, AS_HELP_STRING([--with-sandbox-shell=PATH],[path of a statically-linked shell to use as /bin/sh in sandboxes]),
sandbox_shell=$withval) sandbox_shell=$withval)
AC_SUBST(sandbox_shell) AC_SUBST(sandbox_shell)
if test ${cross_compiling:-no} = no && ! test -z ${sandbox_shell+x}; then
AC_MSG_CHECKING([whether sandbox-shell has the standalone feature])
# busybox shell sometimes allows executing other busybox applets,
# even if they are not in the path, breaking our sandbox
if PATH= $sandbox_shell -c "busybox" 2>&1 | grep -qv "not found"; then
AC_MSG_RESULT(enabled)
AC_MSG_ERROR([Please disable busybox FEATURE_SH_STANDALONE])
else
AC_MSG_RESULT(disabled)
fi
fi
# Expand all variables in config.status. # Expand all variables in config.status.
test "$prefix" = NONE && prefix=$ac_default_prefix test "$prefix" = NONE && prefix=$ac_default_prefix

View file

@ -4,7 +4,7 @@ Most Nix commands interpret the following environment variables:
- `IN_NIX_SHELL`\ - `IN_NIX_SHELL`\
Indicator that tells if the current environment was set up by Indicator that tells if the current environment was set up by
`nix-shell`. Since Nix 2.0 the values are `"pure"` and `"impure"` `nix-shell`. It can have the values `pure` or `impure`.
- `NIX_PATH`\ - `NIX_PATH`\
A colon-separated list of directories used to look up Nix A colon-separated list of directories used to look up Nix

View file

@ -71,18 +71,6 @@ To install it in `$(pwd)/outputs` and test it:
nix (Nix) 3.0 nix (Nix) 3.0
``` ```
To run a functional test:
```console
make tests/test-name-should-auto-complete.sh.test
```
To run the unit-tests for C++ code:
```
make check
```
If you have a flakes-enabled Nix you can replace: If you have a flakes-enabled Nix you can replace:
```console ```console
@ -94,3 +82,29 @@ by:
```console ```console
$ nix develop $ nix develop
``` ```
## Testing
Nix comes with three different flavors of tests: unit, functional and integration.
### Unit-tests
The unit-tests for each Nix library (`libexpr`, `libstore`, etc..) are defined
under `src/{library_name}/tests` using the
[googletest](https://google.github.io/googletest/) framework.
You can run the whole testsuite with `make check`, or the tests for a specific component with `make libfoo-tests_RUN`. Finer-grained filtering is also possible using the [--gtest_filter](https://google.github.io/googletest/advanced.html#running-a-subset-of-the-tests) command-line option.
### Functional tests
The functional tests reside under the `tests` directory and are listed in `tests/local.mk`.
The whole testsuite can be run with `make install && make installcheck`.
Individual tests can be run with `make tests/{testName}.sh.test`.
### Integration tests
The integration tests are defined in the Nix flake under the `hydraJobs.tests` attribute.
These tests include everything that needs to interact with external services or run Nix in a non-trivial distributed setup.
Because these tests are expensive and require more than what the standard github-actions setup provides, they only run on the master branch (on <https://hydra.nixos.org/jobset/nix/master>).
You can run them manually with `nix build .#hydraJobs.tests.{testName}` or `nix-build -A hydraJobs.tests.{testName}`

View file

@ -24,3 +24,19 @@
Selecting derivation outputs using the attribute selection syntax Selecting derivation outputs using the attribute selection syntax
(e.g. `nixpkgs#glibc.dev`) no longer works. (e.g. `nixpkgs#glibc.dev`) no longer works.
* Running nix with the new `--debugger` flag will cause it to start a repl session if
there is an exception thrown during eval, or if `builtins.break` is called. From
there one can inspect symbol values and evaluate nix expressions. In debug mode
the following new repl commands are available:
```
:env Show env stack
:bt Show trace stack
:st Show current trace
:st <idx> Change to another trace in the stack
:c Go until end of program, exception, or builtins.break().
:s Go one step
```
* `builtins.fetchTree` (and flake inputs) can now be used to fetch plain files
over the `http(s)` and `file` protocols in addition to directory tarballs.

View file

@ -9,6 +9,7 @@ ConditionPathIsReadWrite=@localstatedir@/nix/daemon-socket
[Service] [Service]
ExecStart=@@bindir@/nix-daemon nix-daemon --daemon ExecStart=@@bindir@/nix-daemon nix-daemon --daemon
KillMode=process KillMode=process
LimitNOFILE=4096
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View file

@ -442,9 +442,13 @@ add_nix_vol_fstab_line() {
local escaped_mountpoint="${NIX_ROOT/ /'\\\'040}" local escaped_mountpoint="${NIX_ROOT/ /'\\\'040}"
shift shift
# wrap `ex` to work around a problem with vim plugins breaking exit codes; # wrap `ex` to work around a problem with vim plugins breaking exit codes
# (see https://github.com/NixOS/nix/issues/5468) # (see github.com/NixOS/nix/issues/5468)
# we'd prefer EDITOR="/usr/bin/ex --noplugin" but vifs doesn't word-split #
# the first draft used `--noplugin`, but github.com/NixOS/nix/issues/6462
# suggests we need the less-semantic `-u NONE`
#
# we'd prefer EDITOR="/usr/bin/ex -u NONE" but vifs doesn't word-split
# the EDITOR env. # the EDITOR env.
# #
# TODO: at some point we should switch to `--clean`, but it wasn't added # TODO: at some point we should switch to `--clean`, but it wasn't added
@ -452,7 +456,7 @@ add_nix_vol_fstab_line() {
# minver 10.12.6 seems to have released with vim 7.4 # minver 10.12.6 seems to have released with vim 7.4
cat > "$SCRATCH/ex_cleanroom_wrapper" <<EOF cat > "$SCRATCH/ex_cleanroom_wrapper" <<EOF
#!/bin/sh #!/bin/sh
/usr/bin/ex --noplugin "\$@" /usr/bin/ex -u NONE "\$@"
EOF EOF
chmod 755 "$SCRATCH/ex_cleanroom_wrapper" chmod 755 "$SCRATCH/ex_cleanroom_wrapper"
@ -646,8 +650,9 @@ EOF
task "Configuring /etc/synthetic.conf to make a mount-point at $NIX_ROOT" >&2 task "Configuring /etc/synthetic.conf to make a mount-point at $NIX_ROOT" >&2
# technically /etc/synthetic.d/nix is supported in Big Sur+ # technically /etc/synthetic.d/nix is supported in Big Sur+
# but handling both takes even more code... # but handling both takes even more code...
# Note: `-u NONE` disables vim plugins/rc; see note on --clean earlier
_sudo "to add Nix to /etc/synthetic.conf" \ _sudo "to add Nix to /etc/synthetic.conf" \
/usr/bin/ex --noplugin /etc/synthetic.conf <<EOF /usr/bin/ex -u NONE /etc/synthetic.conf <<EOF
:a :a
${NIX_ROOT:1} ${NIX_ROOT:1}
. .
@ -815,7 +820,8 @@ setup_volume_daemon() {
local volume_uuid="$2" local volume_uuid="$2"
if ! test_voldaemon; then if ! test_voldaemon; then
task "Configuring LaunchDaemon to mount '$NIX_VOLUME_LABEL'" >&2 task "Configuring LaunchDaemon to mount '$NIX_VOLUME_LABEL'" >&2
_sudo "to install the Nix volume mounter" /usr/bin/ex --noplugin "$NIX_VOLUME_MOUNTD_DEST" <<EOF # Note: `-u NONE` disables vim plugins/rc; see note on --clean earlier
_sudo "to install the Nix volume mounter" /usr/bin/ex -u NONE "$NIX_VOLUME_MOUNTD_DEST" <<EOF
:a :a
$(generate_mount_daemon "$cmd_type" "$volume_uuid") $(generate_mount_daemon "$cmd_type" "$volume_uuid")
. .

View file

@ -86,6 +86,11 @@ ref<Store> CopyCommand::getDstStore()
EvalCommand::EvalCommand() EvalCommand::EvalCommand()
{ {
addFlag({
.longName = "debugger",
.description = "start an interactive environment if evaluation fails",
.handler = {&startReplOnEvalErrors, true},
});
} }
EvalCommand::~EvalCommand() EvalCommand::~EvalCommand()
@ -103,7 +108,7 @@ ref<Store> EvalCommand::getEvalStore()
ref<EvalState> EvalCommand::getEvalState() ref<EvalState> EvalCommand::getEvalState()
{ {
if (!evalState) if (!evalState) {
evalState = evalState =
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
std::allocate_shared<EvalState>(traceable_allocator<EvalState>(), std::allocate_shared<EvalState>(traceable_allocator<EvalState>(),
@ -113,6 +118,11 @@ ref<EvalState> EvalCommand::getEvalState()
searchPath, getEvalStore(), getStore()) searchPath, getEvalStore(), getStore())
#endif #endif
; ;
if (startReplOnEvalErrors) {
evalState->debugRepl = &runRepl;
};
}
return ref<EvalState>(evalState); return ref<EvalState>(evalState);
} }

View file

@ -57,6 +57,8 @@ struct CopyCommand : virtual StoreCommand
struct EvalCommand : virtual StoreCommand, MixEvalArgs struct EvalCommand : virtual StoreCommand, MixEvalArgs
{ {
bool startReplOnEvalErrors = false;
EvalCommand(); EvalCommand();
~EvalCommand(); ~EvalCommand();
@ -270,4 +272,8 @@ void printClosureDiff(
const StorePath & afterPath, const StorePath & afterPath,
std::string_view indent); std::string_view indent);
void runRepl(
ref<EvalState> evalState,
const ValMap & extraEnv);
} }

View file

@ -625,11 +625,15 @@ std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> InstallableF
auto drvPath = attr->forceDerivation(); auto drvPath = attr->forceDerivation();
std::set<std::string> outputsToInstall; std::set<std::string> outputsToInstall;
std::optional<NixInt> priority;
if (auto aMeta = attr->maybeGetAttr(state->sMeta)) if (auto aMeta = attr->maybeGetAttr(state->sMeta)) {
if (auto aOutputsToInstall = aMeta->maybeGetAttr("outputsToInstall")) if (auto aOutputsToInstall = aMeta->maybeGetAttr("outputsToInstall"))
for (auto & s : aOutputsToInstall->getListOfStrings()) for (auto & s : aOutputsToInstall->getListOfStrings())
outputsToInstall.insert(s); outputsToInstall.insert(s);
if (auto aPriority = aMeta->maybeGetAttr("priority"))
priority = aPriority->getInt();
}
if (outputsToInstall.empty() || std::get_if<AllOutputs>(&outputsSpec)) { if (outputsToInstall.empty() || std::get_if<AllOutputs>(&outputsSpec)) {
outputsToInstall.clear(); outputsToInstall.clear();
@ -647,6 +651,7 @@ std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> InstallableF
auto drvInfo = DerivationInfo { auto drvInfo = DerivationInfo {
.drvPath = std::move(drvPath), .drvPath = std::move(drvPath),
.outputsToInstall = std::move(outputsToInstall), .outputsToInstall = std::move(outputsToInstall),
.priority = priority,
}; };
return {attrPath, getLockedFlake()->flake.lockedRef, std::move(drvInfo)}; return {attrPath, getLockedFlake()->flake.lockedRef, std::move(drvInfo)};

View file

@ -142,6 +142,7 @@ struct InstallableValue : Installable
{ {
StorePath drvPath; StorePath drvPath;
std::set<std::string> outputsToInstall; std::set<std::string> outputsToInstall;
std::optional<NixInt> priority;
}; };
virtual std::vector<DerivationInfo> toDerivations() = 0; virtual std::vector<DerivationInfo> toDerivations() = 0;

View file

@ -6,9 +6,9 @@ libcmd_DIR := $(d)
libcmd_SOURCES := $(wildcard $(d)/*.cc) libcmd_SOURCES := $(wildcard $(d)/*.cc)
libcmd_CXXFLAGS += -I src/libutil -I src/libstore -I src/libexpr -I src/libmain -I src/libfetchers libcmd_CXXFLAGS += -I src/libutil -I src/libstore -I src/libexpr -I src/libmain -I src/libfetchers -I src/nix
libcmd_LDFLAGS += $(LOWDOWN_LIBS) -pthread libcmd_LDFLAGS = $(EDITLINE_LIBS) -llowdown -pthread
libcmd_LIBS = libstore libutil libexpr libmain libfetchers libcmd_LIBS = libstore libutil libexpr libmain libfetchers

View file

@ -48,38 +48,42 @@ struct NixRepl
#endif #endif
{ {
std::string curDir; std::string curDir;
std::unique_ptr<EvalState> state; ref<EvalState> state;
Bindings * autoArgs; Bindings * autoArgs;
size_t debugTraceIndex;
Strings loadedFiles; Strings loadedFiles;
const static int envSize = 32768; const static int envSize = 32768;
StaticEnv staticEnv; std::shared_ptr<StaticEnv> staticEnv;
Env * env; Env * env;
int displ; int displ;
StringSet varNames; StringSet varNames;
const Path historyFile; const Path historyFile;
NixRepl(const Strings & searchPath, nix::ref<Store> store); NixRepl(ref<EvalState> state);
~NixRepl(); ~NixRepl();
void mainLoop(const std::vector<std::string> & files); void mainLoop(const std::vector<std::string> & files);
StringSet completePrefix(const std::string & prefix); StringSet completePrefix(const std::string & prefix);
bool getLine(std::string & input, const std::string &prompt); bool getLine(std::string & input, const std::string & prompt);
StorePath getDerivationPath(Value & v); StorePath getDerivationPath(Value & v);
bool processLine(std::string line); bool processLine(std::string line);
void loadFile(const Path & path); void loadFile(const Path & path);
void loadFlake(const std::string & flakeRef); void loadFlake(const std::string & flakeRef);
void initEnv(); void initEnv();
void loadFiles();
void reloadFiles(); void reloadFiles();
void addAttrsToScope(Value & attrs); void addAttrsToScope(Value & attrs);
void addVarToScope(const Symbol name, Value & v); void addVarToScope(const Symbol name, Value & v);
Expr * parseString(std::string s); Expr * parseString(std::string s);
void evalString(std::string s, Value & v); void evalString(std::string s, Value & v);
void loadDebugTraceEnv(DebugTrace & dt);
typedef std::set<Value *> ValuesSeen; typedef std::set<Value *> ValuesSeen;
std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth); std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth);
std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth, ValuesSeen & seen); std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth, ValuesSeen & seen);
}; };
@ -92,9 +96,10 @@ std::string removeWhitespace(std::string s)
} }
NixRepl::NixRepl(const Strings & searchPath, nix::ref<Store> store) NixRepl::NixRepl(ref<EvalState> state)
: state(std::make_unique<EvalState>(searchPath, store)) : state(state)
, staticEnv(false, &state->staticBaseEnv) , debugTraceIndex(0)
, staticEnv(new StaticEnv(false, state->staticBaseEnv.get()))
, historyFile(getDataDir() + "/nix/repl-history") , historyFile(getDataDir() + "/nix/repl-history")
{ {
curDir = absPath("."); curDir = absPath(".");
@ -198,15 +203,42 @@ namespace {
} }
} }
static std::ostream & showDebugTrace(std::ostream & out, const PosTable & positions, const DebugTrace & dt)
{
if (dt.isError)
out << ANSI_RED "error: " << ANSI_NORMAL;
out << dt.hint.str() << "\n";
// prefer direct pos, but if noPos then try the expr.
auto pos = *dt.pos
? *dt.pos
: positions[dt.expr.getPos() ? dt.expr.getPos() : noPos];
if (pos) {
printAtPos(pos, out);
auto loc = getCodeLines(pos);
if (loc.has_value()) {
out << "\n";
printCodeLines(out, "", pos, *loc);
out << "\n";
}
}
return out;
}
void NixRepl::mainLoop(const std::vector<std::string> & files) void NixRepl::mainLoop(const std::vector<std::string> & files)
{ {
std::string error = ANSI_RED "error:" ANSI_NORMAL " "; std::string error = ANSI_RED "error:" ANSI_NORMAL " ";
notice("Welcome to Nix " + nixVersion + ". Type :? for help.\n"); notice("Welcome to Nix " + nixVersion + ". Type :? for help.\n");
for (auto & i : files) if (!files.empty()) {
loadedFiles.push_back(i); for (auto & i : files)
loadedFiles.push_back(i);
}
reloadFiles(); loadFiles();
if (!loadedFiles.empty()) notice(""); if (!loadedFiles.empty()) notice("");
// Allow nix-repl specific settings in .inputrc // Allow nix-repl specific settings in .inputrc
@ -227,9 +259,12 @@ void NixRepl::mainLoop(const std::vector<std::string> & files)
while (true) { while (true) {
// When continuing input from previous lines, don't print a prompt, just align to the same // When continuing input from previous lines, don't print a prompt, just align to the same
// number of chars as the prompt. // number of chars as the prompt.
if (!getLine(input, input.empty() ? "nix-repl> " : " ")) if (!getLine(input, input.empty() ? "nix-repl> " : " ")) {
// ctrl-D should exit the debugger.
state->debugStop = false;
state->debugQuit = true;
break; break;
}
try { try {
if (!removeWhitespace(input).empty() && !processLine(input)) return; if (!removeWhitespace(input).empty() && !processLine(input)) return;
} catch (ParseError & e) { } catch (ParseError & e) {
@ -240,6 +275,14 @@ void NixRepl::mainLoop(const std::vector<std::string> & files)
} else { } else {
printMsg(lvlError, e.msg()); printMsg(lvlError, e.msg());
} }
} catch (EvalError & e) {
// in debugger mode, an EvalError should trigger another repl session.
// when that session returns the exception will land here. No need to show it again;
// show the error for this repl session instead.
if (state->debugRepl && !state->debugTraces.empty())
showDebugTrace(std::cout, state->positions, state->debugTraces.front());
else
printMsg(lvlError, e.msg());
} catch (Error & e) { } catch (Error & e) {
printMsg(lvlError, e.msg()); printMsg(lvlError, e.msg());
} catch (Interrupted & e) { } catch (Interrupted & e) {
@ -394,6 +437,19 @@ StorePath NixRepl::getDerivationPath(Value & v) {
return *drvPath; return *drvPath;
} }
void NixRepl::loadDebugTraceEnv(DebugTrace & dt)
{
initEnv();
auto se = state->getStaticEnv(dt.expr);
if (se) {
auto vm = mapStaticEnvBindings(state->symbols, *se.get(), dt.env);
// add staticenv vars.
for (auto & [name, value] : *(vm.get()))
addVarToScope(state->symbols.create(name), *value);
}
}
bool NixRepl::processLine(std::string line) bool NixRepl::processLine(std::string line)
{ {
@ -429,12 +485,72 @@ bool NixRepl::processLine(std::string line)
<< " :p <expr> Evaluate and print expression recursively\n" << " :p <expr> Evaluate and print expression recursively\n"
<< " :q Exit nix-repl\n" << " :q Exit nix-repl\n"
<< " :r Reload all files\n" << " :r Reload all files\n"
<< " :s <expr> Build dependencies of derivation, then start nix-shell\n" << " :sh <expr> Build dependencies of derivation, then start nix-shell\n"
<< " :t <expr> Describe result of evaluation\n" << " :t <expr> Describe result of evaluation\n"
<< " :u <expr> Build derivation, then start nix-shell\n" << " :u <expr> Build derivation, then start nix-shell\n"
<< " :doc <expr> Show documentation of a builtin function\n" << " :doc <expr> Show documentation of a builtin function\n"
<< " :log <expr> Show logs for a derivation\n" << " :log <expr> Show logs for a derivation\n"
<< " :st [bool] Enable, disable or toggle showing traces for errors\n"; << " :te [bool] Enable, disable or toggle showing traces for errors\n"
;
if (state->debugRepl) {
std::cout
<< "\n"
<< " Debug mode commands\n"
<< " :env Show env stack\n"
<< " :bt Show trace stack\n"
<< " :st Show current trace\n"
<< " :st <idx> Change to another trace in the stack\n"
<< " :c Go until end of program, exception, or builtins.break\n"
<< " :s Go one step\n"
;
}
}
else if (state->debugRepl && (command == ":bt" || command == ":backtrace")) {
for (const auto & [idx, i] : enumerate(state->debugTraces)) {
std::cout << "\n" << ANSI_BLUE << idx << ANSI_NORMAL << ": ";
showDebugTrace(std::cout, state->positions, i);
}
}
else if (state->debugRepl && (command == ":env")) {
for (const auto & [idx, i] : enumerate(state->debugTraces)) {
if (idx == debugTraceIndex) {
printEnvBindings(*state, i.expr, i.env);
break;
}
}
}
else if (state->debugRepl && (command == ":st")) {
try {
// change the DebugTrace index.
debugTraceIndex = stoi(arg);
} catch (...) { }
for (const auto & [idx, i] : enumerate(state->debugTraces)) {
if (idx == debugTraceIndex) {
std::cout << "\n" << ANSI_BLUE << idx << ANSI_NORMAL << ": ";
showDebugTrace(std::cout, state->positions, i);
std::cout << std::endl;
printEnvBindings(*state, i.expr, i.env);
loadDebugTraceEnv(i);
break;
}
}
}
else if (state->debugRepl && (command == ":s" || command == ":step")) {
// set flag to stop at next DebugTrace; exit repl.
state->debugStop = true;
return false;
}
else if (state->debugRepl && (command == ":c" || command == ":continue")) {
// set flag to run to next breakpoint or end of program; exit repl.
state->debugStop = false;
return false;
} }
else if (command == ":a" || command == ":add") { else if (command == ":a" || command == ":add") {
@ -506,7 +622,7 @@ bool NixRepl::processLine(std::string line)
runNix("nix-shell", {state->store->printStorePath(drvPath)}); runNix("nix-shell", {state->store->printStorePath(drvPath)});
} }
else if (command == ":b" || command == ":bl" || command == ":i" || command == ":s" || command == ":log") { else if (command == ":b" || command == ":bl" || command == ":i" || command == ":sh" || command == ":log") {
Value v; Value v;
evalString(arg, v); evalString(arg, v);
StorePath drvPath = getDerivationPath(v); StorePath drvPath = getDerivationPath(v);
@ -567,8 +683,11 @@ bool NixRepl::processLine(std::string line)
printValue(std::cout, v, 1000000000) << std::endl; printValue(std::cout, v, 1000000000) << std::endl;
} }
else if (command == ":q" || command == ":quit") else if (command == ":q" || command == ":quit") {
state->debugStop = false;
state->debugQuit = true;
return false; return false;
}
else if (command == ":doc") { else if (command == ":doc") {
Value v; Value v;
@ -593,7 +712,7 @@ bool NixRepl::processLine(std::string line)
throw Error("value does not have documentation"); throw Error("value does not have documentation");
} }
else if (command == ":st" || command == ":show-trace") { else if (command == ":te" || command == ":trace-enable") {
if (arg == "false" || (arg == "" && loggerSettings.showTrace)) { if (arg == "false" || (arg == "" && loggerSettings.showTrace)) {
std::cout << "not showing error traces\n"; std::cout << "not showing error traces\n";
loggerSettings.showTrace = false; loggerSettings.showTrace = false;
@ -669,10 +788,10 @@ void NixRepl::initEnv()
env = &state->allocEnv(envSize); env = &state->allocEnv(envSize);
env->up = &state->baseEnv; env->up = &state->baseEnv;
displ = 0; displ = 0;
staticEnv.vars.clear(); staticEnv->vars.clear();
varNames.clear(); varNames.clear();
for (auto & i : state->staticBaseEnv.vars) for (auto & i : state->staticBaseEnv->vars)
varNames.emplace(state->symbols[i.first]); varNames.emplace(state->symbols[i.first]);
} }
@ -681,6 +800,12 @@ void NixRepl::reloadFiles()
{ {
initEnv(); initEnv();
loadFiles();
}
void NixRepl::loadFiles()
{
Strings old = loadedFiles; Strings old = loadedFiles;
loadedFiles.clear(); loadedFiles.clear();
@ -701,12 +826,12 @@ void NixRepl::addAttrsToScope(Value & attrs)
throw Error("environment full; cannot add more variables"); throw Error("environment full; cannot add more variables");
for (auto & i : *attrs.attrs) { for (auto & i : *attrs.attrs) {
staticEnv.vars.emplace_back(i.name, displ); staticEnv->vars.emplace_back(i.name, displ);
env->values[displ++] = i.value; env->values[displ++] = i.value;
varNames.emplace(state->symbols[i.name]); varNames.emplace(state->symbols[i.name]);
} }
staticEnv.sort(); staticEnv->sort();
staticEnv.deduplicate(); staticEnv->deduplicate();
notice("Added %1% variables.", attrs.attrs->size()); notice("Added %1% variables.", attrs.attrs->size());
} }
@ -715,10 +840,10 @@ void NixRepl::addVarToScope(const Symbol name, Value & v)
{ {
if (displ >= envSize) if (displ >= envSize)
throw Error("environment full; cannot add more variables"); throw Error("environment full; cannot add more variables");
if (auto oldVar = staticEnv.find(name); oldVar != staticEnv.vars.end()) if (auto oldVar = staticEnv->find(name); oldVar != staticEnv->vars.end())
staticEnv.vars.erase(oldVar); staticEnv->vars.erase(oldVar);
staticEnv.vars.emplace_back(name, displ); staticEnv->vars.emplace_back(name, displ);
staticEnv.sort(); staticEnv->sort();
env->values[displ++] = &v; env->values[displ++] = &v;
varNames.emplace(state->symbols[name]); varNames.emplace(state->symbols[name]);
} }
@ -886,6 +1011,21 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m
return str; return str;
} }
void runRepl(
ref<EvalState>evalState,
const ValMap & extraEnv)
{
auto repl = std::make_unique<NixRepl>(evalState);
repl->initEnv();
// add 'extra' vars.
for (auto & [name, value] : extraEnv)
repl->addVarToScope(repl->state->symbols.create(name), *value);
repl->mainLoop({});
}
struct CmdRepl : StoreCommand, MixEvalArgs struct CmdRepl : StoreCommand, MixEvalArgs
{ {
std::vector<std::string> files; std::vector<std::string> files;
@ -914,8 +1054,12 @@ struct CmdRepl : StoreCommand, MixEvalArgs
void run(ref<Store> store) override void run(ref<Store> store) override
{ {
evalSettings.pureEval = false; evalSettings.pureEval = false;
auto repl = std::make_unique<NixRepl>(searchPath, openStore());
auto evalState = make_ref<EvalState>(searchPath, store);
auto repl = std::make_unique<NixRepl>(evalState);
repl->autoArgs = getAutoArgs(*repl->state); repl->autoArgs = getAutoArgs(*repl->state);
repl->initEnv();
repl->mainLoop(files); repl->mainLoop(files);
} }
}; };

View file

@ -47,7 +47,7 @@ struct AttrDb
{ {
auto state(_state->lock()); auto state(_state->lock());
Path cacheDir = getCacheDir() + "/nix/eval-cache-v3"; Path cacheDir = getCacheDir() + "/nix/eval-cache-v4";
createDirs(cacheDir); createDirs(cacheDir);
Path dbPath = cacheDir + "/" + fingerprint.to_string(Base16, false) + ".sqlite"; Path dbPath = cacheDir + "/" + fingerprint.to_string(Base16, false) + ".sqlite";
@ -175,6 +175,24 @@ struct AttrDb
}); });
} }
AttrId setInt(
AttrKey key,
int n)
{
return doSQLite([&]()
{
auto state(_state->lock());
state->insertAttribute.use()
(key.first)
(symbols[key.second])
(AttrType::Int)
(n).exec();
return state->db.getLastInsertedRowId();
});
}
AttrId setListOfStrings( AttrId setListOfStrings(
AttrKey key, AttrKey key,
const std::vector<std::string> & l) const std::vector<std::string> & l)
@ -287,6 +305,8 @@ struct AttrDb
} }
case AttrType::Bool: case AttrType::Bool:
return {{rowId, queryAttribute.getInt(2) != 0}}; return {{rowId, queryAttribute.getInt(2) != 0}};
case AttrType::Int:
return {{rowId, int_t{queryAttribute.getInt(2)}}};
case AttrType::ListOfStrings: case AttrType::ListOfStrings:
return {{rowId, tokenizeString<std::vector<std::string>>(queryAttribute.getStr(2), "\t")}}; return {{rowId, tokenizeString<std::vector<std::string>>(queryAttribute.getStr(2), "\t")}};
case AttrType::Missing: case AttrType::Missing:
@ -429,6 +449,8 @@ Value & AttrCursor::forceValue()
} }
else if (v.type() == nBool) else if (v.type() == nBool)
cachedValue = {root->db->setBool(getKey(), v.boolean), v.boolean}; cachedValue = {root->db->setBool(getKey(), v.boolean), v.boolean};
else if (v.type() == nInt)
cachedValue = {root->db->setInt(getKey(), v.integer), int_t{v.integer}};
else if (v.type() == nAttrs) else if (v.type() == nAttrs)
; // FIXME: do something? ; // FIXME: do something?
else else
@ -557,14 +579,14 @@ std::string AttrCursor::getString()
debug("using cached string attribute '%s'", getAttrPathStr()); debug("using cached string attribute '%s'", getAttrPathStr());
return s->first; return s->first;
} else } else
throw TypeError("'%s' is not a string", getAttrPathStr()); root->state.debugThrowLastTrace(TypeError("'%s' is not a string", getAttrPathStr()));
} }
} }
auto & v = forceValue(); auto & v = forceValue();
if (v.type() != nString && v.type() != nPath) if (v.type() != nString && v.type() != nPath)
throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type())); root->state.debugThrowLastTrace(TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type())));
return v.type() == nString ? v.string.s : v.path().to_string(); return v.type() == nString ? v.string.s : v.path().to_string();
} }
@ -588,7 +610,7 @@ string_t AttrCursor::getStringWithContext()
return *s; return *s;
} }
} else } else
throw TypeError("'%s' is not a string", getAttrPathStr()); root->state.debugThrowLastTrace(TypeError("'%s' is not a string", getAttrPathStr()));
} }
} }
@ -599,7 +621,7 @@ string_t AttrCursor::getStringWithContext()
else if (v.type() == nPath) else if (v.type() == nPath)
return {v.path().to_string(), {}}; return {v.path().to_string(), {}};
else else
throw TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type())); root->state.debugThrowLastTrace(TypeError("'%s' is not a string but %s", getAttrPathStr(), showType(v.type())));
} }
bool AttrCursor::getBool() bool AttrCursor::getBool()
@ -612,18 +634,40 @@ bool AttrCursor::getBool()
debug("using cached Boolean attribute '%s'", getAttrPathStr()); debug("using cached Boolean attribute '%s'", getAttrPathStr());
return *b; return *b;
} else } else
throw TypeError("'%s' is not a Boolean", getAttrPathStr()); root->state.debugThrowLastTrace(TypeError("'%s' is not a Boolean", getAttrPathStr()));
} }
} }
auto & v = forceValue(); auto & v = forceValue();
if (v.type() != nBool) if (v.type() != nBool)
throw TypeError("'%s' is not a Boolean", getAttrPathStr()); root->state.debugThrowLastTrace(TypeError("'%s' is not a Boolean", getAttrPathStr()));
return v.boolean; return v.boolean;
} }
NixInt AttrCursor::getInt()
{
if (root->db) {
if (!cachedValue)
cachedValue = root->db->getAttr(getKey());
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
if (auto i = std::get_if<int_t>(&cachedValue->second)) {
debug("using cached Integer attribute '%s'", getAttrPathStr());
return i->x;
} else
throw TypeError("'%s' is not an Integer", getAttrPathStr());
}
}
auto & v = forceValue();
if (v.type() != nInt)
throw TypeError("'%s' is not an Integer", getAttrPathStr());
return v.integer;
}
std::vector<std::string> AttrCursor::getListOfStrings() std::vector<std::string> AttrCursor::getListOfStrings()
{ {
if (root->db) { if (root->db) {
@ -667,14 +711,14 @@ std::vector<Symbol> AttrCursor::getAttrs()
debug("using cached attrset attribute '%s'", getAttrPathStr()); debug("using cached attrset attribute '%s'", getAttrPathStr());
return *attrs; return *attrs;
} else } else
throw TypeError("'%s' is not an attribute set", getAttrPathStr()); root->state.debugThrowLastTrace(TypeError("'%s' is not an attribute set", getAttrPathStr()));
} }
} }
auto & v = forceValue(); auto & v = forceValue();
if (v.type() != nAttrs) if (v.type() != nAttrs)
throw TypeError("'%s' is not an attribute set", getAttrPathStr()); root->state.debugThrowLastTrace(TypeError("'%s' is not an attribute set", getAttrPathStr()));
std::vector<Symbol> attrs; std::vector<Symbol> attrs;
for (auto & attr : *getValue().attrs) for (auto & attr : *getValue().attrs)

View file

@ -45,12 +45,14 @@ enum AttrType {
Failed = 5, Failed = 5,
Bool = 6, Bool = 6,
ListOfStrings = 7, ListOfStrings = 7,
Int = 8,
}; };
struct placeholder_t {}; struct placeholder_t {};
struct missing_t {}; struct missing_t {};
struct misc_t {}; struct misc_t {};
struct failed_t {}; struct failed_t {};
struct int_t { NixInt x; };
typedef uint64_t AttrId; typedef uint64_t AttrId;
typedef std::pair<AttrId, Symbol> AttrKey; typedef std::pair<AttrId, Symbol> AttrKey;
typedef std::pair<std::string, NixStringContext> string_t; typedef std::pair<std::string, NixStringContext> string_t;
@ -63,6 +65,7 @@ typedef std::variant<
misc_t, misc_t,
failed_t, failed_t,
bool, bool,
int_t,
std::vector<std::string> std::vector<std::string>
> AttrValue; > AttrValue;
@ -116,6 +119,8 @@ public:
bool getBool(); bool getBool();
NixInt getInt();
std::vector<std::string> getListOfStrings(); std::vector<std::string> getListOfStrings();
std::vector<Symbol> getAttrs(); std::vector<Symbol> getAttrs();

View file

@ -4,7 +4,6 @@
namespace nix { namespace nix {
/* Note: Various places expect the allocated memory to be zeroed. */ /* Note: Various places expect the allocated memory to be zeroed. */
[[gnu::always_inline]] [[gnu::always_inline]]
inline void * allocBytes(size_t n) inline void * allocBytes(size_t n)

View file

@ -18,6 +18,7 @@
#include <sys/resource.h> #include <sys/resource.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <functional>
#include <sys/resource.h> #include <sys/resource.h>
@ -36,7 +37,6 @@
namespace nix { namespace nix {
static char * allocString(size_t size) static char * allocString(size_t size)
{ {
char * t; char * t;
@ -476,6 +476,9 @@ EvalState::EvalState(
, corepkgsFS(makeMemoryInputAccessor()) , corepkgsFS(makeMemoryInputAccessor())
, store(store) , store(store)
, buildStore(buildStore ? buildStore : store) , buildStore(buildStore ? buildStore : store)
, debugRepl(0)
, debugStop(false)
, debugQuit(false)
, regexCache(makeRegexCache()) , regexCache(makeRegexCache())
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
, valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr)) , valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr))
@ -485,7 +488,7 @@ EvalState::EvalState(
, env1AllocCache(std::make_shared<void *>(nullptr)) , env1AllocCache(std::make_shared<void *>(nullptr))
#endif #endif
, baseEnv(allocEnv(128)) , baseEnv(allocEnv(128))
, staticBaseEnv(false, 0) , staticBaseEnv{std::make_shared<StaticEnv>(false, nullptr)}
{ {
countCalls = getEnv("NIX_COUNT_CALLS").value_or("0") != "0"; countCalls = getEnv("NIX_COUNT_CALLS").value_or("0") != "0";
@ -590,7 +593,7 @@ Value * EvalState::addConstant(const std::string & name, Value & v)
void EvalState::addConstant(const std::string & name, Value * v) void EvalState::addConstant(const std::string & name, Value * v)
{ {
staticBaseEnv.vars.emplace_back(symbols.create(name), baseEnvDispl); staticBaseEnv->vars.emplace_back(symbols.create(name), baseEnvDispl);
baseEnv.values[baseEnvDispl++] = v; baseEnv.values[baseEnvDispl++] = v;
auto name2 = name.substr(0, 2) == "__" ? name.substr(2) : name; auto name2 = name.substr(0, 2) == "__" ? name.substr(2) : name;
baseEnv.values[0]->attrs->push_back(Attr(symbols.create(name2), v)); baseEnv.values[0]->attrs->push_back(Attr(symbols.create(name2), v));
@ -615,7 +618,7 @@ Value * EvalState::addPrimOp(const std::string & name,
Value * v = allocValue(); Value * v = allocValue();
v->mkPrimOp(new PrimOp { .fun = primOp, .arity = arity, .name = name2 }); v->mkPrimOp(new PrimOp { .fun = primOp, .arity = arity, .name = name2 });
staticBaseEnv.vars.emplace_back(symbols.create(name), baseEnvDispl); staticBaseEnv->vars.emplace_back(symbols.create(name), baseEnvDispl);
baseEnv.values[baseEnvDispl++] = v; baseEnv.values[baseEnvDispl++] = v;
baseEnv.values[0]->attrs->push_back(Attr(sym, v)); baseEnv.values[0]->attrs->push_back(Attr(sym, v));
return v; return v;
@ -641,7 +644,7 @@ Value * EvalState::addPrimOp(PrimOp && primOp)
Value * v = allocValue(); Value * v = allocValue();
v->mkPrimOp(new PrimOp(primOp)); v->mkPrimOp(new PrimOp(primOp));
staticBaseEnv.vars.emplace_back(envName, baseEnvDispl); staticBaseEnv->vars.emplace_back(envName, baseEnvDispl);
baseEnv.values[baseEnvDispl++] = v; baseEnv.values[baseEnvDispl++] = v;
baseEnv.values[0]->attrs->push_back(Attr(symbols.create(primOp.name), v)); baseEnv.values[0]->attrs->push_back(Attr(symbols.create(primOp.name), v));
return v; return v;
@ -671,123 +674,284 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
} }
// just for the current level of StaticEnv, not the whole chain.
void printStaticEnvBindings(const SymbolTable & st, const StaticEnv & se)
{
std::cout << ANSI_MAGENTA;
for (auto & i : se.vars)
std::cout << st[i.first] << " ";
std::cout << ANSI_NORMAL;
std::cout << std::endl;
}
// just for the current level of Env, not the whole chain.
void printWithBindings(const SymbolTable & st, const Env & env)
{
if (env.type == Env::HasWithAttrs) {
std::cout << "with: ";
std::cout << ANSI_MAGENTA;
Bindings::iterator j = env.values[0]->attrs->begin();
while (j != env.values[0]->attrs->end()) {
std::cout << st[j->name] << " ";
++j;
}
std::cout << ANSI_NORMAL;
std::cout << std::endl;
}
}
void printEnvBindings(const SymbolTable & st, const StaticEnv & se, const Env & env, int lvl)
{
std::cout << "Env level " << lvl << std::endl;
if (se.up && env.up) {
std::cout << "static: ";
printStaticEnvBindings(st, se);
printWithBindings(st, env);
std::cout << std::endl;
printEnvBindings(st, *se.up, *env.up, ++lvl);
} else {
std::cout << ANSI_MAGENTA;
// for the top level, don't print the double underscore ones;
// they are in builtins.
for (auto & i : se.vars)
if (!hasPrefix(st[i.first], "__"))
std::cout << st[i.first] << " ";
std::cout << ANSI_NORMAL;
std::cout << std::endl;
printWithBindings(st, env); // probably nothing there for the top level.
std::cout << std::endl;
}
}
void printEnvBindings(const EvalState &es, const Expr & expr, const Env & env)
{
// just print the names for now
auto se = es.getStaticEnv(expr);
if (se)
printEnvBindings(es.symbols, *se, env, 0);
}
void mapStaticEnvBindings(const SymbolTable & st, const StaticEnv & se, const Env & env, ValMap & vm)
{
// add bindings for the next level up first, so that the bindings for this level
// override the higher levels.
// The top level bindings (builtins) are skipped since they are added for us by initEnv()
if (env.up && se.up) {
mapStaticEnvBindings(st, *se.up, *env.up, vm);
if (env.type == Env::HasWithAttrs) {
// add 'with' bindings.
Bindings::iterator j = env.values[0]->attrs->begin();
while (j != env.values[0]->attrs->end()) {
vm[st[j->name]] = j->value;
++j;
}
} else {
// iterate through staticenv bindings and add them.
for (auto & i : se.vars)
vm[st[i.first]] = env.values[i.second];
}
}
}
std::unique_ptr<ValMap> mapStaticEnvBindings(const SymbolTable & st, const StaticEnv & se, const Env & env)
{
auto vm = std::make_unique<ValMap>();
mapStaticEnvBindings(st, se, env, *vm);
return vm;
}
void EvalState::runDebugRepl(const Error * error, const Env & env, const Expr & expr)
{
// double check we've got the debugRepl function pointer.
if (!debugRepl)
return;
auto dts =
error && expr.getPos()
? std::make_unique<DebugTraceStacker>(
*this,
DebugTrace {
.pos = error->info().errPos ? *error->info().errPos : positions[expr.getPos()],
.expr = expr,
.env = env,
.hint = error->info().msg,
.isError = true
})
: nullptr;
if (error)
printError("%s\n\n" ANSI_BOLD "Starting REPL to allow you to inspect the current state of the evaluator.\n" ANSI_NORMAL, error->what());
auto se = getStaticEnv(expr);
if (se) {
auto vm = mapStaticEnvBindings(symbols, *se.get(), env);
(debugRepl)(ref<EvalState>(shared_from_this()), *vm);
}
}
/* Every "format" object (even temporary) takes up a few hundred bytes /* Every "format" object (even temporary) takes up a few hundred bytes
of stack space, which is a real killer in the recursive of stack space, which is a real killer in the recursive
evaluator. So here are some helper functions for throwing evaluator. So here are some helper functions for throwing
exceptions. */ exceptions. */
void EvalState::throwEvalError(const PosIdx pos, const char * s, Env & env, Expr & expr)
void EvalState::throwEvalError(const PosIdx pos, const char * s) const
{ {
throw EvalError({ debugThrow(EvalError({
.msg = hintfmt(s), .msg = hintfmt(s),
.errPos = positions[pos] .errPos = positions[pos]
}); }), env, expr);
} }
void EvalState::throwTypeError(const PosIdx pos, const char * s, const Value & v) const void EvalState::throwEvalError(const PosIdx pos, const char * s)
{ {
throw TypeError({ debugThrowLastTrace(EvalError({
.msg = hintfmt(s, showType(v)), .msg = hintfmt(s),
.errPos = positions[pos] .errPos = positions[pos]
}); }));
}
void EvalState::throwEvalError(const char * s, const std::string & s2)
{
debugThrowLastTrace(EvalError(s, s2));
} }
void EvalState::throwEvalError(const PosIdx pos, const Suggestions & suggestions, const char * s, void EvalState::throwEvalError(const PosIdx pos, const Suggestions & suggestions, const char * s,
const std::string & s2) const const std::string & s2, Env & env, Expr & expr)
{ {
throw EvalError(ErrorInfo { debugThrow(EvalError(ErrorInfo{
.msg = hintfmt(s, s2), .msg = hintfmt(s, s2),
.errPos = positions[pos], .errPos = positions[pos],
.suggestions = suggestions, .suggestions = suggestions,
}); }), env, expr);
} }
void EvalState::throwEvalError(const PosIdx pos, const char * s, std::string_view s2) const void EvalState::throwEvalError(const PosIdx pos, const char * s, std::string_view s2)
{ {
throw EvalError(ErrorInfo { debugThrowLastTrace(EvalError({
.msg = hintfmt(s, s2), .msg = hintfmt(s, s2),
.errPos = positions[pos] .errPos = positions[pos]
}); }));
} }
void EvalState::throwEvalError(const char * s, const std::string & s2, const std::string & s3) const void EvalState::throwEvalError(const PosIdx pos, const char * s, const std::string & s2, Env & env, Expr & expr)
{ {
throw EvalError(s, s2, s3); debugThrow(EvalError({
.msg = hintfmt(s, s2),
.errPos = positions[pos]
}), env, expr);
}
void EvalState::throwEvalError(const char * s, const std::string & s2,
const std::string & s3)
{
debugThrowLastTrace(EvalError({
.msg = hintfmt(s, s2),
.errPos = positions[noPos]
}));
} }
void EvalState::throwEvalError(const PosIdx pos, const char * s, const std::string & s2, void EvalState::throwEvalError(const PosIdx pos, const char * s, const std::string & s2,
const std::string & s3) const const std::string & s3)
{ {
throw EvalError({ debugThrowLastTrace(EvalError({
.msg = hintfmt(s, s2, s3), .msg = hintfmt(s, s2),
.errPos = positions[pos] .errPos = positions[pos]
}); }));
} }
void EvalState::throwEvalError(const PosIdx p1, const char * s, const Symbol sym, const PosIdx p2) const void EvalState::throwEvalError(const PosIdx pos, const char * s, const std::string & s2,
const std::string & s3, Env & env, Expr & expr)
{
debugThrow(EvalError({
.msg = hintfmt(s, s2),
.errPos = positions[pos]
}), env, expr);
}
void EvalState::throwEvalError(const PosIdx p1, const char * s, const Symbol sym, const PosIdx p2, Env & env, Expr & expr)
{ {
// p1 is where the error occurred; p2 is a position mentioned in the message. // p1 is where the error occurred; p2 is a position mentioned in the message.
throw EvalError({ debugThrow(EvalError({
.msg = hintfmt(s, symbols[sym], positions[p2]), .msg = hintfmt(s, symbols[sym], positions[p2]),
.errPos = positions[p1] .errPos = positions[p1]
}); }), env, expr);
} }
void EvalState::throwTypeError(const PosIdx pos, const char * s) const void EvalState::throwTypeError(const PosIdx pos, const char * s, const Value & v)
{ {
throw TypeError({ debugThrowLastTrace(TypeError({
.msg = hintfmt(s, showType(v)),
.errPos = positions[pos]
}));
}
void EvalState::throwTypeError(const PosIdx pos, const char * s, const Value & v, Env & env, Expr & expr)
{
debugThrow(TypeError({
.msg = hintfmt(s, showType(v)),
.errPos = positions[pos]
}), env, expr);
}
void EvalState::throwTypeError(const PosIdx pos, const char * s)
{
debugThrowLastTrace(TypeError({
.msg = hintfmt(s), .msg = hintfmt(s),
.errPos = positions[pos] .errPos = positions[pos]
}); }));
} }
void EvalState::throwTypeError(const PosIdx pos, const char * s, const ExprLambda & fun, void EvalState::throwTypeError(const PosIdx pos, const char * s, const ExprLambda & fun,
const Symbol s2) const const Symbol s2, Env & env, Expr &expr)
{ {
throw TypeError({ debugThrow(TypeError({
.msg = hintfmt(s, fun.showNamePos(*this), symbols[s2]), .msg = hintfmt(s, fun.showNamePos(*this), symbols[s2]),
.errPos = positions[pos] .errPos = positions[pos]
}); }), env, expr);
} }
void EvalState::throwTypeError(const PosIdx pos, const Suggestions & suggestions, const char * s, void EvalState::throwTypeError(const PosIdx pos, const Suggestions & suggestions, const char * s,
const ExprLambda & fun, const Symbol s2) const const ExprLambda & fun, const Symbol s2, Env & env, Expr &expr)
{ {
throw TypeError(ErrorInfo { debugThrow(TypeError(ErrorInfo {
.msg = hintfmt(s, fun.showNamePos(*this), symbols[s2]), .msg = hintfmt(s, fun.showNamePos(*this), symbols[s2]),
.errPos = positions[pos], .errPos = positions[pos],
.suggestions = suggestions, .suggestions = suggestions,
}); }), env, expr);
} }
void EvalState::throwTypeError(const char * s, const Value & v, Env & env, Expr &expr)
void EvalState::throwTypeError(const char * s, const Value & v) const
{ {
throw TypeError(s, showType(v)); debugThrow(TypeError({
.msg = hintfmt(s, showType(v)),
.errPos = positions[expr.getPos()],
}), env, expr);
} }
void EvalState::throwAssertionError(const PosIdx pos, const char * s, const std::string & s1) const void EvalState::throwAssertionError(const PosIdx pos, const char * s, const std::string & s1, Env & env, Expr &expr)
{ {
throw AssertionError({ debugThrow(AssertionError({
.msg = hintfmt(s, s1), .msg = hintfmt(s, s1),
.errPos = positions[pos] .errPos = positions[pos]
}); }), env, expr);
} }
void EvalState::throwUndefinedVarError(const PosIdx pos, const char * s, const std::string & s1) const void EvalState::throwUndefinedVarError(const PosIdx pos, const char * s, const std::string & s1, Env & env, Expr &expr)
{ {
throw UndefinedVarError({ debugThrow(UndefinedVarError({
.msg = hintfmt(s, s1), .msg = hintfmt(s, s1),
.errPos = positions[pos] .errPos = positions[pos]
}); }), env, expr);
} }
void EvalState::throwMissingArgumentError(const PosIdx pos, const char * s, const std::string & s1) const void EvalState::throwMissingArgumentError(const PosIdx pos, const char * s, const std::string & s1, Env & env, Expr &expr)
{ {
throw MissingArgumentError({ debugThrow(MissingArgumentError({
.msg = hintfmt(s, s1), .msg = hintfmt(s, s1),
.errPos = positions[pos] .errPos = positions[pos]
}); }), env, expr);
} }
void EvalState::addErrorTrace(Error & e, const char * s, const std::string & s2) const void EvalState::addErrorTrace(Error & e, const char * s, const std::string & s2) const
@ -800,6 +964,32 @@ void EvalState::addErrorTrace(Error & e, const PosIdx pos, const char * s, const
e.addTrace(positions[pos], s, s2); e.addTrace(positions[pos], s, s2);
} }
static std::unique_ptr<DebugTraceStacker> makeDebugTraceStacker(
EvalState & state,
Expr & expr,
Env & env,
std::optional<ErrPos> pos,
const char * s,
const std::string & s2)
{
return std::make_unique<DebugTraceStacker>(state,
DebugTrace {
.pos = pos,
.expr = expr,
.env = env,
.hint = hintfmt(s, s2),
.isError = false
});
}
DebugTraceStacker::DebugTraceStacker(EvalState & evalState, DebugTrace t)
: evalState(evalState)
, trace(std::move(t))
{
evalState.debugTraces.push_front(trace);
if (evalState.debugStop && evalState.debugRepl)
evalState.runDebugRepl(nullptr, trace.env, trace.expr);
}
void Value::mkString(std::string_view s) void Value::mkString(std::string_view s)
{ {
@ -858,12 +1048,11 @@ inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval)
return j->value; return j->value;
} }
if (!env->prevWith) if (!env->prevWith)
throwUndefinedVarError(var.pos, "undefined variable '%1%'", symbols[var.name]); throwUndefinedVarError(var.pos, "undefined variable '%1%'", symbols[var.name], *env, const_cast<ExprVar&>(var));
for (size_t l = env->prevWith; l; --l, env = env->up) ; for (size_t l = env->prevWith; l; --l, env = env->up) ;
} }
} }
void EvalState::mkList(Value & v, size_t size) void EvalState::mkList(Value & v, size_t size)
{ {
v.mkList(size); v.mkList(size);
@ -976,6 +1165,15 @@ void EvalState::evalFile(const SourcePath & path, Value & v, bool mustBeTrivial)
fileParseCache[resolvedPath] = e; fileParseCache[resolvedPath] = e;
try { try {
auto dts = debugRepl
? makeDebugTraceStacker(
*this,
*e,
this->baseEnv,
e->getPos() ? std::optional(ErrPos(positions[e->getPos()])) : std::nullopt,
"while evaluating the file '%1%':", resolvedPath.to_string())
: nullptr;
// Enforce that 'flake.nix' is a direct attrset, not a // Enforce that 'flake.nix' is a direct attrset, not a
// computation. // computation.
if (mustBeTrivial && if (mustBeTrivial &&
@ -1010,7 +1208,7 @@ inline bool EvalState::evalBool(Env & env, Expr * e)
Value v; Value v;
e->eval(*this, env, v); e->eval(*this, env, v);
if (v.type() != nBool) if (v.type() != nBool)
throwTypeError("value is %1% while a Boolean was expected", v); throwTypeError(noPos, "value is %1% while a Boolean was expected", v, env, *e);
return v.boolean; return v.boolean;
} }
@ -1020,7 +1218,7 @@ inline bool EvalState::evalBool(Env & env, Expr * e, const PosIdx pos)
Value v; Value v;
e->eval(*this, env, v); e->eval(*this, env, v);
if (v.type() != nBool) if (v.type() != nBool)
throwTypeError(pos, "value is %1% while a Boolean was expected", v); throwTypeError(pos, "value is %1% while a Boolean was expected", v, env, *e);
return v.boolean; return v.boolean;
} }
@ -1029,7 +1227,7 @@ inline void EvalState::evalAttrs(Env & env, Expr * e, Value & v)
{ {
e->eval(*this, env, v); e->eval(*this, env, v);
if (v.type() != nAttrs) if (v.type() != nAttrs)
throwTypeError("value is %1% while a set was expected", v); throwTypeError(noPos, "value is %1% while a set was expected", v, env, *e);
} }
@ -1134,7 +1332,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
auto nameSym = state.symbols.create(nameVal.string.s); auto nameSym = state.symbols.create(nameVal.string.s);
Bindings::iterator j = v.attrs->find(nameSym); Bindings::iterator j = v.attrs->find(nameSym);
if (j != v.attrs->end()) if (j != v.attrs->end())
state.throwEvalError(i.pos, "dynamic attribute '%1%' already defined at %2%", nameSym, j->pos); state.throwEvalError(i.pos, "dynamic attribute '%1%' already defined at %2%", nameSym, j->pos, env, *this);
i.valueExpr->setName(nameSym); i.valueExpr->setName(nameSym);
/* Keep sorted order so find can catch duplicates */ /* Keep sorted order so find can catch duplicates */
@ -1208,6 +1406,15 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
e->eval(state, env, vTmp); e->eval(state, env, vTmp);
try { try {
auto dts = state.debugRepl
? makeDebugTraceStacker(
state,
*this,
env,
state.positions[pos2],
"while evaluating the attribute '%1%'",
showAttrPath(state, env, attrPath))
: nullptr;
for (auto & i : attrPath) { for (auto & i : attrPath) {
state.nrLookups++; state.nrLookups++;
@ -1230,7 +1437,7 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
state.throwEvalError( state.throwEvalError(
pos, pos,
Suggestions::bestMatches(allAttrNames, state.symbols[name]), Suggestions::bestMatches(allAttrNames, state.symbols[name]),
"attribute '%1%' missing", state.symbols[name]); "attribute '%1%' missing", state.symbols[name], env, *this);
} }
} }
vAttrs = j->value; vAttrs = j->value;
@ -1321,7 +1528,6 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
if (!lambda.hasFormals()) if (!lambda.hasFormals())
env2.values[displ++] = args[0]; env2.values[displ++] = args[0];
else { else {
forceAttrs(*args[0], pos); forceAttrs(*args[0], pos);
@ -1336,7 +1542,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
auto j = args[0]->attrs->get(i.name); auto j = args[0]->attrs->get(i.name);
if (!j) { if (!j) {
if (!i.def) throwTypeError(pos, "%1% called without required argument '%2%'", if (!i.def) throwTypeError(pos, "%1% called without required argument '%2%'",
lambda, i.name); lambda, i.name, *fun.lambda.env, lambda);
env2.values[displ++] = i.def->maybeThunk(*this, env2); env2.values[displ++] = i.def->maybeThunk(*this, env2);
} else { } else {
attrsUsed++; attrsUsed++;
@ -1358,8 +1564,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
pos, pos,
Suggestions::bestMatches(formalNames, symbols[i.name]), Suggestions::bestMatches(formalNames, symbols[i.name]),
"%1% called with unexpected argument '%2%'", "%1% called with unexpected argument '%2%'",
lambda, lambda, i.name, *fun.lambda.env, lambda);
i.name);
} }
abort(); // can't happen abort(); // can't happen
} }
@ -1370,6 +1575,15 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
/* Evaluate the body. */ /* Evaluate the body. */
try { try {
auto dts = debugRepl
? makeDebugTraceStacker(
*this, *lambda.body, env2, positions[lambda.pos],
"while evaluating %s",
lambda.name
? concatStrings("'", symbols[lambda.name], "'")
: "anonymous lambda")
: nullptr;
lambda.body->eval(*this, env2, vCur); lambda.body->eval(*this, env2, vCur);
} catch (Error & e) { } catch (Error & e) {
if (loggerSettings.showTrace.get()) { if (loggerSettings.showTrace.get()) {
@ -1524,8 +1738,8 @@ void EvalState::autoCallFunction(Bindings & args, Value & fun, Value & res)
Nix attempted to evaluate a function as a top level expression; in Nix attempted to evaluate a function as a top level expression; in
this case it must have its arguments supplied either by default this case it must have its arguments supplied either by default
values, or passed explicitly with '--arg' or '--argstr'. See values, or passed explicitly with '--arg' or '--argstr'. See
https://nixos.org/manual/nix/stable/expressions/language-constructs.html#functions.)", symbols[i.name]); https://nixos.org/manual/nix/stable/expressions/language-constructs.html#functions.)", symbols[i.name],
*fun.lambda.env, *fun.lambda.fun);
} }
} }
} }
@ -1557,7 +1771,7 @@ void ExprAssert::eval(EvalState & state, Env & env, Value & v)
if (!state.evalBool(env, cond, pos)) { if (!state.evalBool(env, cond, pos)) {
std::ostringstream out; std::ostringstream out;
cond->show(state.symbols, out); cond->show(state.symbols, out);
state.throwAssertionError(pos, "assertion '%1%' failed", out.str()); state.throwAssertionError(pos, "assertion '%1%' failed", out.str(), env, *this);
} }
body->eval(state, env, v); body->eval(state, env, v);
} }
@ -1743,14 +1957,14 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
nf = n; nf = n;
nf += vTmp->fpoint; nf += vTmp->fpoint;
} else } else
state.throwEvalError(i_pos, "cannot add %1% to an integer", showType(*vTmp)); state.throwEvalError(i_pos, "cannot add %1% to an integer", showType(*vTmp), env, *this);
} else if (firstType == nFloat) { } else if (firstType == nFloat) {
if (vTmp->type() == nInt) { if (vTmp->type() == nInt) {
nf += vTmp->integer; nf += vTmp->integer;
} else if (vTmp->type() == nFloat) { } else if (vTmp->type() == nFloat) {
nf += vTmp->fpoint; nf += vTmp->fpoint;
} else } else
state.throwEvalError(i_pos, "cannot add %1% to a float", showType(*vTmp)); state.throwEvalError(i_pos, "cannot add %1% to a float", showType(*vTmp), env, *this);
} else { } else {
if (s.empty()) s.reserve(es->size()); if (s.empty()) s.reserve(es->size());
auto part = state.coerceToString(i_pos, *vTmp, context, false, firstType == nString); auto part = state.coerceToString(i_pos, *vTmp, context, false, firstType == nString);
@ -1767,7 +1981,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
v.mkFloat(nf); v.mkFloat(nf);
else if (firstType == nPath) { else if (firstType == nPath) {
if (!context.empty()) if (!context.empty())
state.throwEvalError(pos, "a string that refers to a store path cannot be appended to a path"); state.throwEvalError(pos, "a string that refers to a store path cannot be appended to a path", env, *this);
v.mkPath({.accessor = *accessor, .path = CanonPath(str())}); v.mkPath({.accessor = *accessor, .path = CanonPath(str())});
} else } else
v.mkStringMove(c_str(), context); v.mkStringMove(c_str(), context);
@ -1794,6 +2008,12 @@ void EvalState::forceValueDeep(Value & v)
if (v.type() == nAttrs) { if (v.type() == nAttrs) {
for (auto & i : *v.attrs) for (auto & i : *v.attrs)
try { try {
// If the value is a thunk, we're evaling. Otherwise no trace necessary.
auto dts = debugRepl && i.value->isThunk()
? makeDebugTraceStacker(*this, *i.value->thunk.expr, *i.value->thunk.env, positions[i.pos],
"while evaluating the attribute '%1%'", symbols[i.name])
: nullptr;
recurse(*i.value); recurse(*i.value);
} catch (Error & e) { } catch (Error & e) {
addErrorTrace(e, i.pos, "while evaluating the attribute '%1%'", symbols[i.name]); addErrorTrace(e, i.pos, "while evaluating the attribute '%1%'", symbols[i.name]);
@ -1816,6 +2036,7 @@ NixInt EvalState::forceInt(Value & v, const PosIdx pos)
forceValue(v, pos); forceValue(v, pos);
if (v.type() != nInt) if (v.type() != nInt)
throwTypeError(pos, "value is %1% while an integer was expected", v); throwTypeError(pos, "value is %1% while an integer was expected", v);
return v.integer; return v.integer;
} }
@ -1858,10 +2079,7 @@ std::string_view EvalState::forceString(Value & v, const PosIdx pos)
{ {
forceValue(v, pos); forceValue(v, pos);
if (v.type() != nString) { if (v.type() != nString) {
if (pos) throwTypeError(pos, "value is %1% while a string was expected", v);
throwTypeError(pos, "value is %1% while a string was expected", v);
else
throwTypeError("value is %1% while a string was expected", v);
} }
return v.string.s; return v.string.s;
} }
@ -1976,7 +2194,8 @@ BackedStringView EvalState::coerceToString(const PosIdx pos, Value & v, PathSet
if (maybeString) if (maybeString)
return std::move(*maybeString); return std::move(*maybeString);
auto i = v.attrs->find(sOutPath); auto i = v.attrs->find(sOutPath);
if (i == v.attrs->end()) throwTypeError(pos, "cannot coerce a set to a string"); if (i == v.attrs->end())
throwTypeError(pos, "cannot coerce a set to a string");
return coerceToString(pos, *i->value, context, coerceMore, copyToStore); return coerceToString(pos, *i->value, context, coerceMore, copyToStore);
} }
@ -1984,7 +2203,6 @@ BackedStringView EvalState::coerceToString(const PosIdx pos, Value & v, PathSet
return v.external->coerceToString(positions[pos], context, coerceMore, copyToStore); return v.external->coerceToString(positions[pos], context, coerceMore, copyToStore);
if (coerceMore) { if (coerceMore) {
/* Note that `false' is represented as an empty string for /* Note that `false' is represented as an empty string for
shell scripting convenience, just like `null'. */ shell scripting convenience, just like `null'. */
if (v.type() == nBool && v.boolean) return "1"; if (v.type() == nBool && v.boolean) return "1";
@ -2151,7 +2369,9 @@ bool EvalState::eqValues(Value & v1, Value & v2)
return v1.fpoint == v2.fpoint; return v1.fpoint == v2.fpoint;
default: default:
throwEvalError("cannot compare %1% with %2%", showType(v1), showType(v2)); throwEvalError("cannot compare %1% with %2%",
showType(v1),
showType(v2));
} }
} }

View file

@ -14,7 +14,6 @@
#include <unordered_map> #include <unordered_map>
#include <mutex> #include <mutex>
namespace nix { namespace nix {
@ -27,7 +26,6 @@ enum RepairFlag : bool;
typedef void (* PrimOpFun) (EvalState & state, const PosIdx pos, Value * * args, Value & v); typedef void (* PrimOpFun) (EvalState & state, const PosIdx pos, Value * * args, Value & v);
struct PrimOp struct PrimOp
{ {
PrimOpFun fun; PrimOpFun fun;
@ -37,6 +35,11 @@ struct PrimOp
const char * doc = nullptr; const char * doc = nullptr;
}; };
#if HAVE_BOEHMGC
typedef std::map<std::string, Value *, std::less<std::string>, traceable_allocator<std::pair<const std::string, Value *> > > ValMap;
#else
typedef std::map<std::string, Value *> ValMap;
#endif
struct Env struct Env
{ {
@ -46,6 +49,10 @@ struct Env
Value * values[0]; Value * values[0];
}; };
void printEnvBindings(const EvalState &es, const Expr & expr, const Env & env);
void printEnvBindings(const SymbolTable & st, const StaticEnv & se, const Env & env, int lvl = 0);
std::unique_ptr<ValMap> mapStaticEnvBindings(const SymbolTable & st, const StaticEnv & se, const Env & env);
void copyContext(const Value & v, PathSet & context); void copyContext(const Value & v, PathSet & context);
@ -68,8 +75,17 @@ struct RegexCache;
std::shared_ptr<RegexCache> makeRegexCache(); std::shared_ptr<RegexCache> makeRegexCache();
struct DebugTrace {
std::optional<ErrPos> pos;
const Expr & expr;
const Env & env;
hintformat hint;
bool isError;
};
class EvalState void debugError(Error * e, Env & env, Expr & expr);
class EvalState : public std::enable_shared_from_this<EvalState>
{ {
public: public:
SymbolTable symbols; SymbolTable symbols;
@ -108,6 +124,49 @@ public:
RootValue vCallFlake = nullptr; RootValue vCallFlake = nullptr;
RootValue vImportedDrvToDerivation = nullptr; RootValue vImportedDrvToDerivation = nullptr;
/* Debugger */
void (* debugRepl)(ref<EvalState> es, const ValMap & extraEnv);
bool debugStop;
bool debugQuit;
std::list<DebugTrace> debugTraces;
std::map<const Expr*, const std::shared_ptr<const StaticEnv>> exprEnvs;
const std::shared_ptr<const StaticEnv> getStaticEnv(const Expr & expr) const
{
auto i = exprEnvs.find(&expr);
if (i != exprEnvs.end())
return i->second;
else
return std::shared_ptr<const StaticEnv>();;
}
void runDebugRepl(const Error * error, const Env & env, const Expr & expr);
template<class E>
[[gnu::noinline, gnu::noreturn]]
void debugThrow(E && error, const Env & env, const Expr & expr)
{
if (debugRepl)
runDebugRepl(&error, env, expr);
throw error;
}
template<class E>
[[gnu::noinline, gnu::noreturn]]
void debugThrowLastTrace(E && e)
{
// Call this in the situation where Expr and Env are inaccessible.
// The debugger will start in the last context that's in the
// DebugTrace stack.
if (debugRepl && !debugTraces.empty()) {
const DebugTrace & last = debugTraces.front();
runDebugRepl(&e, last.env, last.expr);
}
throw e;
}
private: private:
/* Cache for calls to addToStore(); maps source paths to the store /* Cache for calls to addToStore(); maps source paths to the store
@ -124,7 +183,7 @@ private:
/* A cache from path names to values. */ /* A cache from path names to values. */
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
typedef std::map<SourcePath, Value, std::less<SourcePath>, traceable_allocator<std::pair<const SourcePath, Value> >> FileEvalCache; typedef std::map<SourcePath, Value, std::less<SourcePath>, traceable_allocator<std::pair<const SourcePath, Value>>> FileEvalCache;
#else #else
typedef std::map<SourcePath, Value> FileEvalCache; typedef std::map<SourcePath, Value> FileEvalCache;
#endif #endif
@ -184,10 +243,10 @@ public:
/* Parse a Nix expression from the specified file. */ /* Parse a Nix expression from the specified file. */
Expr * parseExprFromFile(const SourcePath & path); Expr * parseExprFromFile(const SourcePath & path);
Expr * parseExprFromFile(const SourcePath & path, StaticEnv & staticEnv); Expr * parseExprFromFile(const SourcePath & path, std::shared_ptr<StaticEnv> & staticEnv);
/* Parse a Nix expression from the specified string. */ /* Parse a Nix expression from the specified string. */
Expr * parseExprFromString(std::string s, const SourcePath & basePath, StaticEnv & staticEnv); Expr * parseExprFromString(std::string s, const SourcePath & basePath, std::shared_ptr<StaticEnv> & staticEnv);
Expr * parseExprFromString(std::string s, const SourcePath & basePath); Expr * parseExprFromString(std::string s, const SourcePath & basePath);
Expr * parseStdin(); Expr * parseStdin();
@ -248,35 +307,70 @@ public:
std::string_view forceStringNoCtx(Value & v, const PosIdx pos = noPos); std::string_view forceStringNoCtx(Value & v, const PosIdx pos = noPos);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwEvalError(const PosIdx pos, const char * s) const; void throwEvalError(const PosIdx pos, const char * s);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwTypeError(const PosIdx pos, const char * s, const Value & v) const; void throwEvalError(const PosIdx pos, const char * s,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwEvalError(const PosIdx pos, const Suggestions & suggestions, const char * s, void throwEvalError(const char * s, const std::string & s2);
const std::string & s2) const; [[gnu::noinline, gnu::noreturn]]
void throwEvalError(const PosIdx pos, const char * s, std::string_view s2);
[[gnu::noinline, gnu::noreturn]]
void throwEvalError(const char * s, const std::string & s2,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]]
void throwEvalError(const PosIdx pos, const char * s, const std::string & s2,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]]
void throwEvalError(const char * s, const std::string & s2, const std::string & s3,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwEvalError(const PosIdx pos, const char * s, std::string_view s2) const; void throwEvalError(const PosIdx pos, const char * s, std::string_view s2) const;
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwEvalError(const char * s, const std::string & s2, const std::string & s3) const; void throwEvalError(const PosIdx pos, const char * s, const std::string & s2, const std::string & s3,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwEvalError(const PosIdx pos, const char * s, const std::string & s2, const std::string & s3) const; void throwEvalError(const PosIdx pos, const char * s, const std::string & s2, const std::string & s3);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwEvalError(const PosIdx p1, const char * s, const Symbol sym, const PosIdx p2) const; void throwEvalError(const char * s, const std::string & s2, const std::string & s3);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwTypeError(const PosIdx pos, const char * s) const; void throwEvalError(const PosIdx pos, const Suggestions & suggestions, const char * s, const std::string & s2,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwTypeError(const PosIdx pos, const char * s, const ExprLambda & fun, const Symbol s2) const; void throwEvalError(const PosIdx p1, const char * s, const Symbol sym, const PosIdx p2,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwTypeError(const PosIdx pos, const Suggestions & suggestions, const char * s, void throwTypeError(const PosIdx pos, const char * s, const Value & v);
const ExprLambda & fun, const Symbol s2) const;
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwTypeError(const char * s, const Value & v) const; void throwTypeError(const PosIdx pos, const char * s, const Value & v,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwAssertionError(const PosIdx pos, const char * s, const std::string & s1) const; void throwTypeError(const PosIdx pos, const char * s);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwUndefinedVarError(const PosIdx pos, const char * s, const std::string & s1) const; void throwTypeError(const PosIdx pos, const char * s,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]] [[gnu::noinline, gnu::noreturn]]
void throwMissingArgumentError(const PosIdx pos, const char * s, const std::string & s1) const; void throwTypeError(const PosIdx pos, const char * s, const ExprLambda & fun, const Symbol s2,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]]
void throwTypeError(const PosIdx pos, const Suggestions & suggestions, const char * s, const ExprLambda & fun, const Symbol s2,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]]
void throwTypeError(const char * s, const Value & v,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]]
void throwAssertionError(const PosIdx pos, const char * s, const std::string & s1,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]]
void throwUndefinedVarError(const PosIdx pos, const char * s, const std::string & s1,
Env & env, Expr & expr);
[[gnu::noinline, gnu::noreturn]]
void throwMissingArgumentError(const PosIdx pos, const char * s, const std::string & s1,
Env & env, Expr & expr);
[[gnu::noinline]] [[gnu::noinline]]
void addErrorTrace(Error & e, const char * s, const std::string & s2) const; void addErrorTrace(Error & e, const char * s, const std::string & s2) const;
@ -315,7 +409,7 @@ public:
Env & baseEnv; Env & baseEnv;
/* The same, but used during parsing to resolve variables. */ /* The same, but used during parsing to resolve variables. */
StaticEnv staticBaseEnv; // !!! should be private std::shared_ptr<StaticEnv> staticBaseEnv; // !!! should be private
private: private:
@ -361,7 +455,7 @@ private:
FileOrigin origin, FileOrigin origin,
const PathView path, const PathView path,
const SourcePath & basePath, const SourcePath & basePath,
StaticEnv & staticEnv); std::shared_ptr<StaticEnv> & staticEnv);
public: public:
@ -456,6 +550,16 @@ private:
friend struct Value; friend struct Value;
}; };
struct DebugTraceStacker {
DebugTraceStacker(EvalState & evalState, DebugTrace t);
~DebugTraceStacker()
{
// assert(evalState.debugTraces.front() == trace);
evalState.debugTraces.pop_front();
}
EvalState & evalState;
DebugTrace trace;
};
/* Return a string representing the type of the value `v'. */ /* Return a string representing the type of the value `v'. */
std::string_view showType(ValueType type); std::string_view showType(ValueType type);

View file

@ -31,7 +31,7 @@ static void writeTrustedList(const TrustedList & trustedList)
void ConfigFile::apply() void ConfigFile::apply()
{ {
std::set<std::string> whitelist{"bash-prompt", "bash-prompt-suffix", "flake-registry"}; std::set<std::string> whitelist{"bash-prompt", "bash-prompt-prefix", "bash-prompt-suffix", "flake-registry"};
for (auto & [name, value] : settings) { for (auto & [name, value] : settings) {

View file

@ -753,6 +753,7 @@ static void prim_getFlake(EvalState & state, const PosIdx pos, Value * * args, V
lockFlake(state, flakeRef, lockFlake(state, flakeRef,
LockFlags { LockFlags {
.updateLockFile = false, .updateLockFile = false,
.writeLockFile = false,
.useRegistries = !evalSettings.pureEval && fetchSettings.useRegistries, .useRegistries = !evalSettings.pureEval && fetchSettings.useRegistries,
.allowMutable = !evalSettings.pureEval, .allowMutable = !evalSettings.pureEval,
}), }),

View file

@ -73,7 +73,7 @@ public:
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
typedef std::list<DrvInfo, traceable_allocator<DrvInfo> > DrvInfos; typedef std::list<DrvInfo, traceable_allocator<DrvInfo>> DrvInfos;
#else #else
typedef std::list<DrvInfo> DrvInfos; typedef std::list<DrvInfo> DrvInfos;
#endif #endif

View file

@ -198,7 +198,7 @@ or { return OR_KW; }
(...|\$[^\{\"\\]|\\.|\$\\.)+ would have triggered. (...|\$[^\{\"\\]|\\.|\$\\.)+ would have triggered.
This is technically invalid, but we leave the problem to the This is technically invalid, but we leave the problem to the
parser who fails with exact location. */ parser who fails with exact location. */
return STR; return EOF;
} }
\'\'(\ *\n)? { PUSH_STATE(IND_STRING); return IND_STRING_OPEN; } \'\'(\ *\n)? { PUSH_STATE(IND_STRING); return IND_STRING_OPEN; }

View file

@ -6,10 +6,8 @@
#include <cstdlib> #include <cstdlib>
namespace nix { namespace nix {
/* Displaying abstract syntax trees. */ /* Displaying abstract syntax trees. */
static void showString(std::ostream & str, std::string_view s) static void showString(std::ostream & str, std::string_view s)
@ -294,35 +292,46 @@ std::string showAttrPath(const SymbolTable & symbols, const AttrPath & attrPath)
/* Computing levels/displacements for variables. */ /* Computing levels/displacements for variables. */
void Expr::bindVars(const EvalState & es, const StaticEnv & env) void Expr::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
abort(); abort();
} }
void ExprInt::bindVars(const EvalState & es, const StaticEnv & env) void ExprInt::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
} }
void ExprFloat::bindVars(const EvalState & es, const StaticEnv & env) void ExprFloat::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
} }
void ExprString::bindVars(const EvalState & es, const StaticEnv & env) void ExprString::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
} }
void ExprPath::bindVars(const EvalState & es, const StaticEnv & env) void ExprPath::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
} }
void ExprVar::bindVars(const EvalState & es, const StaticEnv & env) void ExprVar::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
/* Check whether the variable appears in the environment. If so, /* Check whether the variable appears in the environment. If so,
set its level and displacement. */ set its level and displacement. */
const StaticEnv * curEnv; const StaticEnv * curEnv;
Level level; Level level;
int withLevel = -1; int withLevel = -1;
for (curEnv = &env, level = 0; curEnv; curEnv = curEnv->up, level++) { for (curEnv = env.get(), level = 0; curEnv; curEnv = curEnv->up, level++) {
if (curEnv->isWith) { if (curEnv->isWith) {
if (withLevel == -1) withLevel = level; if (withLevel == -1) withLevel = level;
} else { } else {
@ -348,8 +357,11 @@ void ExprVar::bindVars(const EvalState & es, const StaticEnv & env)
this->level = withLevel; this->level = withLevel;
} }
void ExprSelect::bindVars(const EvalState & es, const StaticEnv & env) void ExprSelect::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
e->bindVars(es, env); e->bindVars(es, env);
if (def) def->bindVars(es, env); if (def) def->bindVars(es, env);
for (auto & i : attrPath) for (auto & i : attrPath)
@ -357,64 +369,78 @@ void ExprSelect::bindVars(const EvalState & es, const StaticEnv & env)
i.expr->bindVars(es, env); i.expr->bindVars(es, env);
} }
void ExprOpHasAttr::bindVars(const EvalState & es, const StaticEnv & env) void ExprOpHasAttr::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
e->bindVars(es, env); e->bindVars(es, env);
for (auto & i : attrPath) for (auto & i : attrPath)
if (!i.symbol) if (!i.symbol)
i.expr->bindVars(es, env); i.expr->bindVars(es, env);
} }
void ExprAttrs::bindVars(const EvalState & es, const StaticEnv & env) void ExprAttrs::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
const StaticEnv * dynamicEnv = &env; if (es.debugRepl)
StaticEnv newEnv(false, &env, recursive ? attrs.size() : 0); es.exprEnvs.insert(std::make_pair(this, env));
if (recursive) { if (recursive) {
dynamicEnv = &newEnv; auto newEnv = std::make_shared<StaticEnv>(false, env.get(), recursive ? attrs.size() : 0);
Displacement displ = 0; Displacement displ = 0;
for (auto & i : attrs) for (auto & i : attrs)
newEnv.vars.emplace_back(i.first, i.second.displ = displ++); newEnv->vars.emplace_back(i.first, i.second.displ = displ++);
// No need to sort newEnv since attrs is in sorted order. // No need to sort newEnv since attrs is in sorted order.
for (auto & i : attrs) for (auto & i : attrs)
i.second.e->bindVars(es, i.second.inherited ? env : newEnv); i.second.e->bindVars(es, i.second.inherited ? env : newEnv);
}
else for (auto & i : dynamicAttrs) {
i.nameExpr->bindVars(es, newEnv);
i.valueExpr->bindVars(es, newEnv);
}
}
else {
for (auto & i : attrs) for (auto & i : attrs)
i.second.e->bindVars(es, env); i.second.e->bindVars(es, env);
for (auto & i : dynamicAttrs) { for (auto & i : dynamicAttrs) {
i.nameExpr->bindVars(es, *dynamicEnv); i.nameExpr->bindVars(es, env);
i.valueExpr->bindVars(es, *dynamicEnv); i.valueExpr->bindVars(es, env);
}
} }
} }
void ExprList::bindVars(const EvalState & es, const StaticEnv & env) void ExprList::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
for (auto & i : elems) for (auto & i : elems)
i->bindVars(es, env); i->bindVars(es, env);
} }
void ExprLambda::bindVars(const EvalState & es, const StaticEnv & env) void ExprLambda::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
StaticEnv newEnv( if (es.debugRepl)
false, &env, es.exprEnvs.insert(std::make_pair(this, env));
auto newEnv = std::make_shared<StaticEnv>(
false, env.get(),
(hasFormals() ? formals->formals.size() : 0) + (hasFormals() ? formals->formals.size() : 0) +
(!arg ? 0 : 1)); (!arg ? 0 : 1));
Displacement displ = 0; Displacement displ = 0;
if (arg) newEnv.vars.emplace_back(arg, displ++); if (arg) newEnv->vars.emplace_back(arg, displ++);
if (hasFormals()) { if (hasFormals()) {
for (auto & i : formals->formals) for (auto & i : formals->formals)
newEnv.vars.emplace_back(i.name, displ++); newEnv->vars.emplace_back(i.name, displ++);
newEnv.sort(); newEnv->sort();
for (auto & i : formals->formals) for (auto & i : formals->formals)
if (i.def) i.def->bindVars(es, newEnv); if (i.def) i.def->bindVars(es, newEnv);
@ -423,20 +449,26 @@ void ExprLambda::bindVars(const EvalState & es, const StaticEnv & env)
body->bindVars(es, newEnv); body->bindVars(es, newEnv);
} }
void ExprCall::bindVars(const EvalState & es, const StaticEnv & env) void ExprCall::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
fun->bindVars(es, env); fun->bindVars(es, env);
for (auto e : args) for (auto e : args)
e->bindVars(es, env); e->bindVars(es, env);
} }
void ExprLet::bindVars(const EvalState & es, const StaticEnv & env) void ExprLet::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
StaticEnv newEnv(false, &env, attrs->attrs.size()); if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
auto newEnv = std::make_shared<StaticEnv>(false, env.get(), attrs->attrs.size());
Displacement displ = 0; Displacement displ = 0;
for (auto & i : attrs->attrs) for (auto & i : attrs->attrs)
newEnv.vars.emplace_back(i.first, i.second.displ = displ++); newEnv->vars.emplace_back(i.first, i.second.displ = displ++);
// No need to sort newEnv since attrs->attrs is in sorted order. // No need to sort newEnv since attrs->attrs is in sorted order.
@ -446,51 +478,71 @@ void ExprLet::bindVars(const EvalState & es, const StaticEnv & env)
body->bindVars(es, newEnv); body->bindVars(es, newEnv);
} }
void ExprWith::bindVars(const EvalState & es, const StaticEnv & env) void ExprWith::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
/* Does this `with' have an enclosing `with'? If so, record its /* Does this `with' have an enclosing `with'? If so, record its
level so that `lookupVar' can look up variables in the previous level so that `lookupVar' can look up variables in the previous
`with' if this one doesn't contain the desired attribute. */ `with' if this one doesn't contain the desired attribute. */
const StaticEnv * curEnv; const StaticEnv * curEnv;
Level level; Level level;
prevWith = 0; prevWith = 0;
for (curEnv = &env, level = 1; curEnv; curEnv = curEnv->up, level++) for (curEnv = env.get(), level = 1; curEnv; curEnv = curEnv->up, level++)
if (curEnv->isWith) { if (curEnv->isWith) {
prevWith = level; prevWith = level;
break; break;
} }
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
attrs->bindVars(es, env); attrs->bindVars(es, env);
StaticEnv newEnv(true, &env); auto newEnv = std::make_shared<StaticEnv>(true, env.get());
body->bindVars(es, newEnv); body->bindVars(es, newEnv);
} }
void ExprIf::bindVars(const EvalState & es, const StaticEnv & env) void ExprIf::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
cond->bindVars(es, env); cond->bindVars(es, env);
then->bindVars(es, env); then->bindVars(es, env);
else_->bindVars(es, env); else_->bindVars(es, env);
} }
void ExprAssert::bindVars(const EvalState & es, const StaticEnv & env) void ExprAssert::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
cond->bindVars(es, env); cond->bindVars(es, env);
body->bindVars(es, env); body->bindVars(es, env);
} }
void ExprOpNot::bindVars(const EvalState & es, const StaticEnv & env) void ExprOpNot::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
e->bindVars(es, env); e->bindVars(es, env);
} }
void ExprConcatStrings::bindVars(const EvalState & es, const StaticEnv & env) void ExprConcatStrings::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
for (auto & i : *this->es) for (auto & i : *this->es)
i.second->bindVars(es, env); i.second->bindVars(es, env);
} }
void ExprPos::bindVars(const EvalState & es, const StaticEnv & env) void ExprPos::bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env)
{ {
if (es.debugRepl)
es.exprEnvs.insert(std::make_pair(this, env));
} }

View file

@ -21,7 +21,6 @@ MakeError(TypeError, EvalError);
MakeError(UndefinedVarError, Error); MakeError(UndefinedVarError, Error);
MakeError(MissingArgumentError, EvalError); MakeError(MissingArgumentError, EvalError);
/* Position objects. */ /* Position objects. */
struct Pos struct Pos
@ -142,24 +141,25 @@ struct Expr
{ {
virtual ~Expr() { }; virtual ~Expr() { };
virtual void show(const SymbolTable & symbols, std::ostream & str) const; virtual void show(const SymbolTable & symbols, std::ostream & str) const;
virtual void bindVars(const EvalState & es, const StaticEnv & env); virtual void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env);
virtual void eval(EvalState & state, Env & env, Value & v); virtual void eval(EvalState & state, Env & env, Value & v);
virtual Value * maybeThunk(EvalState & state, Env & env); virtual Value * maybeThunk(EvalState & state, Env & env);
virtual void setName(Symbol name); virtual void setName(Symbol name);
virtual PosIdx getPos() const { return noPos; }
}; };
#define COMMON_METHODS \ #define COMMON_METHODS \
void show(const SymbolTable & symbols, std::ostream & str) const; \ void show(const SymbolTable & symbols, std::ostream & str) const; \
void eval(EvalState & state, Env & env, Value & v); \ void eval(EvalState & state, Env & env, Value & v); \
void bindVars(const EvalState & es, const StaticEnv & env); void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env);
struct ExprInt : Expr struct ExprInt : Expr
{ {
NixInt n; NixInt n;
Value v; Value v;
ExprInt(NixInt n) : n(n) { v.mkInt(n); }; ExprInt(NixInt n) : n(n) { v.mkInt(n); };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env); Value * maybeThunk(EvalState & state, Env & env);
COMMON_METHODS
}; };
struct ExprFloat : Expr struct ExprFloat : Expr
@ -167,8 +167,8 @@ struct ExprFloat : Expr
NixFloat nf; NixFloat nf;
Value v; Value v;
ExprFloat(NixFloat nf) : nf(nf) { v.mkFloat(nf); }; ExprFloat(NixFloat nf) : nf(nf) { v.mkFloat(nf); };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env); Value * maybeThunk(EvalState & state, Env & env);
COMMON_METHODS
}; };
struct ExprString : Expr struct ExprString : Expr
@ -176,8 +176,8 @@ struct ExprString : Expr
std::string s; std::string s;
Value v; Value v;
ExprString(std::string s) : s(std::move(s)) { v.mkString(this->s.data()); }; ExprString(std::string s) : s(std::move(s)) { v.mkString(this->s.data()); };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env); Value * maybeThunk(EvalState & state, Env & env);
COMMON_METHODS
}; };
struct ExprPath : Expr struct ExprPath : Expr
@ -189,8 +189,8 @@ struct ExprPath : Expr
{ {
v.mkPath(&path.accessor, path.path.abs().data()); v.mkPath(&path.accessor, path.path.abs().data());
} }
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env); Value * maybeThunk(EvalState & state, Env & env);
COMMON_METHODS
}; };
typedef uint32_t Level; typedef uint32_t Level;
@ -216,8 +216,9 @@ struct ExprVar : Expr
ExprVar(Symbol name) : name(name) { }; ExprVar(Symbol name) : name(name) { };
ExprVar(const PosIdx & pos, Symbol name) : pos(pos), name(name) { }; ExprVar(const PosIdx & pos, Symbol name) : pos(pos), name(name) { };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env); Value * maybeThunk(EvalState & state, Env & env);
PosIdx getPos() const override { return pos; }
COMMON_METHODS
}; };
struct ExprSelect : Expr struct ExprSelect : Expr
@ -227,6 +228,7 @@ struct ExprSelect : Expr
AttrPath attrPath; AttrPath attrPath;
ExprSelect(const PosIdx & pos, Expr * e, const AttrPath & attrPath, Expr * def) : pos(pos), e(e), def(def), attrPath(attrPath) { }; ExprSelect(const PosIdx & pos, Expr * e, const AttrPath & attrPath, Expr * def) : pos(pos), e(e), def(def), attrPath(attrPath) { };
ExprSelect(const PosIdx & pos, Expr * e, Symbol name) : pos(pos), e(e), def(0) { attrPath.push_back(AttrName(name)); }; ExprSelect(const PosIdx & pos, Expr * e, Symbol name) : pos(pos), e(e), def(0) { attrPath.push_back(AttrName(name)); };
PosIdx getPos() const override { return pos; }
COMMON_METHODS COMMON_METHODS
}; };
@ -235,6 +237,7 @@ struct ExprOpHasAttr : Expr
Expr * e; Expr * e;
AttrPath attrPath; AttrPath attrPath;
ExprOpHasAttr(Expr * e, const AttrPath & attrPath) : e(e), attrPath(attrPath) { }; ExprOpHasAttr(Expr * e, const AttrPath & attrPath) : e(e), attrPath(attrPath) { };
PosIdx getPos() const override { return e->getPos(); }
COMMON_METHODS COMMON_METHODS
}; };
@ -263,6 +266,7 @@ struct ExprAttrs : Expr
DynamicAttrDefs dynamicAttrs; DynamicAttrDefs dynamicAttrs;
ExprAttrs(const PosIdx &pos) : recursive(false), pos(pos) { }; ExprAttrs(const PosIdx &pos) : recursive(false), pos(pos) { };
ExprAttrs() : recursive(false) { }; ExprAttrs() : recursive(false) { };
PosIdx getPos() const override { return pos; }
COMMON_METHODS COMMON_METHODS
}; };
@ -271,6 +275,11 @@ struct ExprList : Expr
std::vector<Expr *> elems; std::vector<Expr *> elems;
ExprList() { }; ExprList() { };
COMMON_METHODS COMMON_METHODS
PosIdx getPos() const override
{
return elems.empty() ? noPos : elems.front()->getPos();
}
}; };
struct Formal struct Formal
@ -323,6 +332,7 @@ struct ExprLambda : Expr
void setName(Symbol name); void setName(Symbol name);
std::string showNamePos(const EvalState & state) const; std::string showNamePos(const EvalState & state) const;
inline bool hasFormals() const { return formals != nullptr; } inline bool hasFormals() const { return formals != nullptr; }
PosIdx getPos() const override { return pos; }
COMMON_METHODS COMMON_METHODS
}; };
@ -334,6 +344,7 @@ struct ExprCall : Expr
ExprCall(const PosIdx & pos, Expr * fun, std::vector<Expr *> && args) ExprCall(const PosIdx & pos, Expr * fun, std::vector<Expr *> && args)
: fun(fun), args(args), pos(pos) : fun(fun), args(args), pos(pos)
{ } { }
PosIdx getPos() const override { return pos; }
COMMON_METHODS COMMON_METHODS
}; };
@ -351,6 +362,7 @@ struct ExprWith : Expr
Expr * attrs, * body; Expr * attrs, * body;
size_t prevWith; size_t prevWith;
ExprWith(const PosIdx & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { }; ExprWith(const PosIdx & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { };
PosIdx getPos() const override { return pos; }
COMMON_METHODS COMMON_METHODS
}; };
@ -359,6 +371,7 @@ struct ExprIf : Expr
PosIdx pos; PosIdx pos;
Expr * cond, * then, * else_; Expr * cond, * then, * else_;
ExprIf(const PosIdx & pos, Expr * cond, Expr * then, Expr * else_) : pos(pos), cond(cond), then(then), else_(else_) { }; ExprIf(const PosIdx & pos, Expr * cond, Expr * then, Expr * else_) : pos(pos), cond(cond), then(then), else_(else_) { };
PosIdx getPos() const override { return pos; }
COMMON_METHODS COMMON_METHODS
}; };
@ -367,6 +380,7 @@ struct ExprAssert : Expr
PosIdx pos; PosIdx pos;
Expr * cond, * body; Expr * cond, * body;
ExprAssert(const PosIdx & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { }; ExprAssert(const PosIdx & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { };
PosIdx getPos() const override { return pos; }
COMMON_METHODS COMMON_METHODS
}; };
@ -388,11 +402,12 @@ struct ExprOpNot : Expr
{ \ { \
str << "("; e1->show(symbols, str); str << " " s " "; e2->show(symbols, str); str << ")"; \ str << "("; e1->show(symbols, str); str << " " s " "; e2->show(symbols, str); str << ")"; \
} \ } \
void bindVars(const EvalState & es, const StaticEnv & env) \ void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) \
{ \ { \
e1->bindVars(es, env); e2->bindVars(es, env); \ e1->bindVars(es, env); e2->bindVars(es, env); \
} \ } \
void eval(EvalState & state, Env & env, Value & v); \ void eval(EvalState & state, Env & env, Value & v); \
PosIdx getPos() const override { return pos; } \
}; };
MakeBinOp(ExprOpEq, "==") MakeBinOp(ExprOpEq, "==")
@ -407,9 +422,10 @@ struct ExprConcatStrings : Expr
{ {
PosIdx pos; PosIdx pos;
bool forceString; bool forceString;
std::vector<std::pair<PosIdx, Expr *> > * es; std::vector<std::pair<PosIdx, Expr *>> * es;
ExprConcatStrings(const PosIdx & pos, bool forceString, std::vector<std::pair<PosIdx, Expr *> > * es) ExprConcatStrings(const PosIdx & pos, bool forceString, std::vector<std::pair<PosIdx, Expr *>> * es)
: pos(pos), forceString(forceString), es(es) { }; : pos(pos), forceString(forceString), es(es) { };
PosIdx getPos() const override { return pos; }
COMMON_METHODS COMMON_METHODS
}; };
@ -417,6 +433,7 @@ struct ExprPos : Expr
{ {
PosIdx pos; PosIdx pos;
ExprPos(const PosIdx & pos) : pos(pos) { }; ExprPos(const PosIdx & pos) : pos(pos) { };
PosIdx getPos() const override { return pos; }
COMMON_METHODS COMMON_METHODS
}; };

View file

@ -188,7 +188,7 @@ static Formals * toFormals(ParseData & data, ParserFormals * formals,
static Expr * stripIndentation(const PosIdx pos, SymbolTable & symbols, static Expr * stripIndentation(const PosIdx pos, SymbolTable & symbols,
std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken> > > & es) std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>> & es)
{ {
if (es.empty()) return new ExprString(""); if (es.empty()) return new ExprString("");
@ -228,7 +228,7 @@ static Expr * stripIndentation(const PosIdx pos, SymbolTable & symbols,
} }
/* Strip spaces from each line. */ /* Strip spaces from each line. */
auto * es2 = new std::vector<std::pair<PosIdx, Expr *> >; auto * es2 = new std::vector<std::pair<PosIdx, Expr *>>;
atStartOfLine = true; atStartOfLine = true;
size_t curDropped = 0; size_t curDropped = 0;
size_t n = es.size(); size_t n = es.size();
@ -315,8 +315,8 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, const char * err
StringToken uri; StringToken uri;
StringToken str; StringToken str;
std::vector<nix::AttrName> * attrNames; std::vector<nix::AttrName> * attrNames;
std::vector<std::pair<nix::PosIdx, nix::Expr *> > * string_parts; std::vector<std::pair<nix::PosIdx, nix::Expr *>> * string_parts;
std::vector<std::pair<nix::PosIdx, std::variant<nix::Expr *, StringToken> > > * ind_string_parts; std::vector<std::pair<nix::PosIdx, std::variant<nix::Expr *, StringToken>>> * ind_string_parts;
} }
%type <e> start expr expr_function expr_if expr_op %type <e> start expr expr_function expr_if expr_op
@ -410,7 +410,7 @@ expr_op
| expr_op UPDATE expr_op { $$ = new ExprOpUpdate(CUR_POS, $1, $3); } | expr_op UPDATE expr_op { $$ = new ExprOpUpdate(CUR_POS, $1, $3); }
| expr_op '?' attrpath { $$ = new ExprOpHasAttr($1, *$3); } | expr_op '?' attrpath { $$ = new ExprOpHasAttr($1, *$3); }
| expr_op '+' expr_op | expr_op '+' expr_op
{ $$ = new ExprConcatStrings(CUR_POS, false, new std::vector<std::pair<PosIdx, Expr *> >({{makeCurPos(@1, data), $1}, {makeCurPos(@3, data), $3}})); } { $$ = new ExprConcatStrings(CUR_POS, false, new std::vector<std::pair<PosIdx, Expr *>>({{makeCurPos(@1, data), $1}, {makeCurPos(@3, data), $3}})); }
| expr_op '-' expr_op { $$ = new ExprCall(CUR_POS, new ExprVar(data->symbols.create("__sub")), {$1, $3}); } | expr_op '-' expr_op { $$ = new ExprCall(CUR_POS, new ExprVar(data->symbols.create("__sub")), {$1, $3}); }
| expr_op '*' expr_op { $$ = new ExprCall(CUR_POS, new ExprVar(data->symbols.create("__mul")), {$1, $3}); } | expr_op '*' expr_op { $$ = new ExprCall(CUR_POS, new ExprVar(data->symbols.create("__mul")), {$1, $3}); }
| expr_op '/' expr_op { $$ = new ExprCall(CUR_POS, new ExprVar(data->symbols.create("__div")), {$1, $3}); } | expr_op '/' expr_op { $$ = new ExprCall(CUR_POS, new ExprVar(data->symbols.create("__div")), {$1, $3}); }
@ -498,9 +498,9 @@ string_parts_interpolated
: string_parts_interpolated STR : string_parts_interpolated STR
{ $$ = $1; $1->emplace_back(makeCurPos(@2, data), new ExprString(std::string($2))); } { $$ = $1; $1->emplace_back(makeCurPos(@2, data), new ExprString(std::string($2))); }
| string_parts_interpolated DOLLAR_CURLY expr '}' { $$ = $1; $1->emplace_back(makeCurPos(@2, data), $3); } | string_parts_interpolated DOLLAR_CURLY expr '}' { $$ = $1; $1->emplace_back(makeCurPos(@2, data), $3); }
| DOLLAR_CURLY expr '}' { $$ = new std::vector<std::pair<PosIdx, Expr *> >; $$->emplace_back(makeCurPos(@1, data), $2); } | DOLLAR_CURLY expr '}' { $$ = new std::vector<std::pair<PosIdx, Expr *>>; $$->emplace_back(makeCurPos(@1, data), $2); }
| STR DOLLAR_CURLY expr '}' { | STR DOLLAR_CURLY expr '}' {
$$ = new std::vector<std::pair<PosIdx, Expr *> >; $$ = new std::vector<std::pair<PosIdx, Expr *>>;
$$->emplace_back(makeCurPos(@1, data), new ExprString(std::string($1))); $$->emplace_back(makeCurPos(@1, data), new ExprString(std::string($1)));
$$->emplace_back(makeCurPos(@2, data), $3); $$->emplace_back(makeCurPos(@2, data), $3);
} }
@ -525,7 +525,7 @@ path_start
ind_string_parts ind_string_parts
: ind_string_parts IND_STR { $$ = $1; $1->emplace_back(makeCurPos(@2, data), $2); } : ind_string_parts IND_STR { $$ = $1; $1->emplace_back(makeCurPos(@2, data), $2); }
| ind_string_parts DOLLAR_CURLY expr '}' { $$ = $1; $1->emplace_back(makeCurPos(@2, data), $3); } | ind_string_parts DOLLAR_CURLY expr '}' { $$ = $1; $1->emplace_back(makeCurPos(@2, data), $3); }
| { $$ = new std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken> > >; } | { $$ = new std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>>; }
; ;
binds binds
@ -639,8 +639,13 @@ formal
namespace nix { namespace nix {
Expr * EvalState::parse(char * text, size_t length, FileOrigin origin, Expr * EvalState::parse(
const PathView path, const SourcePath & basePath, StaticEnv & staticEnv) char * text,
size_t length,
FileOrigin origin,
const PathView path,
const SourcePath & basePath,
std::shared_ptr<StaticEnv> & staticEnv)
{ {
yyscan_t scanner; yyscan_t scanner;
std::string file; std::string file;
@ -696,7 +701,7 @@ Expr * EvalState::parseExprFromFile(const SourcePath & path)
} }
Expr * EvalState::parseExprFromFile(const SourcePath & path, StaticEnv & staticEnv) Expr * EvalState::parseExprFromFile(const SourcePath & path, std::shared_ptr<StaticEnv> & staticEnv)
{ {
auto buffer = path.readFile(); auto buffer = path.readFile();
// readFile hopefully have left some extra space for terminators // readFile hopefully have left some extra space for terminators
@ -706,7 +711,7 @@ Expr * EvalState::parseExprFromFile(const SourcePath & path, StaticEnv & staticE
} }
Expr * EvalState::parseExprFromString(std::string s, const SourcePath & basePath, StaticEnv & staticEnv) Expr * EvalState::parseExprFromString(std::string s, const SourcePath & basePath, std::shared_ptr<StaticEnv> & staticEnv)
{ {
s.append("\0\0", 2); s.append("\0\0", 2);
return parse(s.data(), s.size(), foString, "", basePath, staticEnv); return parse(s.data(), s.size(), foString, "", basePath, staticEnv);
@ -773,13 +778,13 @@ SourcePath EvalState::findFile(SearchPath & searchPath, const std::string_view p
if (hasPrefix(path, "nix/")) if (hasPrefix(path, "nix/"))
return {*corepkgsFS, CanonPath(path.substr(3))}; return {*corepkgsFS, CanonPath(path.substr(3))};
throw ThrownError({ debugThrowLastTrace(ThrownError({
.msg = hintfmt(evalSettings.pureEval .msg = hintfmt(evalSettings.pureEval
? "cannot look up '<%s>' in pure evaluation mode (use '--impure' to override)" ? "cannot look up '<%s>' in pure evaluation mode (use '--impure' to override)"
: "file '%s' was not found in the Nix search path (add it using $NIX_PATH or -I)", : "file '%s' was not found in the Nix search path (add it using $NIX_PATH or -I)",
path), path),
.errPos = positions[pos] .errPos = positions[pos]
}); }));
} }

View file

@ -46,7 +46,7 @@ StringMap EvalState::realiseContext(const PathSet & context)
auto [ctx, outputName] = decodeContext(*store, i); auto [ctx, outputName] = decodeContext(*store, i);
auto ctxS = store->printStorePath(ctx); auto ctxS = store->printStorePath(ctx);
if (!store->isValidPath(ctx)) if (!store->isValidPath(ctx))
throw InvalidPathError(store->printStorePath(ctx)); debugThrowLastTrace(InvalidPathError(store->printStorePath(ctx)));
if (!outputName.empty() && ctx.isDerivation()) { if (!outputName.empty() && ctx.isDerivation()) {
drvs.push_back({ctx, {outputName}}); drvs.push_back({ctx, {outputName}});
} else { } else {
@ -57,9 +57,9 @@ StringMap EvalState::realiseContext(const PathSet & context)
if (drvs.empty()) return {}; if (drvs.empty()) return {};
if (!evalSettings.enableImportFromDerivation) if (!evalSettings.enableImportFromDerivation)
throw Error( debugThrowLastTrace(Error(
"cannot build '%1%' during evaluation because the option 'allow-import-from-derivation' is disabled", "cannot build '%1%' during evaluation because the option 'allow-import-from-derivation' is disabled",
store->printStorePath(drvs.begin()->drvPath)); store->printStorePath(drvs.begin()->drvPath)));
/* Build/substitute the context. */ /* Build/substitute the context. */
std::vector<DerivedPath> buildReqs; std::vector<DerivedPath> buildReqs;
@ -72,8 +72,8 @@ StringMap EvalState::realiseContext(const PathSet & context)
for (auto & outputName : outputs) { for (auto & outputName : outputs) {
auto outputPath = get(outputPaths, outputName); auto outputPath = get(outputPaths, outputName);
if (!outputPath) if (!outputPath)
throw Error("derivation '%s' does not have an output named '%s'", debugThrowLastTrace(Error("derivation '%s' does not have an output named '%s'",
store->printStorePath(drvPath), outputName); store->printStorePath(drvPath), outputName));
res.insert_or_assign( res.insert_or_assign(
downstreamPlaceholder(*store, drvPath, outputName), downstreamPlaceholder(*store, drvPath, outputName),
store->printStorePath(*outputPath) store->printStorePath(*outputPath)
@ -213,11 +213,11 @@ static void import(EvalState & state, const PosIdx pos, Value & vPath, Value * v
Env * env = &state.allocEnv(vScope->attrs->size()); Env * env = &state.allocEnv(vScope->attrs->size());
env->up = &state.baseEnv; env->up = &state.baseEnv;
StaticEnv staticEnv(false, &state.staticBaseEnv, vScope->attrs->size()); auto staticEnv = std::make_shared<StaticEnv>(false, state.staticBaseEnv.get(), vScope->attrs->size());
unsigned int displ = 0; unsigned int displ = 0;
for (auto & attr : *vScope->attrs) { for (auto & attr : *vScope->attrs) {
staticEnv.vars.emplace_back(attr.name, displ); staticEnv->vars.emplace_back(attr.name, displ);
env->values[displ++] = attr.value; env->values[displ++] = attr.value;
} }
@ -319,17 +319,16 @@ void prim_importNative(EvalState & state, const PosIdx pos, Value * * args, Valu
void *handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_LOCAL); void *handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_LOCAL);
if (!handle) if (!handle)
throw EvalError("could not open '%1%': %2%", path, dlerror()); state.debugThrowLastTrace(EvalError("could not open '%1%': %2%", path, dlerror()));
dlerror(); dlerror();
ValueInitializer func = (ValueInitializer) dlsym(handle, sym.c_str()); ValueInitializer func = (ValueInitializer) dlsym(handle, sym.c_str());
if(!func) { if(!func) {
char *message = dlerror(); char *message = dlerror();
if (message) if (message)
throw EvalError("could not load symbol '%1%' from '%2%': %3%", sym, path, message); state.debugThrowLastTrace(EvalError("could not load symbol '%1%' from '%2%': %3%", sym, path, message));
else else
throw EvalError("symbol '%1%' from '%2%' resolved to NULL when a function pointer was expected", state.debugThrowLastTrace(EvalError("symbol '%1%' from '%2%' resolved to NULL when a function pointer was expected", sym, path));
sym, path);
} }
(func)(state, v); (func)(state, v);
@ -345,12 +344,11 @@ void prim_exec(EvalState & state, const PosIdx pos, Value * * args, Value & v)
state.forceList(*args[0], pos); state.forceList(*args[0], pos);
auto elems = args[0]->listElems(); auto elems = args[0]->listElems();
auto count = args[0]->listSize(); auto count = args[0]->listSize();
if (count == 0) { if (count == 0)
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("at least one argument to 'exec' required"), .msg = hintfmt("at least one argument to 'exec' required"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
}
PathSet context; PathSet context;
auto program = state.coerceToString(pos, *elems[0], context, false, false).toOwned(); auto program = state.coerceToString(pos, *elems[0], context, false, false).toOwned();
Strings commandArgs; Strings commandArgs;
@ -360,11 +358,11 @@ void prim_exec(EvalState & state, const PosIdx pos, Value * * args, Value & v)
try { try {
auto _ = state.realiseContext(context); // FIXME: Handle CA derivations auto _ = state.realiseContext(context); // FIXME: Handle CA derivations
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("cannot execute '%1%', since path '%2%' is not valid", .msg = hintfmt("cannot execute '%1%', since path '%2%' is not valid",
program, e.path), program, e.path),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
} }
auto output = runProgram(program, true, commandArgs); auto output = runProgram(program, true, commandArgs);
@ -548,7 +546,7 @@ struct CompareValues
if (v1->type() == nInt && v2->type() == nFloat) if (v1->type() == nInt && v2->type() == nFloat)
return v1->integer < v2->fpoint; return v1->integer < v2->fpoint;
if (v1->type() != v2->type()) if (v1->type() != v2->type())
throw EvalError("cannot compare %1% with %2%", showType(*v1), showType(*v2)); state.debugThrowLastTrace(EvalError("cannot compare %1% with %2%", showType(*v1), showType(*v2)));
switch (v1->type()) { switch (v1->type()) {
case nInt: case nInt:
return v1->integer < v2->integer; return v1->integer < v2->integer;
@ -571,14 +569,14 @@ struct CompareValues
} }
} }
default: default:
throw EvalError("cannot compare %1% with %2%", showType(*v1), showType(*v2)); state.debugThrowLastTrace(EvalError("cannot compare %1% with %2%", showType(*v1), showType(*v2)));
} }
} }
}; };
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
typedef std::list<Value *, gc_allocator<Value *> > ValueList; typedef std::list<Value *, gc_allocator<Value *>> ValueList;
#else #else
typedef std::list<Value *> ValueList; typedef std::list<Value *> ValueList;
#endif #endif
@ -601,10 +599,10 @@ static Bindings::iterator getAttr(
auto aPos = attrSet->pos; auto aPos = attrSet->pos;
if (!aPos) { if (!aPos) {
throw TypeError({ state.debugThrowLastTrace(TypeError({
.msg = errorMsg, .msg = errorMsg,
.errPos = state.positions[pos], .errPos = state.positions[pos],
}); }));
} else { } else {
auto e = TypeError({ auto e = TypeError({
.msg = errorMsg, .msg = errorMsg,
@ -614,7 +612,7 @@ static Bindings::iterator getAttr(
// Adding another trace for the function name to make it clear // Adding another trace for the function name to make it clear
// which call received wrong arguments. // which call received wrong arguments.
e.addTrace(state.positions[pos], hintfmt("while invoking '%s'", funcName)); e.addTrace(state.positions[pos], hintfmt("while invoking '%s'", funcName));
throw e; state.debugThrowLastTrace(e);
} }
} }
@ -668,10 +666,10 @@ static void prim_genericClosure(EvalState & state, const PosIdx pos, Value * * a
Bindings::iterator key = Bindings::iterator key =
e->attrs->find(state.sKey); e->attrs->find(state.sKey);
if (key == e->attrs->end()) if (key == e->attrs->end())
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("attribute 'key' required"), .msg = hintfmt("attribute 'key' required"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
state.forceValue(*key->value, pos); state.forceValue(*key->value, pos);
if (!doneKeys.insert(key->value).second) continue; if (!doneKeys.insert(key->value).second) continue;
@ -727,6 +725,41 @@ static RegisterPrimOp primop_genericClosure(RegisterPrimOp::Info {
.fun = prim_genericClosure, .fun = prim_genericClosure,
}); });
static RegisterPrimOp primop_break({
.name = "break",
.args = {"v"},
.doc = R"(
In debug mode (enabled using `--debugger`), pause Nix expression evaluation and enter the REPL.
Otherwise, return the argument `v`.
)",
.fun = [](EvalState & state, const PosIdx pos, Value * * args, Value & v)
{
if (state.debugRepl && !state.debugTraces.empty()) {
auto error = Error(ErrorInfo {
.level = lvlInfo,
.msg = hintfmt("breakpoint reached"),
.errPos = state.positions[pos],
});
auto & dt = state.debugTraces.front();
state.runDebugRepl(&error, dt.env, dt.expr);
if (state.debugQuit) {
// If the user elects to quit the repl, throw an exception.
throw Error(ErrorInfo{
.level = lvlInfo,
.msg = hintfmt("quit the debugger"),
.errPos = state.positions[noPos],
});
}
}
// Return the value we were passed.
v = *args[0];
}
});
static RegisterPrimOp primop_abort({ static RegisterPrimOp primop_abort({
.name = "abort", .name = "abort",
.args = {"s"}, .args = {"s"},
@ -737,7 +770,7 @@ static RegisterPrimOp primop_abort({
{ {
PathSet context; PathSet context;
auto s = state.coerceToString(pos, *args[0], context).toOwned(); auto s = state.coerceToString(pos, *args[0], context).toOwned();
throw Abort("evaluation aborted with the following error message: '%1%'", s); state.debugThrowLastTrace(Abort("evaluation aborted with the following error message: '%1%'", s));
} }
}); });
@ -755,7 +788,7 @@ static RegisterPrimOp primop_throw({
{ {
PathSet context; PathSet context;
auto s = state.coerceToString(pos, *args[0], context).toOwned(); auto s = state.coerceToString(pos, *args[0], context).toOwned();
throw ThrownError(s); state.debugThrowLastTrace(ThrownError(s));
} }
}); });
@ -1010,37 +1043,37 @@ static void prim_derivationStrict(EvalState & state, const PosIdx pos, Value * *
if (s == "recursive") ingestionMethod = FileIngestionMethod::Recursive; if (s == "recursive") ingestionMethod = FileIngestionMethod::Recursive;
else if (s == "flat") ingestionMethod = FileIngestionMethod::Flat; else if (s == "flat") ingestionMethod = FileIngestionMethod::Flat;
else else
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("invalid value '%s' for 'outputHashMode' attribute", s), .msg = hintfmt("invalid value '%s' for 'outputHashMode' attribute", s),
.errPos = state.positions[posDrvName] .errPos = state.positions[posDrvName]
}); }));
}; };
auto handleOutputs = [&](const Strings & ss) { auto handleOutputs = [&](const Strings & ss) {
outputs.clear(); outputs.clear();
for (auto & j : ss) { for (auto & j : ss) {
if (outputs.find(j) != outputs.end()) if (outputs.find(j) != outputs.end())
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("duplicate derivation output '%1%'", j), .msg = hintfmt("duplicate derivation output '%1%'", j),
.errPos = state.positions[posDrvName] .errPos = state.positions[posDrvName]
}); }));
/* !!! Check whether j is a valid attribute /* !!! Check whether j is a valid attribute
name. */ name. */
/* Derivations cannot be named drv, because /* Derivations cannot be named drv, because
then we'd have an attribute drvPath in then we'd have an attribute drvPath in
the resulting set. */ the resulting set. */
if (j == "drv") if (j == "drv")
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("invalid derivation output name 'drv'" ), .msg = hintfmt("invalid derivation output name 'drv'" ),
.errPos = state.positions[posDrvName] .errPos = state.positions[posDrvName]
}); }));
outputs.insert(j); outputs.insert(j);
} }
if (outputs.empty()) if (outputs.empty())
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("derivation cannot have an empty set of outputs"), .msg = hintfmt("derivation cannot have an empty set of outputs"),
.errPos = state.positions[posDrvName] .errPos = state.positions[posDrvName]
}); }));
}; };
try { try {
@ -1165,23 +1198,23 @@ static void prim_derivationStrict(EvalState & state, const PosIdx pos, Value * *
/* Do we have all required attributes? */ /* Do we have all required attributes? */
if (drv.builder == "") if (drv.builder == "")
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("required attribute 'builder' missing"), .msg = hintfmt("required attribute 'builder' missing"),
.errPos = state.positions[posDrvName] .errPos = state.positions[posDrvName]
}); }));
if (drv.platform == "") if (drv.platform == "")
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("required attribute 'system' missing"), .msg = hintfmt("required attribute 'system' missing"),
.errPos = state.positions[posDrvName] .errPos = state.positions[posDrvName]
}); }));
/* Check whether the derivation name is valid. */ /* Check whether the derivation name is valid. */
if (isDerivation(drvName)) if (isDerivation(drvName))
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("derivation names are not allowed to end in '%s'", drvExtension), .msg = hintfmt("derivation names are not allowed to end in '%s'", drvExtension),
.errPos = state.positions[posDrvName] .errPos = state.positions[posDrvName]
}); }));
if (outputHash) { if (outputHash) {
/* Handle fixed-output derivations. /* Handle fixed-output derivations.
@ -1189,10 +1222,10 @@ static void prim_derivationStrict(EvalState & state, const PosIdx pos, Value * *
Ignore `__contentAddressed` because fixed output derivations are Ignore `__contentAddressed` because fixed output derivations are
already content addressed. */ already content addressed. */
if (outputs.size() != 1 || *(outputs.begin()) != "out") if (outputs.size() != 1 || *(outputs.begin()) != "out")
throw Error({ state.debugThrowLastTrace(Error({
.msg = hintfmt("multiple outputs are not supported in fixed-output derivations"), .msg = hintfmt("multiple outputs are not supported in fixed-output derivations"),
.errPos = state.positions[posDrvName] .errPos = state.positions[posDrvName]
}); }));
auto h = newHashAllowEmpty(*outputHash, parseHashTypeOpt(outputHashAlgo)); auto h = newHashAllowEmpty(*outputHash, parseHashTypeOpt(outputHashAlgo));
@ -1361,10 +1394,10 @@ static RegisterPrimOp primop_toPath({
static void prim_storePath(EvalState & state, const PosIdx pos, Value * * args, Value & v) static void prim_storePath(EvalState & state, const PosIdx pos, Value * * args, Value & v)
{ {
if (evalSettings.pureEval) if (evalSettings.pureEval)
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("'%s' is not allowed in pure evaluation mode", "builtins.storePath"), .msg = hintfmt("'%s' is not allowed in pure evaluation mode", "builtins.storePath"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
PathSet context; PathSet context;
// FIXME: check rootPath // FIXME: check rootPath
@ -1375,10 +1408,10 @@ static void prim_storePath(EvalState & state, const PosIdx pos, Value * * args,
if (!state.store->isStorePath(path.abs())) if (!state.store->isStorePath(path.abs()))
path = CanonPath(canonPath(path.abs(), true)); path = CanonPath(canonPath(path.abs(), true));
if (!state.store->isInStore(path.abs())) if (!state.store->isInStore(path.abs()))
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("path '%1%' is not in the Nix store", path), .msg = hintfmt("path '%1%' is not in the Nix store", path),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
auto path2 = state.store->toStorePath(path.abs()).first; auto path2 = state.store->toStorePath(path.abs()).first;
if (!settings.readOnlyMode) if (!settings.readOnlyMode)
state.store->ensurePath(path2); state.store->ensurePath(path2);
@ -1487,7 +1520,7 @@ static void prim_readFile(EvalState & state, const PosIdx pos, Value * * args, V
auto path = realisePath(state, pos, *args[0]); auto path = realisePath(state, pos, *args[0]);
auto s = path.readFile(); auto s = path.readFile();
if (s.find((char) 0) != std::string::npos) if (s.find((char) 0) != std::string::npos)
throw Error("the contents of the file '%1%' cannot be represented as a Nix string", path); state.debugThrowLastTrace(Error("the contents of the file '%1%' cannot be represented as a Nix string", path));
// FIXME: only do queryPathInfo if path.accessor is the store accessor // FIXME: only do queryPathInfo if path.accessor is the store accessor
auto refs = auto refs =
state.store->isInStore(path.path.abs()) ? state.store->isInStore(path.path.abs()) ?
@ -1537,10 +1570,10 @@ static void prim_findFile(EvalState & state, const PosIdx pos, Value * * args, V
auto rewrites = state.realiseContext(context); auto rewrites = state.realiseContext(context);
path = rewriteStrings(path, rewrites); path = rewriteStrings(path, rewrites);
} catch (InvalidPathError & e) { } catch (InvalidPathError & e) {
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("cannot find '%1%', since path '%2%' is not valid", path, e.path), .msg = hintfmt("cannot find '%1%', since path '%2%' is not valid", path, e.path),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
} }
searchPath.emplace_back(prefix, path); searchPath.emplace_back(prefix, path);
@ -1563,10 +1596,10 @@ static void prim_hashFile(EvalState & state, const PosIdx pos, Value * * args, V
auto type = state.forceStringNoCtx(*args[0], pos); auto type = state.forceStringNoCtx(*args[0], pos);
std::optional<HashType> ht = parseHashType(type); std::optional<HashType> ht = parseHashType(type);
if (!ht) if (!ht)
throw Error({ state.debugThrowLastTrace(Error({
.msg = hintfmt("unknown hash type '%1%'", type), .msg = hintfmt("unknown hash type '%1%'", type),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
auto path = realisePath(state, pos, *args[1]); auto path = realisePath(state, pos, *args[1]);
@ -1811,13 +1844,13 @@ static void prim_toFile(EvalState & state, const PosIdx pos, Value * * args, Val
for (auto path : context) { for (auto path : context) {
if (path.at(0) != '/') if (path.at(0) != '/')
throw EvalError( { state.debugThrowLastTrace(EvalError({
.msg = hintfmt( .msg = hintfmt(
"in 'toFile': the file named '%1%' must not contain a reference " "in 'toFile': the file named '%1%' must not contain a reference "
"to a derivation but contains (%2%)", "to a derivation but contains (%2%)",
name, path), name, path),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
refs.insert(state.store->parseStorePath(path)); refs.insert(state.store->parseStorePath(path));
} }
@ -1984,7 +2017,7 @@ static void addPath(
? state.store->computeStorePathFromDump(*source, name, method, htSHA256, refs).first ? state.store->computeStorePathFromDump(*source, name, method, htSHA256, refs).first
: state.store->addToStoreFromDump(*source, name, method, htSHA256, state.repair); : state.store->addToStoreFromDump(*source, name, method, htSHA256, state.repair);
if (expectedHash && expectedStorePath != dstPath) if (expectedHash && expectedStorePath != dstPath)
throw Error("store path mismatch in (possibly filtered) path added from '%s'", path); state.debugThrowLastTrace(Error("store path mismatch in (possibly filtered) path added from '%s'", path));
state.allowAndSetStorePathString(dstPath, v); state.allowAndSetStorePathString(dstPath, v);
} else } else
state.allowAndSetStorePathString(*expectedStorePath, v); state.allowAndSetStorePathString(*expectedStorePath, v);
@ -2002,12 +2035,12 @@ static void prim_filterSource(EvalState & state, const PosIdx pos, Value * * arg
state.forceValue(*args[0], pos); state.forceValue(*args[0], pos);
if (args[0]->type() != nFunction) if (args[0]->type() != nFunction)
throw TypeError({ state.debugThrowLastTrace(TypeError({
.msg = hintfmt( .msg = hintfmt(
"first argument in call to 'filterSource' is not a function but %1%", "first argument in call to 'filterSource' is not a function but %1%",
showType(*args[0])), showType(*args[0])),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
addPath(state, pos, path.baseName(), path, args[0], FileIngestionMethod::Recursive, std::nullopt, v, context); addPath(state, pos, path.baseName(), path, args[0], FileIngestionMethod::Recursive, std::nullopt, v, context);
} }
@ -2091,16 +2124,16 @@ static void prim_path(EvalState & state, const PosIdx pos, Value * * args, Value
else if (n == "sha256") else if (n == "sha256")
expectedHash = newHashAllowEmpty(state.forceStringNoCtx(*attr.value, attr.pos), htSHA256); expectedHash = newHashAllowEmpty(state.forceStringNoCtx(*attr.value, attr.pos), htSHA256);
else else
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("unsupported argument '%1%' to 'addPath'", state.symbols[attr.name]), .msg = hintfmt("unsupported argument '%1%' to 'addPath'", state.symbols[attr.name]),
.errPos = state.positions[attr.pos] .errPos = state.positions[attr.pos]
}); }));
} }
if (!path) if (!path)
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("'path' required"), .msg = hintfmt("'path' required"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
if (name.empty()) if (name.empty())
name = path->baseName(); name = path->baseName();
@ -2472,10 +2505,10 @@ static void prim_functionArgs(EvalState & state, const PosIdx pos, Value * * arg
return; return;
} }
if (!args[0]->isLambda()) if (!args[0]->isLambda())
throw TypeError({ state.debugThrowLastTrace(TypeError({
.msg = hintfmt("'functionArgs' requires a function"), .msg = hintfmt("'functionArgs' requires a function"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
if (!args[0]->lambda.fun->hasFormals()) { if (!args[0]->lambda.fun->hasFormals()) {
v.mkAttrs(&state.emptyBindings); v.mkAttrs(&state.emptyBindings);
@ -2563,7 +2596,7 @@ static void prim_zipAttrsWith(EvalState & state, const PosIdx pos, Value * * arg
attrsSeen[attr.name].first++; attrsSeen[attr.name].first++;
} catch (TypeError & e) { } catch (TypeError & e) {
e.addTrace(state.positions[pos], hintfmt("while invoking '%s'", "zipAttrsWith")); e.addTrace(state.positions[pos], hintfmt("while invoking '%s'", "zipAttrsWith"));
throw; state.debugThrowLastTrace(e);
} }
} }
@ -2650,10 +2683,10 @@ static void elemAt(EvalState & state, const PosIdx pos, Value & list, int n, Val
{ {
state.forceList(list, pos); state.forceList(list, pos);
if (n < 0 || (unsigned int) n >= list.listSize()) if (n < 0 || (unsigned int) n >= list.listSize())
throw Error({ state.debugThrowLastTrace(Error({
.msg = hintfmt("list index %1% is out of bounds", n), .msg = hintfmt("list index %1% is out of bounds", n),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
state.forceValue(*list.listElems()[n], pos); state.forceValue(*list.listElems()[n], pos);
v = *list.listElems()[n]; v = *list.listElems()[n];
} }
@ -2698,10 +2731,10 @@ static void prim_tail(EvalState & state, const PosIdx pos, Value * * args, Value
{ {
state.forceList(*args[0], pos); state.forceList(*args[0], pos);
if (args[0]->listSize() == 0) if (args[0]->listSize() == 0)
throw Error({ state.debugThrowLastTrace(Error({
.msg = hintfmt("'tail' called on an empty list"), .msg = hintfmt("'tail' called on an empty list"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
state.mkList(v, args[0]->listSize() - 1); state.mkList(v, args[0]->listSize() - 1);
for (unsigned int n = 0; n < v.listSize(); ++n) for (unsigned int n = 0; n < v.listSize(); ++n)
@ -2936,10 +2969,10 @@ static void prim_genList(EvalState & state, const PosIdx pos, Value * * args, Va
auto len = state.forceInt(*args[1], pos); auto len = state.forceInt(*args[1], pos);
if (len < 0) if (len < 0)
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("cannot create list of size %1%", len), .msg = hintfmt("cannot create list of size %1%", len),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
state.mkList(v, len); state.mkList(v, len);
@ -3148,7 +3181,7 @@ static void prim_concatMap(EvalState & state, const PosIdx pos, Value * * args,
state.forceList(lists[n], lists[n].determinePos(args[0]->determinePos(pos))); state.forceList(lists[n], lists[n].determinePos(args[0]->determinePos(pos)));
} catch (TypeError &e) { } catch (TypeError &e) {
e.addTrace(state.positions[pos], hintfmt("while invoking '%s'", "concatMap")); e.addTrace(state.positions[pos], hintfmt("while invoking '%s'", "concatMap"));
throw; state.debugThrowLastTrace(e);
} }
len += lists[n].listSize(); len += lists[n].listSize();
} }
@ -3243,10 +3276,10 @@ static void prim_div(EvalState & state, const PosIdx pos, Value * * args, Value
NixFloat f2 = state.forceFloat(*args[1], pos); NixFloat f2 = state.forceFloat(*args[1], pos);
if (f2 == 0) if (f2 == 0)
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("division by zero"), .msg = hintfmt("division by zero"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
if (args[0]->type() == nFloat || args[1]->type() == nFloat) { if (args[0]->type() == nFloat || args[1]->type() == nFloat) {
v.mkFloat(state.forceFloat(*args[0], pos) / state.forceFloat(*args[1], pos)); v.mkFloat(state.forceFloat(*args[0], pos) / state.forceFloat(*args[1], pos));
@ -3255,10 +3288,10 @@ static void prim_div(EvalState & state, const PosIdx pos, Value * * args, Value
NixInt i2 = state.forceInt(*args[1], pos); NixInt i2 = state.forceInt(*args[1], pos);
/* Avoid division overflow as it might raise SIGFPE. */ /* Avoid division overflow as it might raise SIGFPE. */
if (i1 == std::numeric_limits<NixInt>::min() && i2 == -1) if (i1 == std::numeric_limits<NixInt>::min() && i2 == -1)
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("overflow in integer division"), .msg = hintfmt("overflow in integer division"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
v.mkInt(i1 / i2); v.mkInt(i1 / i2);
} }
@ -3386,10 +3419,10 @@ static void prim_substring(EvalState & state, const PosIdx pos, Value * * args,
auto s = state.coerceToString(pos, *args[2], context); auto s = state.coerceToString(pos, *args[2], context);
if (start < 0) if (start < 0)
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("negative start position in 'substring'"), .msg = hintfmt("negative start position in 'substring'"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
v.mkString((unsigned int) start >= s->size() ? "" : s->substr(start, len), context); v.mkString((unsigned int) start >= s->size() ? "" : s->substr(start, len), context);
} }
@ -3437,10 +3470,10 @@ static void prim_hashString(EvalState & state, const PosIdx pos, Value * * args,
auto type = state.forceStringNoCtx(*args[0], pos); auto type = state.forceStringNoCtx(*args[0], pos);
std::optional<HashType> ht = parseHashType(type); std::optional<HashType> ht = parseHashType(type);
if (!ht) if (!ht)
throw Error({ state.debugThrowLastTrace(Error({
.msg = hintfmt("unknown hash type '%1%'", type), .msg = hintfmt("unknown hash type '%1%'", type),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
PathSet context; // discarded PathSet context; // discarded
auto s = state.forceString(*args[1], context, pos); auto s = state.forceString(*args[1], context, pos);
@ -3510,16 +3543,15 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v)
} catch (std::regex_error &e) { } catch (std::regex_error &e) {
if (e.code() == std::regex_constants::error_space) { if (e.code() == std::regex_constants::error_space) {
// limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++ // limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("memory limit exceeded by regular expression '%s'", re), .msg = hintfmt("memory limit exceeded by regular expression '%s'", re),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
} else { } else
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("invalid regular expression '%s'", re), .msg = hintfmt("invalid regular expression '%s'", re),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
}
} }
} }
@ -3554,7 +3586,7 @@ static RegisterPrimOp primop_match({
builtins.match "[[:space:]]+([[:upper:]]+)[[:space:]]+" " FOO " builtins.match "[[:space:]]+([[:upper:]]+)[[:space:]]+" " FOO "
``` ```
Evaluates to `[ "foo" ]`. Evaluates to `[ "FOO" ]`.
)s", )s",
.fun = prim_match, .fun = prim_match,
}); });
@ -3615,16 +3647,15 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
} catch (std::regex_error &e) { } catch (std::regex_error &e) {
if (e.code() == std::regex_constants::error_space) { if (e.code() == std::regex_constants::error_space) {
// limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++ // limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("memory limit exceeded by regular expression '%s'", re), .msg = hintfmt("memory limit exceeded by regular expression '%s'", re),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
} else { } else
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("invalid regular expression '%s'", re), .msg = hintfmt("invalid regular expression '%s'", re),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
}
} }
} }
@ -3700,10 +3731,10 @@ static void prim_replaceStrings(EvalState & state, const PosIdx pos, Value * * a
state.forceList(*args[0], pos); state.forceList(*args[0], pos);
state.forceList(*args[1], pos); state.forceList(*args[1], pos);
if (args[0]->listSize() != args[1]->listSize()) if (args[0]->listSize() != args[1]->listSize())
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("'from' and 'to' arguments to 'replaceStrings' have different lengths"), .msg = hintfmt("'from' and 'to' arguments to 'replaceStrings' have different lengths"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
std::vector<std::string> from; std::vector<std::string> from;
from.reserve(args[0]->listSize()); from.reserve(args[0]->listSize());
@ -3956,7 +3987,7 @@ void EvalState::createBaseEnv()
because attribute lookups expect it to be sorted. */ because attribute lookups expect it to be sorted. */
baseEnv.values[0]->attrs->sort(); baseEnv.values[0]->attrs->sort();
staticBaseEnv.sort(); staticBaseEnv->sort();
/* Note: we have to initialize the 'derivation' constant *after* /* Note: we have to initialize the 'derivation' constant *after*
building baseEnv/staticBaseEnv because it uses 'builtins'. */ building baseEnv/staticBaseEnv because it uses 'builtins'. */

View file

@ -121,16 +121,16 @@ static void fetchTree(
if (auto aType = args[0]->attrs->get(state.sType)) { if (auto aType = args[0]->attrs->get(state.sType)) {
if (type) if (type)
throw Error({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("unexpected attribute 'type'"), .msg = hintfmt("unexpected attribute 'type'"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
type = state.forceStringNoCtx(*aType->value, aType->pos); type = state.forceStringNoCtx(*aType->value, aType->pos);
} else if (!type) } else if (!type)
throw Error({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("attribute 'type' is missing in call to 'fetchTree'"), .msg = hintfmt("attribute 'type' is missing in call to 'fetchTree'"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
attrs.emplace("type", type.value()); attrs.emplace("type", type.value());
@ -166,16 +166,16 @@ static void fetchTree(
else if (attr.value->type() == nInt) else if (attr.value->type() == nInt)
attrs.emplace(state.symbols[attr.name], uint64_t(attr.value->integer)); attrs.emplace(state.symbols[attr.name], uint64_t(attr.value->integer));
else else
throw TypeError("fetchTree argument '%s' is %s while a string, Boolean or integer is expected", state.debugThrowLastTrace(TypeError("fetchTree argument '%s' is %s while a string, Boolean or integer is expected",
state.symbols[attr.name], showType(*attr.value)); state.symbols[attr.name], showType(*attr.value)));
} }
if (!params.allowNameArgument) if (!params.allowNameArgument)
if (auto nameIter = attrs.find("name"); nameIter != attrs.end()) if (auto nameIter = attrs.find("name"); nameIter != attrs.end())
throw Error({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("attribute 'name' isn't supported in call to 'fetchTree'"), .msg = hintfmt("attribute 'name' isnt supported in call to 'fetchTree'"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
input = fetchers::Input::fromAttrs(std::move(attrs)); input = fetchers::Input::fromAttrs(std::move(attrs));
} else { } else {
@ -195,7 +195,7 @@ static void fetchTree(
input = lookupInRegistries(state.store, input).first; input = lookupInRegistries(state.store, input).first;
if (evalSettings.pureEval && !input.isLocked()) if (evalSettings.pureEval && !input.isLocked())
throw Error("in pure evaluation mode, 'fetchTree' requires a locked input, at %s", state.positions[pos]); state.debugThrowLastTrace(EvalError("in pure evaluation mode, 'fetchTree' requires a locked input, at %s", state.positions[pos]));
if (params.returnPath) { if (params.returnPath) {
auto [accessor, input2] = input.lazyFetch(state.store); auto [accessor, input2] = input.lazyFetch(state.store);
@ -259,17 +259,17 @@ static void fetch(EvalState & state, const PosIdx pos, Value * * args, Value & v
else if (n == "name") else if (n == "name")
name = state.forceStringNoCtx(*attr.value, attr.pos); name = state.forceStringNoCtx(*attr.value, attr.pos);
else else
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("unsupported argument '%s' to '%s'", n, who), .msg = hintfmt("unsupported argument '%s' to '%s'", n, who),
.errPos = state.positions[attr.pos] .errPos = state.positions[attr.pos]
}); }));
} }
if (!url) if (!url)
throw EvalError({ state.debugThrowLastTrace(EvalError({
.msg = hintfmt("'url' argument required"), .msg = hintfmt("'url' argument required"),
.errPos = state.positions[pos] .errPos = state.positions[pos]
}); }));
} else } else
url = state.forceStringNoCtx(*args[0], pos); url = state.forceStringNoCtx(*args[0], pos);
@ -281,7 +281,7 @@ static void fetch(EvalState & state, const PosIdx pos, Value * * args, Value & v
name = baseNameOf(*url); name = baseNameOf(*url);
if (evalSettings.pureEval && !expectedHash) if (evalSettings.pureEval && !expectedHash)
throw Error("in pure evaluation mode, '%s' requires a 'sha256' argument", who); state.debugThrowLastTrace(EvalError("in pure evaluation mode, '%s' requires a 'sha256' argument", who));
// early exit if pinned and already in the store // early exit if pinned and already in the store
if (expectedHash && expectedHash->type == htSHA256) { if (expectedHash && expectedHash->type == htSHA256) {
@ -308,8 +308,8 @@ static void fetch(EvalState & state, const PosIdx pos, Value * * args, Value & v
? state.store->queryPathInfo(storePath)->narHash ? state.store->queryPathInfo(storePath)->narHash
: hashFile(htSHA256, state.store->toRealPath(storePath)); : hashFile(htSHA256, state.store->toRealPath(storePath));
if (hash != *expectedHash) if (hash != *expectedHash)
throw Error((unsigned int) 102, "hash mismatch in file downloaded from '%s':\n specified: %s\n got: %s", state.debugThrowLastTrace(EvalError((unsigned int) 102, "hash mismatch in file downloaded from '%s':\n specified: %s\n got: %s",
*url, expectedHash->to_string(Base32, true), hash.to_string(Base32, true)); *url, expectedHash->to_string(Base32, true), hash.to_string(Base32, true)));
} }
state.allowAndSetStorePathString(storePath, v); state.allowAndSetStorePathString(storePath, v);

View file

@ -89,6 +89,7 @@ void printValueAsJSON(EvalState & state, bool strict,
.errPos = state.positions[v.determinePos(pos)] .errPos = state.positions[v.determinePos(pos)]
}); });
e.addTrace(state.positions[pos], hintfmt("message for the trace")); e.addTrace(state.positions[pos], hintfmt("message for the trace"));
state.debugThrowLastTrace(e);
throw e; throw e;
} }
} }
@ -103,7 +104,7 @@ void printValueAsJSON(EvalState & state, bool strict,
void ExternalValueBase::printValueAsJSON(EvalState & state, bool strict, void ExternalValueBase::printValueAsJSON(EvalState & state, bool strict,
JSONPlaceholder & out, PathSet & context) const JSONPlaceholder & out, PathSet & context) const
{ {
throw TypeError("cannot convert %1% to JSON", showType()); state.debugThrowLastTrace(TypeError("cannot convert %1% to JSON", showType()));
} }

View file

@ -427,9 +427,9 @@ public:
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
typedef std::vector<Value *, traceable_allocator<Value *> > ValueVector; typedef std::vector<Value *, traceable_allocator<Value *>> ValueVector;
typedef std::map<Symbol, Value *, std::less<Symbol>, traceable_allocator<std::pair<const Symbol, Value *> > > ValueMap; typedef std::map<Symbol, Value *, std::less<Symbol>, traceable_allocator<std::pair<const Symbol, Value *>>> ValueMap;
typedef std::map<Symbol, ValueVector, std::less<Symbol>, traceable_allocator<std::pair<const Symbol, ValueVector> > > ValueVectorMap; typedef std::map<Symbol, ValueVector, std::less<Symbol>, traceable_allocator<std::pair<const Symbol, ValueVector>>> ValueVectorMap;
#else #else
typedef std::vector<Value *> ValueVector; typedef std::vector<Value *> ValueVector;
typedef std::map<Symbol, Value *> ValueMap; typedef std::map<Symbol, Value *> ValueMap;

View file

@ -27,12 +27,6 @@ namespace {
// old version of git, which will ignore unrecognized `-c` options. // old version of git, which will ignore unrecognized `-c` options.
const std::string gitInitialBranch = "__nix_dummy_branch"; const std::string gitInitialBranch = "__nix_dummy_branch";
std::string getGitDir()
{
// FIXME: respecting GIT_DIR globally seems wrong.
return getEnv("GIT_DIR").value_or(".git");
}
bool isCacheFileWithinTtl(time_t now, const struct stat & st) bool isCacheFileWithinTtl(time_t now, const struct stat & st)
{ {
return st.st_mtime + settings.tarballTtl > now; return st.st_mtime + settings.tarballTtl > now;
@ -263,7 +257,7 @@ struct GitInputScheme : InputScheme
{ {
auto sourcePath = getSourcePath(input); auto sourcePath = getSourcePath(input);
assert(sourcePath); assert(sourcePath);
auto gitDir = getGitDir(); auto gitDir = ".git";
runProgram("git", true, runProgram("git", true,
{ "-C", *sourcePath, "--git-dir", gitDir, "add", "--force", "--intent-to-add", "--", std::string(file) }); { "-C", *sourcePath, "--git-dir", gitDir, "add", "--force", "--intent-to-add", "--", std::string(file) });
@ -304,7 +298,7 @@ struct GitInputScheme : InputScheme
} }
} }
std::string gitDir = getGitDir(); std::string gitDir = ".git";
}; };
RepoInfo getRepoInfo(const Input & input) RepoInfo getRepoInfo(const Input & input)
@ -356,7 +350,7 @@ struct GitInputScheme : InputScheme
on. */ on. */
auto result = runProgram(RunOptions { auto result = runProgram(RunOptions {
.program = "git", .program = "git",
.args = { "-C", repoInfo.url, "--git-dir", getGitDir(), "rev-parse", "--verify", "--no-revs", "HEAD^{commit}" }, .args = { "-C", repoInfo.url, "--git-dir", repoInfo.gitDir, "rev-parse", "--verify", "--no-revs", "HEAD^{commit}" },
.environment = env, .environment = env,
.mergeStderrToStdout = true .mergeStderrToStdout = true
}); });

View file

@ -6,6 +6,7 @@
#include "archive.hh" #include "archive.hh"
#include "tarfile.hh" #include "tarfile.hh"
#include "types.hh" #include "types.hh"
#include "split.hh"
namespace nix::fetchers { namespace nix::fetchers {
@ -168,24 +169,34 @@ std::pair<Tree, time_t> downloadTarball(
}; };
} }
struct TarballInputScheme : InputScheme // An input scheme corresponding to a curl-downloadable resource.
struct CurlInputScheme : InputScheme
{ {
virtual const std::string inputType() const = 0;
const std::set<std::string> transportUrlSchemes = {"file", "http", "https"};
const bool hasTarballExtension(std::string_view path) const
{
return hasSuffix(path, ".zip") || hasSuffix(path, ".tar")
|| hasSuffix(path, ".tgz") || hasSuffix(path, ".tar.gz")
|| hasSuffix(path, ".tar.xz") || hasSuffix(path, ".tar.bz2")
|| hasSuffix(path, ".tar.zst");
}
virtual bool isValidURL(const ParsedURL & url) const = 0;
std::optional<Input> inputFromURL(const ParsedURL & url) override std::optional<Input> inputFromURL(const ParsedURL & url) override
{ {
if (url.scheme != "file" && url.scheme != "http" && url.scheme != "https") return {}; if (!isValidURL(url))
return std::nullopt;
if (!hasSuffix(url.path, ".zip")
&& !hasSuffix(url.path, ".tar")
&& !hasSuffix(url.path, ".tgz")
&& !hasSuffix(url.path, ".tar.gz")
&& !hasSuffix(url.path, ".tar.xz")
&& !hasSuffix(url.path, ".tar.bz2")
&& !hasSuffix(url.path, ".tar.zst"))
return {};
Input input; Input input;
input.attrs.insert_or_assign("type", "tarball");
input.attrs.insert_or_assign("url", url.to_string()); auto urlWithoutApplicationScheme = url;
urlWithoutApplicationScheme.scheme = parseUrlScheme(url.scheme).transport;
input.attrs.insert_or_assign("type", inputType());
input.attrs.insert_or_assign("url", urlWithoutApplicationScheme.to_string());
auto narHash = url.query.find("narHash"); auto narHash = url.query.find("narHash");
if (narHash != url.query.end()) if (narHash != url.query.end())
input.attrs.insert_or_assign("narHash", narHash->second); input.attrs.insert_or_assign("narHash", narHash->second);
@ -194,14 +205,17 @@ struct TarballInputScheme : InputScheme
std::optional<Input> inputFromAttrs(const Attrs & attrs) override std::optional<Input> inputFromAttrs(const Attrs & attrs) override
{ {
if (maybeGetStrAttr(attrs, "type") != "tarball") return {}; auto type = maybeGetStrAttr(attrs, "type");
if (type != inputType()) return {};
std::set<std::string> allowedNames = {"type", "url", "narHash", "name", "unpack"};
for (auto & [name, value] : attrs) for (auto & [name, value] : attrs)
if (name != "type" && name != "url" && /* name != "hash" && */ name != "narHash" && name != "name") if (!allowedNames.count(name))
throw Error("unsupported tarball input attribute '%s'", name); throw Error("unsupported %s input attribute '%s'", *type, name);
Input input; Input input;
input.attrs = attrs; input.attrs = attrs;
//input.locked = (bool) maybeGetStrAttr(input.attrs, "hash"); //input.locked = (bool) maybeGetStrAttr(input.attrs, "hash");
return input; return input;
} }
@ -209,14 +223,9 @@ struct TarballInputScheme : InputScheme
ParsedURL toURL(const Input & input) override ParsedURL toURL(const Input & input) override
{ {
auto url = parseURL(getStrAttr(input.attrs, "url")); auto url = parseURL(getStrAttr(input.attrs, "url"));
// NAR hashes are preferred over file hashes since tar/zip files // NAR hashes are preferred over file hashes since tar/zip files // don't have a canonical representation.
// don't have a canonical representation.
if (auto narHash = input.getNarHash()) if (auto narHash = input.getNarHash())
url.query.insert_or_assign("narHash", narHash->to_string(SRI, true)); url.query.insert_or_assign("narHash", narHash->to_string(SRI, true));
/*
else if (auto hash = maybeGetStrAttr(input.attrs, "hash"))
url.query.insert_or_assign("hash", Hash(*hash).to_string(SRI, true));
*/
return url; return url;
} }
@ -225,6 +234,42 @@ struct TarballInputScheme : InputScheme
return true; return true;
} }
};
struct FileInputScheme : CurlInputScheme
{
const std::string inputType() const override { return "file"; }
bool isValidURL(const ParsedURL & url) const override
{
auto parsedUrlScheme = parseUrlScheme(url.scheme);
return transportUrlSchemes.count(std::string(parsedUrlScheme.transport))
&& (parsedUrlScheme.application
? parsedUrlScheme.application.value() == inputType()
: !hasTarballExtension(url.path));
}
std::pair<StorePath, Input> fetch(ref<Store> store, const Input & input) override
{
auto file = downloadFile(store, getStrAttr(input.attrs, "url"), input.getName(), false);
return {std::move(file.storePath), input};
}
};
struct TarballInputScheme : CurlInputScheme
{
const std::string inputType() const override { return "tarball"; }
bool isValidURL(const ParsedURL & url) const override
{
auto parsedUrlScheme = parseUrlScheme(url.scheme);
return transportUrlSchemes.count(std::string(parsedUrlScheme.transport))
&& (parsedUrlScheme.application
? parsedUrlScheme.application.value() == inputType()
: hasTarballExtension(url.path));
}
std::pair<StorePath, Input> fetch(ref<Store> store, const Input & input) override std::pair<StorePath, Input> fetch(ref<Store> store, const Input & input) override
{ {
auto tree = downloadTarball(store, getStrAttr(input.attrs, "url"), input.getName(), false).first; auto tree = downloadTarball(store, getStrAttr(input.attrs, "url"), input.getName(), false).first;
@ -233,5 +278,6 @@ struct TarballInputScheme : InputScheme
}; };
static auto rTarballInputScheme = OnStartup([] { registerInputScheme(std::make_unique<TarballInputScheme>()); }); static auto rTarballInputScheme = OnStartup([] { registerInputScheme(std::make_unique<TarballInputScheme>()); });
static auto rFileInputScheme = OnStartup([] { registerInputScheme(std::make_unique<FileInputScheme>()); });
} }

View file

@ -93,8 +93,9 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir,
auto prevPriority = state.priorities[dstFile]; auto prevPriority = state.priorities[dstFile];
if (prevPriority == priority) if (prevPriority == priority)
throw Error( throw Error(
"packages '%1%' and '%2%' have the same priority %3%; " "files '%1%' and '%2%' have the same priority %3%; "
"use 'nix-env --set-flag priority NUMBER INSTALLED_PKGNAME' " "use 'nix-env --set-flag priority NUMBER INSTALLED_PKGNAME' "
"or type 'nix profile install --help' if using 'nix profile' to find out how"
"to change the priority of one of the conflicting packages" "to change the priority of one of the conflicting packages"
" (0 being the highest priority)", " (0 being the highest priority)",
srcFile, readLink(dstFile), priority); srcFile, readLink(dstFile), priority);

View file

@ -98,6 +98,15 @@ struct ErrPos {
} }
}; };
std::optional<LinesOfCode> getCodeLines(const ErrPos & errPos);
void printCodeLines(std::ostream & out,
const std::string & prefix,
const ErrPos & errPos,
const LinesOfCode & loc);
void printAtPos(const ErrPos & pos, std::ostream & out);
struct Trace { struct Trace {
std::optional<ErrPos> pos; std::optional<ErrPos> pos;
hintformat hint; hintformat hint;

View file

@ -7,7 +7,7 @@
namespace nix { namespace nix {
/* A simple non-nullable reference-counted pointer. Actually a wrapper /* A simple non-nullable reference-counted pointer. Actually a wrapper
around std::shared_ptr that prevents non-null constructions. */ around std::shared_ptr that prevents null constructions. */
template<typename T> template<typename T>
class ref class ref
{ {

View file

@ -1,6 +1,7 @@
#include "url.hh" #include "url.hh"
#include "url-parts.hh" #include "url-parts.hh"
#include "util.hh" #include "util.hh"
#include "split.hh"
namespace nix { namespace nix {
@ -136,4 +137,21 @@ bool ParsedURL::operator ==(const ParsedURL & other) const
&& fragment == other.fragment; && fragment == other.fragment;
} }
/**
* Parse a URL scheme of the form '(applicationScheme\+)?transportScheme'
* into a tuple '(applicationScheme, transportScheme)'
*
* > parseUrlScheme("http") == ParsedUrlScheme{ {}, "http"}
* > parseUrlScheme("tarball+http") == ParsedUrlScheme{ {"tarball"}, "http"}
*/
ParsedUrlScheme parseUrlScheme(std::string_view scheme)
{
auto application = splitPrefixTo(scheme, '+');
auto transport = scheme;
return ParsedUrlScheme {
.application = application,
.transport = transport,
};
}
} }

View file

@ -27,4 +27,19 @@ std::map<std::string, std::string> decodeQuery(const std::string & query);
ParsedURL parseURL(const std::string & url); ParsedURL parseURL(const std::string & url);
/*
* Although thats not really standardized anywhere, an number of tools
* use a scheme of the form 'x+y' in urls, where y is the transport layer
* scheme, and x is the application layer scheme.
*
* For example git uses `git+https` to designate remotes using a Git
* protocol over http.
*/
struct ParsedUrlScheme {
std::optional<std::string_view> application;
std::string_view transport;
};
ParsedUrlScheme parseUrlScheme(std::string_view scheme);
} }

View file

@ -18,6 +18,9 @@ struct DevelopSettings : Config
Setting<std::string> bashPrompt{this, "", "bash-prompt", Setting<std::string> bashPrompt{this, "", "bash-prompt",
"The bash prompt (`PS1`) in `nix develop` shells."}; "The bash prompt (`PS1`) in `nix develop` shells."};
Setting<std::string> bashPromptPrefix{this, "", "bash-prompt-prefix",
"Prefix prepended to the `PS1` environment variable in `nix develop` shells."};
Setting<std::string> bashPromptSuffix{this, "", "bash-prompt-suffix", Setting<std::string> bashPromptSuffix{this, "", "bash-prompt-suffix",
"Suffix appended to the `PS1` environment variable in `nix develop` shells."}; "Suffix appended to the `PS1` environment variable in `nix develop` shells."};
}; };
@ -482,6 +485,9 @@ struct CmdDevelop : Common, MixEnvironment
if (developSettings.bashPrompt != "") if (developSettings.bashPrompt != "")
script += fmt("[ -n \"$PS1\" ] && PS1=%s;\n", script += fmt("[ -n \"$PS1\" ] && PS1=%s;\n",
shellEscape(developSettings.bashPrompt.get())); shellEscape(developSettings.bashPrompt.get()));
if (developSettings.bashPromptPrefix != "")
script += fmt("[ -n \"$PS1\" ] && PS1=%s\"$PS1\";\n",
shellEscape(developSettings.bashPromptPrefix.get()));
if (developSettings.bashPromptSuffix != "") if (developSettings.bashPromptSuffix != "")
script += fmt("[ -n \"$PS1\" ] && PS1+=%s;\n", script += fmt("[ -n \"$PS1\" ] && PS1+=%s;\n",
shellEscape(developSettings.bashPromptSuffix.get())); shellEscape(developSettings.bashPromptSuffix.get()));

View file

@ -80,8 +80,8 @@ initialised by `stdenv` and exits. This build environment can be
recorded into a profile using `--profile`. recorded into a profile using `--profile`.
The prompt used by the `bash` shell can be customised by setting the The prompt used by the `bash` shell can be customised by setting the
`bash-prompt` and `bash-prompt-suffix` settings in `nix.conf` or in `bash-prompt`, `bash-prompt-prefix`, and `bash-prompt-suffix` settings in
the flake's `nixConfig` attribute. `nix.conf` or in the flake's `nixConfig` attribute.
# Flake output attributes # Flake output attributes

View file

@ -503,7 +503,7 @@ struct CmdFlakeCheck : FlakeCommand
std::string_view replacement = std::string_view replacement =
name == "defaultPackage" ? "packages.<system>.default" : name == "defaultPackage" ? "packages.<system>.default" :
name == "defaultApps" ? "apps.<system>.default" : name == "defaultApp" ? "apps.<system>.default" :
name == "defaultTemplate" ? "templates.default" : name == "defaultTemplate" ? "templates.default" :
name == "defaultBundler" ? "bundlers.<system>.default" : name == "defaultBundler" ? "bundlers.<system>.default" :
name == "overlay" ? "overlays.default" : name == "overlay" ? "overlays.default" :
@ -1073,9 +1073,13 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
else if (attrPath.size() > 0 && attrPathS[0] == "legacyPackages") { else if (attrPath.size() > 0 && attrPathS[0] == "legacyPackages") {
if (attrPath.size() == 1) if (attrPath.size() == 1)
recurse(); recurse();
else if (!showLegacy) else if (!showLegacy){
logger->warn(fmt("%s: " ANSI_WARNING "omitted" ANSI_NORMAL " (use '--legacy' to show)", headerPrefix)); if (!json)
else { logger->cout(fmt("%s " ANSI_WARNING "omitted" ANSI_NORMAL " (use '--legacy' to show)", headerPrefix));
else {
logger->warn(fmt("%s omitted (use '--legacy' to show)", concatStringsSep(".", attrPathS)));
}
} else {
if (visitor.isDerivation()) if (visitor.isDerivation())
showDerivation(); showDerivation();
else if (attrPath.size() <= 2) else if (attrPath.size() <= 2)

View file

@ -181,9 +181,17 @@ Currently the `type` attribute can be one of the following:
* `tarball`: Tarballs. The location of the tarball is specified by the * `tarball`: Tarballs. The location of the tarball is specified by the
attribute `url`. attribute `url`.
In URL form, the schema must be `http://`, `https://` or `file://` In URL form, the schema must be `tarball+http://`, `tarball+https://` or `tarball+file://`.
URLs and the extension must be `.zip`, `.tar`, `.tgz`, `.tar.gz`, If the extension corresponds to a known archive format (`.zip`, `.tar`,
`.tar.xz`, `.tar.bz2` or `.tar.zst`. `.tgz`, `.tar.gz`, `.tar.xz`, `.tar.bz2` or `.tar.zst`), then the `tarball+`
can be dropped.
* `file`: Plain files or directory tarballs, either over http(s) or from the local
disk.
In URL form, the schema must be `file+http://`, `file+https://` or `file+file://`.
If the extension doesnt correspond to a known archive format (as defined by the
`tarball` fetcher), then the `file+` prefix can be dropped.
* `github`: A more efficient way to fetch repositories from * `github`: A more efficient way to fetch repositories from
GitHub. The following attributes are required: GitHub. The following attributes are required:
@ -331,9 +339,10 @@ The following attributes are supported in `flake.nix`:
* `nixConfig`: a set of `nix.conf` options to be set when evaluating any * `nixConfig`: a set of `nix.conf` options to be set when evaluating any
part of a flake. In the interests of security, only a small set of part of a flake. In the interests of security, only a small set of
whitelisted options (currently `bash-prompt`, `bash-prompt-suffix`, whitelisted options (currently `bash-prompt`, `bash-prompt-prefix`,
and `flake-registry`) are allowed to be set without confirmation so long as `bash-prompt-suffix`, and `flake-registry`) are allowed to be set without
`accept-flake-config` is not set in the global configuration. confirmation so long as `accept-flake-config` is not set in the global
configuration.
## Flake inputs ## Flake inputs

View file

@ -37,7 +37,7 @@ struct ProfileElement
StorePathSet storePaths; StorePathSet storePaths;
std::optional<ProfileElementSource> source; std::optional<ProfileElementSource> source;
bool active = true; bool active = true;
// FIXME: priority int priority = 5;
std::string describe() const std::string describe() const
{ {
@ -116,6 +116,9 @@ struct ProfileManifest
for (auto & p : e["storePaths"]) for (auto & p : e["storePaths"])
element.storePaths.insert(state.store->parseStorePath((std::string) p)); element.storePaths.insert(state.store->parseStorePath((std::string) p));
element.active = e["active"]; element.active = e["active"];
if(e.contains("priority")) {
element.priority = e["priority"];
}
if (e.value(sUrl, "") != "") { if (e.value(sUrl, "") != "") {
element.source = ProfileElementSource { element.source = ProfileElementSource {
parseFlakeRef(e[sOriginalUrl]), parseFlakeRef(e[sOriginalUrl]),
@ -153,6 +156,7 @@ struct ProfileManifest
nlohmann::json obj; nlohmann::json obj;
obj["storePaths"] = paths; obj["storePaths"] = paths;
obj["active"] = element.active; obj["active"] = element.active;
obj["priority"] = element.priority;
if (element.source) { if (element.source) {
obj["originalUrl"] = element.source->originalRef.to_string(); obj["originalUrl"] = element.source->originalRef.to_string();
obj["url"] = element.source->resolvedRef.to_string(); obj["url"] = element.source->resolvedRef.to_string();
@ -177,7 +181,7 @@ struct ProfileManifest
for (auto & element : elements) { for (auto & element : elements) {
for (auto & path : element.storePaths) { for (auto & path : element.storePaths) {
if (element.active) if (element.active)
pkgs.emplace_back(store->printStorePath(path), true, 5); pkgs.emplace_back(store->printStorePath(path), true, element.priority);
references.insert(path); references.insert(path);
} }
} }
@ -259,6 +263,17 @@ builtPathsPerInstallable(
struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
{ {
std::optional<int64_t> priority;
CmdProfileInstall() {
addFlag({
.longName = "priority",
.description = "The priority of the package to install.",
.labels = {"priority"},
.handler = {&priority},
});
};
std::string description() override std::string description() override
{ {
return "install a package into a profile"; return "install a package into a profile";
@ -282,6 +297,8 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
for (auto & installable : installables) { for (auto & installable : installables) {
ProfileElement element; ProfileElement element;
if (auto installable2 = std::dynamic_pointer_cast<InstallableFlake>(installable)) { if (auto installable2 = std::dynamic_pointer_cast<InstallableFlake>(installable)) {
// FIXME: make build() return this? // FIXME: make build() return this?
auto [attrPath, resolvedRef, drv] = installable2->toDerivation(); auto [attrPath, resolvedRef, drv] = installable2->toDerivation();
@ -291,8 +308,16 @@ struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
attrPath, attrPath,
installable2->outputsSpec installable2->outputsSpec
}; };
if(drv.priority) {
element.priority = *drv.priority;
}
} }
if(priority) { // if --priority was specified we want to override the priority of the installable
element.priority = *priority;
};
element.updateStorePaths(getEvalStore(), store, builtPaths[installable.get()]); element.updateStorePaths(getEvalStore(), store, builtPaths[installable.get()]);
manifest.elements.push_back(std::move(element)); manifest.elements.push_back(std::move(element));

105
tests/fetchTree-file.sh Normal file
View file

@ -0,0 +1,105 @@
source common.sh
clearStore
cd "$TEST_ROOT"
test_fetch_file () {
echo foo > test_input
input_hash="$(nix hash path test_input)"
nix eval --impure --file - <<EOF
let
tree = builtins.fetchTree { type = "file"; url = "file://$PWD/test_input"; };
in
assert (tree.narHash == "$input_hash");
tree
EOF
}
# Make sure that `http(s)` and `file` flake inputs are properly extracted when
# they should be, and treated as opaque files when they should be
test_file_flake_input () {
rm -fr "$TEST_ROOT/testFlake";
mkdir "$TEST_ROOT/testFlake";
pushd testFlake
mkdir inputs
echo foo > inputs/test_input_file
tar cfa test_input.tar.gz inputs
cp test_input.tar.gz test_input_no_ext
input_tarball_hash="$(nix hash path test_input.tar.gz)"
input_directory_hash="$(nix hash path inputs)"
cat <<EOF > flake.nix
{
inputs.no_ext_default_no_unpack = {
url = "file://$PWD/test_input_no_ext";
flake = false;
};
inputs.no_ext_explicit_unpack = {
url = "tarball+file://$PWD/test_input_no_ext";
flake = false;
};
inputs.tarball_default_unpack = {
url = "file://$PWD/test_input.tar.gz";
flake = false;
};
inputs.tarball_explicit_no_unpack = {
url = "file+file://$PWD/test_input.tar.gz";
flake = false;
};
outputs = { ... }: {};
}
EOF
nix flake update
nix eval --file - <<EOF
with (builtins.fromJSON (builtins.readFile ./flake.lock));
# Url inputs whose extension doesnt match a know archive format should
# not be unpacked by default
assert (nodes.no_ext_default_no_unpack.locked.type == "file");
assert (nodes.no_ext_default_no_unpack.locked.unpack or false == false);
assert (nodes.no_ext_default_no_unpack.locked.narHash == "$input_tarball_hash");
# For backwards compatibility, flake inputs that correspond to the
# old 'tarball' fetcher should still have their type set to 'tarball'
assert (nodes.tarball_default_unpack.locked.type == "tarball");
# Unless explicitely specified, the 'unpack' parameter shouldnt appear here
# because that would break older Nix versions
assert (!nodes.tarball_default_unpack.locked ? unpack);
assert (nodes.tarball_default_unpack.locked.narHash == "$input_directory_hash");
# Explicitely passing the unpack parameter should enforce the desired behavior
assert (nodes.no_ext_explicit_unpack.locked.narHash == nodes.tarball_default_unpack.locked.narHash);
assert (nodes.tarball_explicit_no_unpack.locked.narHash == nodes.no_ext_default_no_unpack.locked.narHash);
true
EOF
popd
[[ -z "${NIX_DAEMON_PACKAGE}" ]] && return 0
# Ensure that a lockfile generated by the current Nix for tarball inputs
# can still be read by an older Nix
cat <<EOF > flake.nix
{
inputs.tarball = {
url = "file://$PWD/test_input.tar.gz";
flake = false;
};
outputs = { self, tarball }: {
foo = builtins.readFile "${tarball}/test_input_file";
};
}
nix flake update
clearStore
"$NIX_DAEMON_PACKAGE/bin/nix" eval .#foo
EOF
}
test_fetch_file
test_file_flake_input

View file

@ -163,6 +163,7 @@ nix build -o $TEST_ROOT/result --expr "(builtins.getFlake \"git+file://$flake1Di
# But should succeed in impure mode. # But should succeed in impure mode.
(! nix build -o $TEST_ROOT/result flake2#bar --impure) (! nix build -o $TEST_ROOT/result flake2#bar --impure)
nix build -o $TEST_ROOT/result flake2#bar --impure --no-write-lock-file nix build -o $TEST_ROOT/result flake2#bar --impure --no-write-lock-file
nix eval --expr "builtins.getFlake \"$flake2Dir\"" --impure
# Building a local flake with an unlocked dependency should fail with --no-update-lock-file. # Building a local flake with an unlocked dependency should fail with --no-update-lock-file.
nix build -o $TEST_ROOT/result $flake2Dir#bar --no-update-lock-file 2>&1 | grep 'requires lock file changes' nix build -o $TEST_ROOT/result $flake2Dir#bar --no-update-lock-file 2>&1 | grep 'requires lock file changes'

View file

@ -0,0 +1,3 @@
# https://github.com/NixOS/nix/issues/6562
# Note that this file must not end with a newline.
a 1"$

View file

@ -23,6 +23,7 @@ nix_tests = \
fetchGit.sh \ fetchGit.sh \
fetchurl.sh \ fetchurl.sh \
fetchPath.sh \ fetchPath.sh \
fetchTree-file.sh \
simple.sh \ simple.sh \
referrers.sh \ referrers.sh \
optimise-store.sh \ optimise-store.sh \

View file

@ -120,3 +120,21 @@ nix profile install "$flake1Dir^man"
(! [ -e $TEST_HOME/.nix-profile/bin/hello ]) (! [ -e $TEST_HOME/.nix-profile/bin/hello ])
[ -e $TEST_HOME/.nix-profile/share/man ] [ -e $TEST_HOME/.nix-profile/share/man ]
(! [ -e $TEST_HOME/.nix-profile/include ]) (! [ -e $TEST_HOME/.nix-profile/include ])
# test priority
nix profile remove 0
# Make another flake.
flake2Dir=$TEST_ROOT/flake2
printf World > $flake1Dir/who
cp -r $flake1Dir $flake2Dir
printf World2 > $flake2Dir/who
nix profile install $flake1Dir
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]]
nix profile install $flake2Dir --priority 100
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]]
nix profile install $flake2Dir --priority 0
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World2" ]]
# nix profile install $flake1Dir --priority 100
# [[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]]