From f6158ea53b90a60899ee8171c04dc1f978fb8723 Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Mon, 26 Feb 2024 00:30:51 -0800 Subject: [PATCH 001/168] finally.hh: include works by itself; mark as nodiscard --- src/libutil/finally.hh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libutil/finally.hh b/src/libutil/finally.hh index db654301f..4cae20a36 100644 --- a/src/libutil/finally.hh +++ b/src/libutil/finally.hh @@ -1,11 +1,13 @@ #pragma once ///@file +#include + /** * A trivial class to run a function at the end of a scope. */ template -class Finally +class [[nodiscard("Finally values must be used")]] Finally { private: Fn fun; From 950b6401f98d1a56fefb2d6101e0513cc601824c Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Fri, 1 Mar 2024 22:12:44 +0100 Subject: [PATCH 002/168] libmain/progress-bar: try harder to avoid escape sequences if !isTTY --- src/libmain/progress-bar.cc | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/libmain/progress-bar.cc b/src/libmain/progress-bar.cc index 3aa012ee1..afba29e1b 100644 --- a/src/libmain/progress-bar.cc +++ b/src/libmain/progress-bar.cc @@ -123,14 +123,18 @@ public: } void pause() override { - state_.lock()->paused = true; - writeToStderr("\r\e[K"); + auto state (state_.lock()); + state->paused = true; + if (state->active) + writeToStderr("\r\e[K"); } void resume() override { - state_.lock()->paused = false; - writeToStderr("\r\e[K"); - state_.lock()->haveUpdate = true; + auto state (state_.lock()); + state->paused = false; + if (state->active) + writeToStderr("\r\e[K"); + state->haveUpdate = true; updateCV.notify_one(); } @@ -162,9 +166,7 @@ public: writeToStderr("\r\e[K" + filterANSIEscapes(s, !isTTY) + ANSI_NORMAL "\n"); draw(state); } else { - auto s2 = s + ANSI_NORMAL "\n"; - if (!isTTY) s2 = filterANSIEscapes(s2, true); - writeToStderr(s2); + writeToStderr(filterANSIEscapes(s, !isTTY) + "\n"); } } @@ -519,7 +521,7 @@ public: std::optional ask(std::string_view msg) override { auto state(state_.lock()); - if (!state->active || !isatty(STDIN_FILENO)) return {}; + if (!state->active) return {}; std::cerr << fmt("\r\e[K%s ", msg); auto s = trim(readLine(STDIN_FILENO)); if (s.size() != 1) return {}; From d9fc4bf5c5a2ad075d4500b005abc839eb98e581 Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Fri, 1 Mar 2024 23:11:24 +0100 Subject: [PATCH 003/168] treewide: replace usages of isatty(STDERR_FILENO) with shouldANSI() --- src/nix-env/nix-env.cc | 3 ++- src/nix/main.cc | 5 ++++- src/nix/prefetch.cc | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 5e3de20c5..b42fa06bc 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -16,6 +16,7 @@ #include "xml-writer.hh" #include "legacy.hh" #include "eval-settings.hh" // for defexpr +#include "terminal.hh" #include #include @@ -1089,7 +1090,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs) return; } - bool tty = isatty(STDOUT_FILENO); + bool tty = shouldANSI(); RunPager pager; Table table; diff --git a/src/nix/main.cc b/src/nix/main.cc index 5af5f2e41..c88b35005 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -16,6 +16,7 @@ #include "loggers.hh" #include "markdown.hh" #include "memory-input-accessor.hh" +#include "terminal.hh" #include #include @@ -375,7 +376,9 @@ void mainWrapped(int argc, char * * argv) setLogFormat("bar"); settings.verboseBuild = false; - if (isatty(STDERR_FILENO)) { + + // If on a terminal, progress will be displayed via progress bars etc. (thus verbosity=notice) + if (nix::shouldANSI()) { verbosity = lvlNotice; } else { verbosity = lvlInfo; diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc index fabec5d88..f96381408 100644 --- a/src/nix/prefetch.cc +++ b/src/nix/prefetch.cc @@ -11,6 +11,7 @@ #include "legacy.hh" #include "posix-source-accessor.hh" #include "misc-store-flags.hh" +#include "terminal.hh" #include @@ -188,7 +189,7 @@ static int main_nix_prefetch_url(int argc, char * * argv) Finally f([]() { stopProgressBar(); }); - if (isatty(STDERR_FILENO)) + if (shouldANSI()) startProgressBar(); auto store = openStore(); From 8c1eeb4681dc2acc60f23341d79470a0340ccdf5 Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Fri, 1 Mar 2024 23:13:00 +0100 Subject: [PATCH 004/168] treewide: shouldANSI() -> isTTY() --- src/libcmd/markdown.cc | 2 +- src/libmain/progress-bar.cc | 2 +- src/libutil/logging.cc | 2 +- src/libutil/terminal.cc | 2 +- src/libutil/terminal.hh | 2 +- src/nix-env/nix-env.cc | 2 +- src/nix/main.cc | 2 +- src/nix/prefetch.cc | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libcmd/markdown.cc b/src/libcmd/markdown.cc index a4e3c5a77..d62ff0d96 100644 --- a/src/libcmd/markdown.cc +++ b/src/libcmd/markdown.cc @@ -50,7 +50,7 @@ std::string renderMarkdownToTerminal(std::string_view markdown) if (!rndr_res) throw Error("allocation error while rendering Markdown"); - return filterANSIEscapes(std::string(buf->data, buf->size), !shouldANSI()); + return filterANSIEscapes(std::string(buf->data, buf->size), !isTTY()); #else return std::string(markdown); #endif diff --git a/src/libmain/progress-bar.cc b/src/libmain/progress-bar.cc index afba29e1b..ce45eae2b 100644 --- a/src/libmain/progress-bar.cc +++ b/src/libmain/progress-bar.cc @@ -537,7 +537,7 @@ public: Logger * makeProgressBar() { - return new ProgressBar(shouldANSI()); + return new ProgressBar(isTTY()); } void startProgressBar() diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 89fbd194a..83db492ca 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -52,7 +52,7 @@ public: : printBuildLogs(printBuildLogs) { systemd = getEnv("IN_SYSTEMD") == "1"; - tty = shouldANSI(); + tty = isTTY(); } bool isVerbose() override { diff --git a/src/libutil/terminal.cc b/src/libutil/terminal.cc index 8febc8771..2ff923405 100644 --- a/src/libutil/terminal.cc +++ b/src/libutil/terminal.cc @@ -7,7 +7,7 @@ namespace nix { -bool shouldANSI() +bool isTTY() { return isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb" diff --git a/src/libutil/terminal.hh b/src/libutil/terminal.hh index 9cb191308..9d8d0c743 100644 --- a/src/libutil/terminal.hh +++ b/src/libutil/terminal.hh @@ -8,7 +8,7 @@ namespace nix { * Determine whether ANSI escape sequences are appropriate for the * present output. */ -bool shouldANSI(); +bool isTTY(); /** * Truncate a string to 'width' printable characters. If 'filterAll' diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index b42fa06bc..0fa9501c1 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -1090,7 +1090,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs) return; } - bool tty = shouldANSI(); + bool tty = isTTY(); RunPager pager; Table table; diff --git a/src/nix/main.cc b/src/nix/main.cc index c88b35005..22f9e7931 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -378,7 +378,7 @@ void mainWrapped(int argc, char * * argv) settings.verboseBuild = false; // If on a terminal, progress will be displayed via progress bars etc. (thus verbosity=notice) - if (nix::shouldANSI()) { + if (nix::isTTY()) { verbosity = lvlNotice; } else { verbosity = lvlInfo; diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc index f96381408..b64e6d899 100644 --- a/src/nix/prefetch.cc +++ b/src/nix/prefetch.cc @@ -189,7 +189,7 @@ static int main_nix_prefetch_url(int argc, char * * argv) Finally f([]() { stopProgressBar(); }); - if (shouldANSI()) + if (isTTY()) startProgressBar(); auto store = openStore(); From c6f0407103ff64e86b5cc340980c010d49b31e8a Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Fri, 1 Mar 2024 23:17:44 +0100 Subject: [PATCH 005/168] libutil/terminal: cache isTTY() --- src/libutil/terminal.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libutil/terminal.cc b/src/libutil/terminal.cc index 2ff923405..096252f03 100644 --- a/src/libutil/terminal.cc +++ b/src/libutil/terminal.cc @@ -9,9 +9,12 @@ namespace nix { bool isTTY() { - return isatty(STDERR_FILENO) + static const bool tty = + isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb" && !(getEnv("NO_COLOR").has_value() || getEnv("NOCOLOR").has_value()); + + return tty; } std::string filterANSIEscapes(std::string_view s, bool filterAll, unsigned int width) From 76aced691552e193e0225af40f8acf484cfeaefe Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Mon, 26 Feb 2024 01:21:54 -0800 Subject: [PATCH 006/168] finally.hh: delete copy constructor which is a bad idea --- src/libutil/finally.hh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libutil/finally.hh b/src/libutil/finally.hh index 4cae20a36..f9f0195a1 100644 --- a/src/libutil/finally.hh +++ b/src/libutil/finally.hh @@ -11,8 +11,15 @@ class [[nodiscard("Finally values must be used")]] Finally { private: Fn fun; + bool movedFrom = false; public: Finally(Fn fun) : fun(std::move(fun)) { } - ~Finally() { fun(); } + // Copying Finallys is definitely not a good idea and will cause them to be + // called twice. + Finally(Finally &other) = delete; + Finally(Finally &&other) : fun(std::move(other.fun)) { + other.movedFrom = true; + } + ~Finally() { if (!movedFrom) fun(); } }; From 70a6ce139bd39f915a6c2c499d741e2c27557dc0 Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Mon, 26 Feb 2024 00:34:11 -0800 Subject: [PATCH 007/168] refactor: move readline stuff into its own file This is in direct preparation for an automation mode of nix repl. --- src/libcmd/repl-interacter.cc | 175 ++++++++++++++++++++++++++++++++++ src/libcmd/repl-interacter.hh | 48 ++++++++++ src/libcmd/repl.cc | 175 ++-------------------------------- src/libcmd/repl.hh | 5 - 4 files changed, 232 insertions(+), 171 deletions(-) create mode 100644 src/libcmd/repl-interacter.cc create mode 100644 src/libcmd/repl-interacter.hh diff --git a/src/libcmd/repl-interacter.cc b/src/libcmd/repl-interacter.cc new file mode 100644 index 000000000..9aa1f7bb9 --- /dev/null +++ b/src/libcmd/repl-interacter.cc @@ -0,0 +1,175 @@ +#include "file-system.hh" +#include "libcmd/repl.hh" +#include + +#ifdef USE_READLINE +#include +#include +#else +// editline < 1.15.2 don't wrap their API for C++ usage +// (added in https://github.com/troglobit/editline/commit/91398ceb3427b730995357e9d120539fb9bb7461). +// This results in linker errors due to to name-mangling of editline C symbols. +// For compatibility with these versions, we wrap the API here +// (wrapping multiple times on newer versions is no problem). +extern "C" { +#include +} +#endif + +#include "signals.hh" +#include "finally.hh" +#include "repl-interacter.hh" + +namespace nix { + +namespace { +// Used to communicate to NixRepl::getLine whether a signal occurred in ::readline. +volatile sig_atomic_t g_signal_received = 0; + +void sigintHandler(int signo) +{ + g_signal_received = signo; +} +}; + +static detail::ReplCompleterMixin * curRepl; // ugly + +static char * completionCallback(char * s, int * match) +{ + auto possible = curRepl->completePrefix(s); + if (possible.size() == 1) { + *match = 1; + auto * res = strdup(possible.begin()->c_str() + strlen(s)); + if (!res) + throw Error("allocation failure"); + return res; + } else if (possible.size() > 1) { + auto checkAllHaveSameAt = [&](size_t pos) { + auto & first = *possible.begin(); + for (auto & p : possible) { + if (p.size() <= pos || p[pos] != first[pos]) + return false; + } + return true; + }; + size_t start = strlen(s); + size_t len = 0; + while (checkAllHaveSameAt(start + len)) + ++len; + if (len > 0) { + *match = 1; + auto * res = strdup(std::string(*possible.begin(), start, len).c_str()); + if (!res) + throw Error("allocation failure"); + return res; + } + } + + *match = 0; + return nullptr; +} + +static int listPossibleCallback(char * s, char *** avp) +{ + auto possible = curRepl->completePrefix(s); + + if (possible.size() > (INT_MAX / sizeof(char *))) + throw Error("too many completions"); + + int ac = 0; + char ** vp = nullptr; + + auto check = [&](auto * p) { + if (!p) { + if (vp) { + while (--ac >= 0) + free(vp[ac]); + free(vp); + } + throw Error("allocation failure"); + } + return p; + }; + + vp = check((char **) malloc(possible.size() * sizeof(char *))); + + for (auto & p : possible) + vp[ac++] = check(strdup(p.c_str())); + + *avp = vp; + + return ac; +} + +ReadlineLikeInteracter::Guard ReadlineLikeInteracter::init(detail::ReplCompleterMixin * repl) +{ + // Allow nix-repl specific settings in .inputrc + rl_readline_name = "nix-repl"; + try { + createDirs(dirOf(historyFile)); + } catch (SystemError & e) { + logWarning(e.info()); + } +#ifndef USE_READLINE + el_hist_size = 1000; +#endif + read_history(historyFile.c_str()); + auto oldRepl = curRepl; + curRepl = repl; + Guard restoreRepl([oldRepl] { curRepl = oldRepl; }); +#ifndef USE_READLINE + rl_set_complete_func(completionCallback); + rl_set_list_possib_func(listPossibleCallback); +#endif + return restoreRepl; +} + +bool ReadlineLikeInteracter::getLine(std::string & input, const std::string & prompt) +{ + struct sigaction act, old; + sigset_t savedSignalMask, set; + + auto setupSignals = [&]() { + act.sa_handler = sigintHandler; + sigfillset(&act.sa_mask); + act.sa_flags = 0; + if (sigaction(SIGINT, &act, &old)) + throw SysError("installing handler for SIGINT"); + + sigemptyset(&set); + sigaddset(&set, SIGINT); + if (sigprocmask(SIG_UNBLOCK, &set, &savedSignalMask)) + throw SysError("unblocking SIGINT"); + }; + auto restoreSignals = [&]() { + if (sigprocmask(SIG_SETMASK, &savedSignalMask, nullptr)) + throw SysError("restoring signals"); + + if (sigaction(SIGINT, &old, 0)) + throw SysError("restoring handler for SIGINT"); + }; + + setupSignals(); + char * s = readline(prompt.c_str()); + Finally doFree([&]() { free(s); }); + restoreSignals(); + + if (g_signal_received) { + g_signal_received = 0; + input.clear(); + return true; + } + + if (!s) + return false; + input += s; + input += '\n'; + return true; +} + +ReadlineLikeInteracter::~ReadlineLikeInteracter() +{ + write_history(historyFile.c_str()); +} + +}; diff --git a/src/libcmd/repl-interacter.hh b/src/libcmd/repl-interacter.hh new file mode 100644 index 000000000..e549bab36 --- /dev/null +++ b/src/libcmd/repl-interacter.hh @@ -0,0 +1,48 @@ +#pragma once +/// @file + +#include "finally.hh" +#include "types.hh" +#include +#include + +namespace nix { + +namespace detail { +/** Provides the completion hooks for the repl, without exposing its complete + * internals. */ +struct ReplCompleterMixin { + virtual StringSet completePrefix(const std::string & prefix) = 0; +}; +}; + +enum class ReplPromptType { + ReplPrompt, + ContinuationPrompt, +}; + +class ReplInteracter +{ +public: + using Guard = Finally>; + + virtual Guard init(detail::ReplCompleterMixin * repl) = 0; + /** Returns a boolean of whether the interacter got EOF */ + virtual bool getLine(std::string & input, const std::string & prompt) = 0; + virtual ~ReplInteracter(){}; +}; + +class ReadlineLikeInteracter : public virtual ReplInteracter +{ + std::string historyFile; +public: + ReadlineLikeInteracter(std::string historyFile) + : historyFile(historyFile) + { + } + virtual Guard init(detail::ReplCompleterMixin * repl) override; + virtual bool getLine(std::string & input, const std::string & prompt) override; + virtual ~ReadlineLikeInteracter() override; +}; + +}; diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 8b83608fa..8af3c5ff3 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -3,32 +3,17 @@ #include #include -#include - -#ifdef USE_READLINE -#include -#include -#else -// editline < 1.15.2 don't wrap their API for C++ usage -// (added in https://github.com/troglobit/editline/commit/91398ceb3427b730995357e9d120539fb9bb7461). -// This results in linker errors due to to name-mangling of editline C symbols. -// For compatibility with these versions, we wrap the API here -// (wrapping multiple times on newer versions is no problem). -extern "C" { -#include -} -#endif - +#include "libcmd/repl-interacter.hh" #include "repl.hh" #include "ansicolor.hh" -#include "signals.hh" #include "shared.hh" #include "eval.hh" #include "eval-cache.hh" #include "eval-inline.hh" #include "eval-settings.hh" #include "attr-path.hh" +#include "signals.hh" #include "store-api.hh" #include "log-store.hh" #include "common-eval-args.hh" @@ -38,7 +23,6 @@ extern "C" { #include "flake/flake.hh" #include "flake/lockfile.hh" #include "users.hh" -#include "terminal.hh" #include "editor-for.hh" #include "finally.hh" #include "markdown.hh" @@ -75,6 +59,7 @@ enum class ProcessLineResult { struct NixRepl : AbstractNixRepl + , detail::ReplCompleterMixin #if HAVE_BOEHMGC , gc #endif @@ -90,17 +75,16 @@ struct NixRepl int displ; StringSet varNames; - const Path historyFile; + std::unique_ptr interacter; NixRepl(const SearchPath & searchPath, nix::ref store,ref state, std::function getValues); - virtual ~NixRepl(); + virtual ~NixRepl() = default; ReplExitStatus mainLoop() override; void initEnv() override; - StringSet completePrefix(const std::string & prefix); - bool getLine(std::string & input, const std::string & prompt); + virtual StringSet completePrefix(const std::string & prefix) override; StorePath getDerivationPath(Value & v); ProcessLineResult processLine(std::string line); @@ -143,16 +127,10 @@ NixRepl::NixRepl(const SearchPath & searchPath, nix::ref store, refstaticBaseEnv.get())) - , historyFile(getDataDir() + "/nix/repl-history") + , interacter(make_unique(getDataDir() + "/nix/repl-history")) { } - -NixRepl::~NixRepl() -{ - write_history(historyFile.c_str()); -} - void runNix(Path program, const Strings & args, const std::optional & input = {}) { @@ -169,79 +147,6 @@ void runNix(Path program, const Strings & args, return; } -static NixRepl * curRepl; // ugly - -static char * completionCallback(char * s, int *match) { - auto possible = curRepl->completePrefix(s); - if (possible.size() == 1) { - *match = 1; - auto *res = strdup(possible.begin()->c_str() + strlen(s)); - if (!res) throw Error("allocation failure"); - return res; - } else if (possible.size() > 1) { - auto checkAllHaveSameAt = [&](size_t pos) { - auto &first = *possible.begin(); - for (auto &p : possible) { - if (p.size() <= pos || p[pos] != first[pos]) - return false; - } - return true; - }; - size_t start = strlen(s); - size_t len = 0; - while (checkAllHaveSameAt(start + len)) ++len; - if (len > 0) { - *match = 1; - auto *res = strdup(std::string(*possible.begin(), start, len).c_str()); - if (!res) throw Error("allocation failure"); - return res; - } - } - - *match = 0; - return nullptr; -} - -static int listPossibleCallback(char *s, char ***avp) { - auto possible = curRepl->completePrefix(s); - - if (possible.size() > (INT_MAX / sizeof(char*))) - throw Error("too many completions"); - - int ac = 0; - char **vp = nullptr; - - auto check = [&](auto *p) { - if (!p) { - if (vp) { - while (--ac >= 0) - free(vp[ac]); - free(vp); - } - throw Error("allocation failure"); - } - return p; - }; - - vp = check((char **)malloc(possible.size() * sizeof(char*))); - - for (auto & p : possible) - vp[ac++] = check(strdup(p.c_str())); - - *avp = vp; - - return ac; -} - -namespace { - // Used to communicate to NixRepl::getLine whether a signal occurred in ::readline. - volatile sig_atomic_t g_signal_received = 0; - - void sigintHandler(int signo) { - g_signal_received = signo; - } -} - static std::ostream & showDebugTrace(std::ostream & out, const PosTable & positions, const DebugTrace & dt) { if (dt.isError) @@ -281,24 +186,7 @@ ReplExitStatus NixRepl::mainLoop() loadFiles(); - // Allow nix-repl specific settings in .inputrc - rl_readline_name = "nix-repl"; - try { - createDirs(dirOf(historyFile)); - } catch (SystemError & e) { - logWarning(e.info()); - } -#ifndef USE_READLINE - el_hist_size = 1000; -#endif - read_history(historyFile.c_str()); - auto oldRepl = curRepl; - curRepl = this; - Finally restoreRepl([&] { curRepl = oldRepl; }); -#ifndef USE_READLINE - rl_set_complete_func(completionCallback); - rl_set_list_possib_func(listPossibleCallback); -#endif + auto _guard = interacter->init(static_cast(this)); std::string input; @@ -307,7 +195,7 @@ ReplExitStatus NixRepl::mainLoop() logger->pause(); // When continuing input from previous lines, don't print a prompt, just align to the same // number of chars as the prompt. - if (!getLine(input, input.empty() ? "nix-repl> " : " ")) { + if (!interacter->getLine(input, input.empty() ? "nix-repl> " : " ")) { // Ctrl-D should exit the debugger. state->debugStop = false; logger->cout(""); @@ -356,51 +244,6 @@ ReplExitStatus NixRepl::mainLoop() } } - -bool NixRepl::getLine(std::string & input, const std::string & prompt) -{ - struct sigaction act, old; - sigset_t savedSignalMask, set; - - auto setupSignals = [&]() { - act.sa_handler = sigintHandler; - sigfillset(&act.sa_mask); - act.sa_flags = 0; - if (sigaction(SIGINT, &act, &old)) - throw SysError("installing handler for SIGINT"); - - sigemptyset(&set); - sigaddset(&set, SIGINT); - if (sigprocmask(SIG_UNBLOCK, &set, &savedSignalMask)) - throw SysError("unblocking SIGINT"); - }; - auto restoreSignals = [&]() { - if (sigprocmask(SIG_SETMASK, &savedSignalMask, nullptr)) - throw SysError("restoring signals"); - - if (sigaction(SIGINT, &old, 0)) - throw SysError("restoring handler for SIGINT"); - }; - - setupSignals(); - char * s = readline(prompt.c_str()); - Finally doFree([&]() { free(s); }); - restoreSignals(); - - if (g_signal_received) { - g_signal_received = 0; - input.clear(); - return true; - } - - if (!s) - return false; - input += s; - input += '\n'; - return true; -} - - StringSet NixRepl::completePrefix(const std::string & prefix) { StringSet completions; diff --git a/src/libcmd/repl.hh b/src/libcmd/repl.hh index 21aa8bfc7..aac79ec74 100644 --- a/src/libcmd/repl.hh +++ b/src/libcmd/repl.hh @@ -3,11 +3,6 @@ #include "eval.hh" -#if HAVE_BOEHMGC -#define GC_INCLUDE_NEW -#include -#endif - namespace nix { struct AbstractNixRepl From ea31b8a117e0a2e18809fd3921209d106d9040c8 Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Mon, 26 Feb 2024 00:43:44 -0800 Subject: [PATCH 008/168] refactor: repl prompts are now the job of the interacter --- src/libcmd/repl-interacter.cc | 19 +++++++++++++++---- src/libcmd/repl-interacter.hh | 4 ++-- src/libcmd/repl.cc | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/libcmd/repl-interacter.cc b/src/libcmd/repl-interacter.cc index 9aa1f7bb9..3e34ecdb6 100644 --- a/src/libcmd/repl-interacter.cc +++ b/src/libcmd/repl-interacter.cc @@ -1,5 +1,3 @@ -#include "file-system.hh" -#include "libcmd/repl.hh" #include #ifdef USE_READLINE @@ -19,6 +17,8 @@ extern "C" { #include "signals.hh" #include "finally.hh" #include "repl-interacter.hh" +#include "file-system.hh" +#include "libcmd/repl.hh" namespace nix { @@ -124,7 +124,18 @@ ReadlineLikeInteracter::Guard ReadlineLikeInteracter::init(detail::ReplCompleter return restoreRepl; } -bool ReadlineLikeInteracter::getLine(std::string & input, const std::string & prompt) +static constexpr const char * promptForType(ReplPromptType promptType) +{ + switch (promptType) { + case ReplPromptType::ReplPrompt: + return "nix-repl> "; + case ReplPromptType::ContinuationPrompt: + return " "; + } + assert(false); +} + +bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptType) { struct sigaction act, old; sigset_t savedSignalMask, set; @@ -150,7 +161,7 @@ bool ReadlineLikeInteracter::getLine(std::string & input, const std::string & pr }; setupSignals(); - char * s = readline(prompt.c_str()); + char * s = readline(promptForType(promptType)); Finally doFree([&]() { free(s); }); restoreSignals(); diff --git a/src/libcmd/repl-interacter.hh b/src/libcmd/repl-interacter.hh index e549bab36..cc70efd07 100644 --- a/src/libcmd/repl-interacter.hh +++ b/src/libcmd/repl-interacter.hh @@ -28,7 +28,7 @@ public: virtual Guard init(detail::ReplCompleterMixin * repl) = 0; /** Returns a boolean of whether the interacter got EOF */ - virtual bool getLine(std::string & input, const std::string & prompt) = 0; + virtual bool getLine(std::string & input, ReplPromptType promptType) = 0; virtual ~ReplInteracter(){}; }; @@ -41,7 +41,7 @@ public: { } virtual Guard init(detail::ReplCompleterMixin * repl) override; - virtual bool getLine(std::string & input, const std::string & prompt) override; + virtual bool getLine(std::string & input, ReplPromptType promptType) override; virtual ~ReadlineLikeInteracter() override; }; diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 8af3c5ff3..228d66f5e 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -195,7 +195,7 @@ ReplExitStatus NixRepl::mainLoop() logger->pause(); // When continuing input from previous lines, don't print a prompt, just align to the same // number of chars as the prompt. - if (!interacter->getLine(input, input.empty() ? "nix-repl> " : " ")) { + if (!interacter->getLine(input, input.empty() ? ReplPromptType::ReplPrompt : ReplPromptType::ContinuationPrompt)) { // Ctrl-D should exit the debugger. state->debugStop = false; logger->cout(""); From 60c2d15f5a7db86eb79c11c4a863789d186e8650 Mon Sep 17 00:00:00 2001 From: Bouke van der Bijl Date: Thu, 14 Mar 2024 14:04:51 +0100 Subject: [PATCH 009/168] git fetcher: use resolveRef for getting revision of reference * Add regression test * Fix 'no repo' test so it doesn't succeed if the data is still in cache * Use git_revparse_single inside git-utils instead of reimplementing the same logic. --- src/libfetchers/git-utils.cc | 24 ++++-------------------- src/libfetchers/git.cc | 2 +- tests/functional/fetchGit.sh | 9 +++++++++ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/libfetchers/git-utils.cc b/src/libfetchers/git-utils.cc index 9cae9034e..b723554cc 100644 --- a/src/libfetchers/git-utils.cc +++ b/src/libfetchers/git-utils.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -199,27 +200,10 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this Hash resolveRef(std::string ref) override { - // Handle revisions used as refs. - { - git_oid oid; - if (git_oid_fromstr(&oid, ref.c_str()) == 0) - return toHash(oid); - } - - // Resolve short names like 'master'. - Reference ref2; - if (!git_reference_dwim(Setter(ref2), *this, ref.c_str())) - ref = git_reference_name(ref2.get()); - - // Resolve full references like 'refs/heads/master'. - Reference ref3; - if (git_reference_lookup(Setter(ref3), *this, ref.c_str())) + Object object; + if (git_revparse_single(Setter(object), *this, ref.c_str())) throw Error("resolving Git reference '%s': %s", ref, git_error_last()->message); - - auto oid = git_reference_target(ref3.get()); - if (!oid) - throw Error("cannot get OID for Git reference '%s'", git_reference_name(ref3.get())); - + auto oid = git_object_id(object.get()); return toHash(*oid); } diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc index 25eabb1dc..34cfd3f5b 100644 --- a/src/libfetchers/git.cc +++ b/src/libfetchers/git.cc @@ -585,7 +585,7 @@ struct GitInputScheme : InputScheme repoInfo.url ); } else - input.attrs.insert_or_assign("rev", Hash::parseAny(chomp(readFile(localRefFile)), HashAlgorithm::SHA1).gitRev()); + input.attrs.insert_or_assign("rev", repo->resolveRef(ref).gitRev()); // cache dir lock is removed at scope end; we will only use read-only operations on specific revisions in the remainder } diff --git a/tests/functional/fetchGit.sh b/tests/functional/fetchGit.sh index 3f2d0d5fb..74d6de4e3 100644 --- a/tests/functional/fetchGit.sh +++ b/tests/functional/fetchGit.sh @@ -43,10 +43,18 @@ path0_=$(nix eval --impure --raw --expr "(builtins.fetchTree git+file://$TEST_RO export _NIX_FORCE_HTTP=1 [[ $(tail -n 1 $path0/hello) = "hello" ]] +# Nuke the cache +rm -rf $TEST_HOME/.cache/nix + # Fetch the default branch. path=$(nix eval --impure --raw --expr "(builtins.fetchGit file://$repo).outPath") [[ $(cat $path/hello) = world ]] +# Fetch when the cache has packed-refs +# Regression test of #8822 +git -C $TEST_HOME/.cache/nix/gitv3/*/ pack-refs --all +path=$(nix eval --impure --raw --expr "(builtins.fetchGit file://$repo).outPath") + # Fetch a rev from another branch git -C $repo checkout -b devtest echo "different file" >> $TEST_ROOT/git/differentbranch @@ -251,6 +259,7 @@ path12=$(nix eval --impure --raw --expr "(builtins.fetchGit { url = file://$repo # should fail if there is no repo rm -rf $repo/.git +rm -rf $TEST_HOME/.cache/nix (! nix eval --impure --raw --expr "(builtins.fetchGit \"file://$repo\").outPath") # should succeed for a repo without commits From fecff520d7ce6598319862efc50c2dc6e1f6e9d9 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 14 Mar 2024 19:10:31 +0100 Subject: [PATCH 010/168] Add a ListBuilder helper for constructing list values Previously, `state.mkList()` would set the type of the value to tList and allocate the list vector, but it would not initialize the values in the list. This has two problems: * If an exception occurs, the list is left in an undefined state. * More importantly, for multithreaded evaluation, if a value transitions from thunk to non-thunk, it should be final (i.e. other threads should be able to access the value safely). To address this, there now is a `ListBuilder` class (analogous to `BindingsBuilder`) to build the list vector prior to the call to `Value::mkList()`. Typical usage: auto list = state.buildList(size); for (auto & v : list) v = ... set value ...; vRes.mkList(list); --- src/libexpr/eval.cc | 24 ++-- src/libexpr/eval.hh | 12 +- src/libexpr/json-to-value.cc | 9 +- src/libexpr/primops.cc | 202 ++++++++++++++++-------------- src/libexpr/primops/context.cc | 6 +- src/libexpr/primops/fromTOML.cc | 8 +- src/libexpr/value.hh | 43 ++++++- src/nix-env/nix-env.cc | 2 +- src/nix-env/user-env.cc | 17 +-- tests/unit/libexpr/value/print.cc | 62 +++++---- 10 files changed, 228 insertions(+), 157 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index bbccfcd29..297832818 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -435,7 +435,8 @@ EvalState::EvalState( static_assert(sizeof(Env) <= 16, "environment must be <= 16 bytes"); - vEmptyList.mkList(0); + vEmptyList.mkList(buildList(0)); + vNull.mkNull(); /* Initialise the Nix expression search path. */ if (!evalSettings.pureEval) { @@ -923,12 +924,11 @@ inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval) } } -void EvalState::mkList(Value & v, size_t size) +ListBuilder::ListBuilder(EvalState & state, size_t size) + : size(size) + , elems(size <= 2 ? inlineElems : (Value * *) allocBytes(size * sizeof(Value *))) { - v.mkList(size); - if (size > 2) - v.bigList.elems = (Value * *) allocBytes(size * sizeof(Value *)); - nrListElems += size; + state.nrListElems += size; } @@ -1353,9 +1353,10 @@ void ExprLet::eval(EvalState & state, Env & env, Value & v) void ExprList::eval(EvalState & state, Env & env, Value & v) { - state.mkList(v, elems.size()); - for (auto [n, v2] : enumerate(v.listItems())) - const_cast(v2) = elems[n]->maybeThunk(state, env); + auto list = state.buildList(elems.size()); + for (const auto & [n, v2] : enumerate(list)) + v2 = elems[n]->maybeThunk(state, env); + v.mkList(list); } @@ -1963,14 +1964,15 @@ void EvalState::concatLists(Value & v, size_t nrLists, Value * * lists, const Po return; } - mkList(v, len); - auto out = v.listElems(); + auto list = buildList(len); + auto out = list.elems; for (size_t n = 0, pos = 0; n < nrLists; ++n) { auto l = lists[n]->listSize(); if (l) memcpy(out + pos, lists[n]->listElems(), l * sizeof(Value *)); pos += l; } + v.mkList(list); } diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 368bb17b3..4a271f4ef 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -186,6 +186,11 @@ public: */ Value vEmptyList; + /** + * Null constant. + */ + Value vNull; + /** * The accessor for the root filesystem. */ @@ -615,7 +620,11 @@ public: return BindingsBuilder(*this, allocBindings(capacity)); } - void mkList(Value & v, size_t length); + ListBuilder buildList(size_t size) + { + return ListBuilder(*this, size); + } + void mkThunk_(Value & v, Expr * expr); void mkPos(Value & v, PosIdx pos); @@ -756,6 +765,7 @@ private: friend void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v); friend struct Value; + friend class ListBuilder; }; struct DebugTraceStacker { diff --git a/src/libexpr/json-to-value.cc b/src/libexpr/json-to-value.cc index 2d12c47c5..20bee193f 100644 --- a/src/libexpr/json-to-value.cc +++ b/src/libexpr/json-to-value.cc @@ -57,11 +57,10 @@ class JSONSax : nlohmann::json_sax { ValueVector values; std::unique_ptr resolve(EvalState & state) override { - Value & v = parent->value(state); - state.mkList(v, values.size()); - for (size_t n = 0; n < values.size(); ++n) { - v.listElems()[n] = values[n]; - } + auto list = state.buildList(values.size()); + for (const auto & [n, v2] : enumerate(list)) + v2 = values[n]; + parent->value(state).mkList(list); return std::move(parent); } void add() override { diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index bc2a70496..32913d72e 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -187,13 +187,13 @@ static void import(EvalState & state, const PosIdx pos, Value & vPath, Value * v NixStringContextElem::DrvDeep { .drvPath = *storePath }, }); attrs.alloc(state.sName).mkString(drv.env["name"]); - auto & outputsVal = attrs.alloc(state.sOutputs); - state.mkList(outputsVal, drv.outputs.size()); + auto list = state.buildList(drv.outputs.size()); for (const auto & [i, o] : enumerate(drv.outputs)) { mkOutputString(state, attrs, *storePath, o); - (outputsVal.listElems()[i] = state.allocValue())->mkString(o.first); + (list[i] = state.allocValue())->mkString(o.first); } + attrs.alloc(state.sOutputs).mkList(list); auto w = state.allocValue(); w->mkAttrs(attrs); @@ -694,10 +694,10 @@ static void prim_genericClosure(EvalState & state, const PosIdx pos, Value * * a } /* Create the result list. */ - state.mkList(v, res.size()); - unsigned int n = 0; - for (auto & i : res) - v.listElems()[n++] = i; + auto list = state.buildList(res.size()); + for (const auto & [n, i] : enumerate(res)) + list[n] = i; + v.mkList(list); } static RegisterPrimOp primop_genericClosure(PrimOp { @@ -2423,14 +2423,15 @@ static void prim_attrNames(EvalState & state, const PosIdx pos, Value * * args, { state.forceAttrs(*args[0], pos, "while evaluating the argument passed to builtins.attrNames"); - state.mkList(v, args[0]->attrs->size()); + auto list = state.buildList(args[0]->attrs->size()); - size_t n = 0; - for (auto & i : *args[0]->attrs) - (v.listElems()[n++] = state.allocValue())->mkString(state.symbols[i.name]); + for (const auto & [n, i] : enumerate(*args[0]->attrs)) + (list[n] = state.allocValue())->mkString(state.symbols[i.name]); - std::sort(v.listElems(), v.listElems() + n, + std::sort(list.begin(), list.end(), [](Value * v1, Value * v2) { return strcmp(v1->c_str(), v2->c_str()) < 0; }); + + v.mkList(list); } static RegisterPrimOp primop_attrNames({ @@ -2450,21 +2451,22 @@ static void prim_attrValues(EvalState & state, const PosIdx pos, Value * * args, { state.forceAttrs(*args[0], pos, "while evaluating the argument passed to builtins.attrValues"); - state.mkList(v, args[0]->attrs->size()); + auto list = state.buildList(args[0]->attrs->size()); - unsigned int n = 0; - for (auto & i : *args[0]->attrs) - v.listElems()[n++] = (Value *) &i; + for (const auto & [n, i] : enumerate(*args[0]->attrs)) + list[n] = (Value *) &i; - std::sort(v.listElems(), v.listElems() + n, + std::sort(list.begin(), list.end(), [&](Value * v1, Value * v2) { std::string_view s1 = state.symbols[((Attr *) v1)->name], s2 = state.symbols[((Attr *) v2)->name]; return s1 < s2; }); - for (unsigned int i = 0; i < n; ++i) - v.listElems()[i] = ((Attr *) v.listElems()[i])->value; + for (auto & v : list) + v = ((Attr *) v)->value; + + v.mkList(list); } static RegisterPrimOp primop_attrValues({ @@ -2805,9 +2807,10 @@ static void prim_catAttrs(EvalState & state, const PosIdx pos, Value * * args, V res[found++] = i->value; } - state.mkList(v, found); + auto list = state.buildList(found); for (unsigned int n = 0; n < found; ++n) - v.listElems()[n] = res[n]; + list[n] = res[n]; + v.mkList(list); } static RegisterPrimOp primop_catAttrs({ @@ -2908,43 +2911,50 @@ static void prim_zipAttrsWith(EvalState & state, const PosIdx pos, Value * * arg // attribute with the merge function application. this way we need not // use (slightly slower) temporary storage the GC does not know about. - std::map> attrsSeen; + struct Item + { + size_t size = 0; + size_t pos = 0; + std::optional list; + }; + + std::map attrsSeen; state.forceFunction(*args[0], pos, "while evaluating the first argument passed to builtins.zipAttrsWith"); state.forceList(*args[1], pos, "while evaluating the second argument passed to builtins.zipAttrsWith"); - const auto listSize = args[1]->listSize(); - const auto listElems = args[1]->listElems(); + const auto listItems = args[1]->listItems(); - for (unsigned int n = 0; n < listSize; ++n) { - Value * vElem = listElems[n]; + for (auto & vElem : listItems) { state.forceAttrs(*vElem, noPos, "while evaluating a value of the list passed as second argument to builtins.zipAttrsWith"); for (auto & attr : *vElem->attrs) - attrsSeen[attr.name].first++; + attrsSeen.try_emplace(attr.name).first->second.size++; + } + + for (auto & [sym, elem] : attrsSeen) + elem.list.emplace(state.buildList(elem.size)); + + for (auto & vElem : listItems) { + for (auto & attr : *vElem->attrs) { + auto & item = attrsSeen.at(attr.name); + (*item.list)[item.pos++] = attr.value; + } } auto attrs = state.buildBindings(attrsSeen.size()); + for (auto & [sym, elem] : attrsSeen) { - auto & list = attrs.alloc(sym); - state.mkList(list, elem.first); - elem.second = list.listElems(); - } - v.mkAttrs(attrs.alreadySorted()); - - for (unsigned int n = 0; n < listSize; ++n) { - Value * vElem = listElems[n]; - for (auto & attr : *vElem->attrs) - *attrsSeen[attr.name].second++ = attr.value; - } - - for (auto & attr : *v.attrs) { auto name = state.allocValue(); - name->mkString(state.symbols[attr.name]); + name->mkString(state.symbols[sym]); auto call1 = state.allocValue(); call1->mkApp(args[0], name); auto call2 = state.allocValue(); - call2->mkApp(call1, attr.value); - attr.value = call2; + auto arg = state.allocValue(); + arg->mkList(*elem.list); + call2->mkApp(call1, arg); + attrs.insert(sym, call2); } + + v.mkAttrs(attrs.alreadySorted()); } static RegisterPrimOp primop_zipAttrsWith({ @@ -3055,9 +3065,10 @@ static void prim_tail(EvalState & state, const PosIdx pos, Value * * args, Value if (args[0]->listSize() == 0) state.error("'tail' called on an empty list").atPos(pos).debugThrow(); - state.mkList(v, args[0]->listSize() - 1); - for (unsigned int n = 0; n < v.listSize(); ++n) - v.listElems()[n] = args[0]->listElems()[n + 1]; + auto list = state.buildList(args[0]->listSize() - 1); + for (const auto & [n, v] : enumerate(list)) + v = args[0]->listElems()[n + 1]; + v.mkList(list); } static RegisterPrimOp primop_tail({ @@ -3088,10 +3099,11 @@ static void prim_map(EvalState & state, const PosIdx pos, Value * * args, Value state.forceFunction(*args[0], pos, "while evaluating the first argument passed to builtins.map"); - state.mkList(v, args[1]->listSize()); - for (unsigned int n = 0; n < v.listSize(); ++n) - (v.listElems()[n] = state.allocValue())->mkApp( + auto list = state.buildList(args[1]->listSize()); + for (const auto & [n, v] : enumerate(list)) + (v = state.allocValue())->mkApp( args[0], args[1]->listElems()[n]); + v.mkList(list); } static RegisterPrimOp primop_map({ @@ -3140,8 +3152,9 @@ static void prim_filter(EvalState & state, const PosIdx pos, Value * * args, Val if (same) v = *args[1]; else { - state.mkList(v, k); - for (unsigned int n = 0; n < k; ++n) v.listElems()[n] = vs[n]; + auto list = state.buildList(k); + for (const auto & [n, v] : enumerate(list)) v = vs[n]; + v.mkList(list); } } @@ -3316,12 +3329,13 @@ static void prim_genList(EvalState & state, const PosIdx pos, Value * * args, Va // as evaluating map without accessing any values makes little sense. state.forceFunction(*args[0], noPos, "while evaluating the first argument passed to builtins.genList"); - state.mkList(v, len); - for (unsigned int n = 0; n < (unsigned int) len; ++n) { + auto list = state.buildList(len); + for (const auto & [n, v] : enumerate(list)) { auto arg = state.allocValue(); arg->mkInt(n); - (v.listElems()[n] = state.allocValue())->mkApp(args[0], arg); + (v = state.allocValue())->mkApp(args[0], arg); } + v.mkList(list); } static RegisterPrimOp primop_genList({ @@ -3355,11 +3369,10 @@ static void prim_sort(EvalState & state, const PosIdx pos, Value * * args, Value state.forceFunction(*args[0], pos, "while evaluating the first argument passed to builtins.sort"); - state.mkList(v, len); - for (unsigned int n = 0; n < len; ++n) { - state.forceValue(*args[1]->listElems()[n], pos); - v.listElems()[n] = args[1]->listElems()[n]; - } + auto list = state.buildList(len); + for (const auto & [n, v] : enumerate(list)) + state.forceValue(*(v = args[1]->listElems()[n]), pos); + v.mkList(list); auto comparator = [&](Value * a, Value * b) { /* Optimization: if the comparator is lessThan, bypass @@ -3424,17 +3437,17 @@ static void prim_partition(EvalState & state, const PosIdx pos, Value * * args, auto attrs = state.buildBindings(2); - auto & vRight = attrs.alloc(state.sRight); auto rsize = right.size(); - state.mkList(vRight, rsize); + auto rlist = state.buildList(rsize); if (rsize) - memcpy(vRight.listElems(), right.data(), sizeof(Value *) * rsize); + memcpy(rlist.elems, right.data(), sizeof(Value *) * rsize); + attrs.alloc(state.sRight).mkList(rlist); - auto & vWrong = attrs.alloc(state.sWrong); auto wsize = wrong.size(); - state.mkList(vWrong, wsize); + auto wlist = state.buildList(wsize); if (wsize) - memcpy(vWrong.listElems(), wrong.data(), sizeof(Value *) * wsize); + memcpy(wlist.elems, wrong.data(), sizeof(Value *) * wsize); + attrs.alloc(state.sWrong).mkList(wlist); v.mkAttrs(attrs); } @@ -3481,10 +3494,10 @@ static void prim_groupBy(EvalState & state, const PosIdx pos, Value * * args, Va auto attrs2 = state.buildBindings(attrs.size()); for (auto & i : attrs) { - auto & list = attrs2.alloc(i.first); auto size = i.second.size(); - state.mkList(list, size); - memcpy(list.listElems(), i.second.data(), sizeof(Value *) * size); + auto list = state.buildList(size); + memcpy(list.elems, i.second.data(), sizeof(Value *) * size); + attrs2.alloc(i.first).mkList(list); } v.mkAttrs(attrs2.alreadySorted()); @@ -3531,14 +3544,15 @@ static void prim_concatMap(EvalState & state, const PosIdx pos, Value * * args, len += lists[n].listSize(); } - state.mkList(v, len); - auto out = v.listElems(); + auto list = state.buildList(len); + auto out = list.elems; for (unsigned int n = 0, pos = 0; n < nrLists; ++n) { auto l = lists[n].listSize(); if (l) memcpy(out + pos, lists[n].listElems(), l * sizeof(Value *)); pos += l; } + v.mkList(list); } static RegisterPrimOp primop_concatMap({ @@ -3986,14 +4000,13 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v) } // the first match is the whole string - const size_t len = match.size() - 1; - state.mkList(v, len); - for (size_t i = 0; i < len; ++i) { - if (!match[i+1].matched) - (v.listElems()[i] = state.allocValue())->mkNull(); + auto list = state.buildList(match.size() - 1); + for (const auto & [i, v2] : enumerate(list)) + if (!match[i + 1].matched) + (v2 = state.allocValue())->mkNull(); else - (v.listElems()[i] = state.allocValue())->mkString(match[i + 1].str()); - } + (v2 = state.allocValue())->mkString(match[i + 1].str()); + v.mkList(list); } catch (std::regex_error & e) { if (e.code() == std::regex_constants::error_space) { @@ -4062,11 +4075,12 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v) // Any matches results are surrounded by non-matching results. const size_t len = std::distance(begin, end); - state.mkList(v, 2 * len + 1); + auto list = state.buildList(2 * len + 1); size_t idx = 0; if (len == 0) { - v.listElems()[idx++] = args[1]; + list[0] = args[1]; + v.mkList(list); return; } @@ -4075,28 +4089,31 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v) auto match = *i; // Add a string for non-matched characters. - (v.listElems()[idx++] = state.allocValue())->mkString(match.prefix().str()); + (list[idx++] = state.allocValue())->mkString(match.prefix().str()); // Add a list for matched substrings. const size_t slen = match.size() - 1; - auto elem = v.listElems()[idx++] = state.allocValue(); // Start at 1, beacause the first match is the whole string. - state.mkList(*elem, slen); - for (size_t si = 0; si < slen; ++si) { + auto list2 = state.buildList(slen); + for (const auto & [si, v2] : enumerate(list2)) { if (!match[si + 1].matched) - (elem->listElems()[si] = state.allocValue())->mkNull(); + v2 = &state.vNull; else - (elem->listElems()[si] = state.allocValue())->mkString(match[si + 1].str()); + (v2 = state.allocValue())->mkString(match[si + 1].str()); } + (list[idx++] = state.allocValue())->mkList(list2); + // Add a string for non-matched suffix characters. if (idx == 2 * len) - (v.listElems()[idx++] = state.allocValue())->mkString(match.suffix().str()); + (list[idx++] = state.allocValue())->mkString(match.suffix().str()); } assert(idx == 2 * len + 1); + v.mkList(list); + } catch (std::regex_error & e) { if (e.code() == std::regex_constants::error_space) { // limit is _GLIBCXX_REGEX_STATE_LIMIT for libstdc++ @@ -4316,9 +4333,10 @@ static void prim_splitVersion(EvalState & state, const PosIdx pos, Value * * arg break; components.emplace_back(component); } - state.mkList(v, components.size()); + auto list = state.buildList(components.size()); for (const auto & [n, component] : enumerate(components)) - (v.listElems()[n] = state.allocValue())->mkString(std::move(component)); + (list[n] = state.allocValue())->mkString(std::move(component)); + v.mkList(list); } static RegisterPrimOp primop_splitVersion({ @@ -4559,14 +4577,14 @@ void EvalState::createBaseEnv() }); /* Add a value containing the current Nix expression search path. */ - mkList(v, searchPath.elements.size()); - int n = 0; - for (auto & i : searchPath.elements) { + auto list = buildList(searchPath.elements.size()); + for (const auto & [n, i] : enumerate(searchPath.elements)) { auto attrs = buildBindings(2); attrs.alloc("path").mkString(i.path.s); attrs.alloc("prefix").mkString(i.prefix.s); - (v.listElems()[n++] = allocValue())->mkAttrs(attrs); + (list[n] = allocValue())->mkAttrs(attrs); } + v.mkList(list); addConstant("__nixPath", v, { .type = nList, .doc = R"( diff --git a/src/libexpr/primops/context.cc b/src/libexpr/primops/context.cc index 1eec8b316..4d000b2ce 100644 --- a/src/libexpr/primops/context.cc +++ b/src/libexpr/primops/context.cc @@ -207,10 +207,10 @@ static void prim_getContext(EvalState & state, const PosIdx pos, Value * * args, if (info.second.allOutputs) infoAttrs.alloc(sAllOutputs).mkBool(true); if (!info.second.outputs.empty()) { - auto & outputsVal = infoAttrs.alloc(state.sOutputs); - state.mkList(outputsVal, info.second.outputs.size()); + auto list = state.buildList(info.second.outputs.size()); for (const auto & [i, output] : enumerate(info.second.outputs)) - (outputsVal.listElems()[i] = state.allocValue())->mkString(output); + (list[i] = state.allocValue())->mkString(output); + infoAttrs.alloc(state.sOutputs).mkList(list); } attrs.alloc(state.store->printStorePath(info.first)).mkAttrs(infoAttrs); } diff --git a/src/libexpr/primops/fromTOML.cc b/src/libexpr/primops/fromTOML.cc index 94be7960a..9bee8ca38 100644 --- a/src/libexpr/primops/fromTOML.cc +++ b/src/libexpr/primops/fromTOML.cc @@ -38,10 +38,10 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V { auto array = toml::get>(t); - size_t size = array.size(); - state.mkList(v, size); - for (size_t i = 0; i < size; ++i) - visit(*(v.listElems()[i] = state.allocValue()), array[i]); + auto list = state.buildList(array.size()); + for (const auto & [n, v] : enumerate(list)) + visit(*(v = state.allocValue()), array[n]); + v.mkList(list); } break;; case toml::value_t::boolean: diff --git a/src/libexpr/value.hh b/src/libexpr/value.hh index e7aea4949..9f0600efb 100644 --- a/src/libexpr/value.hh +++ b/src/libexpr/value.hh @@ -18,6 +18,7 @@ namespace nix { +struct Value; class BindingsBuilder; @@ -134,6 +135,34 @@ class ExternalValueBase std::ostream & operator << (std::ostream & str, const ExternalValueBase & v); +class ListBuilder +{ + const size_t size; + Value * inlineElems[2] = {nullptr, nullptr}; +public: + Value * * elems; + ListBuilder(EvalState & state, size_t size); + + ListBuilder(ListBuilder && x) + : size(x.size) + , inlineElems{x.inlineElems[0], x.inlineElems[1]} + , elems(size <= 2 ? inlineElems : x.elems) + { } + + Value * & operator [](size_t n) + { + return elems[n]; + } + + typedef Value * * iterator; + + iterator begin() { return &elems[0]; } + iterator end() { return &elems[size]; } + + friend class Value; +}; + + struct Value { private: @@ -323,16 +352,20 @@ public: Value & mkAttrs(BindingsBuilder & bindings); - inline void mkList(size_t size) + void mkList(const ListBuilder & builder) { clearValue(); - if (size == 1) + if (builder.size == 1) { + smallList[0] = builder.inlineElems[0]; internalType = tList1; - else if (size == 2) + } else if (builder.size == 2) { + smallList[0] = builder.inlineElems[0]; + smallList[1] = builder.inlineElems[1]; internalType = tList2; - else { + } else { + bigList.size = builder.size; + bigList.elems = builder.elems; internalType = tListN; - bigList.size = size; } } diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 5e3de20c5..f79755375 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -172,7 +172,7 @@ static void loadSourceExpr(EvalState & state, const SourcePath & path, Value & v directory). */ else if (st.type == InputAccessor::tDirectory) { auto attrs = state.buildBindings(maxAttrs); - state.mkList(attrs.alloc("_combineChannels"), 0); + attrs.insert(state.symbols.create("_combineChannels"), &state.vEmptyList); StringSet seen; getAllExprs(state, path, seen, attrs); v.mkAttrs(attrs); diff --git a/src/nix-env/user-env.cc b/src/nix-env/user-env.cc index 8bebe2b9e..dd27344aa 100644 --- a/src/nix-env/user-env.cc +++ b/src/nix-env/user-env.cc @@ -49,10 +49,8 @@ bool createUserEnv(EvalState & state, PackageInfos & elems, /* Construct the whole top level derivation. */ StorePathSet references; - Value manifest; - state.mkList(manifest, elems.size()); - size_t n = 0; - for (auto & i : elems) { + auto list = state.buildList(elems.size()); + for (const auto & [n, i] : enumerate(elems)) { /* Create a pseudo-derivation containing the name, system, output paths, and optionally the derivation path, as well as the meta attributes. */ @@ -72,10 +70,9 @@ bool createUserEnv(EvalState & state, PackageInfos & elems, attrs.alloc(state.sDrvPath).mkString(state.store->printStorePath(*drvPath)); // Copy each output meant for installation. - auto & vOutputs = attrs.alloc(state.sOutputs); - state.mkList(vOutputs, outputs.size()); + auto outputsList = state.buildList(outputs.size()); for (const auto & [m, j] : enumerate(outputs)) { - (vOutputs.listElems()[m] = state.allocValue())->mkString(j.first); + (outputsList[m] = state.allocValue())->mkString(j.first); auto outputAttrs = state.buildBindings(2); outputAttrs.alloc(state.sOutPath).mkString(state.store->printStorePath(*j.second)); attrs.alloc(j.first).mkAttrs(outputAttrs); @@ -87,6 +84,7 @@ bool createUserEnv(EvalState & state, PackageInfos & elems, references.insert(*j.second); } + attrs.alloc(state.sOutputs).mkList(outputsList); // Copy the meta attributes. auto meta = state.buildBindings(metaNames.size()); @@ -98,11 +96,14 @@ bool createUserEnv(EvalState & state, PackageInfos & elems, attrs.alloc(state.sMeta).mkAttrs(meta); - (manifest.listElems()[n++] = state.allocValue())->mkAttrs(attrs); + (list[n] = state.allocValue())->mkAttrs(attrs); if (drvPath) references.insert(*drvPath); } + Value manifest; + manifest.mkList(list); + /* Also write a copy of the list of user environment elements to the store; we need it for future modifications of the environment. */ diff --git a/tests/unit/libexpr/value/print.cc b/tests/unit/libexpr/value/print.cc index d2d699a64..43b545035 100644 --- a/tests/unit/libexpr/value/print.cc +++ b/tests/unit/libexpr/value/print.cc @@ -79,11 +79,11 @@ TEST_F(ValuePrintingTests, tList) Value vTwo; vTwo.mkInt(2); + auto list = state.buildList(3); + list.elems[0] = &vOne; + list.elems[1] = &vTwo; Value vList; - state.mkList(vList, 5); - vList.bigList.elems[0] = &vOne; - vList.bigList.elems[1] = &vTwo; - vList.bigList.size = 3; + vList.mkList(list); test(vList, "[ 1 2 «nullptr» ]"); } @@ -249,12 +249,12 @@ TEST_F(ValuePrintingTests, depthList) Value vNested; vNested.mkAttrs(builder2.finish()); + auto list = state.buildList(3); + list.elems[0] = &vOne; + list.elems[1] = &vTwo; + list.elems[2] = &vNested; Value vList; - state.mkList(vList, 5); - vList.bigList.elems[0] = &vOne; - vList.bigList.elems[1] = &vTwo; - vList.bigList.elems[2] = &vNested; - vList.bigList.size = 3; + vList.mkList(list); test(vList, "[ 1 2 { ... } ]", PrintOptions { .maxDepth = 1 }); test(vList, "[ 1 2 { nested = { ... }; one = 1; two = 2; } ]", PrintOptions { .maxDepth = 2 }); @@ -539,11 +539,11 @@ TEST_F(ValuePrintingTests, ansiColorsList) Value vTwo; vTwo.mkInt(2); + auto list = state.buildList(3); + list.elems[0] = &vOne; + list.elems[1] = &vTwo; Value vList; - state.mkList(vList, 5); - vList.bigList.elems[0] = &vOne; - vList.bigList.elems[1] = &vTwo; - vList.bigList.size = 3; + vList.mkList(list); test(vList, "[ " ANSI_CYAN "1" ANSI_NORMAL " " ANSI_CYAN "2" ANSI_NORMAL " " ANSI_MAGENTA "«nullptr»" ANSI_NORMAL " ]", @@ -670,11 +670,11 @@ TEST_F(ValuePrintingTests, ansiColorsListRepeated) Value vEmpty; vEmpty.mkAttrs(emptyBuilder.finish()); + auto list = state.buildList(2); + list.elems[0] = &vEmpty; + list.elems[1] = &vEmpty; Value vList; - state.mkList(vList, 3); - vList.bigList.elems[0] = &vEmpty; - vList.bigList.elems[1] = &vEmpty; - vList.bigList.size = 2; + vList.mkList(list); test(vList, "[ { } " ANSI_MAGENTA "«repeated»" ANSI_NORMAL " ]", @@ -690,11 +690,11 @@ TEST_F(ValuePrintingTests, listRepeated) Value vEmpty; vEmpty.mkAttrs(emptyBuilder.finish()); + auto list = state.buildList(2); + list.elems[0] = &vEmpty; + list.elems[1] = &vEmpty; Value vList; - state.mkList(vList, 3); - vList.bigList.elems[0] = &vEmpty; - vList.bigList.elems[1] = &vEmpty; - vList.bigList.size = 2; + vList.mkList(list); test(vList, "[ { } «repeated» ]", PrintOptions { }); test(vList, @@ -750,11 +750,12 @@ TEST_F(ValuePrintingTests, ansiColorsListElided) Value vTwo; vTwo.mkInt(2); + { + auto list = state.buildList(2); + list.elems[0] = &vOne; + list.elems[1] = &vTwo; Value vList; - state.mkList(vList, 4); - vList.bigList.elems[0] = &vOne; - vList.bigList.elems[1] = &vTwo; - vList.bigList.size = 2; + vList.mkList(list); test(vList, "[ " ANSI_CYAN "1" ANSI_NORMAL " " ANSI_FAINT "«1 item elided»" ANSI_NORMAL " ]", @@ -762,12 +763,18 @@ TEST_F(ValuePrintingTests, ansiColorsListElided) .ansiColors = true, .maxListItems = 1 }); + } Value vThree; vThree.mkInt(3); - vList.bigList.elems[2] = &vThree; - vList.bigList.size = 3; + { + auto list = state.buildList(3); + list.elems[0] = &vOne; + list.elems[1] = &vTwo; + list.elems[2] = &vThree; + Value vList; + vList.mkList(list); test(vList, "[ " ANSI_CYAN "1" ANSI_NORMAL " " ANSI_FAINT "«2 items elided»" ANSI_NORMAL " ]", @@ -775,6 +782,7 @@ TEST_F(ValuePrintingTests, ansiColorsListElided) .ansiColors = true, .maxListItems = 1 }); + } } } // namespace nix From 3e6730ee62b10acff12e7dd66e1269d7a9da7a21 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 15 Mar 2024 18:22:39 +0100 Subject: [PATCH 011/168] Mark Value pointers in Value::elems as const This catches modification of finalized values (e.g. in prim_sort). --- src/libexpr/eval.cc | 2 +- src/libexpr/eval.hh | 2 +- src/libexpr/primops.cc | 5 +++-- src/libexpr/value.hh | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 297832818..fb4cfdccf 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -1946,7 +1946,7 @@ void ExprOpConcatLists::eval(EvalState & state, Env & env, Value & v) } -void EvalState::concatLists(Value & v, size_t nrLists, Value * * lists, const PosIdx pos, std::string_view errorCtx) +void EvalState::concatLists(Value & v, size_t nrLists, Value * const * lists, const PosIdx pos, std::string_view errorCtx) { nrListConcats++; diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 4a271f4ef..7db911fce 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -671,7 +671,7 @@ public: const SingleDerivedPath & p, Value & v); - void concatLists(Value & v, size_t nrLists, Value * * lists, const PosIdx pos, std::string_view errorCtx); + void concatLists(Value & v, size_t nrLists, Value * const * lists, const PosIdx pos, std::string_view errorCtx); /** * Print statistics, if enabled. diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 32913d72e..9449a8f7c 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -3372,7 +3372,6 @@ static void prim_sort(EvalState & state, const PosIdx pos, Value * * args, Value auto list = state.buildList(len); for (const auto & [n, v] : enumerate(list)) state.forceValue(*(v = args[1]->listElems()[n]), pos); - v.mkList(list); auto comparator = [&](Value * a, Value * b) { /* Optimization: if the comparator is lessThan, bypass @@ -3391,7 +3390,9 @@ static void prim_sort(EvalState & state, const PosIdx pos, Value * * args, Value /* FIXME: std::sort can segfault if the comparator is not a strict weak ordering. What to do? std::stable_sort() seems more resilient, but no guarantees... */ - std::stable_sort(v.listElems(), v.listElems() + len, comparator); + std::stable_sort(list.begin(), list.end(), comparator); + + v.mkList(list); } static RegisterPrimOp primop_sort({ diff --git a/src/libexpr/value.hh b/src/libexpr/value.hh index 9f0600efb..885621cf5 100644 --- a/src/libexpr/value.hh +++ b/src/libexpr/value.hh @@ -246,7 +246,7 @@ public: Bindings * attrs; struct { size_t size; - Value * * elems; + Value * const * elems; } bigList; Value * smallList[2]; ClosureThunk thunk; @@ -425,7 +425,7 @@ public: return internalType == tList1 || internalType == tList2 || internalType == tListN; } - Value * * listElems() + Value * const * listElems() { return internalType == tList1 || internalType == tList2 ? smallList : bigList.elems; } From ad217ddbbc759f1b67bf1368032f364ee483b790 Mon Sep 17 00:00:00 2001 From: lelgenio Date: Mon, 18 Mar 2024 16:23:17 -0300 Subject: [PATCH 012/168] Document builtins.storePath being disabled by pure-eval --- src/libexpr/eval-settings.hh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libexpr/eval-settings.hh b/src/libexpr/eval-settings.hh index c5581b9ff..60d3a6f25 100644 --- a/src/libexpr/eval-settings.hh +++ b/src/libexpr/eval-settings.hh @@ -76,9 +76,10 @@ struct EvalSettings : Config - Restrict file system and network access to files specified by cryptographic hash - Disable impure constants: - - [`bultins.currentSystem`](@docroot@/language/builtin-constants.md#builtins-currentSystem) + - [`builtins.currentSystem`](@docroot@/language/builtin-constants.md#builtins-currentSystem) - [`builtins.currentTime`](@docroot@/language/builtin-constants.md#builtins-currentTime) - [`builtins.nixPath`](@docroot@/language/builtin-constants.md#builtins-nixPath) + - [`builtins.storePath`](@docroot@/language/builtin-constants.md#builtins-storePath) )" }; From a5262fb880826d278477b77a595b571559476030 Mon Sep 17 00:00:00 2001 From: Jonathan Ringer Date: Tue, 19 Mar 2024 17:37:04 -0700 Subject: [PATCH 013/168] Document how to build many outputs of a flake package --- src/nix/build.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/nix/build.md b/src/nix/build.md index 0fbb39cc3..4c6f6049f 100644 --- a/src/nix/build.md +++ b/src/nix/build.md @@ -40,6 +40,18 @@ R""( lrwxrwxrwx 1 … ./result-dev -> /nix/store/dkm3gwl0xrx0wrw6zi5x3px3lpgjhlw4-glibc-2.32-dev ``` +* Build all outputs: + + ```console + # nix build nixpkgs#openssl^* --print-out-paths + /nix/store/gvad6v0cmq1qccmc4wphsazqbj0xzjsl-openssl-3.0.13-bin + /nix/store/a07jqdrc8afnk8r6f3lnhh4gvab7chk4-openssl-3.0.13-debug + /nix/store/yg75achq89wgqn2fi3gglgsd77kjpi03-openssl-3.0.13-dev + /nix/store/bvdcihi8c88fw31cg6gzzmpnwglpn1jv-openssl-3.0.13-doc + /nix/store/gjqcvq47cmxazxga0cirspm3jywkmvfv-openssl-3.0.13-man + /nix/store/7nmrrad8skxr47f9hfl3xc0pfqmwq51b-openssl-3.0.13 + ``` + * Build attribute `build.x86_64-linux` from (non-flake) Nix expression `release.nix`: From c448636f7c8b50d3d41085bb72e5f608ac1344c1 Mon Sep 17 00:00:00 2001 From: Jesse Schalken Date: Wed, 20 Mar 2024 15:23:31 +1100 Subject: [PATCH 014/168] Fix loop over $NIX_PROFILES in nix-profile-daemon.fish.in --- scripts/nix-profile-daemon.fish.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/nix-profile-daemon.fish.in b/scripts/nix-profile-daemon.fish.in index c23aa64f0..346dce5dd 100644 --- a/scripts/nix-profile-daemon.fish.in +++ b/scripts/nix-profile-daemon.fish.in @@ -28,7 +28,7 @@ else end # Set $NIX_SSL_CERT_FILE so that Nixpkgs applications like curl work. -if test -n "$NIX_SSH_CERT_FILE" +if test -n "$NIX_SSL_CERT_FILE" : # Allow users to override the NIX_SSL_CERT_FILE else if test -e /etc/ssl/certs/ca-certificates.crt # NixOS, Ubuntu, Debian, Gentoo, Arch set --export NIX_SSL_CERT_FILE /etc/ssl/certs/ca-certificates.crt @@ -44,7 +44,7 @@ else if test -e "$NIX_LINK/etc/ca-bundle.crt" # old cacert in Nix profile set --export NIX_SSL_CERT_FILE "$NIX_LINK/etc/ca-bundle.crt" else # Fall back to what is in the nix profiles, favouring whatever is defined last. - for i in $NIX_PROFILES + for i in (string split ' ' $NIX_PROFILES) if test -e "$i/etc/ssl/certs/ca-bundle.crt" set --export NIX_SSL_CERT_FILE "$i/etc/ssl/certs/ca-bundle.crt" end From 40a7929c8e221e6096c3edd2dd0af948a29e3141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Baylac-Jacqu=C3=A9?= Date: Sat, 8 Jun 2019 00:41:19 +0200 Subject: [PATCH 015/168] Daemon: warn when an untrusted user cannot override a setting In a daemon-based Nix setup, some options cannot be overridden by a client unless the client's user is considered trusted. Currently, if an untrusted user tries to override one of those options, we are silently ignoring it. This can be pretty confusing in certain situations. e.g. a user thinks he disabled the sandbox when in reality he did not. We are now sending a warning message letting know the user some options have been ignored. Related to #1761. This is a cherry-pick of 9e0f5f803f6cbfe9925cef69a0e58cbf5375bfaf. The above commit has been reverted by a59e77d9e54e8e7bf0f3c3f40c22cd34b7a81225 to prevent spamming warnings with experimental features, but these are now totally ignored on the daemon side, so there's no reason for the revert any more. --- src/libstore/daemon.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index 917813342..2c808015d 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -254,7 +254,7 @@ struct ClientSettings else if (setSubstituters(settings.substituters)) ; else - debug("ignoring the client-specified setting '%s', because it is a restricted setting and you are not a trusted user", name); + warn("ignoring the client-specified setting '%s', because it is a restricted setting and you are not a trusted user", name); } catch (UsageError & e) { warn(e.what()); } From 0b08dd45b03afd5161966893e42080bf130f527c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Mar 2024 21:28:38 +0100 Subject: [PATCH 016/168] prim_match: Use state.vNull --- src/libexpr/primops.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 9449a8f7c..2cb78e35f 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -4004,7 +4004,7 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v) auto list = state.buildList(match.size() - 1); for (const auto & [i, v2] : enumerate(list)) if (!match[i + 1].matched) - (v2 = state.allocValue())->mkNull(); + v2 = &state.vNull; else (v2 = state.allocValue())->mkString(match[i + 1].str()); v.mkList(list); From d4b0b7f15280659424acc1109679bda7dae11e92 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Mar 2024 21:34:23 +0100 Subject: [PATCH 017/168] createBaseEnv: Use state.vNull --- src/libexpr/primops.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 2cb78e35f..61a11b226 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -4430,8 +4430,7 @@ void EvalState::createBaseEnv() )", }); - v.mkNull(); - addConstant("null", v, { + addConstant("null", &vNull, { .type = nNull, .doc = R"( Primitive value. From 4c8a33ce468ae7b369368e6a26a3180030262f23 Mon Sep 17 00:00:00 2001 From: Jonathan Ringer Date: Wed, 20 Mar 2024 14:42:44 -0700 Subject: [PATCH 018/168] Update src/nix/build.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com> --- src/nix/build.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix/build.md b/src/nix/build.md index 4c6f6049f..5dfdd44a7 100644 --- a/src/nix/build.md +++ b/src/nix/build.md @@ -43,7 +43,7 @@ R""( * Build all outputs: ```console - # nix build nixpkgs#openssl^* --print-out-paths + # nix build "nixpkgs#openssl^*" --print-out-paths /nix/store/gvad6v0cmq1qccmc4wphsazqbj0xzjsl-openssl-3.0.13-bin /nix/store/a07jqdrc8afnk8r6f3lnhh4gvab7chk4-openssl-3.0.13-debug /nix/store/yg75achq89wgqn2fi3gglgsd77kjpi03-openssl-3.0.13-dev From d71e74838aade579f9e5e2771ba26b7077398e93 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Mar 2024 22:56:42 +0100 Subject: [PATCH 019/168] readDir: Allocate type strings only once --- src/libexpr/eval.cc | 4 ++++ src/libexpr/eval.hh | 9 +++++++++ src/libexpr/primops.cc | 21 +++++++++------------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index fb4cfdccf..a6e8a4a8b 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -437,6 +437,10 @@ EvalState::EvalState( vEmptyList.mkList(buildList(0)); vNull.mkNull(); + vStringRegular.mkString("regular"); + vStringDirectory.mkString("directory"); + vStringSymlink.mkString("symlink"); + vStringUnknown.mkString("unknown"); /* Initialise the Nix expression search path. */ if (!evalSettings.pureEval) { diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 7db911fce..a405888c1 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -191,6 +191,15 @@ public: */ Value vNull; + /** `"regular"` */ + Value vStringRegular; + /** `"directory"` */ + Value vStringDirectory; + /** `"symlink"` */ + Value vStringSymlink; + /** `"unknown"` */ + Value vStringUnknown; + /** * The accessor for the root filesystem. */ diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 61a11b226..2022f6dcf 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1775,20 +1775,20 @@ static RegisterPrimOp primop_hashFile({ .fun = prim_hashFile, }); -static std::string_view fileTypeToString(InputAccessor::Type type) +static Value * fileTypeToString(EvalState & state, InputAccessor::Type type) { return - type == InputAccessor::Type::tRegular ? "regular" : - type == InputAccessor::Type::tDirectory ? "directory" : - type == InputAccessor::Type::tSymlink ? "symlink" : - "unknown"; + type == InputAccessor::Type::tRegular ? &state.vStringRegular : + type == InputAccessor::Type::tDirectory ? &state.vStringDirectory : + type == InputAccessor::Type::tSymlink ? &state.vStringSymlink : + &state.vStringUnknown; } static void prim_readFileType(EvalState & state, const PosIdx pos, Value * * args, Value & v) { auto path = realisePath(state, pos, *args[0], std::nullopt); /* Retrieve the directory entry type and stringize it. */ - v.mkString(fileTypeToString(path.lstat().type)); + v = *fileTypeToString(state, path.lstat().type); } static RegisterPrimOp primop_readFileType({ @@ -1819,8 +1819,8 @@ static void prim_readDir(EvalState & state, const PosIdx pos, Value * * args, Va Value * readFileType = nullptr; for (auto & [name, type] : entries) { - auto & attr = attrs.alloc(name); if (!type) { + auto & attr = attrs.alloc(name); // Some filesystems or operating systems may not be able to return // detailed node info quickly in this case we produce a thunk to // query the file type lazily. @@ -1832,7 +1832,7 @@ static void prim_readDir(EvalState & state, const PosIdx pos, Value * * args, Va } else { // This branch of the conditional is much more likely. // Here we just stringize the directory entry type. - attr.mkString(fileTypeToString(*type)); + attrs.insert(state.symbols.create(name), fileTypeToString(state, *type)); } } @@ -2193,11 +2193,8 @@ bool EvalState::callPathFilter( Value arg1; arg1.mkString(pathArg); - Value arg2; // assert that type is not "unknown" - arg2.mkString(fileTypeToString(st.type)); - - Value * args []{&arg1, &arg2}; + Value * args []{&arg1, fileTypeToString(*this, st.type)}; Value res; callFunction(*filterFun, 2, args, res, pos); From a865049c4f39cb7773f97a67cfa12f5b650a86ee Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Mar 2024 23:06:23 +0100 Subject: [PATCH 020/168] tryEval: Allocate true and false once --- src/libexpr/eval.cc | 2 ++ src/libexpr/eval.hh | 18 +++++++++++++++++- src/libexpr/primops.cc | 7 ++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index a6e8a4a8b..a62cee299 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -437,6 +437,8 @@ EvalState::EvalState( vEmptyList.mkList(buildList(0)); vNull.mkNull(); + vTrue.mkBool(true); + vFalse.mkBool(false); vStringRegular.mkString("regular"); vStringDirectory.mkString("directory"); vStringSymlink.mkString("symlink"); diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index a405888c1..eac83fe34 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -187,10 +187,26 @@ public: Value vEmptyList; /** - * Null constant. + * `null` constant. + * + * This is _not_ a singleton. Pointer equality is _not_ sufficient. */ Value vNull; + /** + * `true` constant. + * + * This is _not_ a singleton. Pointer equality is _not_ sufficient. + */ + Value vTrue; + + /** + * `true` constant. + * + * This is _not_ a singleton. Pointer equality is _not_ sufficient. + */ + Value vFalse; + /** `"regular"` */ Value vStringRegular; /** `"directory"` */ diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 2022f6dcf..0f2aaa83f 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -896,10 +896,11 @@ static void prim_tryEval(EvalState & state, const PosIdx pos, Value * * args, Va try { state.forceValue(*args[0], pos); attrs.insert(state.sValue, args[0]); - attrs.alloc("success").mkBool(true); + attrs.insert(state.symbols.create("success"), &state.vTrue); } catch (AssertionError & e) { - attrs.alloc(state.sValue).mkBool(false); - attrs.alloc("success").mkBool(false); + // `value = false;` is unfortunate but removing it is a breaking change. + attrs.insert(state.sValue, &state.vFalse); + attrs.insert(state.symbols.create("success"), &state.vFalse); } // restore the debugRepl pointer if we saved it earlier. From 8c6e0df45f9091fc27143f24d4fe782c661393e2 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Mar 2024 23:07:00 +0100 Subject: [PATCH 021/168] value.hh: Fix warning about {struct/class} Value --- src/libexpr/value.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libexpr/value.hh b/src/libexpr/value.hh index 885621cf5..335801b34 100644 --- a/src/libexpr/value.hh +++ b/src/libexpr/value.hh @@ -159,7 +159,7 @@ public: iterator begin() { return &elems[0]; } iterator end() { return &elems[size]; } - friend class Value; + friend struct Value; }; From 1fcdd1640ec2e63f14487c1af60514fb62ffef19 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Mar 2024 23:11:54 +0100 Subject: [PATCH 022/168] functionArgs: Allocate bools only once --- src/libexpr/eval.cc | 3 +++ src/libexpr/eval.hh | 5 +++++ src/libexpr/primops.cc | 3 +-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index a62cee299..5e2f71649 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -937,6 +937,9 @@ ListBuilder::ListBuilder(EvalState & state, size_t size) state.nrListElems += size; } +Value * EvalState::getBool(bool b) { + return b ? &vTrue : &vFalse; +} unsigned long nrThunks = 0; diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index eac83fe34..f15d19653 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -650,6 +650,11 @@ public: return ListBuilder(*this, size); } + /** + * Return a boolean `Value *` without allocating. + */ + Value *getBool(bool b); + void mkThunk_(Value & v, Expr * expr); void mkPos(Value & v, PosIdx pos); diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 0f2aaa83f..d0fcfd194 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -2845,8 +2845,7 @@ static void prim_functionArgs(EvalState & state, const PosIdx pos, Value * * arg auto attrs = state.buildBindings(args[0]->lambda.fun->formals->formals.size()); for (auto & i : args[0]->lambda.fun->formals->formals) - // !!! should optimise booleans (allocate only once) - attrs.alloc(i.name, i.pos).mkBool(i.def); + attrs.insert(i.name, state.getBool(i.def), i.pos); v.mkAttrs(attrs); } From 83fc988bec374d6beb197e6dde9aef20b6f52b8d Mon Sep 17 00:00:00 2001 From: Andrea Bedini Date: Mon, 18 Mar 2024 23:57:15 +0800 Subject: [PATCH 023/168] Create compile-commands.json with Make --- .gitignore | 1 + mk/compilation-database.mk | 11 +++++++++++ mk/lib.mk | 8 ++++++++ mk/patterns.mk | 36 +++++++++++++++++++++++++++++++++--- mk/tracing.mk | 2 ++ 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 mk/compilation-database.mk diff --git a/.gitignore b/.gitignore index 7bf77adf4..01fafa5a9 100644 --- a/.gitignore +++ b/.gitignore @@ -142,6 +142,7 @@ GTAGS # auto-generated compilation database compile_commands.json +*.compile_commands.json nix-rust/target diff --git a/mk/compilation-database.mk b/mk/compilation-database.mk new file mode 100644 index 000000000..f69dc0de0 --- /dev/null +++ b/mk/compilation-database.mk @@ -0,0 +1,11 @@ +compile-commands-json-files := + +define write-compile-commands + _srcs := $$(sort $$(foreach src, $$($(1)_SOURCES), $$(src))) + + $(1)_COMPILE_COMMANDS_JSON := $$(addprefix $(buildprefix), $$(addsuffix .compile_commands.json, $$(basename $$(_srcs)))) + + compile-commands-json-files += $$($(1)_COMPILE_COMMANDS_JSON) + + clean-files += $$($(1)_COMPILE_COMMANDS_JSON) +endef diff --git a/mk/lib.mk b/mk/lib.mk index fe0add1c9..a002d823f 100644 --- a/mk/lib.mk +++ b/mk/lib.mk @@ -68,6 +68,7 @@ include mk/patterns.mk include mk/templates.mk include mk/cxx-big-literal.mk include mk/tests.mk +include mk/compilation-database.mk # Include all sub-Makefiles. @@ -97,6 +98,13 @@ $(foreach test-group, $(install-tests-groups), \ $(eval $(call run-test,$(test),$(install_test_init))) \ $(eval $(test-group).test-group: $(test).test))) +# Compilation database. +$(foreach lib, $(libraries), $(eval $(call write-compile-commands,$(lib)))) +$(foreach prog, $(programs), $(eval $(call write-compile-commands,$(prog)))) + +compile_commands.json: $(compile-commands-json-files) + @jq --slurp '.' $^ >$@ + # Include makefiles requiring built programs. $(foreach mf, $(makefiles-late), $(eval $(call include-sub-makefile,$(mf)))) diff --git a/mk/patterns.mk b/mk/patterns.mk index c81150260..4caa2039e 100644 --- a/mk/patterns.mk +++ b/mk/patterns.mk @@ -1,11 +1,41 @@ + +# These are the complete command lines we use to compile C and C++ files. +# - $< is the source file. +# - $1 is the object file to create. +CC_CMD=$(CC) -o $1 -c $< $(CPPFLAGS) $(GLOBAL_CFLAGS) $(CFLAGS) $($1_CFLAGS) -MMD -MF $(call filename-to-dep,$1) -MP +CXX_CMD=$(CXX) -o $1 -c $< $(CPPFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($1_CXXFLAGS) $(ERROR_SWITCH_ENUM) -MMD -MF $(call filename-to-dep,$1) -MP + +# We use COMPILE_COMMANDS_JSON_CMD to turn a compilation command (like CC_CMD +# or CXX_CMD above) into a comple_commands.json file. We rely on bash native +# word splitting to define the positional arguments. +# - $< is the source file being compiled. +COMPILE_COMMANDS_JSON_CMD=jq --null-input '{ directory: $$ENV.PWD, file: "$<", arguments: $$ARGS.positional }' --args -- + + $(buildprefix)%.o: %.cc @mkdir -p "$(dir $@)" - $(trace-cxx) $(CXX) -o $@ -c $< $(CPPFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($@_CXXFLAGS) $(ERROR_SWITCH_ENUM) -MMD -MF $(call filename-to-dep, $@) -MP + $(trace-cxx) $(call CXX_CMD,$@) $(buildprefix)%.o: %.cpp @mkdir -p "$(dir $@)" - $(trace-cxx) $(CXX) -o $@ -c $< $(CPPFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($@_CXXFLAGS) $(ERROR_SWITCH_ENUM) -MMD -MF $(call filename-to-dep, $@) -MP + $(trace-cxx) $(call CXX_CMD,$@) $(buildprefix)%.o: %.c @mkdir -p "$(dir $@)" - $(trace-cc) $(CC) -o $@ -c $< $(CPPFLAGS) $(GLOBAL_CFLAGS) $(CFLAGS) $($@_CFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP + $(trace-cc) $(call CC_CMD,$@) + +# In the following we need to replace the .compile_commands.json extension in $@ with .o +# to make the object file. This is needed because CC_CMD and CXX_CMD do further expansions +# based on the object file name (i.e. *_CXXFLAGS and filename-to-dep). + +$(buildprefix)%.compile_commands.json: %.cc + @mkdir -p "$(dir $@)" + $(trace-jq) $(COMPILE_COMMANDS_JSON_CMD) $(call CXX_CMD,$(@:.compile_commands.json=.o)) > $@ + +$(buildprefix)%.compile_commands.json: %.cpp + @mkdir -p "$(dir $@)" + $(trace-jq) $(COMPILE_COMMANDS_JSON_CMD) $(call CXX_CMD,$(@:.compile_commands.json=.o)) > $@ + +$(buildprefix)%.compile_commands.json: %.c + @mkdir -p "$(dir $@)" + $(trace-jq) $(COMPILE_COMMANDS_JSON_CMD) $(call CC_CMD,$(@:.compile_commands.json=.o)) > $@ diff --git a/mk/tracing.mk b/mk/tracing.mk index 1fc5573d7..09db1e617 100644 --- a/mk/tracing.mk +++ b/mk/tracing.mk @@ -10,6 +10,8 @@ ifeq ($(V), 0) trace-install = @echo " INST " $@; trace-mkdir = @echo " MKDIR " $@; trace-test = @echo " TEST " $@; + trace-sh = @echo " SH " $@; + trace-jq = @echo " JQ " $@; suppress = @ From d0824f661e712c384332953db54bb523834909ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= Date: Thu, 21 Mar 2024 16:54:28 +0100 Subject: [PATCH 024/168] Document the new `compile_commands.json` target --- doc/manual/src/contributing/hacking.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/manual/src/contributing/hacking.md b/doc/manual/src/contributing/hacking.md index 916ec3077..28ed49666 100644 --- a/doc/manual/src/contributing/hacking.md +++ b/doc/manual/src/contributing/hacking.md @@ -258,7 +258,7 @@ See [supported compilation environments](#compilation-environments) and instruct To use the LSP with your editor, you first need to [set up `clangd`](https://clangd.llvm.org/installation#project-setup) by running: ```console -make clean && bear -- make -j$NIX_BUILD_CORES default check install +make compile_commands.json ``` Configure your editor to use the `clangd` from the shell, either by running it inside the development shell, or by using [nix-direnv](https://github.com/nix-community/nix-direnv) and [the appropriate editor plugin](https://github.com/direnv/direnv/wiki#editor-integration). From c13a31f6396ea5c035926d99243b314bc414ed9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= Date: Fri, 22 Mar 2024 11:40:29 +0100 Subject: [PATCH 025/168] Update the release cycle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Align the “frequent” release cycle with the calendar - The 6-month release cycle is hard to keep track of. A monthly release will make it much easier to remember the release date. - Officialise the support for a stable version maintained for as long as NixOS stable - This is already the case in practice, it just happens that the “stable” Nixpkgs version is whichever version was deemed stable-enough at the time of the NixOS release. Officialise that by cutting a new major release alongside each NixOS one. Note that this breaks whatever semver compatibility Nix might pretend to have, but I don't think it makes sense any way. --- doc/manual/src/SUMMARY.md.in | 2 +- doc/manual/src/release-notes/index.md | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/doc/manual/src/SUMMARY.md.in b/doc/manual/src/SUMMARY.md.in index 1149fc7b4..43b9e925f 100644 --- a/doc/manual/src/SUMMARY.md.in +++ b/doc/manual/src/SUMMARY.md.in @@ -119,7 +119,7 @@ - [Experimental Features](contributing/experimental-features.md) - [CLI guideline](contributing/cli-guideline.md) - [C++ style guide](contributing/cxx.md) -- [Release Notes](release-notes/index.md) +- [Releases](release-notes/index.md) {{#include ./SUMMARY-rl-next.md}} - [Release 2.21 (2024-03-11)](release-notes/rl-2.21.md) - [Release 2.20 (2024-01-29)](release-notes/rl-2.20.md) diff --git a/doc/manual/src/release-notes/index.md b/doc/manual/src/release-notes/index.md index cc805e631..150253baa 100644 --- a/doc/manual/src/release-notes/index.md +++ b/doc/manual/src/release-notes/index.md @@ -1,12 +1,15 @@ # Nix Release Notes -Nix has a release cycle of roughly 6 weeks. +The Nix release cycle is calendar-based as follows: + +- A new minor version (`XX.YY+1.0`) is published every month and supported for two months; +- A new major version (`XX+1.1.0`) is published twice a year, in April and October, and supported for eight months. + +The rationale behind that cycle is that +- Minor versions stay close to master and bring early access to new features for the user who need them; +- Major versions are aligned with the NixOS releases (released one month before NixOS and supported for as long at it). + +Bugfixes and security issues are backported to every supported version. +Patch releases are published as needed. + Notable changes and additions are announced in the release notes for each version. - -Bugfixes can be backported on request to previous Nix releases. -We typically backport only as far back as the Nix version used in the latest NixOS release, which is announced in the [NixOS release notes](https://nixos.org/manual/nixos/stable/release-notes.html#ch-release-notes). - -Backports never skip releases. -If a feature is backported to version `x.y`, it must also be available in version `x.(y+1)`. -This ensures that upgrading from an older version with backports is still safe and no backported functionality will go missing. - From 4e2f11b6927cb0e4171b2758196f2310760617d2 Mon Sep 17 00:00:00 2001 From: Tharun T Date: Fri, 22 Mar 2024 18:34:55 +0530 Subject: [PATCH 026/168] doc build output correction Signed-off-by: Tharun T --- doc/manual/src/contributing/documentation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/manual/src/contributing/documentation.md b/doc/manual/src/contributing/documentation.md index 1dddb207c..46cca759d 100644 --- a/doc/manual/src/contributing/documentation.md +++ b/doc/manual/src/contributing/documentation.md @@ -30,7 +30,7 @@ To build the manual incrementally, [enter the development shell](./hacking.md) a make manual-html -j $NIX_BUILD_CORES ``` -and open `./outputs/out/share/doc/nix/manual/language/index.html`. +and open `./outputs/doc/share/doc/nix/manual/language/index.html`. In order to reflect changes to the [Makefile for the manual], clear all generated files before re-building: From bfd36402acc976c32ec8349a2a25fa4288d65475 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 22 Mar 2024 18:11:24 +0100 Subject: [PATCH 027/168] EvalCache: Fix missing format string argument Fixes terminate called after throwing an instance of 'boost::wrapexcept' what(): boost::too_few_args: format-string referred to more arguments than were passed Aborted (core dumped) for type errors in AttrCursor. --- src/libexpr/eval-cache.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libexpr/eval-cache.cc b/src/libexpr/eval-cache.cc index 2fc69e796..1538eb056 100644 --- a/src/libexpr/eval-cache.cc +++ b/src/libexpr/eval-cache.cc @@ -581,7 +581,7 @@ std::string AttrCursor::getString() auto & v = forceValue(); if (v.type() != nString && v.type() != nPath) - root->state.error("'%s' is not a string but %s", getAttrPathStr()).debugThrow(); + root->state.error("'%s' is not a string but %s", getAttrPathStr(), showType(v)).debugThrow(); return v.type() == nString ? v.c_str() : v.path().to_string(); } @@ -630,7 +630,7 @@ string_t AttrCursor::getStringWithContext() else if (v.type() == nPath) return {v.path().to_string(), {}}; else - root->state.error("'%s' is not a string but %s", getAttrPathStr()).debugThrow(); + root->state.error("'%s' is not a string but %s", getAttrPathStr(), showType(v)).debugThrow(); } bool AttrCursor::getBool() From cc29d85d06dbb050ac7df0f532ec0bec6212232b Mon Sep 17 00:00:00 2001 From: Picnoir Date: Fri, 22 Mar 2024 22:31:41 +0100 Subject: [PATCH 028/168] Doc: explicitly specify which shell to use to get clangd I was using by mistake the .#nix-clangStdenv shell to retrieve clangd. This clangd is unusable with the project and constantly segfaults. Let's explicitly state which shell the user should use in the docs. I don't really understand the source of this segfault. I assume it's related to a clang version incompatibility. (16.0.6 for .#nix-clangStdenv 14.0.6 for .#native-clangStdenvPackages) --- doc/manual/src/contributing/hacking.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/manual/src/contributing/hacking.md b/doc/manual/src/contributing/hacking.md index 28ed49666..2ff70f500 100644 --- a/doc/manual/src/contributing/hacking.md +++ b/doc/manual/src/contributing/hacking.md @@ -261,7 +261,7 @@ To use the LSP with your editor, you first need to [set up `clangd`](https://cla make compile_commands.json ``` -Configure your editor to use the `clangd` from the shell, either by running it inside the development shell, or by using [nix-direnv](https://github.com/nix-community/nix-direnv) and [the appropriate editor plugin](https://github.com/direnv/direnv/wiki#editor-integration). +Configure your editor to use the `clangd` from the `.#native-clangStdenvPackages` shell. You can do that either by running it inside the development shell, or by using [nix-direnv](https://github.com/nix-community/nix-direnv) and [the appropriate editor plugin](https://github.com/direnv/direnv/wiki#editor-integration). > **Note** > From 717391731cfd85f2e9f5186ed69c2f832a7b86b9 Mon Sep 17 00:00:00 2001 From: detroyejr Date: Fri, 22 Mar 2024 19:30:03 -0400 Subject: [PATCH 029/168] flakes: remove experimental repl-flake --- src/libutil/experimental-features.cc | 10 ---------- src/libutil/experimental-features.hh | 1 - src/nix/repl.cc | 9 --------- 3 files changed, 20 deletions(-) diff --git a/src/libutil/experimental-features.cc b/src/libutil/experimental-features.cc index 374e674af..4fc07afaf 100644 --- a/src/libutil/experimental-features.cc +++ b/src/libutil/experimental-features.cc @@ -203,16 +203,6 @@ constexpr std::array xpFeatureDetails )", .trackingUrl = "https://github.com/NixOS/nix/milestone/40", }, - { - .tag = Xp::ReplFlake, - .name = "repl-flake", - .description = R"( - *Enabled with [`flakes`](#xp-feature-flakes) since 2.19* - - Allow passing [installables](@docroot@/command-ref/new-cli/nix.md#installables) to `nix repl`, making its interface consistent with the other experimental commands. - )", - .trackingUrl = "https://github.com/NixOS/nix/milestone/32", - }, { .tag = Xp::AutoAllocateUids, .name = "auto-allocate-uids", diff --git a/src/libutil/experimental-features.hh b/src/libutil/experimental-features.hh index eae4fa9b8..47c21280f 100644 --- a/src/libutil/experimental-features.hh +++ b/src/libutil/experimental-features.hh @@ -26,7 +26,6 @@ enum struct ExperimentalFeature RecursiveNix, NoUrlLiterals, FetchClosure, - ReplFlake, AutoAllocateUids, Cgroups, DaemonTrustOverride, diff --git a/src/nix/repl.cc b/src/nix/repl.cc index 63fe3044b..8bbfe0f07 100644 --- a/src/nix/repl.cc +++ b/src/nix/repl.cc @@ -47,15 +47,6 @@ struct CmdRepl : RawInstallablesCommand void applyDefaultInstallables(std::vector & rawInstallables) override { - if (!experimentalFeatureSettings.isEnabled(Xp::Flakes) && !(file) && rawInstallables.size() >= 1) { - warn("future versions of Nix will require using `--file` to load a file"); - if (rawInstallables.size() > 1) - warn("more than one input file is not currently supported"); - auto filePath = rawInstallables[0].data(); - file = std::optional(filePath); - rawInstallables.front() = rawInstallables.back(); - rawInstallables.pop_back(); - } if (rawInstallables.empty() && (file.has_value() || expr.has_value())) { rawInstallables.push_back("."); } From c625b4535724ebefc2bef11beb4bec212587fdb5 Mon Sep 17 00:00:00 2001 From: detroyejr Date: Fri, 22 Mar 2024 21:39:48 -0400 Subject: [PATCH 030/168] flakes: add remove-repl-flake.md note --- doc/manual/rl-next/remove-repl-flake.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 doc/manual/rl-next/remove-repl-flake.md diff --git a/doc/manual/rl-next/remove-repl-flake.md b/doc/manual/rl-next/remove-repl-flake.md new file mode 100644 index 000000000..16a899c3c --- /dev/null +++ b/doc/manual/rl-next/remove-repl-flake.md @@ -0,0 +1,12 @@ +--- +synopsis: Remove experimental repl-flake +significant: significant +issues: 10103 +prs: 10299 +--- + +This PR removes the repl-flake feature that was adopted to provide a migration path when changing the behavior of `nix repl`. Moving forward this command will behave more like the rest of the modern cli. + +- Removes any repl-flake references. +- Removes the parts of `applyDefaultInstallables` that are no longer needed in repl.cc. +- Fix/Add any tests. From b11dd58fe4bd75b25a0f458888dd0ceb8d7d62e1 Mon Sep 17 00:00:00 2001 From: detroyejr Date: Fri, 22 Mar 2024 21:56:19 -0400 Subject: [PATCH 031/168] flakes: test to ensure we get an error if --file isn't used --- tests/functional/repl.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/functional/repl.sh b/tests/functional/repl.sh index 4938c2267..f11fa7140 100644 --- a/tests/functional/repl.sh +++ b/tests/functional/repl.sh @@ -47,6 +47,9 @@ testRepl () { | grep "attribute 'currentSystem' missing" nix repl "${nixArgs[@]}" 2>&1 <<< "builtins.currentSystem" \ | grep "$(nix-instantiate --eval -E 'builtins.currentSystem')" + + expectStderr 1 nix repl ${testDir}/simple.nix \ + | grepQuiet -s "error: path '$testDir/simple.nix' is not a flake" } # Simple test, try building a drv From 50885b81c929779bffe5fea4199688d21e9f707b Mon Sep 17 00:00:00 2001 From: detroyejr Date: Sat, 23 Mar 2024 06:31:49 -0400 Subject: [PATCH 032/168] fix: correct remove-repl-flake.md --- doc/manual/rl-next/remove-repl-flake.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/manual/rl-next/remove-repl-flake.md b/doc/manual/rl-next/remove-repl-flake.md index 16a899c3c..a4ddeaf59 100644 --- a/doc/manual/rl-next/remove-repl-flake.md +++ b/doc/manual/rl-next/remove-repl-flake.md @@ -1,6 +1,6 @@ --- synopsis: Remove experimental repl-flake -significant: significant +significance: significant issues: 10103 prs: 10299 --- From c3fb2aa1f9d1fa756dac38d3588c836c5a5395dc Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 22 Mar 2024 22:41:50 +0100 Subject: [PATCH 033/168] fix: Treat empty TMPDIR as unset Fixes an instance of nix: src/libutil/util.cc:139: nix::Path nix::canonPath(PathView, bool): Assertion `path != ""' failed. ... which I've been getting in one of my shells for some reason. I have yet to find out why TMPDIR was empty, but it's no reason for Nix to break. --- src/libstore/globals.cc | 2 +- src/libutil/file-system.cc | 8 ++++++-- src/libutil/file-system.hh | 4 ++++ src/nix-build/nix-build.cc | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index d22ae4ca0..fa0938d7b 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -415,7 +415,7 @@ void initLibStore() { sshd). This breaks build users because they don't have access to the TMPDIR, in particular in ‘nix-store --serve’. */ #if __APPLE__ - if (hasPrefix(getEnv("TMPDIR").value_or("/tmp"), "/var/folders/")) + if (hasPrefix(defaultTempDir(), "/var/folders/")) unsetenv("TMPDIR"); #endif diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 9dd6a5133..9f81ee452 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -494,10 +494,14 @@ void AutoDelete::reset(const Path & p, bool recursive) { ////////////////////////////////////////////////////////////////////// +std::string defaultTempDir() { + return getEnvNonEmpty("TMPDIR").value_or("/tmp"); +} + static Path tempName(Path tmpRoot, const Path & prefix, bool includePid, std::atomic & counter) { - tmpRoot = canonPath(tmpRoot.empty() ? getEnv("TMPDIR").value_or("/tmp") : tmpRoot, true); + tmpRoot = canonPath(tmpRoot.empty() ? defaultTempDir() : tmpRoot, true); if (includePid) return fmt("%1%/%2%-%3%-%4%", tmpRoot, prefix, getpid(), counter++); else @@ -537,7 +541,7 @@ Path createTempDir(const Path & tmpRoot, const Path & prefix, std::pair createTempFile(const Path & prefix) { - Path tmpl(getEnv("TMPDIR").value_or("/tmp") + "/" + prefix + ".XXXXXX"); + Path tmpl(defaultTempDir() + "/" + prefix + ".XXXXXX"); // Strictly speaking, this is UB, but who cares... // FIXME: use O_TMPFILE. AutoCloseFD fd(mkstemp((char *) tmpl.c_str())); diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index 963265e34..9d565c881 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -234,6 +234,10 @@ Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix", */ std::pair createTempFile(const Path & prefix = "nix"); +/** + * Return `TMPDIR`, or the default temporary directory if unset or empty. + */ +Path defaultTempDir(); /** * Used in various places. diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index a372e4b1c..418ee0ddd 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -476,7 +476,7 @@ static void main_nix_build(int argc, char * * argv) auto env = getEnv(); auto tmp = getEnv("TMPDIR"); - if (!tmp) tmp = getEnv("XDG_RUNTIME_DIR").value_or("/tmp"); + if (!tmp || tmp->empty()) tmp = getEnv("XDG_RUNTIME_DIR").value_or("/tmp"); if (pure) { decltype(env) newEnv; From b9e7f5aa2df3f0e223f5c44b8089cbf9b81be691 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 00:34:21 +0100 Subject: [PATCH 034/168] fix: Treat empty XDG_RUNTIME_DIR as unset See preceding commit. Not observed in the wild, but is sensible and consistent with TMPDIR behavior. --- src/nix-build/nix-build.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 418ee0ddd..35eef5b83 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -475,8 +475,9 @@ static void main_nix_build(int argc, char * * argv) // Set the environment. auto env = getEnv(); - auto tmp = getEnv("TMPDIR"); - if (!tmp || tmp->empty()) tmp = getEnv("XDG_RUNTIME_DIR").value_or("/tmp"); + auto tmp = getEnvNonEmpty("TMPDIR"); + if (!tmp) + tmp = getEnvNonEmpty("XDG_RUNTIME_DIR").value_or("/tmp"); if (pure) { decltype(env) newEnv; From fd31945742710984de22805ee8d97fbd83c3f8eb Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 00:45:15 +0100 Subject: [PATCH 035/168] local-derivation-goal.cc: Reuse defaultTempDir() --- src/libstore/build/local-derivation-goal.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index a9b6a8dbf..f65afdf07 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -2090,7 +2090,7 @@ void LocalDerivationGoal::runChild() /* The tmpDir in scope points at the temporary build directory for our derivation. Some packages try different mechanisms to find temporary directories, so we want to open up a broader place for them to dump their files, if needed. */ - Path globalTmpDir = canonPath(getEnvNonEmpty("TMPDIR").value_or("/tmp"), true); + Path globalTmpDir = canonPath(defaultTempDir(), true); /* They don't like trailing slashes on subpath directives */ if (globalTmpDir.back() == '/') globalTmpDir.pop_back(); From dd26f413791b7885558afcc628623648b7fa6396 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 00:45:57 +0100 Subject: [PATCH 036/168] local-derivation-goal.cc: Remove *all* trailing slashes --- src/libstore/build/local-derivation-goal.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index f65afdf07..612434e4d 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -2093,7 +2093,8 @@ void LocalDerivationGoal::runChild() Path globalTmpDir = canonPath(defaultTempDir(), true); /* They don't like trailing slashes on subpath directives */ - if (globalTmpDir.back() == '/') globalTmpDir.pop_back(); + while (!globalTmpDir.empty() && globalTmpDir.back() == '/') + globalTmpDir.pop_back(); if (getEnv("_NIX_TEST_NO_SANDBOX") != "1") { builder = "/usr/bin/sandbox-exec"; From 850c9a6cafb74a82b8111dd6aeb4c0d434aba414 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 00:46:15 +0100 Subject: [PATCH 037/168] HttpBinaryCacheStore: Remove *all* trailing slashes --- src/libstore/http-binary-cache-store.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index 85c5eed4c..5da87e935 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -49,7 +49,7 @@ public: , BinaryCacheStore(params) , cacheUri(scheme + "://" + _cacheUri) { - if (cacheUri.back() == '/') + while (!cacheUri.empty() && cacheUri.back() == '/') cacheUri.pop_back(); diskCache = getNarInfoDiskCache(); From 3b7f2bf99751bb51a9e9c4dab0fe2db1a6ff07ca Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 00:54:44 +0100 Subject: [PATCH 038/168] git/dumpTree: Assert name not empty before back() --- src/libutil/git.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libutil/git.cc b/src/libutil/git.cc index 0b6e35222..a60589baa 100644 --- a/src/libutil/git.cc +++ b/src/libutil/git.cc @@ -251,6 +251,7 @@ void dumpTree(const Tree & entries, Sink & sink, for (auto & [name, entry] : entries) { auto name2 = name; if (entry.mode == Mode::Directory) { + assert(!name2.empty()); assert(name2.back() == '/'); name2.pop_back(); } From 175afc71069cd821b58792a077d8cf4a7cebefe6 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 01:26:12 +0100 Subject: [PATCH 039/168] Test and document builtins.baseNameOf --- src/libexpr/primops.cc | 15 ++++++--- .../functional/lang/eval-okay-baseNameOf.exp | 1 + .../functional/lang/eval-okay-baseNameOf.nix | 32 +++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 tests/functional/lang/eval-okay-baseNameOf.exp create mode 100644 tests/functional/lang/eval-okay-baseNameOf.nix diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index d0fcfd194..cdee1f361 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1580,11 +1580,18 @@ static void prim_baseNameOf(EvalState & state, const PosIdx pos, Value * * args, static RegisterPrimOp primop_baseNameOf({ .name = "baseNameOf", - .args = {"s"}, + .args = {"x"}, .doc = R"( - Return the *base name* of the string *s*, that is, everything - following the final slash in the string. This is similar to the GNU - `basename` command. + Return the *base name* of either a [path value](@docroot@/language/values.md#type-path) *x* or a string *x*, depending on which type is passed, and according to the following rules. + + For a path value, the *base name* is considered to be the part of the path after the last directory separator, including any file extensions. + This is the simple case, as path values don't have trailing slashes. + + When the argument is a string, a more involved logic applies. If the string ends with a `/`, only this one final slash is removed. + + After this, the *base name* is returned as previously described, assuming `/` as the directory separator. (Note that evaluation must be platform independent.) + + This is somewhat similar to the [GNU `basename`](https://www.gnu.org/software/coreutils/manual/html_node/basename-invocation.html) command, but GNU `basename` will strip any number of trailing slashes. )", .fun = prim_baseNameOf, }); diff --git a/tests/functional/lang/eval-okay-baseNameOf.exp b/tests/functional/lang/eval-okay-baseNameOf.exp new file mode 100644 index 000000000..52c33a57c --- /dev/null +++ b/tests/functional/lang/eval-okay-baseNameOf.exp @@ -0,0 +1 @@ +"ok" diff --git a/tests/functional/lang/eval-okay-baseNameOf.nix b/tests/functional/lang/eval-okay-baseNameOf.nix new file mode 100644 index 000000000..a7afdd896 --- /dev/null +++ b/tests/functional/lang/eval-okay-baseNameOf.nix @@ -0,0 +1,32 @@ +assert baseNameOf "" == ""; +assert baseNameOf "." == "."; +assert baseNameOf ".." == ".."; +assert baseNameOf "a" == "a"; +assert baseNameOf "a." == "a."; +assert baseNameOf "a.." == "a.."; +assert baseNameOf "a.b" == "a.b"; +assert baseNameOf "a.b." == "a.b."; +assert baseNameOf "a.b.." == "a.b.."; +assert baseNameOf "a/" == "a"; +assert baseNameOf "a/." == "."; +assert baseNameOf "a/.." == ".."; +assert baseNameOf "a/b" == "b"; +assert baseNameOf "a/b." == "b."; +assert baseNameOf "a/b.." == "b.."; +assert baseNameOf "a/b/c" == "c"; +assert baseNameOf "a/b/c." == "c."; +assert baseNameOf "a/b/c.." == "c.."; +assert baseNameOf "a/b/c/d" == "d"; +assert baseNameOf "a/b/c/d." == "d."; +assert baseNameOf "a\\b" == "a\\b"; +assert baseNameOf "C:a" == "C:a"; +assert baseNameOf "a//b" == "b"; + +# It's been like this for close to a decade. We ought to commit to it. +# https://github.com/NixOS/nix/pull/582#issuecomment-121014450 +assert baseNameOf "a//" == ""; + +assert baseNameOf ./foo == "foo"; +assert baseNameOf ./foo/bar == "bar"; + +"ok" From 754a15e2db12e39b383a69b1fab67651f7665bd7 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 01:37:58 +0100 Subject: [PATCH 040/168] builtins.baseNameOf: Fork --- src/libexpr/primops.cc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index cdee1f361..db4237130 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1568,12 +1568,32 @@ static RegisterPrimOp primop_pathExists({ .fun = prim_pathExists, }); +// Ideally, all trailing slashes should have been removed, but it's been like this for +// almost a decade as of writing. Changing it will affect reproducibility. +static std::string_view legacyBaseNameOf(std::string_view path) +{ + if (path.empty()) + return ""; + + auto last = path.size() - 1; + if (path[last] == '/' && last > 0) + last -= 1; + + auto pos = path.rfind('/', last); + if (pos == path.npos) + pos = 0; + else + pos += 1; + + return path.substr(pos, last - pos + 1); +} + /* Return the base name of the given string, i.e., everything following the last slash. */ static void prim_baseNameOf(EvalState & state, const PosIdx pos, Value * * args, Value & v) { NixStringContext context; - v.mkString(baseNameOf(*state.coerceToString(pos, *args[0], context, + v.mkString(legacyBaseNameOf(*state.coerceToString(pos, *args[0], context, "while evaluating the first argument passed to builtins.baseNameOf", false, false)), context); } From 9884018dfae766a3b7fd3e9bc0302e75feccf52d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 01:38:22 +0100 Subject: [PATCH 041/168] baseNameOf(): Remove all trailing slashes --- src/libutil/file-system.cc | 2 +- tests/unit/libutil/tests.cc | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 9dd6a5133..fc0f216ea 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -128,7 +128,7 @@ std::string_view baseNameOf(std::string_view path) return ""; auto last = path.size() - 1; - if (path[last] == '/' && last > 0) + while (last > 0 && path[last] == '/') last -= 1; auto pos = path.rfind('/', last); diff --git a/tests/unit/libutil/tests.cc b/tests/unit/libutil/tests.cc index 4406fd184..d7e9edf0a 100644 --- a/tests/unit/libutil/tests.cc +++ b/tests/unit/libutil/tests.cc @@ -151,6 +151,16 @@ namespace nix { ASSERT_EQ(p1, "dir"); } + TEST(baseNameOf, trailingSlashes) { + auto p1 = baseNameOf("/dir//"); + ASSERT_EQ(p1, "dir"); + } + + TEST(baseNameOf, absoluteNothingSlashNothing) { + auto p1 = baseNameOf("//"); + ASSERT_EQ(p1, ""); + } + /* ---------------------------------------------------------------------------- * isInDir * --------------------------------------------------------------------------*/ From fbf493758c0134a9b4c73b7dd38fc4c1d480b76c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 02:08:28 +0100 Subject: [PATCH 042/168] doc/language: Link to nix.dev introduction Closes #10283 --- doc/manual/src/language/index.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/manual/src/language/index.md b/doc/manual/src/language/index.md index 5388c6dc4..650412f1b 100644 --- a/doc/manual/src/language/index.md +++ b/doc/manual/src/language/index.md @@ -1,7 +1,13 @@ # Nix Language The Nix language is designed for conveniently creating and composing *derivations* – precise descriptions of how contents of existing files are used to derive new files. -It is: + +> **Tip** +> +> These pages are written as a reference. +> If you are learning Nix, nix.dev has a good [introduction to the Nix language](https://nix.dev/tutorials/nix-language). + +The language is: - *domain-specific* From 2a44b11f55939a97e3712a4525e60c4d4b12ea13 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sat, 23 Mar 2024 22:01:30 -0400 Subject: [PATCH 043/168] Fix `git-hashing/simple.sh` I realized it was checking NAR hashes before of added objects, which makes little sense --- we don't really care about ancillary NAR hashes. Now, the bottom `nix store add` tests compare the CA field with a git hash to hashes calculated by Git. This matches top `nix hash path` ones in using git as a source of truth. --- tests/functional/git-hashing/simple.sh | 74 +++++++++++++++----------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/tests/functional/git-hashing/simple.sh b/tests/functional/git-hashing/simple.sh index 604e1a175..f43168eb2 100644 --- a/tests/functional/git-hashing/simple.sh +++ b/tests/functional/git-hashing/simple.sh @@ -6,8 +6,9 @@ git init "$repo" git -C "$repo" config user.email "you@example.com" git -C "$repo" config user.name "Your Name" +# Compare Nix's and git's implementation of git hashing try () { - hash=$(nix hash path --mode git --format base16 --algo sha1 $TEST_ROOT/hash-path) + local hash=$(nix hash path --mode git --format base16 --algo sha1 $TEST_ROOT/hash-path) [[ "$hash" == "$1" ]] git -C "$repo" rm -rf hash-path || true @@ -15,7 +16,7 @@ try () { git -C "$repo" add hash-path git -C "$repo" commit -m "x" git -C "$repo" status - hash2=$(git -C "$TEST_ROOT/scratch" rev-parse HEAD:hash-path) + local hash2=$(git -C "$TEST_ROOT/scratch" rev-parse HEAD:hash-path) [[ "$hash2" = "$1" ]] } @@ -32,36 +33,45 @@ echo "Run Hello World" > $TEST_ROOT/hash-path/executable chmod +x $TEST_ROOT/hash-path/executable try "e5c0a11a556801a5c9dcf330ca9d7e2c572697f4" -rm -rf $TEST_ROOT/dummy1 -echo Hello World! > $TEST_ROOT/dummy1 -path1=$(nix store add --mode git --hash-algo sha1 $TEST_ROOT/dummy1) -hash1=$(nix-store -q --hash $path1) -test "$hash1" = "sha256:1brffhvj2c0z6x8qismd43m0iy8dsgfmy10bgg9w11szway2wp9v" +# Check Nix added object has matching git hash +try2 () { + local hashPath="$1" + local expected="$2" -rm -rf $TEST_ROOT/dummy2 -mkdir -p $TEST_ROOT/dummy2 -echo Hello World! > $TEST_ROOT/dummy2/hello -path2=$(nix store add --mode git --hash-algo sha1 $TEST_ROOT/dummy2) -hash2=$(nix-store -q --hash $path2) -test "$hash2" = "sha256:1vhv7zxam7x277q0y0jcypm7hwhccbzss81vkdgf0ww5sm2am4y0" + local path=$(nix store add --mode git --hash-algo sha1 "$repo/$hashPath") -rm -rf $TEST_ROOT/dummy3 -mkdir -p $TEST_ROOT/dummy3 -mkdir -p $TEST_ROOT/dummy3/dir -touch $TEST_ROOT/dummy3/dir/file -echo Hello World! > $TEST_ROOT/dummy3/dir/file -touch $TEST_ROOT/dummy3/dir/executable -chmod +x $TEST_ROOT/dummy3/dir/executable -echo Run Hello World! > $TEST_ROOT/dummy3/dir/executable -path3=$(nix store add --mode git --hash-algo sha1 $TEST_ROOT/dummy3) -hash3=$(nix-store -q --hash $path3) -test "$hash3" = "sha256:08y3nm3mvn9qvskqnf13lfgax5lh73krxz4fcjd5cp202ggpw9nv" + git -C "$repo" add "$hashPath" + git -C "$repo" commit -m "x" + git -C "$repo" status + local hashFromGit=$(git -C "$repo" rev-parse "HEAD:$hashPath") + [[ "$hashFromGit" == "$2" ]] -rm -rf $TEST_ROOT/dummy3 -mkdir -p $TEST_ROOT/dummy3 -mkdir -p $TEST_ROOT/dummy3/dir -touch $TEST_ROOT/dummy3/dir/file -ln -s './hello/world.txt' $TEST_ROOT/dummy3/dir/symlink -path3=$(nix store add --mode git --hash-algo sha1 $TEST_ROOT/dummy3) -hash3=$(nix-store -q --hash $path3) -test "$hash3" = "sha256:1dwazas8irzpar89s8k2bnp72imfw7kgg4aflhhsfnicg8h428f3" + local caFromNix=$(nix path-info --json "$path" | jq -r ".[] | .ca") + [[ "fixed:git:sha1:$(nix hash convert --to nix32 "sha1:$hashFromGit")" = "$caFromNix" ]] +} + +rm -rf "$repo/dummy1" +echo Hello World! > "$repo/dummy1" +try2 dummy1 "980a0d5f19a64b4b30a87d4206aade58726b60e3" + +rm -rf "$repo/dummy2" +mkdir -p "$repo/dummy2" +echo Hello World! > "$repo/dummy2/hello" +try2 dummy2 "8b8e43b937854f4083ea56777821abda2799e850" + +rm -rf "$repo/dummy3" +mkdir -p "$repo/dummy3" +mkdir -p "$repo/dummy3/dir" +touch "$repo/dummy3/dir/file" +echo Hello World! > "$repo/dummy3/dir/file" +touch "$repo/dummy3/dir/executable" +chmod +x "$repo/dummy3/dir/executable" +echo Run Hello World! > "$repo/dummy3/dir/executable" +try2 dummy3 "f227adfaf60d2778aabbf93df6dd061272d2dc85" + +rm -rf "$repo/dummy4" +mkdir -p "$repo/dummy4" +mkdir -p "$repo/dummy4/dir" +touch "$repo/dummy4/dir/file" +ln -s './hello/world.txt' "$repo/dummy4/dir/symlink" +try2 dummy4 "06f3e789820fc488d602358f03e3a1cbf993bf33" From 53c15336b06dcf26689c35fb3166ca6a4846b967 Mon Sep 17 00:00:00 2001 From: Tharun T Date: Mon, 25 Mar 2024 07:43:31 +0530 Subject: [PATCH 044/168] derivation output selection test --- tests/functional/flakes/flakes.sh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/functional/flakes/flakes.sh b/tests/functional/flakes/flakes.sh index 427290883..f6931a578 100644 --- a/tests/functional/flakes/flakes.sh +++ b/tests/functional/flakes/flakes.sh @@ -285,6 +285,35 @@ git -C "$flake3Dir" add flake.lock git -C "$flake3Dir" commit -m 'Add lockfile' +# Test accessing output in installables with `.` (foobarbaz.) +cat > "$flake3Dir/flake.nix" < \$foo/file + echo "out" > \$out/file + ''; + outputSpecified = true; + }; + }; +} +EOF + +cp ../config.nix "$flake3Dir" +git -C "$flake3Dir" add flake.nix config.nix +git -C "$flake3Dir" commit -m 'multi outputs flake' + +nix build "$flake3Dir#hello.foo" --json --no-link | jq --exit-status ' + (.[0] | + (.drvPath | match(".*hello.drv")) and + (.outputs | keys == ["foo"])) +' + # Test whether registry caching works. nix registry list --flake-registry "file://$registry" | grepQuiet flake3 mv "$registry" "$registry.tmp" From f78161bb8e87a79e0cdeff0c55266b58e45071d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= <7226587+thufschmitt@users.noreply.github.com> Date: Mon, 25 Mar 2024 10:47:19 +0100 Subject: [PATCH 045/168] Adress feedback from the PR Trim down the proposal quite a bit, making it much closer to the previous text, just more explicit about what we support. --- doc/manual/src/release-notes/index.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/doc/manual/src/release-notes/index.md b/doc/manual/src/release-notes/index.md index 150253baa..331ddf685 100644 --- a/doc/manual/src/release-notes/index.md +++ b/doc/manual/src/release-notes/index.md @@ -2,14 +2,15 @@ The Nix release cycle is calendar-based as follows: -- A new minor version (`XX.YY+1.0`) is published every month and supported for two months; -- A new major version (`XX+1.1.0`) is published twice a year, in April and October, and supported for eight months. +Nix has a release cycle of roughly 6 weeks. +Notable changes and additions are announced in the release notes for each version. -The rationale behind that cycle is that -- Minor versions stay close to master and bring early access to new features for the user who need them; -- Major versions are aligned with the NixOS releases (released one month before NixOS and supported for as long at it). +The supported Nix versions are: +- The latest release +- The version used in the stable NixOS release, which is announced in the [NixOS release notes](https://nixos.org/manual/nixos/stable/release-notes.html#ch-release-notes). Bugfixes and security issues are backported to every supported version. Patch releases are published as needed. Notable changes and additions are announced in the release notes for each version. + From 45001c332d99a1decacfc128fc7f0578c95f3ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= <7226587+thufschmitt@users.noreply.github.com> Date: Mon, 25 Mar 2024 10:48:22 +0100 Subject: [PATCH 046/168] Remove accidental duplicate --- doc/manual/src/release-notes/index.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/doc/manual/src/release-notes/index.md b/doc/manual/src/release-notes/index.md index 331ddf685..d4e6292a6 100644 --- a/doc/manual/src/release-notes/index.md +++ b/doc/manual/src/release-notes/index.md @@ -11,6 +11,3 @@ The supported Nix versions are: Bugfixes and security issues are backported to every supported version. Patch releases are published as needed. - -Notable changes and additions are announced in the release notes for each version. - From bead1a1cde7bba6442266a39fc61d7fab5de68ae Mon Sep 17 00:00:00 2001 From: John Ericson Date: Mon, 25 Mar 2024 11:26:13 -0400 Subject: [PATCH 047/168] Add @Ericson2314 as libstore codeowner --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 526fecabf..59db217d9 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -14,4 +14,4 @@ src/libexpr/primops.cc @roberth # Libstore layer -/src/libstore @thufschmitt +/src/libstore @thufschmitt @ericson2314 From 6221770c9de4d28137206bdcd1a67eea12e1e499 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 15:04:06 +0100 Subject: [PATCH 048/168] tests/functional: Add count() --- tests/functional/common/vars-and-functions.sh.in | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/functional/common/vars-and-functions.sh.in b/tests/functional/common/vars-and-functions.sh.in index 3975986c0..e7e2fc770 100644 --- a/tests/functional/common/vars-and-functions.sh.in +++ b/tests/functional/common/vars-and-functions.sh.in @@ -283,6 +283,11 @@ grepQuietInverse() { ! grep "$@" > /dev/null } +# Return the number of arguments +count() { + echo $# +} + trap onError ERR fi # COMMON_VARS_AND_FUNCTIONS_SH_SOURCED From b1fe388d33530f0157dcf9f461348b61eda13228 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 15:08:29 +0100 Subject: [PATCH 049/168] Remove uncalled for message --- src/libstore/build/local-derivation-goal.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index 612434e4d..e6c402d7f 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -2089,7 +2089,7 @@ void LocalDerivationGoal::runChild() bool allowLocalNetworking = parsedDrv->getBoolAttr("__darwinAllowLocalNetworking"); /* The tmpDir in scope points at the temporary build directory for our derivation. Some packages try different mechanisms - to find temporary directories, so we want to open up a broader place for them to dump their files, if needed. */ + to find temporary directories, so we want to open up a broader place for them to put their files, if needed. */ Path globalTmpDir = canonPath(defaultTempDir(), true); /* They don't like trailing slashes on subpath directives */ From 8b16cced18925aa612049d08d5e78eccbf0530e4 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 15:07:00 +0100 Subject: [PATCH 050/168] Add build-dir setting --- src/libstore/build/local-derivation-goal.cc | 2 +- src/libstore/globals.hh | 30 +++++++++++++++++---- tests/functional/check.sh | 15 +++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index e6c402d7f..d52ebf064 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -488,7 +488,7 @@ void LocalDerivationGoal::startBuilder() /* Create a temporary directory where the build will take place. */ - tmpDir = createTempDir("", "nix-build-" + std::string(drvPath.name()), false, false, 0700); + tmpDir = createTempDir(settings.buildDir.get().value_or(""), "nix-build-" + std::string(drvPath.name()), false, false, 0700); chownToBuilder(tmpDir); diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index e6544976a..e6acf0a4f 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -687,16 +687,36 @@ public: Setting sandboxShmSize{ this, "50%", "sandbox-dev-shm-size", R"( - This option determines the maximum size of the `tmpfs` filesystem - mounted on `/dev/shm` in Linux sandboxes. For the format, see the - description of the `size` option of `tmpfs` in mount(8). The default - is `50%`. + *Linux only* + + This option determines the maximum size of the `tmpfs` filesystem + mounted on `/dev/shm` in Linux sandboxes. For the format, see the + description of the `size` option of `tmpfs` in mount(8). The default + is `50%`. )"}; Setting sandboxBuildDir{this, "/build", "sandbox-build-dir", - "The build directory inside the sandbox."}; + R"( + *Linux only* + + The build directory inside the sandbox. + + This directory is backed by [`build-dir`](#conf-build-dir) on the host. + )"}; #endif + Setting> buildDir{this, std::nullopt, "build-dir", + R"( + The directory on the host, in which derivations' temporary build directories are created. + + If not set, Nix will use the system temporary directory indicated by the `TMPDIR` environment variable. + Note that builds are often performed by the Nix daemon, so its `TMPDIR` is used, and not that of the Nix command line interface. + + This is also the location where [`--keep-failed`](@docroot@/command-ref/opt-common.md#opt-keep-failed) leaves its files. + + If Nix runs without sandbox, or if the platform does not support sandboxing with bind mounts (e.g. macOS), then the [`builder`](@docroot@/language/derivations.md#attr-builder)'s environment will contain this directory, instead of the virtual location [`sandbox-build-dir`](#conf-sandbox-build-dir). + )"}; + Setting allowedImpureHostPrefixes{this, {}, "allowed-impure-host-deps", "Which prefixes to allow derivations to ask for access to (primarily for Darwin)."}; diff --git a/tests/functional/check.sh b/tests/functional/check.sh index e13abf747..38883c5d7 100644 --- a/tests/functional/check.sh +++ b/tests/functional/check.sh @@ -34,6 +34,21 @@ nix-build check.nix -A failed --argstr checkBuildId $checkBuildId \ [ "$status" = "100" ] if checkBuildTempDirRemoved $TEST_ROOT/log; then false; fi +test_custom_build_dir() { + local customBuildDir="$TEST_ROOT/custom-build-dir" + + # Nix does not create the parent directories, and perhaps it shouldn't try to + # decide the permissions of build-dir. + mkdir "$customBuildDir" + nix-build check.nix -A failed --argstr checkBuildId $checkBuildId \ + --no-out-link --keep-failed --option build-dir "$TEST_ROOT/custom-build-dir" 2> $TEST_ROOT/log || status=$? + [ "$status" = "100" ] + [[ 1 == "$(count "$customBuildDir/nix-build-"*)" ]] + local buildDir="$customBuildDir/nix-build-"* + grep $checkBuildId $buildDir/checkBuildId +} +test_custom_build_dir + nix-build check.nix -A deterministic --argstr checkBuildId $checkBuildId \ --no-out-link 2> $TEST_ROOT/log checkBuildTempDirRemoved $TEST_ROOT/log From 2d4edb945bc02c074a922bfa3f38cbf21976728b Mon Sep 17 00:00:00 2001 From: K900 Date: Tue, 26 Mar 2024 10:55:15 +0300 Subject: [PATCH 051/168] build-remote: fix format string shenanigans HintFmt(string) invokes the HintFmt("%s", literal) constructor, which is not what we want here. Add a constructor with a proper name and call that. Next step: rename all the other ones to HintFmt::literal(string). Fixes https://github.com/NixOS/nix/issues/10238 --- src/build-remote/build-remote.cc | 2 +- src/libutil/fmt.hh | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/build-remote/build-remote.cc b/src/build-remote/build-remote.cc index 118468477..18eee830b 100644 --- a/src/build-remote/build-remote.cc +++ b/src/build-remote/build-remote.cc @@ -202,7 +202,7 @@ static int main_build_remote(int argc, char * * argv) else drvstr = ""; - auto error = HintFmt(errorText); + auto error = HintFmt::fromFormatString(errorText); error % drvstr % neededSystem diff --git a/src/libutil/fmt.hh b/src/libutil/fmt.hh index abbaf95b6..c178257d4 100644 --- a/src/libutil/fmt.hh +++ b/src/libutil/fmt.hh @@ -144,6 +144,10 @@ public: : HintFmt("%s", Uncolored(literal)) { } + static HintFmt fromFormatString(const std::string & format) { + return HintFmt(boost::format(format)); + } + /** * Interpolate the given arguments into the format string. */ From a2c3333b97582392732adabb017d392ecbebad6e Mon Sep 17 00:00:00 2001 From: Cyclic4179 <147778028+Cyclic4179@users.noreply.github.com> Date: Wed, 27 Mar 2024 07:47:36 +0100 Subject: [PATCH 052/168] fix #10336 --- src/libutil/args.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libutil/args.cc b/src/libutil/args.cc index a981ed9fb..834fc7314 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -285,7 +285,7 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang) std::string line; std::getline(stream,line); - static const std::string commentChars("#/\\%@*-"); + static const std::string commentChars("#/\\%@*-("); std::string shebangContent; while (std::getline(stream,line) && !line.empty() && commentChars.find(line[0]) != std::string::npos){ line = chomp(line); From 6f3972498bc73b6d3d81fb8291cc273e5f6c80b2 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 27 Mar 2024 12:59:41 +0100 Subject: [PATCH 053/168] Run some VM tests on GitHub --- .github/workflows/ci.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8bd355cca..818e81ec8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -159,3 +159,11 @@ jobs: # deprecated 2024-02-24 docker tag nix:$NIX_VERSION $IMAGE_ID:master docker push $IMAGE_ID:master + + vm_tests: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main + - run: nix build -L .#hydraJobs.tests.githubFlakes .#hydraJobs.tests.tarballFlakes From 6227cd06bd290290769c26ea91c1a6e4720842e3 Mon Sep 17 00:00:00 2001 From: Cyclic4179 <147778028+Cyclic4179@users.noreply.github.com> Date: Wed, 27 Mar 2024 14:55:02 +0100 Subject: [PATCH 054/168] add flakes.sh test for shebang supported types of comments --- tests/functional/flakes/flakes.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/functional/flakes/flakes.sh b/tests/functional/flakes/flakes.sh index 427290883..14529c475 100644 --- a/tests/functional/flakes/flakes.sh +++ b/tests/functional/flakes/flakes.sh @@ -93,6 +93,23 @@ foo EOF chmod +x $nonFlakeDir/shebang-comments.sh +cat > $nonFlakeDir/shebang-different-comments.sh < $nonFlakeDir/shebang-reject.sh <&1 | grepQuiet -F 'error: unsupported unquoted character in nix shebang: *. Use double backticks to escape?' From 9f7b2b93ceb479571e4c0f4bbe587e66528e4ec7 Mon Sep 17 00:00:00 2001 From: Cyclic4179 <147778028+Cyclic4179@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:03:54 +0100 Subject: [PATCH 055/168] fixup! add flakes.sh test for shebang supported types of comments --- tests/functional/flakes/flakes.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/functional/flakes/flakes.sh b/tests/functional/flakes/flakes.sh index 14529c475..4f41cae0a 100644 --- a/tests/functional/flakes/flakes.sh +++ b/tests/functional/flakes/flakes.sh @@ -97,12 +97,13 @@ cat > $nonFlakeDir/shebang-different-comments.sh < Date: Sat, 23 Mar 2024 23:56:05 +0100 Subject: [PATCH 056/168] Always print addErrorContext traces --- src/libexpr/primops.cc | 2 +- src/libutil/error.cc | 38 ++++++++++++------- src/libutil/error.hh | 9 ++++- tests/functional/lang.sh | 10 ++++- .../eval-fail-addErrorContext-example.err.exp | 38 +++++++++++++++++++ .../eval-fail-addErrorContext-example.flags | 1 + .../eval-fail-addErrorContext-example.nix | 9 +++++ 7 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 tests/functional/lang/eval-fail-addErrorContext-example.err.exp create mode 100644 tests/functional/lang/eval-fail-addErrorContext-example.flags create mode 100644 tests/functional/lang/eval-fail-addErrorContext-example.nix diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index d0fcfd194..8a188e0c4 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -826,7 +826,7 @@ static void prim_addErrorContext(EvalState & state, const PosIdx pos, Value * * auto message = state.coerceToString(pos, *args[0], context, "while evaluating the error message passed to builtins.addErrorContext", false, false).toOwned(); - e.addTrace(nullptr, HintFmt(message)); + e.addTrace(nullptr, HintFmt(message), TraceKind::Custom); throw; } } diff --git a/src/libutil/error.cc b/src/libutil/error.cc index d1e864a1a..fa7dadb27 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -11,9 +11,9 @@ namespace nix { -void BaseError::addTrace(std::shared_ptr && e, HintFmt hint) +void BaseError::addTrace(std::shared_ptr && e, HintFmt hint, TraceKind kind) { - err.traces.push_front(Trace { .pos = std::move(e), .hint = hint }); + err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .kind = kind }); } void throwExceptionSelfCheck(){ @@ -379,29 +379,39 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s // A consecutive sequence of stack traces that are all in `tracesSeen`. std::vector skippedTraces; size_t count = 0; + bool truncate = false; for (const auto & trace : einfo.traces) { if (trace.hint.str().empty()) continue; if (!showTrace && count > 3) { - oss << "\n" << ANSI_WARNING "(stack trace truncated; use '--show-trace' to show the full trace)" ANSI_NORMAL << "\n"; - break; + truncate = true; } - if (tracesSeen.count(trace)) { - skippedTraces.push_back(trace); - continue; + if (!truncate || trace.kind == TraceKind::Custom) { + + if (tracesSeen.count(trace)) { + skippedTraces.push_back(trace); + continue; + } + + tracesSeen.insert(trace); + + printSkippedTracesMaybe(oss, ellipsisIndent, count, skippedTraces, tracesSeen); + + count++; + + printTrace(oss, ellipsisIndent, count, trace); } - tracesSeen.insert(trace); - - printSkippedTracesMaybe(oss, ellipsisIndent, count, skippedTraces, tracesSeen); - - count++; - - printTrace(oss, ellipsisIndent, count, trace); } + printSkippedTracesMaybe(oss, ellipsisIndent, count, skippedTraces, tracesSeen); + + if (truncate) { + oss << "\n" << ANSI_WARNING "(stack trace truncated; use '--show-trace' to show the full, detailed trace)" ANSI_NORMAL << "\n"; + } + oss << "\n" << prefix; } diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 89f5ad021..6701a75b3 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -61,9 +61,16 @@ void printCodeLines(std::ostream & out, const Pos & errPos, const LinesOfCode & loc); +enum struct TraceKind { + Other, + /** Produced by builtins.addErrorContext. Always printed. */ + Custom, +}; + struct Trace { std::shared_ptr pos; HintFmt hint; + TraceKind kind = TraceKind::Other; }; inline bool operator<(const Trace& lhs, const Trace& rhs); @@ -161,7 +168,7 @@ public: addTrace(std::move(e), HintFmt(std::string(fs), args...)); } - void addTrace(std::shared_ptr && e, HintFmt hint); + void addTrace(std::shared_ptr && e, HintFmt hint, TraceKind kind = TraceKind::Other); bool hasTrace() const { return !err.traces.empty(); } diff --git a/tests/functional/lang.sh b/tests/functional/lang.sh index 12df32c87..e35795a7a 100755 --- a/tests/functional/lang.sh +++ b/tests/functional/lang.sh @@ -68,8 +68,16 @@ done for i in lang/eval-fail-*.nix; do echo "evaluating $i (should fail)"; i=$(basename "$i" .nix) + flags="$( + if [[ -e "lang/$i.flags" ]]; then + sed -e 's/#.*//' < "lang/$i.flags" + else + # note that show-trace is also set by init.sh + echo "--eval --strict --show-trace" + fi + )" if - expectStderr 1 nix-instantiate --eval --strict --show-trace "lang/$i.nix" \ + expectStderr 1 nix-instantiate $flags "lang/$i.nix" \ | sed "s!$(pwd)!/pwd!g" > "lang/$i.err" then diffAndAccept "$i" err err.exp diff --git a/tests/functional/lang/eval-fail-addErrorContext-example.err.exp b/tests/functional/lang/eval-fail-addErrorContext-example.err.exp new file mode 100644 index 000000000..3d390cd37 --- /dev/null +++ b/tests/functional/lang/eval-fail-addErrorContext-example.err.exp @@ -0,0 +1,38 @@ +error: + … while calling the 'addErrorContext' builtin + at /pwd/lang/eval-fail-addErrorContext-example.nix:6:7: + 5| else + 6| builtins.addErrorContext + | ^ + 7| "while counting down; n = ${toString n}" + + … while counting down; n = 10 + + … while calling the 'addErrorContext' builtin + at /pwd/lang/eval-fail-addErrorContext-example.nix:6:7: + 5| else + 6| builtins.addErrorContext + | ^ + 7| "while counting down; n = ${toString n}" + + … while counting down; n = 9 + + … while counting down; n = 8 + + … while counting down; n = 7 + + … while counting down; n = 6 + + … while counting down; n = 5 + + … while counting down; n = 4 + + … while counting down; n = 3 + + … while counting down; n = 2 + + … while counting down; n = 1 + + (stack trace truncated; use '--show-trace' to show the full, detailed trace) + + error: kaboom diff --git a/tests/functional/lang/eval-fail-addErrorContext-example.flags b/tests/functional/lang/eval-fail-addErrorContext-example.flags new file mode 100644 index 000000000..9b1f6458f --- /dev/null +++ b/tests/functional/lang/eval-fail-addErrorContext-example.flags @@ -0,0 +1 @@ +--eval --strict --no-show-trace diff --git a/tests/functional/lang/eval-fail-addErrorContext-example.nix b/tests/functional/lang/eval-fail-addErrorContext-example.nix new file mode 100644 index 000000000..996b24688 --- /dev/null +++ b/tests/functional/lang/eval-fail-addErrorContext-example.nix @@ -0,0 +1,9 @@ +let + countDown = n: + if n == 0 + then throw "kaboom" + else + builtins.addErrorContext + "while counting down; n = ${toString n}" + ("x" + countDown (n - 1)); +in countDown 10 From 981c3090570ca3b9c75098ab482049cc3b4dc2b0 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 00:04:30 +0100 Subject: [PATCH 057/168] Remove trace item: while calling the 'addErrorContext' builtin --- src/libexpr/eval.cc | 6 ++++-- src/libexpr/eval.hh | 7 +++++++ src/libexpr/primops.cc | 2 ++ .../lang/eval-fail-addErrorContext-example.err.exp | 14 -------------- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 5e2f71649..86251adf7 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -1665,7 +1665,8 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value & try { fn->fun(*this, vCur.determinePos(noPos), args, vCur); } catch (Error & e) { - addErrorTrace(e, pos, "while calling the '%1%' builtin", fn->name); + if (fn->addTrace) + addErrorTrace(e, pos, "while calling the '%1%' builtin", fn->name); throw; } @@ -1713,7 +1714,8 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value & // so the debugger allows to inspect the wrong parameters passed to the builtin. fn->fun(*this, vCur.determinePos(noPos), vArgs, vCur); } catch (Error & e) { - addErrorTrace(e, pos, "while calling the '%1%' builtin", fn->name); + if (fn->addTrace) + addErrorTrace(e, pos, "while calling the '%1%' builtin", fn->name); throw; } diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index f15d19653..f45971290 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -69,6 +69,13 @@ struct PrimOp */ const char * doc = nullptr; + /** + * Add a trace item, `while calling the '' builtin` + * + * This is used to remove the redundant item for `builtins.addErrorContext`. + */ + bool addTrace = true; + /** * Implementation of the primop. */ diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 8a188e0c4..2345a5e3c 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -834,6 +834,8 @@ static void prim_addErrorContext(EvalState & state, const PosIdx pos, Value * * static RegisterPrimOp primop_addErrorContext(PrimOp { .name = "__addErrorContext", .arity = 2, + // The normal trace item is redundant + .addTrace = false, .fun = prim_addErrorContext, }); diff --git a/tests/functional/lang/eval-fail-addErrorContext-example.err.exp b/tests/functional/lang/eval-fail-addErrorContext-example.err.exp index 3d390cd37..4fad8f5c8 100644 --- a/tests/functional/lang/eval-fail-addErrorContext-example.err.exp +++ b/tests/functional/lang/eval-fail-addErrorContext-example.err.exp @@ -1,20 +1,6 @@ error: - … while calling the 'addErrorContext' builtin - at /pwd/lang/eval-fail-addErrorContext-example.nix:6:7: - 5| else - 6| builtins.addErrorContext - | ^ - 7| "while counting down; n = ${toString n}" - … while counting down; n = 10 - … while calling the 'addErrorContext' builtin - at /pwd/lang/eval-fail-addErrorContext-example.nix:6:7: - 5| else - 6| builtins.addErrorContext - | ^ - 7| "while counting down; n = ${toString n}" - … while counting down; n = 9 … while counting down; n = 8 From bed541b04e88fea5e9317ece34ed887db09a5bc0 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 00:16:16 +0100 Subject: [PATCH 058/168] error.cc: Make printTrace static --- src/libutil/error.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libutil/error.cc b/src/libutil/error.cc index fa7dadb27..e9c6437cd 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -163,7 +163,7 @@ static bool printPosMaybe(std::ostream & oss, std::string_view indent, const std return hasPos; } -void printTrace( +static void printTrace( std::ostream & output, const std::string_view & indent, size_t & count, From d4fa0a84a55857f034b84015f99fe2b206230be8 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 27 Mar 2024 16:27:06 +0100 Subject: [PATCH 059/168] refact: TraceKind -> TracePrint Co-authored-by: Rebecca Turner --- src/libexpr/primops.cc | 2 +- src/libutil/error.cc | 6 +++--- src/libutil/error.hh | 18 ++++++++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 2345a5e3c..9ec32a0de 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -826,7 +826,7 @@ static void prim_addErrorContext(EvalState & state, const PosIdx pos, Value * * auto message = state.coerceToString(pos, *args[0], context, "while evaluating the error message passed to builtins.addErrorContext", false, false).toOwned(); - e.addTrace(nullptr, HintFmt(message), TraceKind::Custom); + e.addTrace(nullptr, HintFmt(message), TracePrint::Always); throw; } } diff --git a/src/libutil/error.cc b/src/libutil/error.cc index e9c6437cd..036e19e26 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -11,9 +11,9 @@ namespace nix { -void BaseError::addTrace(std::shared_ptr && e, HintFmt hint, TraceKind kind) +void BaseError::addTrace(std::shared_ptr && e, HintFmt hint, TracePrint print) { - err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .kind = kind }); + err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .print = print }); } void throwExceptionSelfCheck(){ @@ -388,7 +388,7 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s truncate = true; } - if (!truncate || trace.kind == TraceKind::Custom) { + if (!truncate || trace.print == TracePrint::Always) { if (tracesSeen.count(trace)) { skippedTraces.push_back(trace); diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 6701a75b3..d23625a54 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -61,16 +61,22 @@ void printCodeLines(std::ostream & out, const Pos & errPos, const LinesOfCode & loc); -enum struct TraceKind { - Other, - /** Produced by builtins.addErrorContext. Always printed. */ - Custom, +/** + * When a stack frame is printed. + */ +enum struct TracePrint { + /** + * The default behavior; always printed when `--show-trace` is set. + */ + Default, + /** Always printed. Produced by `builtins.addErrorContext`. */ + Always, }; struct Trace { std::shared_ptr pos; HintFmt hint; - TraceKind kind = TraceKind::Other; + TracePrint print = TracePrint::Default; }; inline bool operator<(const Trace& lhs, const Trace& rhs); @@ -168,7 +174,7 @@ public: addTrace(std::move(e), HintFmt(std::string(fs), args...)); } - void addTrace(std::shared_ptr && e, HintFmt hint, TraceKind kind = TraceKind::Other); + void addTrace(std::shared_ptr && e, HintFmt hint, TracePrint print = TracePrint::Default); bool hasTrace() const { return !err.traces.empty(); } From 7205a6bbc93db7ea9b013c73413acaa4bc06dc35 Mon Sep 17 00:00:00 2001 From: annalee <150648636+a-n-n-a-l-e-e@users.noreply.github.com> Date: Sat, 23 Mar 2024 06:41:33 +0000 Subject: [PATCH 060/168] enable persistent WAL mode for sqlite db allow processes without write access to the directory containing the db to read the db when all connections are closed. Without this setting and with WAL enabled and no open db connections unprivileged processes will fail to open the db due the WAL files not existing and not able to create them. When the WAL files are persistent unprivileged processeses can read the db when there are no open connections. Additionally, journal_size_limit is set to 2^40, which results in the WAL files being truncated to 0 on exit, as well as limiting the WAL files to 2^40 bytes following a checkpoint. https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html#sqlitefcntlpersistwal https://www.sqlite.org/pragma.html#pragma_journal_size_limit https://github.com/sqlite/sqlite/blob/ed517a708284b6e00b6ae5f1e3f702bbfcbd32ed/src/wal.c#L2518 Fixes https://github.com/NixOS/nix/issues/10300 --- src/libstore/local-store.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 1bbeaa912..e4c4126aa 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -552,6 +552,19 @@ void LocalStore::openDB(State & state, bool create) sqlite3_exec(db, ("pragma main.journal_mode = " + mode + ";").c_str(), 0, 0, 0) != SQLITE_OK) SQLiteError::throw_(db, "setting journal mode"); + if (mode == "wal") { + /* persist the WAL files when the db connection is closed. This allows + for read-only connections without write permissions on the + containing directory to succeed on a closed db. Setting the + journal_size_limit to 2^40 bytes results in the WAL files getting + truncated to 0 on exit and limits the on disk size of the WAL files + to 2^40 bytes following a checkpoint */ + if (sqlite3_exec(db, "pragma main.journal_size_limit = 1099511627776;", 0, 0, 0) == SQLITE_OK) { + int enable = 1; + sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, &enable); + } + } + /* Increase the auto-checkpoint interval to 40000 pages. This seems enough to ensure that instantiating the NixOS system derivation is done in a single fsync(). */ From ae2b2849c97399b7aa81b2df76ff738887545b8d Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 12 Jan 2024 20:21:33 -0500 Subject: [PATCH 061/168] Disable GC on windows We can build the dep and the our GC code is totally portable, but for some reason we get link errors saying `GC_throw_bad_alloc` is missing. --- package.nix | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.nix b/package.nix index 7d9a39771..db17badf0 100644 --- a/package.nix +++ b/package.nix @@ -75,7 +75,10 @@ # sounds so long as evaluation just takes places within short-lived # processes. (When the process exits, the memory is reclaimed; it is # only leaked *within* the process.) -, enableGC ? true +# +# Temporarily disabled on Windows because the `GC_throw_bad_alloc` +# symbol is missing during linking. +, enableGC ? !stdenv.hostPlatform.isWindows # Whether to enable Markdown rendering in the Nix binary. , enableMarkdown ? !stdenv.hostPlatform.isWindows From 4a61827d2d3f45b7bd8503691611409cdea2d46e Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 14 Nov 2023 17:18:58 -0500 Subject: [PATCH 062/168] Hack to make sure the DLL linking job works with the check output --- package.nix | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/package.nix b/package.nix index db17badf0..a334ae48c 100644 --- a/package.nix +++ b/package.nix @@ -338,6 +338,12 @@ in { echo "doc internal-api-docs $out/share/doc/nix/internal-api/html" >> ''${!outputDoc}/nix-support/hydra-build-products ''; + # So the check output gets links for DLLs in the out output. + preFixup = lib.optionalString (stdenv.hostPlatform.isWindows && builtins.elem "check" finalAttrs.outputs) '' + ln -s "$check/lib/"*.dll "$check/bin" + ln -s "$out/bin/"*.dll "$check/bin" + ''; + doInstallCheck = attrs.doInstallCheck; installCheckFlags = "sysconfdir=$(out)/etc"; From 77205b2042d9fb888b88faf33bd71ac823068368 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sat, 2 Sep 2023 17:10:28 -0400 Subject: [PATCH 063/168] Allow for ergnomically putting Unix-only files in subdirs by creating `INLCUDE_$(pkg)` vars Separate platform-specific files will allow avoiding a lot of CPP. --- local.mk | 2 +- src/libcmd/local.mk | 2 +- src/libexpr/local.mk | 5 ++++- src/libfetchers/local.mk | 6 +++++- src/libmain/local.mk | 2 +- src/libstore/local.mk | 8 ++++++-- src/libutil/local.mk | 5 ++++- src/nix/local.mk | 2 +- src/resolve-system-dependencies/local.mk | 2 +- tests/functional/plugins/local.mk | 2 +- tests/functional/test-libstoreconsumer/local.mk | 2 +- tests/unit/libexpr/local.mk | 8 ++++---- tests/unit/libstore/local.mk | 4 ++-- tests/unit/libutil/local.mk | 2 +- 14 files changed, 33 insertions(+), 19 deletions(-) diff --git a/local.mk b/local.mk index 3f3abb9f0..7e3c77a65 100644 --- a/local.mk +++ b/local.mk @@ -7,4 +7,4 @@ $(foreach i, config.h $(wildcard src/lib*/*.hh), \ $(GCH): src/libutil/util.hh config.h -GCH_CXXFLAGS = -I src/libutil +GCH_CXXFLAGS = $(INCLUDE_libutil) diff --git a/src/libcmd/local.mk b/src/libcmd/local.mk index abb7459a7..7a7c46ee2 100644 --- a/src/libcmd/local.mk +++ b/src/libcmd/local.mk @@ -6,7 +6,7 @@ libcmd_DIR := $(d) libcmd_SOURCES := $(wildcard $(d)/*.cc) -libcmd_CXXFLAGS += -I src/libutil -I src/libstore -I src/libexpr -I src/libmain -I src/libfetchers +libcmd_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) $(INCLUDE_libexpr) -I src/libmain libcmd_LDFLAGS = $(EDITLINE_LIBS) $(LOWDOWN_LIBS) $(THREAD_LDFLAGS) diff --git a/src/libexpr/local.mk b/src/libexpr/local.mk index 0c3e36750..17f793ec3 100644 --- a/src/libexpr/local.mk +++ b/src/libexpr/local.mk @@ -11,8 +11,11 @@ libexpr_SOURCES := \ $(wildcard $(d)/flake/*.cc) \ $(d)/lexer-tab.cc \ $(d)/parser-tab.cc +# Not just for this library itself, but also for downstream libraries using this library -libexpr_CXXFLAGS += -I src/libutil -I src/libstore -I src/libfetchers -I src/libmain -I src/libexpr +INCLUDE_libexpr := -I $(d) + +libexpr_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) -I src/libmain $(INCLUDE_libexpr) libexpr_LIBS = libutil libstore libfetchers diff --git a/src/libfetchers/local.mk b/src/libfetchers/local.mk index e54db4937..e229a0993 100644 --- a/src/libfetchers/local.mk +++ b/src/libfetchers/local.mk @@ -6,7 +6,11 @@ libfetchers_DIR := $(d) libfetchers_SOURCES := $(wildcard $(d)/*.cc) -libfetchers_CXXFLAGS += -I src/libutil -I src/libstore +# Not just for this library itself, but also for downstream libraries using this library + +INCLUDE_libfetchers := -I $(d) + +libfetchers_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) libfetchers_LDFLAGS += $(THREAD_LDFLAGS) $(LIBGIT2_LIBS) -larchive diff --git a/src/libmain/local.mk b/src/libmain/local.mk index 5c7061863..fde28a820 100644 --- a/src/libmain/local.mk +++ b/src/libmain/local.mk @@ -6,7 +6,7 @@ libmain_DIR := $(d) libmain_SOURCES := $(wildcard $(d)/*.cc) -libmain_CXXFLAGS += -I src/libutil -I src/libstore +libmain_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) libmain_LDFLAGS += $(OPENSSL_LIBS) diff --git a/src/libstore/local.mk b/src/libstore/local.mk index f86643849..7eaa317e5 100644 --- a/src/libstore/local.mk +++ b/src/libstore/local.mk @@ -10,7 +10,7 @@ libstore_LIBS = libutil libstore_LDFLAGS += $(SQLITE3_LIBS) $(LIBCURL_LIBS) $(THREAD_LDFLAGS) ifdef HOST_LINUX - libstore_LDFLAGS += -ldl + libstore_LDFLAGS += -ldl endif $(foreach file,$(libstore_FILES),$(eval $(call install-data-in,$(d)/$(file),$(datadir)/nix/sandbox))) @@ -27,8 +27,12 @@ ifeq ($(HAVE_SECCOMP), 1) libstore_LDFLAGS += $(LIBSECCOMP_LIBS) endif +# Not just for this library itself, but also for downstream libraries using this library + +INCLUDE_libstore := -I $(d) -I $(d)/build + libstore_CXXFLAGS += \ - -I src/libutil -I src/libstore -I src/libstore/build \ + $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libstore) \ -DNIX_PREFIX=\"$(prefix)\" \ -DNIX_STORE_DIR=\"$(storedir)\" \ -DNIX_DATA_DIR=\"$(datadir)\" \ diff --git a/src/libutil/local.mk b/src/libutil/local.mk index 200026c1e..4ffb6bd9d 100644 --- a/src/libutil/local.mk +++ b/src/libutil/local.mk @@ -6,7 +6,10 @@ libutil_DIR := $(d) libutil_SOURCES := $(wildcard $(d)/*.cc $(d)/signature/*.cc) -libutil_CXXFLAGS += -I src/libutil +# Not just for this library itself, but also for downstream libraries using this library + +INCLUDE_libutil := -I $(d) +libutil_CXXFLAGS += $(INCLUDE_libutil) libutil_LDFLAGS += $(THREAD_LDFLAGS) $(LIBCURL_LIBS) $(SODIUM_LIBS) $(OPENSSL_LIBS) $(LIBBROTLI_LIBS) $(LIBARCHIVE_LIBS) $(BOOST_LDFLAGS) -lboost_context diff --git a/src/nix/local.mk b/src/nix/local.mk index 1d6f560d6..55544b564 100644 --- a/src/nix/local.mk +++ b/src/nix/local.mk @@ -14,7 +14,7 @@ nix_SOURCES := \ $(wildcard src/nix-instantiate/*.cc) \ $(wildcard src/nix-store/*.cc) \ -nix_CXXFLAGS += -I src/libutil -I src/libstore -I src/libfetchers -I src/libexpr -I src/libmain -I src/libcmd -I doc/manual +nix_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) $(INCLUDE_libexpr) -I src/libmain -I src/libcmd -I doc/manual nix_LIBS = libexpr libmain libfetchers libstore libutil libcmd diff --git a/src/resolve-system-dependencies/local.mk b/src/resolve-system-dependencies/local.mk index fc48a8417..f28fdab3b 100644 --- a/src/resolve-system-dependencies/local.mk +++ b/src/resolve-system-dependencies/local.mk @@ -6,7 +6,7 @@ resolve-system-dependencies_DIR := $(d) resolve-system-dependencies_INSTALL_DIR := $(libexecdir)/nix -resolve-system-dependencies_CXXFLAGS += -I src/libutil -I src/libstore -I src/libmain +resolve-system-dependencies_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) -I src/libmain resolve-system-dependencies_LIBS := libstore libmain libutil diff --git a/tests/functional/plugins/local.mk b/tests/functional/plugins/local.mk index 40350aa96..2314e1341 100644 --- a/tests/functional/plugins/local.mk +++ b/tests/functional/plugins/local.mk @@ -8,4 +8,4 @@ libplugintest_ALLOW_UNDEFINED := 1 libplugintest_EXCLUDE_FROM_LIBRARY_LIST := 1 -libplugintest_CXXFLAGS := -I src/libutil -I src/libstore -I src/libexpr -I src/libfetchers +libplugintest_CXXFLAGS := $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libexpr) $(INCLUDE_libfetchers) diff --git a/tests/functional/test-libstoreconsumer/local.mk b/tests/functional/test-libstoreconsumer/local.mk index a1825c405..3e8581c57 100644 --- a/tests/functional/test-libstoreconsumer/local.mk +++ b/tests/functional/test-libstoreconsumer/local.mk @@ -8,7 +8,7 @@ test-libstoreconsumer_INSTALL_DIR := test-libstoreconsumer_SOURCES := \ $(wildcard $(d)/*.cc) \ -test-libstoreconsumer_CXXFLAGS += -I src/libutil -I src/libstore +test-libstoreconsumer_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) test-libstoreconsumer_LIBS = libstore libutil diff --git a/tests/unit/libexpr/local.mk b/tests/unit/libexpr/local.mk index 25810ad9c..eda65508d 100644 --- a/tests/unit/libexpr/local.mk +++ b/tests/unit/libexpr/local.mk @@ -23,10 +23,10 @@ libexpr-tests_EXTRA_INCLUDES = \ -I tests/unit/libexpr-support \ -I tests/unit/libstore-support \ -I tests/unit/libutil-support \ - -I src/libexpr \ - -I src/libfetchers \ - -I src/libstore \ - -I src/libutil + $(INCLUDE_libexpr) \ + $(INCLUDE_libfetchers) \ + $(INCLUDE_libstore) \ + $(INCLUDE_libutil) libexpr-tests_CXXFLAGS += $(libexpr-tests_EXTRA_INCLUDES) diff --git a/tests/unit/libstore/local.mk b/tests/unit/libstore/local.mk index 63f6d011f..960dece89 100644 --- a/tests/unit/libstore/local.mk +++ b/tests/unit/libstore/local.mk @@ -19,8 +19,8 @@ libstore-tests_SOURCES := $(wildcard $(d)/*.cc) libstore-tests_EXTRA_INCLUDES = \ -I tests/unit/libstore-support \ -I tests/unit/libutil-support \ - -I src/libstore \ - -I src/libutil + $(INCLUDE_libstore) \ + $(INCLUDE_libutil) libstore-tests_CXXFLAGS += $(libstore-tests_EXTRA_INCLUDES) diff --git a/tests/unit/libutil/local.mk b/tests/unit/libutil/local.mk index 930efb90b..536607322 100644 --- a/tests/unit/libutil/local.mk +++ b/tests/unit/libutil/local.mk @@ -18,7 +18,7 @@ libutil-tests_SOURCES := $(wildcard $(d)/*.cc) libutil-tests_EXTRA_INCLUDES = \ -I tests/unit/libutil-support \ - -I src/libutil + $(INCLUDE_libutil) libutil-tests_CXXFLAGS += $(libutil-tests_EXTRA_INCLUDES) From c0dd111af1999515b08bbfd95d8623a73b5fbc88 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 27 Mar 2024 20:47:51 +0100 Subject: [PATCH 064/168] Fix flake evaluation in chroot stores This is a temporary fix until we can pass `SourcePath`s rather than `StorePath`s to `call-flake.nix`. Fixes #10331. --- src/libexpr/flake/flake.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/libexpr/flake/flake.cc b/src/libexpr/flake/flake.cc index bca473453..4a781beb8 100644 --- a/src/libexpr/flake/flake.cc +++ b/src/libexpr/flake/flake.cc @@ -10,6 +10,7 @@ #include "finally.hh" #include "fetch-settings.hh" #include "value-to-json.hh" +#include "local-fs-store.hh" namespace nix { @@ -755,7 +756,17 @@ void callFlake(EvalState & state, auto lockedNode = node.dynamic_pointer_cast(); - auto [storePath, subdir] = state.store->toStorePath(sourcePath.path.abs()); + // FIXME: This is a hack to support chroot stores. Remove this + // once we can pass a sourcePath rather than a storePath to + // call-flake.nix. + auto path = sourcePath.path.abs(); + if (auto store = state.store.dynamic_pointer_cast()) { + auto realStoreDir = store->getRealStoreDir(); + if (isInDir(path, realStoreDir)) + path = store->storeDir + path.substr(realStoreDir.size()); + } + + auto [storePath, subdir] = state.store->toStorePath(path); emitTreeAttrs( state, From 52359ca00ae1e3b3ae5ab831edb98ecdb9d5261a Mon Sep 17 00:00:00 2001 From: Tharun T Date: Thu, 28 Mar 2024 02:20:33 +0530 Subject: [PATCH 065/168] move test to correct file --- tests/functional/flakes/build-paths.sh | 19 +++++++++++++++++ tests/functional/flakes/flakes.sh | 29 -------------------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/tests/functional/flakes/build-paths.sh b/tests/functional/flakes/build-paths.sh index ff012e1b3..98827947d 100644 --- a/tests/functional/flakes/build-paths.sh +++ b/tests/functional/flakes/build-paths.sh @@ -56,6 +56,18 @@ cat > $flake1Dir/flake.nix < \$foo/file + echo "out" > \$out/file + ''; + outputSpecified = true; + }; }; } EOF @@ -94,3 +106,10 @@ nix build --json --out-link $TEST_ROOT/result $flake1Dir#a12 expectStderr 1 nix build --impure --json --out-link $TEST_ROOT/result $flake1Dir#a13 \ | grepQuiet "has 2 entries in its context. It should only have exactly one entry" + +# Test accessing output in installables with `.` (foobarbaz.) +nix build --json --no-link $flake1Dir#a14.foo | jq --exit-status ' + (.[0] | + (.drvPath | match(".*dot-installable.drv")) and + (.outputs | keys == ["foo"])) +' diff --git a/tests/functional/flakes/flakes.sh b/tests/functional/flakes/flakes.sh index f6931a578..427290883 100644 --- a/tests/functional/flakes/flakes.sh +++ b/tests/functional/flakes/flakes.sh @@ -285,35 +285,6 @@ git -C "$flake3Dir" add flake.lock git -C "$flake3Dir" commit -m 'Add lockfile' -# Test accessing output in installables with `.` (foobarbaz.) -cat > "$flake3Dir/flake.nix" < \$foo/file - echo "out" > \$out/file - ''; - outputSpecified = true; - }; - }; -} -EOF - -cp ../config.nix "$flake3Dir" -git -C "$flake3Dir" add flake.nix config.nix -git -C "$flake3Dir" commit -m 'multi outputs flake' - -nix build "$flake3Dir#hello.foo" --json --no-link | jq --exit-status ' - (.[0] | - (.drvPath | match(".*hello.drv")) and - (.outputs | keys == ["foo"])) -' - # Test whether registry caching works. nix registry list --flake-registry "file://$registry" | grepQuiet flake3 mv "$registry" "$registry.tmp" From 37f8edce99519c5c79d3659ac90d91eb9e7f5d82 Mon Sep 17 00:00:00 2001 From: "vac (Brendan)" Date: Thu, 28 Mar 2024 05:26:50 +0800 Subject: [PATCH 066/168] docs: add cacert in macos multi-user upgrade (#10237) --- doc/manual/src/installation/upgrading.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/manual/src/installation/upgrading.md b/doc/manual/src/installation/upgrading.md index 38edcdbc5..a433f1d30 100644 --- a/doc/manual/src/installation/upgrading.md +++ b/doc/manual/src/installation/upgrading.md @@ -28,7 +28,7 @@ $ sudo su ## macOS multi-user ```console -$ sudo nix-env --install --file '' --attr nix -I nixpkgs=channel:nixpkgs-unstable +$ sudo nix-env --install --file '' --attr nix cacert -I nixpkgs=channel:nixpkgs-unstable $ sudo launchctl remove org.nixos.nix-daemon $ sudo launchctl load /Library/LaunchDaemons/org.nixos.nix-daemon.plist ``` From c0b6907ccdaf3d3911cfdb2ff2d000e1683997c7 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 27 Mar 2024 22:28:05 +0100 Subject: [PATCH 067/168] doc/local.mk: Add manual-html-open phony target for auto-opening the browser (#10308) --- doc/manual/local.mk | 10 ++++++++++ doc/manual/src/contributing/documentation.md | 4 +--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/manual/local.mk b/doc/manual/local.mk index b77168885..698a9289b 100644 --- a/doc/manual/local.mk +++ b/doc/manual/local.mk @@ -175,6 +175,16 @@ $(d)/src/SUMMARY-rl-next.md: $(d)/src/release-notes/rl-next.md # Generate the HTML manual. .PHONY: manual-html manual-html: $(docdir)/manual/index.html + +# Open the built HTML manual in the default browser. +manual-html-open: $(docdir)/manual/index.html + @echo " OPEN " $<; \ + xdg-open $< \ + || open $< \ + || { \ + echo "Could not open the manual in a browser. Please open '$<'" >&2; \ + false; \ + } install: $(docdir)/manual/index.html # Generate 'nix' manpages. diff --git a/doc/manual/src/contributing/documentation.md b/doc/manual/src/contributing/documentation.md index 46cca759d..88b0bdaa9 100644 --- a/doc/manual/src/contributing/documentation.md +++ b/doc/manual/src/contributing/documentation.md @@ -27,11 +27,9 @@ and open `./result-doc/share/doc/nix/manual/index.html`. To build the manual incrementally, [enter the development shell](./hacking.md) and run: ```console -make manual-html -j $NIX_BUILD_CORES +make manual-html-open -j $NIX_BUILD_CORES ``` -and open `./outputs/doc/share/doc/nix/manual/language/index.html`. - In order to reflect changes to the [Makefile for the manual], clear all generated files before re-building: [Makefile for the manual]: https://github.com/NixOS/nix/blob/master/doc/manual/local.mk From c39afb28db3262e286415ddc8cb5ce847ef0ed36 Mon Sep 17 00:00:00 2001 From: "Yang, Bo" Date: Wed, 27 Mar 2024 16:46:50 -0700 Subject: [PATCH 068/168] Clarify stringLength is counting bytes --- src/libexpr/primops.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index db4237130..84f24de5a 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -3859,7 +3859,7 @@ static RegisterPrimOp primop_stringLength({ .name = "__stringLength", .args = {"e"}, .doc = R"( - Return the length of the string *e*. If *e* is not a string, + Return the number of bytes of the string *e*. If *e* is not a string, evaluation is aborted. )", .fun = prim_stringLength, From 4702317506926afba5542486ceaa964190b779db Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 14 Jul 2023 15:52:12 +0200 Subject: [PATCH 069/168] libutil: add C bindings --- local.mk | 2 +- src/libutil/error.hh | 4 + src/libutil/nix_api_util.cc | 148 ++++++++++++++++++ src/libutil/nix_api_util.h | 223 ++++++++++++++++++++++++++++ src/libutil/nix_api_util_internal.h | 61 ++++++++ 5 files changed, 437 insertions(+), 1 deletion(-) create mode 100644 src/libutil/nix_api_util.cc create mode 100644 src/libutil/nix_api_util.h create mode 100644 src/libutil/nix_api_util_internal.h diff --git a/local.mk b/local.mk index 7e3c77a65..f48eb63ee 100644 --- a/local.mk +++ b/local.mk @@ -2,7 +2,7 @@ GLOBAL_CXXFLAGS += -Wno-deprecated-declarations -Werror=switch # Allow switch-enum to be overridden for files that do not support it, usually because of dependency headers. ERROR_SWITCH_ENUM = -Werror=switch-enum -$(foreach i, config.h $(wildcard src/lib*/*.hh), \ +$(foreach i, config.h $(wildcard src/lib*/*.hh) $(wildcard src/lib*/*.h), \ $(eval $(call install-file-in, $(i), $(includedir)/nix, 0644))) $(GCH): src/libutil/util.hh config.h diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 89f5ad021..5212805bf 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -137,6 +137,10 @@ public: : err(e) { } + std::string message() { + return err.msg.str(); + } + const char * what() const noexcept override { return calcWhat().c_str(); } const std::string & msg() const { return calcWhat(); } const ErrorInfo & info() const { calcWhat(); return err; } diff --git a/src/libutil/nix_api_util.cc b/src/libutil/nix_api_util.cc new file mode 100644 index 000000000..4f892637c --- /dev/null +++ b/src/libutil/nix_api_util.cc @@ -0,0 +1,148 @@ +#include "nix_api_util.h" +#include "config.hh" +#include "error.hh" +#include "nix_api_util_internal.h" +#include "util.hh" + +#include +#include + +nix_c_context *nix_c_context_create() { return new nix_c_context(); } + +void nix_c_context_free(nix_c_context *context) { delete context; } + +nix_err nix_context_error(nix_c_context *context) { + if (context == nullptr) { + throw; + } + try { + throw; + } catch (nix::Error &e) { + /* Storing this exception is annoying, take what we need here */ + context->last_err = e.what(); + context->info = e.info(); + int status; + const char *demangled = + abi::__cxa_demangle(typeid(e).name(), 0, 0, &status); + if (demangled) { + context->name = demangled; + // todo: free(demangled); + } else { + context->name = typeid(e).name(); + } + context->last_err_code = NIX_ERR_NIX_ERROR; + return context->last_err_code; + } catch (const std::exception &e) { + context->last_err = e.what(); + context->last_err_code = NIX_ERR_UNKNOWN; + return context->last_err_code; + } + // unreachable +} + +nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg) { + if (context == nullptr) { + // todo last_err_code + throw new nix::Error("Nix C api error", msg); + } + context->last_err_code = err; + context->last_err = msg; + return err; +} + +const char *nix_version_get() { return PACKAGE_VERSION; } + +// Implementations +nix_err nix_setting_get(nix_c_context *context, const char *key, char *value, + int n) { + if (context) + context->last_err_code = NIX_OK; + try { + std::map settings; + nix::globalConfig.getSettings(settings); + if (settings.contains(key)) + return nix_export_std_string(context, settings[key].value, value, n); + else { + return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); + } + } + NIXC_CATCH_ERRS +} + +nix_err nix_setting_set(nix_c_context *context, const char *key, + const char *value) { + if (context) + context->last_err_code = NIX_OK; + if (nix::globalConfig.set(key, value)) + return NIX_OK; + else { + return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); + } +} + +nix_err nix_libutil_init(nix_c_context *context) { + if (context) + context->last_err_code = NIX_OK; + try { + nix::initLibUtil(); + return NIX_OK; + } + NIXC_CATCH_ERRS +} + +const char *nix_err_msg(nix_c_context *context, + const nix_c_context *read_context, unsigned int *n) { + if (context) + context->last_err_code = NIX_OK; + if (read_context->last_err) { + if (n) + *n = read_context->last_err->size(); + return read_context->last_err->c_str(); + } + nix_set_err_msg(context, NIX_ERR_UNKNOWN, "No error message"); + return nullptr; +} + +nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context, + char *value, int n) { + if (context) + context->last_err_code = NIX_OK; + if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, + "Last error was not a nix error"); + } + return nix_export_std_string(context, read_context->name, value, n); +} + +nix_err nix_err_info_msg(nix_c_context *context, + const nix_c_context *read_context, char *value, + int n) { + if (context) + context->last_err_code = NIX_OK; + if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, + "Last error was not a nix error"); + } + return nix_export_std_string(context, read_context->info->msg.str(), value, + n); +} + +nix_err nix_err_code(nix_c_context *context, + const nix_c_context *read_context) { + if (context) + context->last_err_code = NIX_OK; + return read_context->last_err_code; +} + +// internal +nix_err nix_export_std_string(nix_c_context *context, + const std::string_view str, char *dest, + unsigned int n) { + size_t i = str.copy(dest, n - 1); + dest[i] = 0; + if (i == n - 1) { + return nix_set_err_msg(context, NIX_ERR_OVERFLOW, + "Provided buffer too short"); + } else + return NIX_OK; +} diff --git a/src/libutil/nix_api_util.h b/src/libutil/nix_api_util.h new file mode 100644 index 000000000..095564296 --- /dev/null +++ b/src/libutil/nix_api_util.h @@ -0,0 +1,223 @@ +#ifndef NIX_API_UTIL_H +#define NIX_API_UTIL_H + +/** @file + * @brief Main entry for the libutil C bindings + * + * Also contains error handling utilities + */ + +#ifdef __cplusplus +extern "C" { +#endif +// cffi start + +// Error codes +/** + * @brief Type for error codes in the NIX system + * + * This type can have one of several predefined constants: + * - NIX_OK: No error occurred (0) + * - NIX_ERR_UNKNOWN: An unknown error occurred (-1) + * - NIX_ERR_OVERFLOW: An overflow error occurred (-2) + * - NIX_ERR_KEY: A key error occurred (-3) + * - NIX_ERR_NIX_ERROR: A generic Nix error occurred (-4) + */ +typedef int nix_err; + +/** + * @brief No error occurred. + * + * This error code is returned when no error has occurred during the function + * execution. + */ +#define NIX_OK 0 + +/** + * @brief An unknown error occurred. + * + * This error code is returned when an unknown error occurred during the + * function execution. + */ +#define NIX_ERR_UNKNOWN -1 + +/** + * @brief An overflow error occurred. + * + * This error code is returned when an overflow error occurred during the + * function execution. + */ +#define NIX_ERR_OVERFLOW -2 + +/** + * @brief A key error occurred. + * + * This error code is returned when a key error occurred during the function + * execution. + */ +#define NIX_ERR_KEY -3 + +/** + * @brief A generic Nix error occurred. + * + * This error code is returned when a generic Nix error occurred during the + * function execution. + */ +#define NIX_ERR_NIX_ERROR -4 + +/** + * @brief This object stores error state. + * + * Passed as a first parameter to C functions that can fail, will store error + * information. Optional wherever it is used, passing NULL will throw a C++ + * exception instead. The first field is a nix_err, that can be read directly to + * check for errors. + * @note These can be reused between different function calls, + * but make sure not to use them for multiple calls simultaneously (which can + * happen in callbacks). + */ +typedef struct nix_c_context nix_c_context; + +// Function prototypes + +/** + * @brief Allocate a new nix_c_context. + * @throws std::bad_alloc + * @return allocated nix_c_context, owned by the caller. Free using + * `nix_c_context_free`. + */ +nix_c_context *nix_c_context_create(); +/** + * @brief Free a nix_c_context. Does not fail. + * @param[out] context The context to free, mandatory. + */ +void nix_c_context_free(nix_c_context *context); + +/** + * @brief Initializes nix_libutil and its dependencies. + * + * This function can be called multiple times, but should be called at least + * once prior to any other nix function. + * + * @param[out] context Optional, stores error information + * @return NIX_OK if the initialization is successful, or an error code + * otherwise. + */ +nix_err nix_libutil_init(nix_c_context *context); + +/** + * @brief Retrieves a setting from the nix global configuration. + * + * This function requires nix_libutil_init() to be called at least once prior to + * its use. + * + * @param[out] context optional, Stores error information + * @param[in] key The key of the setting to retrieve. + * @param[out] value A pointer to a buffer where the value of the setting will + * be stored. + * @param[in] n The size of the buffer pointed to by value. + * @return NIX_ERR_KEY if the setting is unknown, NIX_ERR_OVERFLOW if the + * provided buffer is too short, or NIX_OK if the setting was retrieved + * successfully. + */ +nix_err nix_setting_get(nix_c_context *context, const char *key, char *value, + int n); + +/** + * @brief Sets a setting in the nix global configuration. + * + * Use "extra-" to append to the setting's value. + * + * Settings only apply for new States. Call nix_plugins_init() when you are done + * with the settings to load any plugins. + * + * @param[out] context optional, Stores error information + * @param[in] key The key of the setting to set. + * @param[in] value The value to set for the setting. + * @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was + * set successfully. + */ +nix_err nix_setting_set(nix_c_context *context, const char *key, + const char *value); + +// todo: nix_plugins_init() + +/** + * @brief Retrieves the nix library version. + * + * Does not fail. + * @return A static string representing the version of the nix library. + */ +const char *nix_version_get(); + +/** + * @brief Retrieves the most recent error message from a context. + * + * @pre This function should only be called after a previous nix function has + * returned an error. + * + * @param[out] context optional, the context to store errors in if this function + * fails + * @param[in] ctx the context to retrieve the error message from + * @param[out] n optional: a pointer to an unsigned int that is set to the + * length of the error. + * @return nullptr if no error message was ever set, + * a borrowed pointer to the error message otherwise. + */ +const char *nix_err_msg(nix_c_context *context, const nix_c_context *ctx, + unsigned int *n); + +/** + * @brief Retrieves the error message from errorInfo in a context. + * + * Used to inspect nix Error messages. + * + * @pre This function should only be called after a previous nix function has + * returned a NIX_ERR_NIX_ERROR + * + * @param[out] context optional, the context to store errors in if this function + * fails + * @param[in] read_context the context to retrieve the error message from + * @param[out] value The allocated area to write the error string to. + * @param[in] n Maximum size of the returned string. + * @return NIX_OK if there were no errors, an error code otherwise. + */ +nix_err nix_err_info_msg(nix_c_context *context, + const nix_c_context *read_context, char *value, int n); + +/** + * @brief Retrieves the error name from a context. + * + * Used to inspect nix Error messages. + * + * @pre This function should only be called after a previous nix function has + * returned a NIX_ERR_NIX_ERROR + * + * @param context optional, the context to store errors in if this function + * fails + * @param[in] read_context the context to retrieve the error message from + * @param[out] value The allocated area to write the error string to. + * @param[in] n Maximum size of the returned string. + * @return NIX_OK if there were no errors, an error code otherwise. + */ +nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context, + char *value, int n); + +/** + * @brief Retrieves the most recent error code from a nix_c_context + * + * Equivalent to reading the first field of the context. + * + * @param[out] context optional, the context to store errors in if this function + * fails + * @param[in] read_context the context to retrieve the error message from + * @return most recent error code stored in the context. + */ +nix_err nix_err_code(nix_c_context *context, const nix_c_context *read_context); + +// cffi end +#ifdef __cplusplus +} +#endif + +#endif // NIX_API_UTIL_H diff --git a/src/libutil/nix_api_util_internal.h b/src/libutil/nix_api_util_internal.h new file mode 100644 index 000000000..9ece28588 --- /dev/null +++ b/src/libutil/nix_api_util_internal.h @@ -0,0 +1,61 @@ +#ifndef NIX_API_UTIL_INTERNAL_H +#define NIX_API_UTIL_INTERNAL_H + +#include + +// forward declaration +namespace nix { +class Error; +}; + +struct nix_c_context { + nix_err last_err_code = NIX_OK; + std::optional last_err = {}; + std::optional info = {}; + std::string name = ""; +}; + +nix_err nix_context_error(nix_c_context *context); + +/** + * Internal use only. + * + * Sets the most recent error message. + * + * @param context context to write the error message to, or NULL + * @param err The error code to set and return + * @param msg The error message to set. + * @returns the error code set + */ +nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg); + +/** + * Internal use only. + * + * Export a std::string across the C api boundary + * @param context optional, the context to store errors in if this function + * fails + * @param str The string to export + * @param value The allocated area to write the string to. + * @param n Maximum size of the returned string. + * @return NIX_OK if there were no errors, NIX_ERR_OVERFLOW if the string length + * exceeds `n`. + */ +nix_err nix_export_std_string(nix_c_context *context, + const std::string_view str, char *dest, + unsigned int n); + +#define NIXC_CATCH_ERRS \ + catch (...) { \ + return nix_context_error(context); \ + } \ + return NIX_OK; + +#define NIXC_CATCH_ERRS_RES(def) \ + catch (...) { \ + nix_context_error(context); \ + return def; \ + } +#define NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_RES(nullptr) + +#endif // NIX_API_UTIL_INTERNAL_H From 1d41600498cfa1c19d7bb0ef8b6f6e0cfb232d60 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 14 Jul 2023 15:53:01 +0200 Subject: [PATCH 070/168] libstore: add C bindings --- src/libstore/globals.cc | 1 + src/libstore/nix_api_store.cc | 117 ++++++++++++++++++++++++ src/libstore/nix_api_store.h | 122 ++++++++++++++++++++++++++ src/libstore/nix_api_store_internal.h | 8 ++ 4 files changed, 248 insertions(+) create mode 100644 src/libstore/nix_api_store.cc create mode 100644 src/libstore/nix_api_store.h create mode 100644 src/libstore/nix_api_store_internal.h diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index fa0938d7b..b9ad8ac18 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -404,6 +404,7 @@ void assertLibStoreInitialized() { } void initLibStore() { + if (initLibStoreDone) return; initLibUtil(); diff --git a/src/libstore/nix_api_store.cc b/src/libstore/nix_api_store.cc new file mode 100644 index 000000000..312e5f2a8 --- /dev/null +++ b/src/libstore/nix_api_store.cc @@ -0,0 +1,117 @@ +#include "nix_api_store.h" +#include "nix_api_store_internal.h" +#include "nix_api_util.h" +#include "nix_api_util_internal.h" + +#include "store-api.hh" + +#include "globals.hh" + +struct StorePath { + nix::StorePath path; +}; + +nix_err nix_libstore_init(nix_c_context *context) { + if (context) + context->last_err_code = NIX_OK; + try { + nix::initLibStore(); + } + NIXC_CATCH_ERRS +} + +Store *nix_store_open(nix_c_context *context, const char *uri, + const char ***params) { + if (context) + context->last_err_code = NIX_OK; + try { + if (!uri) { + return new Store{nix::openStore()}; + } else { + std::string uri_str = uri; + if (!params) + return new Store{nix::openStore(uri_str)}; + + nix::Store::Params params_map; + for (size_t i = 0; params[i] != nullptr; i++) { + params_map[params[i][0]] = params[i][1]; + } + return new Store{nix::openStore(uri_str, params_map)}; + } + } + NIXC_CATCH_ERRS_NULL +} + +void nix_store_unref(Store *store) { delete store; } + +nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest, + unsigned int n) { + if (context) + context->last_err_code = NIX_OK; + try { + auto res = store->ptr->getUri(); + return nix_export_std_string(context, res, dest, n); + } + NIXC_CATCH_ERRS +} + +nix_err nix_store_get_version(nix_c_context *context, Store *store, char *dest, + unsigned int n) { + if (context) + context->last_err_code = NIX_OK; + try { + auto res = store->ptr->getVersion(); + if (res) { + return nix_export_std_string(context, *res, dest, n); + } else { + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, + "store does not have a version"); + } + } + NIXC_CATCH_ERRS +} + +bool nix_store_is_valid_path(nix_c_context *context, Store *store, + StorePath *path) { + if (context) + context->last_err_code = NIX_OK; + try { + return store->ptr->isValidPath(path->path); + } + NIXC_CATCH_ERRS_RES(false); +} + +StorePath *nix_store_parse_path(nix_c_context *context, Store *store, + const char *path) { + if (context) + context->last_err_code = NIX_OK; + try { + nix::StorePath s = store->ptr->parseStorePath(path); + return new StorePath{std::move(s)}; + } + NIXC_CATCH_ERRS_NULL +} + +nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, + void (*iter)(const char *, const char *)) { + if (context) + context->last_err_code = NIX_OK; + try { + store->ptr->buildPaths({ + nix::DerivedPath::Built{ + .drvPath = path->path, + .outputs = nix::OutputsSpec::All{}, + }, + }); + if (iter) { + for (auto &[outputName, outputPath] : + store->ptr->queryDerivationOutputMap(path->path)) { + auto op = store->ptr->printStorePath(outputPath); + iter(outputName.c_str(), op.c_str()); + } + } + } + NIXC_CATCH_ERRS +} + +void nix_store_path_free(StorePath *sp) { delete sp; } diff --git a/src/libstore/nix_api_store.h b/src/libstore/nix_api_store.h new file mode 100644 index 000000000..1ab7a4eb7 --- /dev/null +++ b/src/libstore/nix_api_store.h @@ -0,0 +1,122 @@ +#ifndef NIX_API_STORE_H +#define NIX_API_STORE_H +/** @file + * @brief Main entry for the libexpr C bindings + */ + +#include "nix_api_util.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif +// cffi start + +/** @brief reference to a nix store */ +typedef struct Store Store; +/** @brief nix store path */ +typedef struct StorePath StorePath; + +/** + * @brief Initializes the Nix store library + * + * This function should be called before creating a Store + * This function can be called multiple times. + * + * @param[out] context Optional, stores error information + * @return NIX_OK if the initialization was successful, an error code otherwise. + */ +nix_err nix_libstore_init(nix_c_context *context); + +/** + * @brief Open a nix store + * @param[out] context Optional, stores error information + * @param[in] uri URI of the nix store, copied + * @param[in] params optional, array of key-value pairs, {{"endpoint", + * "https://s3.local"}} + * @return ref-counted Store pointer, NULL in case of errors + * @see nix_store_unref + */ +Store *nix_store_open(nix_c_context *, const char *uri, const char ***params); + +/** + * @brief Unref a nix store + * + * Does not fail. + * It'll be closed and deallocated when all references are gone. + * @param[in] builder the store to unref + */ +void nix_store_unref(Store *store); + +/** + * @brief get the URI of a nix store + * @param[out] context Optional, stores error information + * @param[in] store nix store reference + * @param[out] dest The allocated area to write the string to. + * @param[in] n Maximum size of the returned string. + * @return error code, NIX_OK on success. + */ +nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest, + unsigned int n); + +// returns: owned StorePath* +/** + * @brief parse a nix store path into a StorePath + * + * Don't forget to free this path using nix_store_path_free + * @param[out] context Optional, stores error information + * @param[in] store nix store reference + * @param[in] path Path string to parse, copied + * @return owned store path, NULL on error + */ +StorePath *nix_store_parse_path(nix_c_context *context, Store *store, + const char *path); + +/** @brief Deallocate a nix StorePath + * + * Does not fail. + * @param[in] p the path to free + */ +void nix_store_path_free(StorePath *p); + +/** + * @brief check if a storepath is valid (exists in the store) + * @param[out] context Optional, stores error information + * @param[in] store nix store reference + * @param[in] path Path to check + * @return true or false, error info in context + */ +bool nix_store_is_valid_path(nix_c_context *context, Store *store, + StorePath *path); +// nix_err nix_store_ensure(Store*, const char*); +// nix_err nix_store_build_paths(Store*); +/** + * @brief Build a nix store path + * + * Blocking, calls cb once for each built output + * + * @param[out] context Optional, stores error information + * @param[in] store nix store reference + * @param[in] path Path to build + * @param[in] cb called for every built output + */ +nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, + void (*cb)(const char *outname, const char *out)); + +/** + * @brief get the version of a nix store + * @param[out] context Optional, stores error information + * @param[in] store nix store reference + * @param[out] dest The allocated area to write the string to. + * @param[in] n Maximum size of the returned string. + * @return error code, NIX_OK on success. + */ +nix_err nix_store_get_version(nix_c_context *, Store *store, char *dest, + unsigned int n); + +// cffi end +#ifdef __cplusplus +} +#endif + +#endif // NIX_API_STORE_H diff --git a/src/libstore/nix_api_store_internal.h b/src/libstore/nix_api_store_internal.h new file mode 100644 index 000000000..59524ea8e --- /dev/null +++ b/src/libstore/nix_api_store_internal.h @@ -0,0 +1,8 @@ +#ifndef NIX_API_STORE_INTERNAL_H +#define NIX_API_STORE_INTERNAL_H +#include "store-api.hh" + +struct Store { + nix::ref ptr; +}; +#endif From e76652a5d36d407397970cd1b737a91a10bde1af Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 14 Jul 2023 15:53:30 +0200 Subject: [PATCH 071/168] libexpr: add C bindings --- src/libexpr/eval.cc | 4 + src/libexpr/nix_api_expr.cc | 141 +++++++++ src/libexpr/nix_api_expr.h | 176 +++++++++++ src/libexpr/nix_api_expr_internal.h | 17 ++ src/libexpr/nix_api_external.cc | 198 +++++++++++++ src/libexpr/nix_api_external.h | 195 ++++++++++++ src/libexpr/nix_api_value.cc | 439 ++++++++++++++++++++++++++++ src/libexpr/nix_api_value.h | 355 ++++++++++++++++++++++ src/libexpr/search-path.cc | 2 +- src/libexpr/value.hh | 1 + 10 files changed, 1527 insertions(+), 1 deletion(-) create mode 100644 src/libexpr/nix_api_expr.cc create mode 100644 src/libexpr/nix_api_expr.h create mode 100644 src/libexpr/nix_api_expr_internal.h create mode 100644 src/libexpr/nix_api_external.cc create mode 100644 src/libexpr/nix_api_external.h create mode 100644 src/libexpr/nix_api_value.cc create mode 100644 src/libexpr/nix_api_value.h diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 5e2f71649..a93e531b6 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -897,6 +897,10 @@ void Value::mkStringMove(const char * s, const NixStringContext & context) copyContextToValue(*this, context); } +void Value::mkPath(std::string_view path) +{ + mkPath(makeImmutableString(path)); +} void Value::mkPath(const SourcePath & path) { diff --git a/src/libexpr/nix_api_expr.cc b/src/libexpr/nix_api_expr.cc new file mode 100644 index 000000000..df8a66053 --- /dev/null +++ b/src/libexpr/nix_api_expr.cc @@ -0,0 +1,141 @@ +#include +#include +#include +#include + +#include "config.hh" +#include "eval.hh" +#include "globals.hh" +#include "util.hh" + +#include "nix_api_expr.h" +#include "nix_api_expr_internal.h" +#include "nix_api_store.h" +#include "nix_api_store_internal.h" +#include "nix_api_util.h" +#include "nix_api_util_internal.h" + +#ifdef HAVE_BOEHMGC +#define GC_INCLUDE_NEW 1 +#include "gc_cpp.h" +#endif + +nix_err nix_libexpr_init(nix_c_context *context) { + if (context) + context->last_err_code = NIX_OK; + { + auto ret = nix_libutil_init(context); + if (ret != NIX_OK) + return ret; + } + { + auto ret = nix_libstore_init(context); + if (ret != NIX_OK) + return ret; + } + try { + nix::initGC(); + } + NIXC_CATCH_ERRS +} + +Expr *nix_parse_expr_from_string(nix_c_context *context, State *state, + const char *expr, const char *path, + GCRef *ref) { + if (context) + context->last_err_code = NIX_OK; + try { + Expr *result = state->state.parseExprFromString( + expr, state->state.rootPath(nix::CanonPath(path))); + if (ref) + ref->ptr = result; + return result; + } + NIXC_CATCH_ERRS_NULL +} + +nix_err nix_expr_eval(nix_c_context *context, State *state, Expr *expr, + Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + state->state.eval((nix::Expr *)expr, *(nix::Value *)value); + } + NIXC_CATCH_ERRS +} + +nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, + Value *arg, Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + state->state.callFunction(*(nix::Value *)fn, *(nix::Value *)arg, + *(nix::Value *)value, nix::noPos); + } + NIXC_CATCH_ERRS +} + +nix_err nix_value_force(nix_c_context *context, State *state, Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + state->state.forceValue(*(nix::Value *)value, nix::noPos); + } + NIXC_CATCH_ERRS +} + +nix_err nix_value_force_deep(nix_c_context *context, State *state, + Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + state->state.forceValueDeep(*(nix::Value *)value); + } + NIXC_CATCH_ERRS +} + +State *nix_state_create(nix_c_context *context, const char **searchPath_c, + Store *store) { + if (context) + context->last_err_code = NIX_OK; + try { + nix::Strings searchPath; + if (searchPath_c != nullptr) + for (size_t i = 0; searchPath_c[i] != nullptr; i++) + searchPath.push_back(searchPath_c[i]); + + return new State{ + nix::EvalState(nix::SearchPath::parse(searchPath), store->ptr)}; + } + NIXC_CATCH_ERRS_NULL +} + +void nix_state_free(State *state) { delete state; } + +GCRef *nix_gc_ref(nix_c_context *context, void *obj) { + if (context) + context->last_err_code = NIX_OK; + try { +#if HAVE_BOEHMGC + return new (NoGC) GCRef{obj}; +#else + return new GCRef{obj}; +#endif + } + NIXC_CATCH_ERRS_NULL +} + +void nix_gc_free(GCRef *ref) { +#if HAVE_BOEHMGC + GC_FREE(ref); +#else + delete ref; +#endif +} + +void nix_gc_register_finalizer(void *obj, void *cd, + void (*finalizer)(void *obj, void *cd)) { +#ifdef HAVE_BOEHMGC + GC_REGISTER_FINALIZER(obj, finalizer, cd, 0, 0); +#endif +} diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h new file mode 100644 index 000000000..90aec8d18 --- /dev/null +++ b/src/libexpr/nix_api_expr.h @@ -0,0 +1,176 @@ +#ifndef NIX_API_EXPR_H +#define NIX_API_EXPR_H +/** @file + * @brief Main entry for the libexpr C bindings + */ + +#include "nix_api_store.h" +#include "nix_api_util.h" + +#ifdef __cplusplus +extern "C" { +#endif +// cffi start + +// Type definitions +/** + * @brief Represents a parsed nix Expression, can be evaluated into a Value. + * + * Owned by the GC. + */ +typedef void Expr; // nix::Expr +/** + * @brief Represents a nix evaluator state. + * + * Multiple can be created for multi-threaded + * operation. + */ +typedef struct State State; // nix::EvalState +/** + * @brief Represents a nix value. + * + * Owned by the GC. + */ +typedef void Value; // nix::Value +/** + * @brief Reference for the GC + * + * Nix uses a garbage collector that may not be able to see into + * your stack and heap. Keep GCRef objects around for every + * garbage-collected object that you want to keep alive. + */ +typedef struct GCRef GCRef; // void* + +// Function propotypes +/** + * @brief Initializes the Nix expression evaluator. + * + * This function should be called before creating a State. + * This function can be called multiple times. + * + * @param[out] context Optional, stores error information + * @return NIX_OK if the initialization was successful, an error code otherwise. + */ +nix_err nix_libexpr_init(nix_c_context *context); + +/** + * @brief Parses a Nix expression from a string. + * + * The returned expression is owned by the garbage collector. + * Pass a gcref to keep a reference. + * + * @param[out] context Optional, stores error information + * @param[in] state Evaluator state. + * @param[in] expr The Nix expression to parse. + * @param[in] path The file path to associate with the expression. + * @param[out] ref Optional, will store a reference to the returned value. + * @return A parsed expression or NULL on failure. + */ +Expr *nix_parse_expr_from_string(nix_c_context *context, State *state, + const char *expr, const char *path, + GCRef *ref); + +/** + * @brief Evaluates a parsed Nix expression. + * + * @param[out] context Optional, stores error information + * @param[in] state The state of the evaluation. + * @param[in] expr The Nix expression to evaluate. + * @param[in] value The result of the evaluation. + * @return NIX_OK if the evaluation was successful, an error code otherwise. + */ +nix_err nix_expr_eval(nix_c_context *context, State *state, Expr *expr, + Value *value); + +/** + * @brief Calls a Nix function with an argument. + * + * @param[out] context Optional, stores error information + * @param[in] state The state of the evaluation. + * @param[in] fn The Nix function to call. + * @param[in] arg The argument to pass to the function. + * @param[out] value The result of the function call. + * @return NIX_OK if the function call was successful, an error code otherwise. + */ +nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, + Value *arg, Value *value); + +/** + * @brief Forces the evaluation of a Nix value. + * + * @param[out] context Optional, stores error information + * @param[in] state The state of the evaluation. + * @param[in,out] value The Nix value to force. + * @return NIX_OK if the force operation was successful, an error code + * otherwise. + */ +nix_err nix_value_force(nix_c_context *context, State *state, Value *value); + +/** + * @brief Forces the deep evaluation of a Nix value. + * + * @param[out] context Optional, stores error information + * @param[in] state The state of the evaluation. + * @param[in,out] value The Nix value to force. + * @return NIX_OK if the deep force operation was successful, an error code + * otherwise. + */ +nix_err nix_value_force_deep(nix_c_context *context, State *state, + Value *value); + +/** + * @brief Creates a new Nix state. + * + * @param[out] context Optional, stores error information + * @param[in] searchPath The NIX_PATH. + * @param[in] store The Nix store to use. + * @return A new Nix state or NULL on failure. + */ +State *nix_state_create(nix_c_context *context, const char **searchPath, + Store *store); + +/** + * @brief Frees a Nix state. + * + * Does not fail. + * + * @param[in] state The state to free. + */ +void nix_state_free(State *state); + +/** + * @brief Creates a new garbage collector reference. + * + * @param[out] context Optional, stores error information + * @param[in] obj The object to create a reference for. + * @return A new garbage collector reference or NULL on failure. + */ +GCRef *nix_gc_ref(nix_c_context *context, void *obj); + +/** + * @brief Frees a garbage collector reference. + * + * Does not fail. + * + * @param[in] ref The reference to free. + */ +void nix_gc_free(GCRef *ref); + +/** + * @brief Register a callback that gets called when the object is garbage + * collected. + * @note objects can only have a single finalizer. This function overwrites + * silently. + * @param[in] obj the object to watch + * @param[in] cd the data to pass to the finalizer + * @param[in] finalizer the callback function, called with obj and cd + */ +void nix_gc_register_finalizer(void *obj, void *cd, + void (*finalizer)(void *obj, void *cd)); + +// cffi end +#ifdef __cplusplus +} +#endif + +#endif // NIX_API_EXPR_H diff --git a/src/libexpr/nix_api_expr_internal.h b/src/libexpr/nix_api_expr_internal.h new file mode 100644 index 000000000..424ca2874 --- /dev/null +++ b/src/libexpr/nix_api_expr_internal.h @@ -0,0 +1,17 @@ +#ifndef NIX_API_EXPR_INTERNAL_H +#define NIX_API_EXPR_INTERNAL_H + +// forward declaration +namespace nix { +class EvalState; +}; + +struct State { + nix::EvalState state; +}; + +struct GCRef { + void *ptr; +}; + +#endif // NIX_API_EXPR_INTERNAL_H diff --git a/src/libexpr/nix_api_external.cc b/src/libexpr/nix_api_external.cc new file mode 100644 index 000000000..971a175fb --- /dev/null +++ b/src/libexpr/nix_api_external.cc @@ -0,0 +1,198 @@ +#include "attr-set.hh" +#include "config.hh" +#include "eval.hh" +#include "gc/gc.h" +#include "globals.hh" +#include "value.hh" + +#include "nix_api_expr.h" +#include "nix_api_expr_internal.h" +#include "nix_api_external.h" +#include "nix_api_util.h" +#include "nix_api_util_internal.h" +#include "nix_api_value.h" +#include "value/context.hh" + +#include + +#ifdef HAVE_BOEHMGC +#define GC_INCLUDE_NEW 1 +#include "gc_cpp.h" +#endif + +struct nix_returned_string { + std::string str; +}; + +struct nix_printer { + std::ostream &s; +}; + +struct nix_string_context { + nix::NixStringContext &ctx; +}; + +nix_returned_string *nix_external_alloc_string(const char *c) { + return new nix_returned_string{c}; +} +void nix_external_dealloc_string(nix_returned_string *str) { delete str; } + +nix_err nix_external_print(nix_c_context *context, nix_printer *printer, + const char *c) { + if (context) + context->last_err_code = NIX_OK; + try { + printer->s << c; + } + NIXC_CATCH_ERRS +} + +nix_err nix_external_add_string_context(nix_c_context *context, + nix_string_context *ctx, + const char *c) { + if (context) + context->last_err_code = NIX_OK; + try { + auto r = nix::NixStringContextElem::parse(c); + ctx->ctx.insert(r); + } + NIXC_CATCH_ERRS +} + +class NixCExternalValue : public nix::ExternalValueBase { + NixCExternalValueDesc &desc; + void *v; + +public: + NixCExternalValue(NixCExternalValueDesc &desc, void *v) : desc(desc), v(v){}; + void *get_ptr() { return v; } + /** + * Print out the value + */ + virtual std::ostream &print(std::ostream &str) const override { + nix_printer p{str}; + desc.print(v, &p); + return str; + } + + /** + * Return a simple string describing the type + */ + virtual std::string showType() const override { + std::unique_ptr r(desc.showType(v)); + return std::move(r->str); + } + + /** + * Return a string to be used in builtins.typeOf + */ + virtual std::string typeOf() const override { + std::unique_ptr r(desc.typeOf(v)); + return std::move(r->str); + } + + /** + * Coerce the value to a string. + */ + virtual std::string coerceToString(const nix::Pos &pos, + nix::NixStringContext &context, + bool copyMore, + bool copyToStore) const override { + if (!desc.coerceToString) { + return nix::ExternalValueBase::coerceToString(pos, context, copyMore, + copyToStore); + } + nix_string_context ctx{context}; + // todo: pos, errors + std::unique_ptr r( + desc.coerceToString(v, &ctx, copyMore, copyToStore)); + if (!r) { + return nix::ExternalValueBase::coerceToString(pos, context, copyMore, + copyToStore); + } + return std::move(r->str); + } + + /** + * Compare to another value of the same type. + */ + virtual bool operator==(const ExternalValueBase &b) const override { + if (!desc.equal) { + return false; + } + auto r = dynamic_cast(&b); + if (!r) + return false; + return desc.equal(v, r->v); + } + + /** + * Print the value as JSON. + */ + virtual nlohmann::json + printValueAsJSON(nix::EvalState &state, bool strict, + nix::NixStringContext &context, + bool copyToStore = true) const override { + if (!desc.printValueAsJSON) { + return nix::ExternalValueBase::printValueAsJSON(state, strict, context, + copyToStore); + } + nix_string_context ctx{context}; + std::unique_ptr r( + desc.printValueAsJSON((State *)&state, strict, &ctx, copyToStore)); + if (!r) { + return nix::ExternalValueBase::printValueAsJSON(state, strict, context, + copyToStore); + } + return nlohmann::json::parse(r->str); + } + + /** + * Print the value as XML. + */ + virtual void printValueAsXML(nix::EvalState &state, bool strict, + bool location, nix::XMLWriter &doc, + nix::NixStringContext &context, + nix::PathSet &drvsSeen, + const nix::PosIdx pos) const override { + if (!desc.printValueAsXML) { + return nix::ExternalValueBase::printValueAsXML( + state, strict, location, doc, context, drvsSeen, pos); + } + nix_string_context ctx{context}; + desc.printValueAsXML((State *)&state, strict, location, &doc, &ctx, + &drvsSeen, *reinterpret_cast(&pos)); + } + + virtual ~NixCExternalValue() override{}; +}; + +ExternalValue *nix_create_external_value(nix_c_context *context, + NixCExternalValueDesc *desc, void *v, + GCRef *gc) { + if (context) + context->last_err_code = NIX_OK; + try { + auto ret = new +#ifdef HAVE_BOEHMGC + (GC) +#endif + NixCExternalValue(*desc, v); + if (gc) + gc->ptr = ret; + return (ExternalValue *)ret; + } + NIXC_CATCH_ERRS_NULL +} + +void *nix_get_external_value_content(nix_c_context *context, ExternalValue *b) { + if (context) + context->last_err_code = NIX_OK; + try { + auto r = dynamic_cast((nix::ExternalValueBase *)b); + if (r) + return r->get_ptr(); + return nullptr; + } + NIXC_CATCH_ERRS_NULL +} diff --git a/src/libexpr/nix_api_external.h b/src/libexpr/nix_api_external.h new file mode 100644 index 000000000..2bb53e349 --- /dev/null +++ b/src/libexpr/nix_api_external.h @@ -0,0 +1,195 @@ +#ifndef NIX_API_EXTERNAL_H +#define NIX_API_EXTERNAL_H +/** @file + * @brief libexpr C bindings dealing with external values + */ + +#include "nix_api_expr.h" +#include "nix_api_util.h" +#include "nix_api_value.h" +#include "stdbool.h" +#include "stddef.h" +#include "stdint.h" + +#ifdef __cplusplus +extern "C" { +#endif +// cffi start + +/** + * @brief Represents a string meant for consumption by nix. + */ +typedef struct nix_returned_string nix_returned_string; +/** + * @brief Wraps a stream that can output multiple string pieces. + */ +typedef struct nix_printer nix_printer; +/** + * @brief A list of string context items + */ +typedef struct nix_string_context nix_string_context; + +/** + * @brief Allocate a nix_returned_string from a const char*. + * + * Copies the passed string. + * @param[in] c The string to copy + * @returns A nix_returned_string* + */ +nix_returned_string *nix_external_alloc_string(const char *c); + +/** + * @brief Deallocate a nix_returned_string + * + * There's generally no need to call this, since + * returning the string will pass ownership to nix, + * but you can use it in case of errors. + * @param[in] str The string to deallocate + */ +void nix_external_dealloc_string(nix_returned_string *str); + +/** + * Print to the nix_printer + * + * @param[out] context Optional, stores error information + * @param printer The nix_printer to print to + * @param[in] str The string to print + * @returns NIX_OK if everything worked + */ +nix_err nix_external_print(nix_c_context *context, nix_printer *printer, + const char *str); + +/** + * Add string context to the nix_string_context object + * @param[out] context Optional, stores error information + * @param[out] string_context The nix_string_context to add to + * @param[in] c The context string to add + * @returns NIX_OK if everything worked + */ +nix_err nix_external_add_string_context(nix_c_context *context, + nix_string_context *string_context, + const char *c); + +/** + * @brief Definition for a class of external values + * + * Create and implement one of these, then pass it to nix_create_external_value + * Make sure to keep it alive while the external value lives. + * + * Optional functions can be set to NULL + * + * @see nix_create_external_value + */ +typedef struct NixCExternalValueDesc { + /** + * @brief Called when printing the external value + * + * @param[in] self the void* passed to nix_create_external_value + * @param[out] printer The printer to print to, pass to nix_external_print + */ + void (*print)(void *self, nix_printer *printer); + /** + * @brief Called on :t + * @param[in] self the void* passed to nix_create_external_value + * @returns a nix_returned_string, ownership passed to nix + */ + nix_returned_string *(*showType)(void *self); // std::string + /** + * @brief Called on `builtins.typeOf` + * @param self the void* passed to nix_create_external_value + * @returns a nix_returned_string, ownership passed to nix + */ + nix_returned_string *(*typeOf)(void *self); // std::string + /** + * @brief Called on "${str}" and builtins.toString. + * + * The latter with coerceMore=true + * Optional, the default is to throw an error. + * @param[in] self the void* passed to nix_create_external_value + * @param[out] c writable string context for the resulting string + * @param[in] coerceMore boolean, try to coerce to strings in more cases + * instead of throwing an error + * @param[in] copyToStore boolean, whether to copy referenced paths to store + * or keep them as-is + * @returns a nix_returned_string, ownership passed to nix. Optional, + * returning NULL will make the conversion throw an error. + */ + nix_returned_string *(*coerceToString)(void *self, nix_string_context *c, + int coerceMore, int copyToStore); + /** + * @brief Try to compare two external values + * + * Optional, the default is always false. + * If the other object was not a Nix C external value, this comparison will + * also return false + * @param[in] self the void* passed to nix_create_external_value + * @param[in] other the void* passed to the other object's + * nix_create_external_value + * @returns true if the objects are deemed to be equal + */ + int (*equal)(void *self, void *other); + /** + * @brief Convert the external value to json + * + * Optional, the default is to throw an error + * @param[in] state The evaluator state + * @param[in] strict boolean Whether to force the value before printing + * @param[out] c writable string context for the resulting string + * @param[in] copyToStore whether to copy referenced paths to store or keep + * them as-is + * @returns string that gets parsed as json. Optional, returning NULL will + * make the conversion throw an error. + */ + nix_returned_string *(*printValueAsJSON)(State *, int strict, + nix_string_context *c, + bool copyToStore); + /** + * @brief Convert the external value to XML + * + * Optional, the default is to throw an error + * @todo The mechanisms for this call are incomplete. There are no C + * bindings to work with XML, pathsets and positions. + * @param[in] state The evaluator state + * @param[in] strict boolean Whether to force the value before printing + * @param[in] location boolean Whether to include position information in the + * xml + * @param[out] doc XML document to output to + * @param[out] c writable string context for the resulting string + * @param[in,out] drvsSeen a path set to avoid duplicating derivations + * @param[in] pos The position of the call. + */ + void (*printValueAsXML)(State *, int strict, int location, void *doc, + nix_string_context *c, void *drvsSeen, int pos); +} NixCExternalValueDesc; + +/** + * @brief Create an external value, that can be given to nix_set_external + * + * Pass a gcref to keep a reference. + * @param[out] context Optional, stores error information + * @param[in] desc a NixCExternalValueDesc, you should keep this alive as long + * as the ExternalValue lives + * @param[in] v the value to store + * @param[out] ref Optional, will store a reference to the returned value. + * @returns external value, owned by the garbage collector + * @see nix_set_external + */ +ExternalValue *nix_create_external_value(nix_c_context *context, + NixCExternalValueDesc *desc, void *v, + GCRef *ref); + +/** + * @brief Extract the pointer from a nix c external value. + * @param[out] context Optional, stores error information + * @param[in] b The external value + * @returns The pointer, or null if the external value was not from nix c. + * @see nix_get_external + */ +void *nix_get_external_value_content(nix_c_context *context, ExternalValue *b); + +// cffi end +#ifdef __cplusplus +} +#endif + +#endif // NIX_API_EXTERNAL_H diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc new file mode 100644 index 000000000..f58500367 --- /dev/null +++ b/src/libexpr/nix_api_value.cc @@ -0,0 +1,439 @@ +#include "attr-set.hh" +#include "config.hh" +#include "eval.hh" +#include "gc/gc.h" +#include "globals.hh" +#include "value.hh" + +#include "nix_api_expr.h" +#include "nix_api_expr_internal.h" +#include "nix_api_util.h" +#include "nix_api_util_internal.h" +#include "nix_api_value.h" + +#ifdef HAVE_BOEHMGC +#define GC_INCLUDE_NEW 1 +#include "gc_cpp.h" +#endif + +// Helper function to throw an exception if value is null +static const nix::Value &check_value_not_null(const Value *value) { + if (!value) { + throw std::runtime_error("Value is null"); + } + return *((const nix::Value *)value); +} + +static nix::Value &check_value_not_null(Value *value) { + if (!value) { + throw std::runtime_error("Value is null"); + } + return *((nix::Value *)value); +} + +PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, + const char *name, const char **args, const char *doc, + GCRef *ref) { + if (context) + context->last_err_code = NIX_OK; + try { + auto fun2 = (nix::PrimOpFun)fun; + auto p = new +#ifdef HAVE_BOEHMGC + (GC) +#endif + nix::PrimOp{.name = name, .args = {}, .doc = doc, .fun = fun2}; + if (args) + for (size_t i = 0; args[i]; i++) + p->args.emplace_back(*args); + if (ref) + ref->ptr = p; + return (PrimOp *)p; + } + NIXC_CATCH_ERRS_NULL +} + +Value *nix_alloc_value(nix_c_context *context, State *state, GCRef *ref) { + if (context) + context->last_err_code = NIX_OK; + try { + Value *res = state->state.allocValue(); + if (ref) + ref->ptr = res; + return res; + } + NIXC_CATCH_ERRS_NULL +} + +ValueType nix_get_type(nix_c_context *context, const Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + using namespace nix; + switch (v.type()) { + case nThunk: + return NIX_TYPE_THUNK; + case nInt: + return NIX_TYPE_INT; + case nFloat: + return NIX_TYPE_FLOAT; + case nBool: + return NIX_TYPE_BOOL; + case nString: + return NIX_TYPE_STRING; + case nPath: + return NIX_TYPE_PATH; + case nNull: + return NIX_TYPE_NULL; + case nAttrs: + return NIX_TYPE_ATTRS; + case nList: + return NIX_TYPE_LIST; + case nFunction: + return NIX_TYPE_FUNCTION; + case nExternal: + return NIX_TYPE_EXTERNAL; + } + return NIX_TYPE_NULL; + } + NIXC_CATCH_ERRS_RES(NIX_TYPE_NULL); +} + +const char *nix_get_typename(nix_c_context *context, const Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + auto s = nix::showType(v); + return strdup(s.c_str()); + } + NIXC_CATCH_ERRS_NULL +} + +bool nix_get_bool(nix_c_context *context, const Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nBool); + return v.boolean; + } + NIXC_CATCH_ERRS_RES(false); +} + +const char *nix_get_string(nix_c_context *context, const Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nString); + return v.string.s; + } + NIXC_CATCH_ERRS_NULL +} + +const char *nix_get_path_string(nix_c_context *context, const Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nPath); + return v._path; + } + NIXC_CATCH_ERRS_NULL +} + +unsigned int nix_get_list_size(nix_c_context *context, const Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nList); + return v.listSize(); + } + NIXC_CATCH_ERRS_RES(0); +} + +unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nAttrs); + return v.attrs->size(); + } + NIXC_CATCH_ERRS_RES(0); +} + +double nix_get_double(nix_c_context *context, const Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nFloat); + return v.fpoint; + } + NIXC_CATCH_ERRS_RES(NAN); +} + +int64_t nix_get_int(nix_c_context *context, const Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nInt); + return v.integer; + } + NIXC_CATCH_ERRS_RES(0); +} + +ExternalValue *nix_get_external(nix_c_context *context, Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nExternal); + return (ExternalValue *)v.external; + } + NIXC_CATCH_ERRS_NULL; +} + +Value *nix_get_list_byidx(nix_c_context *context, const Value *value, + unsigned int ix, GCRef *ref) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nList); + return (Value *)v.listElems()[ix]; + } + NIXC_CATCH_ERRS_NULL +} + +Value *nix_get_attr_byname(nix_c_context *context, const Value *value, + State *state, const char *name, GCRef *ref) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nAttrs); + nix::Symbol s = state->state.symbols.create(name); + auto attr = v.attrs->get(s); + if (attr) { + if (ref) + ref->ptr = attr->value; + return attr->value; + } + nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute"); + return nullptr; + } + NIXC_CATCH_ERRS_NULL +} + +bool nix_has_attr_byname(nix_c_context *context, const Value *value, + State *state, const char *name) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + assert(v.type() == nix::nAttrs); + nix::Symbol s = state->state.symbols.create(name); + auto attr = v.attrs->get(s); + if (attr) + return true; + return false; + } + NIXC_CATCH_ERRS_RES(false); +} + +Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, + State *state, unsigned int i, const char **name, + GCRef *ref) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + const nix::Attr &a = (*v.attrs)[i]; + *name = ((const std::string &)(state->state.symbols[a.name])).c_str(); + if (ref) + ref->ptr = a.value; + return a.value; + } + NIXC_CATCH_ERRS_NULL +} + +nix_err nix_set_bool(nix_c_context *context, Value *value, bool b) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + v.mkBool(b); + } + NIXC_CATCH_ERRS +} + +// todo string context +nix_err nix_set_string(nix_c_context *context, Value *value, const char *str) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + v.mkString(std::string_view(str)); + } + NIXC_CATCH_ERRS +} + +nix_err nix_set_path_string(nix_c_context *context, Value *value, + const char *str) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + v.mkPath(std::string_view(str)); + } + NIXC_CATCH_ERRS +} + +nix_err nix_set_double(nix_c_context *context, Value *value, double d) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + v.mkFloat(d); + } + NIXC_CATCH_ERRS +} + +nix_err nix_set_int(nix_c_context *context, Value *value, int64_t i) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + v.mkInt(i); + } + NIXC_CATCH_ERRS +} + +nix_err nix_set_null(nix_c_context *context, Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + v.mkNull(); + } + NIXC_CATCH_ERRS +} + +nix_err nix_set_external(nix_c_context *context, Value *value, + ExternalValue *val) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + auto r = (nix::ExternalValueBase *)val; + v.mkExternal(r); + } + NIXC_CATCH_ERRS +} + +nix_err nix_make_list(nix_c_context *context, State *s, Value *value, + unsigned int size) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + s->state.mkList(v, size); + } + NIXC_CATCH_ERRS +} + +nix_err nix_set_list_byidx(nix_c_context *context, Value *value, + unsigned int ix, Value *elem) { + if (context) + context->last_err_code = NIX_OK; + try { + // todo: assert that this is a list + auto &v = check_value_not_null(value); + auto &e = check_value_not_null(elem); + v.listElems()[ix] = &e; + } + NIXC_CATCH_ERRS +} + +nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *p) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + v.mkPrimOp((nix::PrimOp *)p); + } + NIXC_CATCH_ERRS +} + +nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + auto &s = check_value_not_null(source); + v = s; + } + NIXC_CATCH_ERRS +} + +nix_err nix_set_thunk(nix_c_context *context, State *s, Value *value, + Expr *expr) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + s->state.mkThunk_(v, (nix::Expr *)expr); + } + NIXC_CATCH_ERRS +} + +typedef std::shared_ptr BindingsBuilder_Inner; + +nix_err nix_make_attrs(nix_c_context *context, Value *value, + BindingsBuilder *b) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + nix::BindingsBuilder &builder = **(BindingsBuilder_Inner *)b; + v.mkAttrs(builder); + } + NIXC_CATCH_ERRS +} + +BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state, + size_t capacity) { + if (context) + context->last_err_code = NIX_OK; + try { + auto bb = state->state.buildBindings(capacity); + auto res = new BindingsBuilder_Inner(); + *res = std::allocate_shared( + traceable_allocator(), bb); + return res; + } + NIXC_CATCH_ERRS_NULL +} + +nix_err nix_bindings_builder_insert(nix_c_context *context, BindingsBuilder *b, + const char *name, Value *value) { + if (context) + context->last_err_code = NIX_OK; + try { + nix::BindingsBuilder &builder = **(BindingsBuilder_Inner *)b; + auto &v = check_value_not_null(value); + nix::Symbol s = builder.state.symbols.create(name); + builder.insert(s, &v); + } + NIXC_CATCH_ERRS +} + +void nix_bindings_builder_unref(BindingsBuilder *bb) { + delete (BindingsBuilder_Inner *)bb; +} diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h new file mode 100644 index 000000000..5ecedefeb --- /dev/null +++ b/src/libexpr/nix_api_value.h @@ -0,0 +1,355 @@ +#ifndef NIX_API_VALUE_H +#define NIX_API_VALUE_H + +/** @file + * @brief libexpr C bindings dealing with values + */ + +#include "nix_api_util.h" +#include "stdbool.h" +#include "stddef.h" +#include "stdint.h" + +#ifdef __cplusplus +extern "C" { +#endif +// cffi start + +// Type definitions +typedef enum { + NIX_TYPE_THUNK, + NIX_TYPE_INT, + NIX_TYPE_FLOAT, + NIX_TYPE_BOOL, + NIX_TYPE_STRING, + NIX_TYPE_PATH, + NIX_TYPE_NULL, + NIX_TYPE_ATTRS, + NIX_TYPE_LIST, + NIX_TYPE_FUNCTION, + NIX_TYPE_EXTERNAL +} ValueType; + +// forward declarations +typedef void Value; +typedef void Expr; +typedef struct State State; +typedef struct GCRef GCRef; +// type defs +/** @brief Stores an under-construction set of bindings + * Reference-counted + * @see nix_make_bindings_builder, nix_bindings_builder_unref, nix_make_attrs + * @see nix_bindings_builder_insert + */ +typedef void BindingsBuilder; + +/** @brief PrimOp function + * + * Owned by the GC + * @see nix_alloc_primop, nix_set_primop + */ +typedef struct PrimOp PrimOp; +/** @brief External Value + * + * Owned by the GC + * @see nix_api_external.h + */ +typedef struct ExternalValue ExternalValue; + +/** @brief Function pointer for primops + * @param[in] state Evaluator state + * @param[in] pos position of function call + * @param[in] args list of arguments + * @param[out] v return value + * @see nix_alloc_primop, nix_set_primop + */ +typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *v); + +/** @brief Allocate a primop + * + * Owned by the GC + * Pass a gcref to keep a reference. + * + * @param[out] context Optional, stores error information + * @param[in] fun callback + * @param[in] arity expected amount of function arguments + * @param[in] name function name + * @param[in] args array of argument names + * @param[in] doc optional, documentation for this primop + * @param[out] ref Optional, will store a reference to the returned value. + * @return primop, or null in case of errors + * @see nix_set_primop + */ +PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, + const char *name, const char **args, const char *doc, + GCRef *ref); + +// Function prototypes + +/** @brief Allocate a Nix value + * + * Owned by the GC + * Pass a gcref to keep a reference. + * @param[out] context Optional, stores error information + * @param[in] state nix evaluator state + * @param[out] ref Optional, will store a reference to the returned value. + * @return value, or null in case of errors + * + */ +Value *nix_alloc_value(nix_c_context *context, State *state, GCRef *ref); +/** @name Getters + */ +/**@{*/ +/** @brief Get value type + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return type of nix value + */ +ValueType nix_get_type(nix_c_context *context, const Value *value); +/** @brief Get type name of value + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return type name, owned string + * @todo way to free the result + */ +const char *nix_get_typename(nix_c_context *context, const Value *value); + +/** @brief Get boolean value + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return true or false, error info via context + */ +bool nix_get_bool(nix_c_context *context, const Value *value); +/** @brief Get string + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return string + * @return NULL in case of error. + */ +const char *nix_get_string(nix_c_context *context, const Value *value); +/** @brief Get path as string + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return string + * @return NULL in case of error. + */ +const char *nix_get_path_string(nix_c_context *context, const Value *value); +/** @brief Get the length of a list + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return length of list, error info via context + */ +unsigned int nix_get_list_size(nix_c_context *context, const Value *value); +/** @brief Get the element count of an attrset + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return attrset element count, error info via context + */ +unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value); +/** @brief Get float value in 64 bits + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return float contents, error info via context + */ +double nix_get_double(nix_c_context *context, const Value *value); +/** @brief Get int value + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return int contents, error info via context + */ +int64_t nix_get_int(nix_c_context *context, const Value *value); +/** @brief Get external reference + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @return reference to external, NULL in case of error + */ +ExternalValue *nix_get_external(nix_c_context *context, Value *); + +/** @brief Get the ix'th element of a list + * + * Pass a gcref to keep a reference. + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @param[in] ix list element to get + * @param[out] ref Optional, will store a reference to the returned value. + * @return value, NULL in case of errors + */ +Value *nix_get_list_byidx(nix_c_context *context, const Value *value, + unsigned int ix, GCRef *ref); +/** @brief Get an attr by name + * + * Pass a gcref to keep a reference. + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @param[in] state nix evaluator state + * @param[in] name attribute name + * @param[out] ref Optional, will store a reference to the returned value. + * @return value, NULL in case of errors + */ +Value *nix_get_attr_byname(nix_c_context *context, const Value *value, + State *state, const char *name, GCRef *ref); + +/** @brief Check if an attribute name exists on a value + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @param[in] state nix evaluator state + * @param[in] name attribute name + * @return value, NULL in case of errors + */ +bool nix_has_attr_byname(nix_c_context *context, const Value *value, + State *state, const char *name); + +/** @brief Get an attribute by index in the sorted bindings + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @param[in] state nix evaluator state + * @param[in] i attribute index + * @param[out] name will store a pointer to the attribute name + * @return value, NULL in case of errors + */ +Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, + State *state, unsigned int i, const char **name, + GCRef *ref); +/**@}*/ +/** @name Setters + */ +/**@{*/ +/** @brief Set boolean value + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] b the boolean value + * @return error code, NIX_OK on success. + */ +nix_err nix_set_bool(nix_c_context *context, Value *value, bool b); +/** @brief Set a string + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] str the string, copied + * @return error code, NIX_OK on success. + */ +nix_err nix_set_string(nix_c_context *context, Value *value, const char *str); +/** @brief Set a path + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] str the path string, copied + * @return error code, NIX_OK on success. + */ +nix_err nix_set_path_string(nix_c_context *context, Value *value, + const char *str); +/** @brief Set a double + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] d the double + * @return error code, NIX_OK on success. + */ +nix_err nix_set_double(nix_c_context *context, Value *value, double d); +/** @brief Set an int + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] i the int + * @return error code, NIX_OK on success. + */ +nix_err nix_set_int(nix_c_context *context, Value *value, int64_t i); +/** @brief Set null + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @return error code, NIX_OK on success. + */ +nix_err nix_set_null(nix_c_context *context, Value *value); +/** @brief Set an external value + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] val the external value to set. Will be GC-referenced by the value. + * @return error code, NIX_OK on success. + */ +nix_err nix_set_external(nix_c_context *context, Value *value, + ExternalValue *val); +/** @brief Allocate a list + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] size size of list + * @return error code, NIX_OK on success. + */ +nix_err nix_make_list(nix_c_context *context, State *s, Value *value, + unsigned int size); +/** @brief Manipulate a list by index + * + * Don't do this mid-computation. + * @pre your list should be at least 'ix+1' items long + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] ix index to manipulate + * @param[in] elem the value to set, will be gc-referenced by the value + * @return error code, NIX_OK on success. + */ +nix_err nix_set_list_byidx(nix_c_context *context, Value *value, + unsigned int ix, Value *elem); +/** @brief Create an attribute set from a bindings builder + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] b bindings builder to use. Make sure to unref this afterwards. + * @return error code, NIX_OK on success. + */ +nix_err nix_make_attrs(nix_c_context *context, Value *value, + BindingsBuilder *b); +/** @brief Set primop + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] op primop, will be gc-referenced by the value + * @see nix_alloc_primop + * @return error code, NIX_OK on success. + */ +nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *op); +/** @brief Copy from another value + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] source value to copy from + * @return error code, NIX_OK on success. + */ +nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source); +/** @brief Make a thunk from an expr. + * + * Expr will be evaluated when the value is forced + * @param[out] context Optional, stores error information + * @param[out] value Nix value to modify + * @param[in] expr the expr to thunk + * @return error code, NIX_OK on success. + */ +nix_err nix_set_thunk(nix_c_context *context, State *s, Value *value, + Expr *expr); +/**@}*/ + +/** @brief Create a bindings builder + +* @param[out] context Optional, stores error information +* @param[in] state nix evaluator state +* @param[in] capacity how many bindings you'll add. Don't exceed. +* @return owned reference to a bindings builder. Make sure to unref when you're +done. +*/ +BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state, + size_t capacity); +/** @brief Insert bindings into a builder + * @param[out] context Optional, stores error information + * @param[in] builder BindingsBuilder to insert into + * @param[in] name attribute name, copied into the symbol store + * @param[in] value value to give the binding + * @return error code, NIX_OK on success. + */ +nix_err nix_bindings_builder_insert(nix_c_context *context, + BindingsBuilder *builder, const char *name, + Value *value); +/** @brief Unref a bindings builder + * + * Does not fail. + * It'll be deallocated when all references are gone. + * @param[in] builder the builder to unref + */ +void nix_bindings_builder_unref(BindingsBuilder *builder); + +// cffi end +#ifdef __cplusplus +} +#endif + +#endif // NIX_API_VALUE_H diff --git a/src/libexpr/search-path.cc b/src/libexpr/search-path.cc index a25767496..e2c3e050a 100644 --- a/src/libexpr/search-path.cc +++ b/src/libexpr/search-path.cc @@ -44,7 +44,7 @@ SearchPath::Elem SearchPath::Elem::parse(std::string_view rawElem) } -SearchPath parseSearchPath(const Strings & rawElems) +SearchPath SearchPath::parse(const Strings & rawElems) { SearchPath res; for (auto & rawElem : rawElems) diff --git a/src/libexpr/value.hh b/src/libexpr/value.hh index 335801b34..b7b3c6434 100644 --- a/src/libexpr/value.hh +++ b/src/libexpr/value.hh @@ -328,6 +328,7 @@ public: } void mkPath(const SourcePath & path); + void mkPath(std::string_view path); inline void mkPath(InputAccessor * accessor, const char * path) { From 748b322dddaf0e789ed7dfa920523e7f19ebbe09 Mon Sep 17 00:00:00 2001 From: Puck Meerburg Date: Sun, 23 Jul 2023 11:32:17 +0000 Subject: [PATCH 072/168] nix_api_value: fix primop arity --- src/libexpr/nix_api_value.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index f58500367..ba36fdc3c 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -42,7 +42,11 @@ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, #ifdef HAVE_BOEHMGC (GC) #endif - nix::PrimOp{.name = name, .args = {}, .doc = doc, .fun = fun2}; + nix::PrimOp{.name = name, + .args = {}, + .arity = (size_t)arity, + .doc = doc, + .fun = fun2}; if (args) for (size_t i = 0; args[i]; i++) p->args.emplace_back(*args); From 4a4936136bda3e1d4b32dd49ef6af7653388eaa0 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 27 Jul 2023 12:59:09 +0200 Subject: [PATCH 073/168] nix_api_value: fix documentation for get_attr_byname --- src/libexpr/nix_api_value.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index 5ecedefeb..125189c94 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -194,7 +194,7 @@ Value *nix_get_attr_byname(nix_c_context *context, const Value *value, * @param[in] value Nix value to inspect * @param[in] state nix evaluator state * @param[in] name attribute name - * @return value, NULL in case of errors + * @return value, error info via context */ bool nix_has_attr_byname(nix_c_context *context, const Value *value, State *state, const char *name); From c3b5b8eb629f05edf5c66f9d1c8c86492b215a9f Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 27 Jul 2023 13:10:17 +0200 Subject: [PATCH 074/168] nix_api_expr, store: fix minor documentation issues --- src/libexpr/nix_api_expr.h | 5 +++-- src/libstore/nix_api_store.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h index 90aec8d18..f99fee1b1 100644 --- a/src/libexpr/nix_api_expr.h +++ b/src/libexpr/nix_api_expr.h @@ -41,7 +41,7 @@ typedef void Value; // nix::Value */ typedef struct GCRef GCRef; // void* -// Function propotypes +// Function prototypes /** * @brief Initializes the Nix expression evaluator. * @@ -76,7 +76,8 @@ Expr *nix_parse_expr_from_string(nix_c_context *context, State *state, * @param[out] context Optional, stores error information * @param[in] state The state of the evaluation. * @param[in] expr The Nix expression to evaluate. - * @param[in] value The result of the evaluation. + * @param[out] value The result of the evaluation. You should allocate this + * yourself. * @return NIX_OK if the evaluation was successful, an error code otherwise. */ nix_err nix_expr_eval(nix_c_context *context, State *state, Expr *expr, diff --git a/src/libstore/nix_api_store.h b/src/libstore/nix_api_store.h index 1ab7a4eb7..ba7b9ec5e 100644 --- a/src/libstore/nix_api_store.h +++ b/src/libstore/nix_api_store.h @@ -1,7 +1,7 @@ #ifndef NIX_API_STORE_H #define NIX_API_STORE_H /** @file - * @brief Main entry for the libexpr C bindings + * @brief Main entry for the libstore C bindings */ #include "nix_api_util.h" From efcddcdd2f58c7a83db77cbe102555331db6656e Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 27 Jul 2023 13:11:25 +0200 Subject: [PATCH 075/168] nix_api_external: fix missing void* self param --- src/libexpr/nix_api_external.cc | 4 ++-- src/libexpr/nix_api_external.h | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/libexpr/nix_api_external.cc b/src/libexpr/nix_api_external.cc index 971a175fb..5fe0819f4 100644 --- a/src/libexpr/nix_api_external.cc +++ b/src/libexpr/nix_api_external.cc @@ -139,7 +139,7 @@ public: } nix_string_context ctx{context}; std::unique_ptr r( - desc.printValueAsJSON((State *)&state, strict, &ctx, copyToStore)); + desc.printValueAsJSON(v, (State *)&state, strict, &ctx, copyToStore)); if (!r) { return nix::ExternalValueBase::printValueAsJSON(state, strict, context, copyToStore); @@ -160,7 +160,7 @@ public: state, strict, location, doc, context, drvsSeen, pos); } nix_string_context ctx{context}; - desc.printValueAsXML((State *)&state, strict, location, &doc, &ctx, + desc.printValueAsXML(v, (State *)&state, strict, location, &doc, &ctx, &drvsSeen, *reinterpret_cast(&pos)); } diff --git a/src/libexpr/nix_api_external.h b/src/libexpr/nix_api_external.h index 2bb53e349..66d289bac 100644 --- a/src/libexpr/nix_api_external.h +++ b/src/libexpr/nix_api_external.h @@ -132,6 +132,7 @@ typedef struct NixCExternalValueDesc { * @brief Convert the external value to json * * Optional, the default is to throw an error + * @param[in] self the void* passed to nix_create_external_value * @param[in] state The evaluator state * @param[in] strict boolean Whether to force the value before printing * @param[out] c writable string context for the resulting string @@ -140,7 +141,7 @@ typedef struct NixCExternalValueDesc { * @returns string that gets parsed as json. Optional, returning NULL will * make the conversion throw an error. */ - nix_returned_string *(*printValueAsJSON)(State *, int strict, + nix_returned_string *(*printValueAsJSON)(void *self, State *, int strict, nix_string_context *c, bool copyToStore); /** @@ -149,6 +150,7 @@ typedef struct NixCExternalValueDesc { * Optional, the default is to throw an error * @todo The mechanisms for this call are incomplete. There are no C * bindings to work with XML, pathsets and positions. + * @param[in] self the void* passed to nix_create_external_value * @param[in] state The evaluator state * @param[in] strict boolean Whether to force the value before printing * @param[in] location boolean Whether to include position information in the @@ -158,8 +160,9 @@ typedef struct NixCExternalValueDesc { * @param[in,out] drvsSeen a path set to avoid duplicating derivations * @param[in] pos The position of the call. */ - void (*printValueAsXML)(State *, int strict, int location, void *doc, - nix_string_context *c, void *drvsSeen, int pos); + void (*printValueAsXML)(void *self, State *, int strict, int location, + void *doc, nix_string_context *c, void *drvsSeen, + int pos); } NixCExternalValueDesc; /** From 1e583c4ebd2d475f9352192331f35727f4455b1b Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 27 Jul 2023 13:14:40 +0200 Subject: [PATCH 076/168] nix_api_value: nix_{get,set}_double -> nix_{get,set}_float --- src/libexpr/nix_api_value.cc | 4 ++-- src/libexpr/nix_api_value.h | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index ba36fdc3c..4a4a56ac3 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -170,7 +170,7 @@ unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value) { NIXC_CATCH_ERRS_RES(0); } -double nix_get_double(nix_c_context *context, const Value *value) { +double nix_get_float(nix_c_context *context, const Value *value) { if (context) context->last_err_code = NIX_OK; try { @@ -299,7 +299,7 @@ nix_err nix_set_path_string(nix_c_context *context, Value *value, NIXC_CATCH_ERRS } -nix_err nix_set_double(nix_c_context *context, Value *value, double d) { +nix_err nix_set_float(nix_c_context *context, Value *value, double d) { if (context) context->last_err_code = NIX_OK; try { diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index 125189c94..6d1604a6f 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -151,7 +151,7 @@ unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value); * @param[in] value Nix value to inspect * @return float contents, error info via context */ -double nix_get_double(nix_c_context *context, const Value *value); +double nix_get_float(nix_c_context *context, const Value *value); /** @brief Get int value * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect @@ -236,13 +236,13 @@ nix_err nix_set_string(nix_c_context *context, Value *value, const char *str); */ nix_err nix_set_path_string(nix_c_context *context, Value *value, const char *str); -/** @brief Set a double +/** @brief Set a float * @param[out] context Optional, stores error information * @param[out] value Nix value to modify - * @param[in] d the double + * @param[in] d the float, 64-bits * @return error code, NIX_OK on success. */ -nix_err nix_set_double(nix_c_context *context, Value *value, double d); +nix_err nix_set_float(nix_c_context *context, Value *value, double d); /** @brief Set an int * @param[out] context Optional, stores error information * @param[out] value Nix value to modify From 1777e4a5bb40a98c1c70b2baa4f5485373d46564 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 27 Jul 2023 14:51:23 +0200 Subject: [PATCH 077/168] nix_api_store: add userdata param to nix_store_build --- src/libstore/nix_api_store.cc | 6 ++++-- src/libstore/nix_api_store.h | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libstore/nix_api_store.cc b/src/libstore/nix_api_store.cc index 312e5f2a8..c81ad49ee 100644 --- a/src/libstore/nix_api_store.cc +++ b/src/libstore/nix_api_store.cc @@ -93,7 +93,9 @@ StorePath *nix_store_parse_path(nix_c_context *context, Store *store, } nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, - void (*iter)(const char *, const char *)) { + void *userdata, + void (*iter)(void *userdata, const char *, + const char *)) { if (context) context->last_err_code = NIX_OK; try { @@ -107,7 +109,7 @@ nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, for (auto &[outputName, outputPath] : store->ptr->queryDerivationOutputMap(path->path)) { auto op = store->ptr->printStorePath(outputPath); - iter(outputName.c_str(), op.c_str()); + iter(userdata, outputName.c_str(), op.c_str()); } } } diff --git a/src/libstore/nix_api_store.h b/src/libstore/nix_api_store.h index ba7b9ec5e..6157faa82 100644 --- a/src/libstore/nix_api_store.h +++ b/src/libstore/nix_api_store.h @@ -98,10 +98,13 @@ bool nix_store_is_valid_path(nix_c_context *context, Store *store, * @param[out] context Optional, stores error information * @param[in] store nix store reference * @param[in] path Path to build + * @param[in] userdata data to pass to every callback invocation * @param[in] cb called for every built output */ nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, - void (*cb)(const char *outname, const char *out)); + void *userdata, + void (*cb)(void *userdata, const char *outname, + const char *out)); /** * @brief get the version of a nix store From aa85f7d917850b352d35069f24550e20e0c6ff4c Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 27 Jul 2023 15:57:48 +0200 Subject: [PATCH 078/168] nix_api_expr: merge nix_parse_expr and nix_expr_eval, remove Expr --- src/libexpr/nix_api_expr.cc | 22 +++++----------------- src/libexpr/nix_api_expr.h | 33 ++++++--------------------------- src/libexpr/nix_api_value.cc | 11 ----------- src/libexpr/nix_api_value.h | 11 ----------- 4 files changed, 11 insertions(+), 66 deletions(-) diff --git a/src/libexpr/nix_api_expr.cc b/src/libexpr/nix_api_expr.cc index df8a66053..46c8835f2 100644 --- a/src/libexpr/nix_api_expr.cc +++ b/src/libexpr/nix_api_expr.cc @@ -39,27 +39,15 @@ nix_err nix_libexpr_init(nix_c_context *context) { NIXC_CATCH_ERRS } -Expr *nix_parse_expr_from_string(nix_c_context *context, State *state, - const char *expr, const char *path, - GCRef *ref) { +nix_err nix_expr_eval_from_string(nix_c_context *context, State *state, + const char *expr, const char *path, + Value *value) { if (context) context->last_err_code = NIX_OK; try { - Expr *result = state->state.parseExprFromString( + nix::Expr *parsedExpr = state->state.parseExprFromString( expr, state->state.rootPath(nix::CanonPath(path))); - if (ref) - ref->ptr = result; - return result; - } - NIXC_CATCH_ERRS_NULL -} - -nix_err nix_expr_eval(nix_c_context *context, State *state, Expr *expr, - Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - state->state.eval((nix::Expr *)expr, *(nix::Value *)value); + state->state.eval(parsedExpr, *(nix::Value *)value); } NIXC_CATCH_ERRS } diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h index f99fee1b1..e53aa5cd9 100644 --- a/src/libexpr/nix_api_expr.h +++ b/src/libexpr/nix_api_expr.h @@ -13,12 +13,6 @@ extern "C" { // cffi start // Type definitions -/** - * @brief Represents a parsed nix Expression, can be evaluated into a Value. - * - * Owned by the GC. - */ -typedef void Expr; // nix::Expr /** * @brief Represents a nix evaluator state. * @@ -54,34 +48,19 @@ typedef struct GCRef GCRef; // void* nix_err nix_libexpr_init(nix_c_context *context); /** - * @brief Parses a Nix expression from a string. - * - * The returned expression is owned by the garbage collector. - * Pass a gcref to keep a reference. - * - * @param[out] context Optional, stores error information - * @param[in] state Evaluator state. - * @param[in] expr The Nix expression to parse. - * @param[in] path The file path to associate with the expression. - * @param[out] ref Optional, will store a reference to the returned value. - * @return A parsed expression or NULL on failure. - */ -Expr *nix_parse_expr_from_string(nix_c_context *context, State *state, - const char *expr, const char *path, - GCRef *ref); - -/** - * @brief Evaluates a parsed Nix expression. + * @brief Parses and evaluates a Nix expression from a string. * * @param[out] context Optional, stores error information * @param[in] state The state of the evaluation. - * @param[in] expr The Nix expression to evaluate. + * @param[in] expr The Nix expression to parse. + * @param[in] path The file path to associate with the expression. * @param[out] value The result of the evaluation. You should allocate this * yourself. * @return NIX_OK if the evaluation was successful, an error code otherwise. */ -nix_err nix_expr_eval(nix_c_context *context, State *state, Expr *expr, - Value *value); +nix_err nix_expr_eval_from_string(nix_c_context *context, State *state, + const char *expr, const char *path, + Value *value); /** * @brief Calls a Nix function with an argument. diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index 4a4a56ac3..6a2f19de9 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -386,17 +386,6 @@ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) { NIXC_CATCH_ERRS } -nix_err nix_set_thunk(nix_c_context *context, State *s, Value *value, - Expr *expr) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - s->state.mkThunk_(v, (nix::Expr *)expr); - } - NIXC_CATCH_ERRS -} - typedef std::shared_ptr BindingsBuilder_Inner; nix_err nix_make_attrs(nix_c_context *context, Value *value, diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index 6d1604a6f..22ecfa86b 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -32,7 +32,6 @@ typedef enum { // forward declarations typedef void Value; -typedef void Expr; typedef struct State State; typedef struct GCRef GCRef; // type defs @@ -307,16 +306,6 @@ nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *op); * @return error code, NIX_OK on success. */ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source); -/** @brief Make a thunk from an expr. - * - * Expr will be evaluated when the value is forced - * @param[out] context Optional, stores error information - * @param[out] value Nix value to modify - * @param[in] expr the expr to thunk - * @return error code, NIX_OK on success. - */ -nix_err nix_set_thunk(nix_c_context *context, State *s, Value *value, - Expr *expr); /**@}*/ /** @brief Create a bindings builder From 022b918db171c9614e7c27f633452fb2bf9e6c57 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 27 Jul 2023 15:58:18 +0200 Subject: [PATCH 079/168] nix_api_expr: remove bindingsbuilder refcounting --- src/libexpr/nix_api_expr_internal.h | 7 ++++++- src/libexpr/nix_api_value.cc | 27 ++++++++++++++------------- src/libexpr/nix_api_value.h | 14 +++++++------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/libexpr/nix_api_expr_internal.h b/src/libexpr/nix_api_expr_internal.h index 424ca2874..e9031d311 100644 --- a/src/libexpr/nix_api_expr_internal.h +++ b/src/libexpr/nix_api_expr_internal.h @@ -4,7 +4,8 @@ // forward declaration namespace nix { class EvalState; -}; +class BindingsBuilder; +}; // namespace nix struct State { nix::EvalState state; @@ -14,4 +15,8 @@ struct GCRef { void *ptr; }; +struct BindingsBuilder { + nix::BindingsBuilder builder; +}; + #endif // NIX_API_EXPR_INTERNAL_H diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index 6a2f19de9..74e8395fc 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -386,16 +386,13 @@ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) { NIXC_CATCH_ERRS } -typedef std::shared_ptr BindingsBuilder_Inner; - nix_err nix_make_attrs(nix_c_context *context, Value *value, BindingsBuilder *b) { if (context) context->last_err_code = NIX_OK; try { auto &v = check_value_not_null(value); - nix::BindingsBuilder &builder = **(BindingsBuilder_Inner *)b; - v.mkAttrs(builder); + v.mkAttrs(b->builder); } NIXC_CATCH_ERRS } @@ -406,10 +403,11 @@ BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state, context->last_err_code = NIX_OK; try { auto bb = state->state.buildBindings(capacity); - auto res = new BindingsBuilder_Inner(); - *res = std::allocate_shared( - traceable_allocator(), bb); - return res; + return new +#if HAVE_BOEHMGC + (NoGC) +#endif + BindingsBuilder{std::move(bb)}; } NIXC_CATCH_ERRS_NULL } @@ -419,14 +417,17 @@ nix_err nix_bindings_builder_insert(nix_c_context *context, BindingsBuilder *b, if (context) context->last_err_code = NIX_OK; try { - nix::BindingsBuilder &builder = **(BindingsBuilder_Inner *)b; auto &v = check_value_not_null(value); - nix::Symbol s = builder.state.symbols.create(name); - builder.insert(s, &v); + nix::Symbol s = b->builder.state.symbols.create(name); + b->builder.insert(s, &v); } NIXC_CATCH_ERRS } -void nix_bindings_builder_unref(BindingsBuilder *bb) { - delete (BindingsBuilder_Inner *)bb; +void nix_bindings_builder_free(BindingsBuilder *bb) { +#if HAVE_BOEHMGC + GC_FREE((nix::BindingsBuilder *)bb); +#else + delete (nix::BindingsBuilder *)bb; +#endif } diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index 22ecfa86b..6aae5cf3c 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -36,11 +36,12 @@ typedef struct State State; typedef struct GCRef GCRef; // type defs /** @brief Stores an under-construction set of bindings - * Reference-counted - * @see nix_make_bindings_builder, nix_bindings_builder_unref, nix_make_attrs + * + * Do not reuse. + * @see nix_make_bindings_builder, nix_bindings_builder_free, nix_make_attrs * @see nix_bindings_builder_insert */ -typedef void BindingsBuilder; +typedef struct BindingsBuilder BindingsBuilder; /** @brief PrimOp function * @@ -328,13 +329,12 @@ BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state, nix_err nix_bindings_builder_insert(nix_c_context *context, BindingsBuilder *builder, const char *name, Value *value); -/** @brief Unref a bindings builder +/** @brief Free a bindings builder * * Does not fail. - * It'll be deallocated when all references are gone. - * @param[in] builder the builder to unref + * @param[in] builder the builder to free */ -void nix_bindings_builder_unref(BindingsBuilder *builder); +void nix_bindings_builder_free(BindingsBuilder *builder); // cffi end #ifdef __cplusplus From bebee700eadaf1bd8f15dca4db91033d8dd16b4e Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 28 Jul 2023 10:03:08 +0200 Subject: [PATCH 080/168] nix_api_external: own return strings on the nix side Change from nix_returned_string that passes ownership, into a nix_string_return parameter that can be set using nix_set_string_return. --- src/libexpr/nix_api_external.cc | 33 +++++++++++----------- src/libexpr/nix_api_external.h | 49 ++++++++++++++------------------- 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/src/libexpr/nix_api_external.cc b/src/libexpr/nix_api_external.cc index 5fe0819f4..1bf49f65a 100644 --- a/src/libexpr/nix_api_external.cc +++ b/src/libexpr/nix_api_external.cc @@ -20,7 +20,7 @@ #include "gc_cpp.h" #endif -struct nix_returned_string { +struct nix_string_return { std::string str; }; @@ -32,10 +32,9 @@ struct nix_string_context { nix::NixStringContext &ctx; }; -nix_returned_string *nix_external_alloc_string(const char *c) { - return new nix_returned_string{c}; +void nix_set_string_return(nix_string_return *str, const char *c) { + str->str = c; } -void nix_external_dealloc_string(nix_returned_string *str) { delete str; } nix_err nix_external_print(nix_c_context *context, nix_printer *printer, const char *c) { @@ -79,16 +78,18 @@ public: * Return a simple string describing the type */ virtual std::string showType() const override { - std::unique_ptr r(desc.showType(v)); - return std::move(r->str); + nix_string_return res; + desc.showType(v, &res); + return std::move(res.str); } /** * Return a string to be used in builtins.typeOf */ virtual std::string typeOf() const override { - std::unique_ptr r(desc.typeOf(v)); - return std::move(r->str); + nix_string_return res; + desc.typeOf(v, &res); + return std::move(res.str); } /** @@ -103,14 +104,14 @@ public: copyToStore); } nix_string_context ctx{context}; + nix_string_return res{""}; // todo: pos, errors - std::unique_ptr r( - desc.coerceToString(v, &ctx, copyMore, copyToStore)); - if (!r) { + desc.coerceToString(v, &ctx, copyMore, copyToStore, &res); + if (res.str.empty()) { return nix::ExternalValueBase::coerceToString(pos, context, copyMore, copyToStore); } - return std::move(r->str); + return std::move(res.str); } /** @@ -138,13 +139,13 @@ public: copyToStore); } nix_string_context ctx{context}; - std::unique_ptr r( - desc.printValueAsJSON(v, (State *)&state, strict, &ctx, copyToStore)); - if (!r) { + nix_string_return res{""}; + desc.printValueAsJSON(v, (State *)&state, strict, &ctx, copyToStore, &res); + if (res.str.empty()) { return nix::ExternalValueBase::printValueAsJSON(state, strict, context, copyToStore); } - return nlohmann::json::parse(r->str); + return nlohmann::json::parse(res.str); } /** diff --git a/src/libexpr/nix_api_external.h b/src/libexpr/nix_api_external.h index 66d289bac..4521f4736 100644 --- a/src/libexpr/nix_api_external.h +++ b/src/libexpr/nix_api_external.h @@ -17,9 +17,10 @@ extern "C" { // cffi start /** - * @brief Represents a string meant for consumption by nix. + * @brief Represents a string owned by nix. + * @see nix_set_owned_string */ -typedef struct nix_returned_string nix_returned_string; +typedef struct nix_string_return nix_string_return; /** * @brief Wraps a stream that can output multiple string pieces. */ @@ -30,23 +31,13 @@ typedef struct nix_printer nix_printer; typedef struct nix_string_context nix_string_context; /** - * @brief Allocate a nix_returned_string from a const char*. + * @brief Sets the contents of a nix_string_return * * Copies the passed string. - * @param[in] c The string to copy - * @returns A nix_returned_string* + * @param[out] str the nix_string_return to write to + * @param[in] c The string to copy */ -nix_returned_string *nix_external_alloc_string(const char *c); - -/** - * @brief Deallocate a nix_returned_string - * - * There's generally no need to call this, since - * returning the string will pass ownership to nix, - * but you can use it in case of errors. - * @param[in] str The string to deallocate - */ -void nix_external_dealloc_string(nix_returned_string *str); +void nix_set_string_return(nix_string_return *str, const char *c); /** * Print to the nix_printer @@ -91,15 +82,15 @@ typedef struct NixCExternalValueDesc { /** * @brief Called on :t * @param[in] self the void* passed to nix_create_external_value - * @returns a nix_returned_string, ownership passed to nix + * @param[out] res the return value */ - nix_returned_string *(*showType)(void *self); // std::string + void (*showType)(void *self, nix_string_return *res); /** * @brief Called on `builtins.typeOf` * @param self the void* passed to nix_create_external_value - * @returns a nix_returned_string, ownership passed to nix + * @param[out] res the return value */ - nix_returned_string *(*typeOf)(void *self); // std::string + void (*typeOf)(void *self, nix_string_return *res); /** * @brief Called on "${str}" and builtins.toString. * @@ -111,11 +102,11 @@ typedef struct NixCExternalValueDesc { * instead of throwing an error * @param[in] copyToStore boolean, whether to copy referenced paths to store * or keep them as-is - * @returns a nix_returned_string, ownership passed to nix. Optional, - * returning NULL will make the conversion throw an error. + * @param[out] res the return value. Not touching this, or setting it to the + * empty string, will make the conversion throw an error. */ - nix_returned_string *(*coerceToString)(void *self, nix_string_context *c, - int coerceMore, int copyToStore); + void (*coerceToString)(void *self, nix_string_context *c, int coerceMore, + int copyToStore, nix_string_return *res); /** * @brief Try to compare two external values * @@ -138,12 +129,12 @@ typedef struct NixCExternalValueDesc { * @param[out] c writable string context for the resulting string * @param[in] copyToStore whether to copy referenced paths to store or keep * them as-is - * @returns string that gets parsed as json. Optional, returning NULL will - * make the conversion throw an error. + * @param[out] res the return value. Gets parsed as JSON. Not touching this, + * or setting it to the empty string, will make the conversion throw an error. */ - nix_returned_string *(*printValueAsJSON)(void *self, State *, int strict, - nix_string_context *c, - bool copyToStore); + void (*printValueAsJSON)(void *self, State *, int strict, + nix_string_context *c, bool copyToStore, + nix_string_return *res); /** * @brief Convert the external value to XML * From ded0ef6f6c775929ed94ef0415662258213b3bd9 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 28 Jul 2023 10:49:21 +0200 Subject: [PATCH 081/168] nix_api_expr: switch to refcounting Remove GCRef, keep references in a map. Change to nix_gc_incref and nix_gc_decref, where users will mostly use nix_gc_decref. --- src/libexpr/nix_api_expr.cc | 48 +++++++++++++++++++---------- src/libexpr/nix_api_expr.h | 30 +++++++----------- src/libexpr/nix_api_expr_internal.h | 4 --- src/libexpr/nix_api_external.cc | 6 ++-- src/libexpr/nix_api_external.h | 7 ++--- src/libexpr/nix_api_value.cc | 28 ++++++++--------- src/libexpr/nix_api_value.h | 29 +++++++---------- 7 files changed, 71 insertions(+), 81 deletions(-) diff --git a/src/libexpr/nix_api_expr.cc b/src/libexpr/nix_api_expr.cc index 46c8835f2..1eb3693a2 100644 --- a/src/libexpr/nix_api_expr.cc +++ b/src/libexpr/nix_api_expr.cc @@ -16,6 +16,7 @@ #include "nix_api_util_internal.h" #ifdef HAVE_BOEHMGC +#include #define GC_INCLUDE_NEW 1 #include "gc_cpp.h" #endif @@ -100,27 +101,42 @@ State *nix_state_create(nix_c_context *context, const char **searchPath_c, void nix_state_free(State *state) { delete state; } -GCRef *nix_gc_ref(nix_c_context *context, void *obj) { - if (context) - context->last_err_code = NIX_OK; - try { -#if HAVE_BOEHMGC - return new (NoGC) GCRef{obj}; -#else - return new GCRef{obj}; -#endif +#ifdef HAVE_BOEHMGC +std::unordered_map< + const void *, unsigned int, std::hash, + std::equal_to, + traceable_allocator>> + nix_refcounts; + +std::mutex nix_refcount_lock; + +void nix_gc_incref(const void *p) { + std::scoped_lock lock(nix_refcount_lock); + auto f = nix_refcounts.find(p); + if (f != nix_refcounts.end()) { + f->second++; + } else { + nix_refcounts[p] = 1; } - NIXC_CATCH_ERRS_NULL } -void nix_gc_free(GCRef *ref) { -#if HAVE_BOEHMGC - GC_FREE(ref); -#else - delete ref; -#endif +void nix_gc_decref(const void *p) { + std::scoped_lock lock(nix_refcount_lock); + auto f = nix_refcounts.find(p); + if (f != nix_refcounts.end()) { + if (f->second == 1) + nix_refcounts.erase(f); + else + f->second--; + } + // todo: else { throw? } } +#else +void nix_gc_incref(const void *){}; +void nix_gc_decref(const void *){}; +#endif + void nix_gc_register_finalizer(void *obj, void *cd, void (*finalizer)(void *obj, void *cd)) { #ifdef HAVE_BOEHMGC diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h index e53aa5cd9..ae2806343 100644 --- a/src/libexpr/nix_api_expr.h +++ b/src/libexpr/nix_api_expr.h @@ -26,14 +26,6 @@ typedef struct State State; // nix::EvalState * Owned by the GC. */ typedef void Value; // nix::Value -/** - * @brief Reference for the GC - * - * Nix uses a garbage collector that may not be able to see into - * your stack and heap. Keep GCRef objects around for every - * garbage-collected object that you want to keep alive. - */ -typedef struct GCRef GCRef; // void* // Function prototypes /** @@ -119,22 +111,22 @@ State *nix_state_create(nix_c_context *context, const char **searchPath, void nix_state_free(State *state); /** - * @brief Creates a new garbage collector reference. + * @brief Increase the GC refcount. * - * @param[out] context Optional, stores error information - * @param[in] obj The object to create a reference for. - * @return A new garbage collector reference or NULL on failure. + * The nix C api keeps alive objects by refcounting. + * When you're done with a refcounted pointer, call nix_gc_decref. + * + * Does not fail + * + * @param[in] object The object to keep alive */ -GCRef *nix_gc_ref(nix_c_context *context, void *obj); - +void nix_gc_incref(const void *); /** - * @brief Frees a garbage collector reference. + * @brief Decrease the GC refcount * - * Does not fail. - * - * @param[in] ref The reference to free. + * @param[in] object The object to stop referencing */ -void nix_gc_free(GCRef *ref); +void nix_gc_decref(const void *); /** * @brief Register a callback that gets called when the object is garbage diff --git a/src/libexpr/nix_api_expr_internal.h b/src/libexpr/nix_api_expr_internal.h index e9031d311..3ee3b18af 100644 --- a/src/libexpr/nix_api_expr_internal.h +++ b/src/libexpr/nix_api_expr_internal.h @@ -11,10 +11,6 @@ struct State { nix::EvalState state; }; -struct GCRef { - void *ptr; -}; - struct BindingsBuilder { nix::BindingsBuilder builder; }; diff --git a/src/libexpr/nix_api_external.cc b/src/libexpr/nix_api_external.cc index 1bf49f65a..d72adee80 100644 --- a/src/libexpr/nix_api_external.cc +++ b/src/libexpr/nix_api_external.cc @@ -169,8 +169,7 @@ public: }; ExternalValue *nix_create_external_value(nix_c_context *context, - NixCExternalValueDesc *desc, void *v, - GCRef *gc) { + NixCExternalValueDesc *desc, void *v) { if (context) context->last_err_code = NIX_OK; try { @@ -179,8 +178,7 @@ ExternalValue *nix_create_external_value(nix_c_context *context, (GC) #endif NixCExternalValue(*desc, v); - if (gc) - gc->ptr = ret; + nix_gc_incref(ret); return (ExternalValue *)ret; } NIXC_CATCH_ERRS_NULL diff --git a/src/libexpr/nix_api_external.h b/src/libexpr/nix_api_external.h index 4521f4736..45e95346b 100644 --- a/src/libexpr/nix_api_external.h +++ b/src/libexpr/nix_api_external.h @@ -159,18 +159,17 @@ typedef struct NixCExternalValueDesc { /** * @brief Create an external value, that can be given to nix_set_external * - * Pass a gcref to keep a reference. + * Owned by the GC. Use nix_gc_decref when you're done with the pointer. + * * @param[out] context Optional, stores error information * @param[in] desc a NixCExternalValueDesc, you should keep this alive as long * as the ExternalValue lives * @param[in] v the value to store - * @param[out] ref Optional, will store a reference to the returned value. * @returns external value, owned by the garbage collector * @see nix_set_external */ ExternalValue *nix_create_external_value(nix_c_context *context, - NixCExternalValueDesc *desc, void *v, - GCRef *ref); + NixCExternalValueDesc *desc, void *v); /** * @brief Extract the pointer from a nix c external value. diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index 74e8395fc..f34907ef1 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -32,8 +32,7 @@ static nix::Value &check_value_not_null(Value *value) { } PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, - const char *name, const char **args, const char *doc, - GCRef *ref) { + const char *name, const char **args, const char *doc) { if (context) context->last_err_code = NIX_OK; try { @@ -50,20 +49,18 @@ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, if (args) for (size_t i = 0; args[i]; i++) p->args.emplace_back(*args); - if (ref) - ref->ptr = p; + nix_gc_incref(p); return (PrimOp *)p; } NIXC_CATCH_ERRS_NULL } -Value *nix_alloc_value(nix_c_context *context, State *state, GCRef *ref) { +Value *nix_alloc_value(nix_c_context *context, State *state) { if (context) context->last_err_code = NIX_OK; try { Value *res = state->state.allocValue(); - if (ref) - ref->ptr = res; + nix_gc_incref(res); return res; } NIXC_CATCH_ERRS_NULL @@ -204,19 +201,21 @@ ExternalValue *nix_get_external(nix_c_context *context, Value *value) { } Value *nix_get_list_byidx(nix_c_context *context, const Value *value, - unsigned int ix, GCRef *ref) { + unsigned int ix) { if (context) context->last_err_code = NIX_OK; try { auto &v = check_value_not_null(value); assert(v.type() == nix::nList); - return (Value *)v.listElems()[ix]; + auto *p = v.listElems()[ix]; + nix_gc_incref(p); + return (Value *)p; } NIXC_CATCH_ERRS_NULL } Value *nix_get_attr_byname(nix_c_context *context, const Value *value, - State *state, const char *name, GCRef *ref) { + State *state, const char *name) { if (context) context->last_err_code = NIX_OK; try { @@ -225,8 +224,7 @@ Value *nix_get_attr_byname(nix_c_context *context, const Value *value, nix::Symbol s = state->state.symbols.create(name); auto attr = v.attrs->get(s); if (attr) { - if (ref) - ref->ptr = attr->value; + nix_gc_incref(attr->value); return attr->value; } nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute"); @@ -252,16 +250,14 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value, } Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, - State *state, unsigned int i, const char **name, - GCRef *ref) { + State *state, unsigned int i, const char **name) { if (context) context->last_err_code = NIX_OK; try { auto &v = check_value_not_null(value); const nix::Attr &a = (*v.attrs)[i]; *name = ((const std::string &)(state->state.symbols[a.name])).c_str(); - if (ref) - ref->ptr = a.value; + nix_gc_incref(a.value); return a.value; } NIXC_CATCH_ERRS_NULL diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index 6aae5cf3c..8fa85b25d 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -33,7 +33,6 @@ typedef enum { // forward declarations typedef void Value; typedef struct State State; -typedef struct GCRef GCRef; // type defs /** @brief Stores an under-construction set of bindings * @@ -67,8 +66,7 @@ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *v); /** @brief Allocate a primop * - * Owned by the GC - * Pass a gcref to keep a reference. + * Owned by the GC. Use nix_gc_decref when you're done with the pointer * * @param[out] context Optional, stores error information * @param[in] fun callback @@ -76,27 +74,23 @@ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *v); * @param[in] name function name * @param[in] args array of argument names * @param[in] doc optional, documentation for this primop - * @param[out] ref Optional, will store a reference to the returned value. * @return primop, or null in case of errors * @see nix_set_primop */ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, - const char *name, const char **args, const char *doc, - GCRef *ref); + const char *name, const char **args, const char *doc); // Function prototypes /** @brief Allocate a Nix value * - * Owned by the GC - * Pass a gcref to keep a reference. + * Owned by the GC. Use nix_gc_decref when you're done with the pointer * @param[out] context Optional, stores error information * @param[in] state nix evaluator state - * @param[out] ref Optional, will store a reference to the returned value. * @return value, or null in case of errors * */ -Value *nix_alloc_value(nix_c_context *context, State *state, GCRef *ref); +Value *nix_alloc_value(nix_c_context *context, State *state); /** @name Getters */ /**@{*/ @@ -167,27 +161,25 @@ ExternalValue *nix_get_external(nix_c_context *context, Value *); /** @brief Get the ix'th element of a list * - * Pass a gcref to keep a reference. + * Owned by the GC. Use nix_gc_decref when you're done with the pointer * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @param[in] ix list element to get - * @param[out] ref Optional, will store a reference to the returned value. * @return value, NULL in case of errors */ Value *nix_get_list_byidx(nix_c_context *context, const Value *value, - unsigned int ix, GCRef *ref); + unsigned int ix); /** @brief Get an attr by name * - * Pass a gcref to keep a reference. + * Owned by the GC. Use nix_gc_decref when you're done with the pointer * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @param[in] state nix evaluator state * @param[in] name attribute name - * @param[out] ref Optional, will store a reference to the returned value. * @return value, NULL in case of errors */ Value *nix_get_attr_byname(nix_c_context *context, const Value *value, - State *state, const char *name, GCRef *ref); + State *state, const char *name); /** @brief Check if an attribute name exists on a value * @param[out] context Optional, stores error information @@ -200,6 +192,8 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value, State *state, const char *name); /** @brief Get an attribute by index in the sorted bindings + * + * Owned by the GC. Use nix_gc_decref when you're done with the pointer * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @param[in] state nix evaluator state @@ -208,8 +202,7 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value, * @return value, NULL in case of errors */ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, - State *state, unsigned int i, const char **name, - GCRef *ref); + State *state, unsigned int i, const char **name); /**@}*/ /** @name Setters */ From ada2af4f885e74876df46e52ef8f2d73a3be90b9 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 28 Jul 2023 13:47:54 +0200 Subject: [PATCH 082/168] nix_api_expr: add nix_gc_now() --- src/libexpr/nix_api_expr.cc | 7 +++++-- src/libexpr/nix_api_expr.h | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/libexpr/nix_api_expr.cc b/src/libexpr/nix_api_expr.cc index 1eb3693a2..84d55ec13 100644 --- a/src/libexpr/nix_api_expr.cc +++ b/src/libexpr/nix_api_expr.cc @@ -132,9 +132,12 @@ void nix_gc_decref(const void *p) { // todo: else { throw? } } +void nix_gc_now() { GC_gcollect(); } + #else -void nix_gc_incref(const void *){}; -void nix_gc_decref(const void *){}; +void nix_gc_incref(const void *) {} +void nix_gc_decref(const void *) {} +void nix_gc_now() {} #endif void nix_gc_register_finalizer(void *obj, void *cd, diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h index ae2806343..9efb3dde1 100644 --- a/src/libexpr/nix_api_expr.h +++ b/src/libexpr/nix_api_expr.h @@ -128,6 +128,13 @@ void nix_gc_incref(const void *); */ void nix_gc_decref(const void *); +/** + * @brief Trigger the garbage collector manually + * + * You should not need to do this, but it can be useful for debugging. + */ +void nix_gc_now(); + /** * @brief Register a callback that gets called when the object is garbage * collected. From 866558af34a000bab8eff843ef67a98b9702b08b Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 28 Jul 2023 16:21:29 +0200 Subject: [PATCH 083/168] nix_api_expr: add error handling to incref, decref --- src/libexpr/nix_api_expr.cc | 53 ++++++++++++++++++++++----------- src/libexpr/nix_api_expr.h | 4 +-- src/libexpr/nix_api_external.cc | 2 +- src/libexpr/nix_api_value.cc | 10 +++---- 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/libexpr/nix_api_expr.cc b/src/libexpr/nix_api_expr.cc index 84d55ec13..880030380 100644 --- a/src/libexpr/nix_api_expr.cc +++ b/src/libexpr/nix_api_expr.cc @@ -110,33 +110,50 @@ std::unordered_map< std::mutex nix_refcount_lock; -void nix_gc_incref(const void *p) { - std::scoped_lock lock(nix_refcount_lock); - auto f = nix_refcounts.find(p); - if (f != nix_refcounts.end()) { - f->second++; - } else { - nix_refcounts[p] = 1; +nix_err nix_gc_incref(nix_c_context *context, const void *p) { + if (context) + context->last_err_code = NIX_OK; + try { + std::scoped_lock lock(nix_refcount_lock); + auto f = nix_refcounts.find(p); + if (f != nix_refcounts.end()) { + f->second++; + } else { + nix_refcounts[p] = 1; + } } + NIXC_CATCH_ERRS } -void nix_gc_decref(const void *p) { - std::scoped_lock lock(nix_refcount_lock); - auto f = nix_refcounts.find(p); - if (f != nix_refcounts.end()) { - if (f->second == 1) - nix_refcounts.erase(f); - else - f->second--; +nix_err nix_gc_decref(nix_c_context *context, const void *p) { + + if (context) + context->last_err_code = NIX_OK; + try { + std::scoped_lock lock(nix_refcount_lock); + auto f = nix_refcounts.find(p); + if (f != nix_refcounts.end()) { + if (--f->second == 0) + nix_refcounts.erase(f); + } else + throw std::runtime_error("nix_gc_decref: object was not referenced"); } - // todo: else { throw? } + NIXC_CATCH_ERRS } void nix_gc_now() { GC_gcollect(); } #else -void nix_gc_incref(const void *) {} -void nix_gc_decref(const void *) {} +void nix_gc_incref(nix_c_context *context, const void *) { + if (context) + context->last_err_code = NIX_OK; + return NIX_OK; +} +void nix_gc_decref(nix_c_context *context, const void *) { + if (context) + context->last_err_code = NIX_OK; + return NIX_OK; +} void nix_gc_now() {} #endif diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h index 9efb3dde1..c56ef89bb 100644 --- a/src/libexpr/nix_api_expr.h +++ b/src/libexpr/nix_api_expr.h @@ -120,13 +120,13 @@ void nix_state_free(State *state); * * @param[in] object The object to keep alive */ -void nix_gc_incref(const void *); +nix_err nix_gc_incref(nix_c_context *, const void *); /** * @brief Decrease the GC refcount * * @param[in] object The object to stop referencing */ -void nix_gc_decref(const void *); +nix_err nix_gc_decref(nix_c_context *, const void *); /** * @brief Trigger the garbage collector manually diff --git a/src/libexpr/nix_api_external.cc b/src/libexpr/nix_api_external.cc index d72adee80..a927a4037 100644 --- a/src/libexpr/nix_api_external.cc +++ b/src/libexpr/nix_api_external.cc @@ -178,7 +178,7 @@ ExternalValue *nix_create_external_value(nix_c_context *context, (GC) #endif NixCExternalValue(*desc, v); - nix_gc_incref(ret); + nix_gc_incref(nullptr, ret); return (ExternalValue *)ret; } NIXC_CATCH_ERRS_NULL diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index f34907ef1..6e02b3310 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -49,7 +49,7 @@ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, if (args) for (size_t i = 0; args[i]; i++) p->args.emplace_back(*args); - nix_gc_incref(p); + nix_gc_incref(nullptr, p); return (PrimOp *)p; } NIXC_CATCH_ERRS_NULL @@ -60,7 +60,7 @@ Value *nix_alloc_value(nix_c_context *context, State *state) { context->last_err_code = NIX_OK; try { Value *res = state->state.allocValue(); - nix_gc_incref(res); + nix_gc_incref(nullptr, res); return res; } NIXC_CATCH_ERRS_NULL @@ -208,7 +208,7 @@ Value *nix_get_list_byidx(nix_c_context *context, const Value *value, auto &v = check_value_not_null(value); assert(v.type() == nix::nList); auto *p = v.listElems()[ix]; - nix_gc_incref(p); + nix_gc_incref(nullptr, p); return (Value *)p; } NIXC_CATCH_ERRS_NULL @@ -224,7 +224,7 @@ Value *nix_get_attr_byname(nix_c_context *context, const Value *value, nix::Symbol s = state->state.symbols.create(name); auto attr = v.attrs->get(s); if (attr) { - nix_gc_incref(attr->value); + nix_gc_incref(nullptr, attr->value); return attr->value; } nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute"); @@ -257,7 +257,7 @@ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, auto &v = check_value_not_null(value); const nix::Attr &a = (*v.attrs)[i]; *name = ((const std::string &)(state->state.symbols[a.name])).c_str(); - nix_gc_incref(a.value); + nix_gc_incref(nullptr, a.value); return a.value; } NIXC_CATCH_ERRS_NULL From b0741f712871ba4ab54e50e82b763b2e043c4f88 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Sun, 30 Jul 2023 16:36:51 +0200 Subject: [PATCH 084/168] external-api-doc: introduce and improve documentation --- Makefile | 5 ++- configure.ac | 5 +++ doc/external-api/.gitignore | 3 ++ doc/external-api/doxygen.cfg.in | 54 ++++++++++++++++++++++++++ doc/external-api/local.mk | 19 +++++++++ src/libexpr/nix_api_expr.h | 68 ++++++++++++++++++++++++++------- src/libexpr/nix_api_external.h | 6 +++ src/libexpr/nix_api_value.h | 18 ++++++++- src/libstore/nix_api_store.h | 11 +++++- src/libutil/nix_api_util.h | 56 +++++++++++++++++++++++++-- 10 files changed, 225 insertions(+), 20 deletions(-) create mode 100644 doc/external-api/.gitignore create mode 100644 doc/external-api/doxygen.cfg.in create mode 100644 doc/external-api/local.mk diff --git a/Makefile b/Makefile index c3dc83c77..4f60d0d8b 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,10 @@ makefiles = \ misc/zsh/local.mk \ misc/systemd/local.mk \ misc/launchd/local.mk \ - misc/upstart/local.mk + misc/upstart/local.mk \ + doc/manual/local.mk \ + doc/internal-api/local.mk \ + doc/external-api/local.mk endif ifeq ($(ENABLE_UNIT_TESTS), yes) diff --git a/configure.ac b/configure.ac index 676b145a5..c3823c01c 100644 --- a/configure.ac +++ b/configure.ac @@ -150,6 +150,11 @@ AC_ARG_ENABLE(unit-tests, AS_HELP_STRING([--disable-unit-tests],[Do not build th ENABLE_UNIT_TESTS=$enableval, ENABLE_UNIT_TESTS=$ENABLE_BUILD) AC_SUBST(ENABLE_UNIT_TESTS) +# Build external API docs by default +AC_ARG_ENABLE(external_api_docs, AS_HELP_STRING([--enable-external-api-docs],[Build API docs for Nix's C interface]), + external_api_docs=$enableval, external_api_docs=yes) +AC_SUBST(external_api_docs) + AS_IF( [test "$ENABLE_BUILD" == "no" && test "$ENABLE_UNIT_TESTS" == "yes"], [AC_MSG_ERROR([Cannot enable unit tests when building overall is disabled. Please do not pass '--enable-unit-tests' or do not pass '--disable-build'.])]) diff --git a/doc/external-api/.gitignore b/doc/external-api/.gitignore new file mode 100644 index 000000000..dab28b6b0 --- /dev/null +++ b/doc/external-api/.gitignore @@ -0,0 +1,3 @@ +/doxygen.cfg +/html +/latex diff --git a/doc/external-api/doxygen.cfg.in b/doc/external-api/doxygen.cfg.in new file mode 100644 index 000000000..19350b2c6 --- /dev/null +++ b/doc/external-api/doxygen.cfg.in @@ -0,0 +1,54 @@ +# Doxyfile 1.9.5 + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded by +# double-quotes, unless you are using Doxywizard) that should identify the +# project for which the documentation is generated. This name is used in the +# title of most generated pages and in a few other places. +# The default value is: My Project. + +PROJECT_NAME = "Nix" + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. This +# could be handy for archiving the generated documentation or if some version +# control system is used. + +PROJECT_NUMBER = @PACKAGE_VERSION@ + +# Using the PROJECT_BRIEF tag one can provide an optional one line description +# for a project that appears at the top of each page and should give viewer a +# quick idea about the purpose of the project. Keep the description short. + +PROJECT_BRIEF = "Nix, the purely functional package manager; stable external interfaces" + +# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output. +# The default value is: YES. + +GENERATE_LATEX = NO + +# The INPUT tag is used to specify the files and/or directories that contain +# documented source files. You may enter file names like myfile.cpp or +# directories like /usr/src/myproject. Separate the files or directories with +# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING +# Note: If this tag is empty the current directory is searched. + +# FIXME Make this list more maintainable somehow. We could maybe generate this +# in the Makefile, but we would need to change how `.in` files are preprocessed +# so they can expand variables despite configure variables. + +INPUT = \ + src/libutil \ + src/libexpr \ + src/libstore + +FILE_PATTERNS = nix_api_*.h + +# The INCLUDE_PATH tag can be used to specify one or more directories that +# contain include files that are not input files but should be processed by the +# preprocessor. Note that the INCLUDE_PATH is not recursive, so the setting of +# RECURSIVE has no effect here. +# This tag requires that the tag SEARCH_INCLUDES is set to YES. + +INCLUDE_PATH = @RAPIDCHECK_HEADERS@ +EXCLUDE_PATTERNS = *_internal.h +GENERATE_TREEVIEW = YES +OPTIMIZE_OUTPUT_FOR_C = YES diff --git a/doc/external-api/local.mk b/doc/external-api/local.mk new file mode 100644 index 000000000..a0f6e26fc --- /dev/null +++ b/doc/external-api/local.mk @@ -0,0 +1,19 @@ +.PHONY: external-api-html + +ifeq ($(internal_api_docs), yes) + +$(docdir)/external-api/html/index.html $(docdir)/external-api/latex: $(d)/doxygen.cfg + mkdir -p $(docdir)/external-api + { cat $< ; echo "OUTPUT_DIRECTORY=$(docdir)/external-api" ; } | doxygen - + +# Generate the HTML API docs for Nix's unstable internal interfaces. +external-api-html: $(docdir)/external-api/html/index.html + +else + +# Make a nicer error message +external-api-html: + @echo "Internal API docs are disabled. Configure with '--enable-external-api-docs', or avoid calling 'make external-api-html'." + @exit 1 + +endif diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h index c56ef89bb..f67a3d13f 100644 --- a/src/libexpr/nix_api_expr.h +++ b/src/libexpr/nix_api_expr.h @@ -1,5 +1,26 @@ #ifndef NIX_API_EXPR_H #define NIX_API_EXPR_H +/** @defgroup libexpr libexpr + * @brief Bindings to the Nix evaluator + * + * Example (without error handling): + * @code{.c} + * int main() { + * nix_libexpr_init(NULL); + * + * Store* store = nix_store_open(NULL, "dummy", NULL); + * State* state = nix_state_create(NULL, NULL /* empty NIX_PATH */, store); +*Value *value = nix_alloc_value(NULL, state); +**nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value); +*nix_value_force(NULL, state, value); +*printf("nix version: %s\n", nix_get_string(NULL, value)); +**nix_gc_decref(NULL, value); +*nix_state_free(state); +*nix_store_unref(store); +*return 0; +* +} +*@endcode *@{* / /** @file * @brief Main entry for the libexpr C bindings */ @@ -8,22 +29,25 @@ #include "nix_api_util.h" #ifdef __cplusplus -extern "C" { + extern "C" { #endif -// cffi start + // cffi start -// Type definitions -/** - * @brief Represents a nix evaluator state. - * - * Multiple can be created for multi-threaded - * operation. - */ -typedef struct State State; // nix::EvalState + // Type definitions + /** + * @brief Represents a nix evaluator state. + * + * Multiple can be created for multi-threaded + * operation. + * @struct State + */ + typedef struct State State; // nix::EvalState /** * @brief Represents a nix value. * * Owned by the GC. + * @struct Value + * @see value_manip */ typedef void Value; // nix::Value @@ -110,23 +134,36 @@ State *nix_state_create(nix_c_context *context, const char **searchPath, */ void nix_state_free(State *state); +/** @addtogroup GC + * @brief Reference counting and garbage collector operations + * + * Nix's evaluator uses a garbage collector. To ease C interop, we implement + * a reference counting scheme, where objects will be deallocated + * when there are no references from the Nix side, and the reference count kept + * by the C API reaches `0`. + * + * Functions returning a garbage-collected object will automatically increase + * the refcount for you. You should make sure to call `nix_gc_decref` when + * you're done. + * @{ + */ /** * @brief Increase the GC refcount. * * The nix C api keeps alive objects by refcounting. * When you're done with a refcounted pointer, call nix_gc_decref. * - * Does not fail - * + * @param[out] context Optional, stores error information * @param[in] object The object to keep alive */ -nix_err nix_gc_incref(nix_c_context *, const void *); +nix_err nix_gc_incref(nix_c_context *context, const void *object); /** * @brief Decrease the GC refcount * + * @param[out] context Optional, stores error information * @param[in] object The object to stop referencing */ -nix_err nix_gc_decref(nix_c_context *, const void *); +nix_err nix_gc_decref(nix_c_context *context, const void *object); /** * @brief Trigger the garbage collector manually @@ -147,9 +184,12 @@ void nix_gc_now(); void nix_gc_register_finalizer(void *obj, void *cd, void (*finalizer)(void *obj, void *cd)); +/** @} */ // cffi end #ifdef __cplusplus } #endif +/** @} */ + #endif // NIX_API_EXPR_H diff --git a/src/libexpr/nix_api_external.h b/src/libexpr/nix_api_external.h index 45e95346b..692f8000c 100644 --- a/src/libexpr/nix_api_external.h +++ b/src/libexpr/nix_api_external.h @@ -1,5 +1,10 @@ #ifndef NIX_API_EXTERNAL_H #define NIX_API_EXTERNAL_H +/** @ingroup libexpr + * @addtogroup Externals + * @brief Deal with external values + * @{ + */ /** @file * @brief libexpr C bindings dealing with external values */ @@ -184,5 +189,6 @@ void *nix_get_external_value_content(nix_c_context *context, ExternalValue *b); #ifdef __cplusplus } #endif +/** @} */ #endif // NIX_API_EXTERNAL_H diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index 8fa85b25d..110dd086f 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -1,6 +1,9 @@ #ifndef NIX_API_VALUE_H #define NIX_API_VALUE_H +/** @addtogroup libexpr + * @{ + */ /** @file * @brief libexpr C bindings dealing with values */ @@ -35,6 +38,7 @@ typedef void Value; typedef struct State State; // type defs /** @brief Stores an under-construction set of bindings + * @ingroup value_manip * * Do not reuse. * @see nix_make_bindings_builder, nix_bindings_builder_free, nix_make_attrs @@ -43,18 +47,23 @@ typedef struct State State; typedef struct BindingsBuilder BindingsBuilder; /** @brief PrimOp function + * @ingroup primops * * Owned by the GC * @see nix_alloc_primop, nix_set_primop */ typedef struct PrimOp PrimOp; /** @brief External Value + * @ingroup Externals * * Owned by the GC - * @see nix_api_external.h */ typedef struct ExternalValue ExternalValue; +/** @defgroup primops + * @brief Create your own primops + * @{ + */ /** @brief Function pointer for primops * @param[in] state Evaluator state * @param[in] pos position of function call @@ -79,6 +88,7 @@ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *v); */ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, const char *name, const char **args, const char *doc); +/** @} */ // Function prototypes @@ -91,6 +101,10 @@ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, * */ Value *nix_alloc_value(nix_c_context *context, State *state); +/** @addtogroup value_manip Manipulating values + * @brief Functions to inspect and change nix Value's + * @{ + */ /** @name Getters */ /**@{*/ @@ -328,10 +342,12 @@ nix_err nix_bindings_builder_insert(nix_c_context *context, * @param[in] builder the builder to free */ void nix_bindings_builder_free(BindingsBuilder *builder); +/**@}*/ // cffi end #ifdef __cplusplus } #endif +/** @} */ #endif // NIX_API_VALUE_H diff --git a/src/libstore/nix_api_store.h b/src/libstore/nix_api_store.h index 6157faa82..bc01f0ad2 100644 --- a/src/libstore/nix_api_store.h +++ b/src/libstore/nix_api_store.h @@ -1,5 +1,12 @@ #ifndef NIX_API_STORE_H #define NIX_API_STORE_H +/** + * @defgroup libstore libstore + * @brief C bindings for nix libstore + * + * libstore is used for talking to a Nix store + * @{ + */ /** @file * @brief Main entry for the libstore C bindings */ @@ -121,5 +128,7 @@ nix_err nix_store_get_version(nix_c_context *, Store *store, char *dest, #ifdef __cplusplus } #endif - +/** + * @} + */ #endif // NIX_API_STORE_H diff --git a/src/libutil/nix_api_util.h b/src/libutil/nix_api_util.h index 095564296..f626f2ccb 100644 --- a/src/libutil/nix_api_util.h +++ b/src/libutil/nix_api_util.h @@ -1,6 +1,13 @@ #ifndef NIX_API_UTIL_H #define NIX_API_UTIL_H - +/** + * @defgroup libutil libutil + * @brief C bindings for nix libutil + * + * libutil is used for functionality shared between + * different Nix modules. + * @{ + */ /** @file * @brief Main entry for the libutil C bindings * @@ -12,6 +19,31 @@ extern "C" { #endif // cffi start +/** @defgroup errors Handling errors + * @brief Dealing with errors from the Nix side + * + * To handle errors that can be returned from the Nix API + * nix_c_context can be passed any function that potentially returns an error. + * + * Error information will be stored in this context, and can be retrieved + * using nix_err_code, nix_err_msg. + * + * Passing NULL instead will cause the API to throw C++ errors. + * + * Example: + * @code{.c} + * int main() { + * nix_c_context* ctx = nix_c_context_create(); + * nix_libutil_init(ctx); + * if (nix_err_code(ctx) != NIX_OK) { + * printf("error: %s\n", nix_err_msg(NULL, ctx, NULL)); + * return 1; + * } + * return 0; + * } + * @endcode + * @{ + */ // Error codes /** * @brief Type for error codes in the NIX system @@ -67,6 +99,7 @@ typedef int nix_err; /** * @brief This object stores error state. + * @struct nix_c_context * * Passed as a first parameter to C functions that can fail, will store error * information. Optional wherever it is used, passing NULL will throw a C++ @@ -92,6 +125,9 @@ nix_c_context *nix_c_context_create(); * @param[out] context The context to free, mandatory. */ void nix_c_context_free(nix_c_context *context); +/** + * @} + */ /** * @brief Initializes nix_libutil and its dependencies. @@ -105,6 +141,9 @@ void nix_c_context_free(nix_c_context *context); */ nix_err nix_libutil_init(nix_c_context *context); +/** @defgroup settings + * @{ + */ /** * @brief Retrieves a setting from the nix global configuration. * @@ -128,8 +167,8 @@ nix_err nix_setting_get(nix_c_context *context, const char *key, char *value, * * Use "extra-" to append to the setting's value. * - * Settings only apply for new States. Call nix_plugins_init() when you are done - * with the settings to load any plugins. + * Settings only apply for new State%s. Call nix_plugins_init() when you are + * done with the settings to load any plugins. * * @param[out] context optional, Stores error information * @param[in] key The key of the setting to set. @@ -140,6 +179,9 @@ nix_err nix_setting_get(nix_c_context *context, const char *key, char *value, nix_err nix_setting_set(nix_c_context *context, const char *key, const char *value); +/** + * @} + */ // todo: nix_plugins_init() /** @@ -150,6 +192,9 @@ nix_err nix_setting_set(nix_c_context *context, const char *key, */ const char *nix_version_get(); +/** @addtogroup errors + * @{ + */ /** * @brief Retrieves the most recent error message from a context. * @@ -215,9 +260,14 @@ nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context, */ nix_err nix_err_code(nix_c_context *context, const nix_c_context *read_context); +/** + * @} + */ + // cffi end #ifdef __cplusplus } #endif +/** @} */ #endif // NIX_API_UTIL_H From f41a7e326ba917b3394c5368f90bb0cfa21db5c3 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Sun, 30 Jul 2023 16:46:20 +0200 Subject: [PATCH 085/168] nix_err_code: do not fail --- src/libutil/nix_api_util.cc | 5 +---- src/libutil/nix_api_util.h | 6 +++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/libutil/nix_api_util.cc b/src/libutil/nix_api_util.cc index 4f892637c..a5b575a3c 100644 --- a/src/libutil/nix_api_util.cc +++ b/src/libutil/nix_api_util.cc @@ -127,10 +127,7 @@ nix_err nix_err_info_msg(nix_c_context *context, n); } -nix_err nix_err_code(nix_c_context *context, - const nix_c_context *read_context) { - if (context) - context->last_err_code = NIX_OK; +nix_err nix_err_code(const nix_c_context *read_context) { return read_context->last_err_code; } diff --git a/src/libutil/nix_api_util.h b/src/libutil/nix_api_util.h index f626f2ccb..63854e4d8 100644 --- a/src/libutil/nix_api_util.h +++ b/src/libutil/nix_api_util.h @@ -253,12 +253,12 @@ nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context, * * Equivalent to reading the first field of the context. * - * @param[out] context optional, the context to store errors in if this function - * fails + * Does not fail + * * @param[in] read_context the context to retrieve the error message from * @return most recent error code stored in the context. */ -nix_err nix_err_code(nix_c_context *context, const nix_c_context *read_context); +nix_err nix_err_code(const nix_c_context *read_context); /** * @} From e58a9384c67eb8b229c309b86580a3f778535ce0 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 31 Jul 2023 09:02:28 +0200 Subject: [PATCH 086/168] nix_api_expr, nix_api_util: slightly improve documentation --- src/libexpr/nix_api_expr.h | 49 ++++++++++++++++++++------------------ src/libutil/nix_api_util.h | 21 ++++++++++------ 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h index f67a3d13f..1211c587f 100644 --- a/src/libexpr/nix_api_expr.h +++ b/src/libexpr/nix_api_expr.h @@ -9,18 +9,21 @@ * nix_libexpr_init(NULL); * * Store* store = nix_store_open(NULL, "dummy", NULL); - * State* state = nix_state_create(NULL, NULL /* empty NIX_PATH */, store); -*Value *value = nix_alloc_value(NULL, state); -**nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value); -*nix_value_force(NULL, state, value); -*printf("nix version: %s\n", nix_get_string(NULL, value)); -**nix_gc_decref(NULL, value); -*nix_state_free(state); -*nix_store_unref(store); -*return 0; -* -} -*@endcode *@{* / + * State* state = nix_state_create(NULL, NULL, store); // empty nix path + * Value *value = nix_alloc_value(NULL, state); + * + * nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value); + * nix_value_force(NULL, state, value); + * printf("nix version: %s\n", nix_get_string(NULL, value)); + * + * nix_gc_decref(NULL, value); + * nix_state_free(state); + * nix_store_unref(store); + * return 0; + * } + * @endcode + * @{ + */ /** @file * @brief Main entry for the libexpr C bindings */ @@ -29,19 +32,19 @@ #include "nix_api_util.h" #ifdef __cplusplus - extern "C" { +extern "C" { #endif - // cffi start +// cffi start - // Type definitions - /** - * @brief Represents a nix evaluator state. - * - * Multiple can be created for multi-threaded - * operation. - * @struct State - */ - typedef struct State State; // nix::EvalState +// Type definitions +/** + * @brief Represents a nix evaluator state. + * + * Multiple can be created for multi-threaded + * operation. + * @struct State + */ +typedef struct State State; // nix::EvalState /** * @brief Represents a nix value. * diff --git a/src/libutil/nix_api_util.h b/src/libutil/nix_api_util.h index 63854e4d8..98c837a84 100644 --- a/src/libutil/nix_api_util.h +++ b/src/libutil/nix_api_util.h @@ -22,11 +22,11 @@ extern "C" { /** @defgroup errors Handling errors * @brief Dealing with errors from the Nix side * - * To handle errors that can be returned from the Nix API - * nix_c_context can be passed any function that potentially returns an error. + * To handle errors that can be returned from the Nix API, + * a nix_c_context can be passed to any function that potentially returns an error. * * Error information will be stored in this context, and can be retrieved - * using nix_err_code, nix_err_msg. + * using nix_err_code and nix_err_msg. * * Passing NULL instead will cause the API to throw C++ errors. * @@ -101,10 +101,17 @@ typedef int nix_err; * @brief This object stores error state. * @struct nix_c_context * - * Passed as a first parameter to C functions that can fail, will store error - * information. Optional wherever it is used, passing NULL will throw a C++ - * exception instead. The first field is a nix_err, that can be read directly to - * check for errors. + * Passed as a first parameter to functions that can fail, to store error + * information. + * + * Optional wherever it can be used, passing NULL instead will throw a C++ + * exception. + * + * The struct is laid out so that it can also be cast to nix_err* to inspect + * directly: + * @code{.c} + * assert(*(nix_err*)ctx == NIX_OK); + * @endcode * @note These can be reused between different function calls, * but make sure not to use them for multiple calls simultaneously (which can * happen in callbacks). From e74d6c1b3d13f68ae78546f5372436bb12095d26 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 3 Aug 2023 15:45:39 +0200 Subject: [PATCH 087/168] nix_api_expr: document nix_value_force --- src/libexpr/nix_api_expr.h | 11 +++++++++++ src/libutil/nix_api_util.h | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h index 1211c587f..94eaa5a6c 100644 --- a/src/libexpr/nix_api_expr.h +++ b/src/libexpr/nix_api_expr.h @@ -97,9 +97,15 @@ nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, /** * @brief Forces the evaluation of a Nix value. * + * The Nix interpreter is lazy, and not-yet-evaluated Values can be + * of type NIX_TYPE_THUNK instead of their actual value. + * + * This function converts Values into their final type. + * * @param[out] context Optional, stores error information * @param[in] state The state of the evaluation. * @param[in,out] value The Nix value to force. + * @post values is not of type NIX_TYPE_THUNK * @return NIX_OK if the force operation was successful, an error code * otherwise. */ @@ -108,6 +114,11 @@ nix_err nix_value_force(nix_c_context *context, State *state, Value *value); /** * @brief Forces the deep evaluation of a Nix value. * + * Recursively calls nix_value_force + * + * @see nix_value_force + * @warning Calling this function on a recursive data structure will cause a + * stack overflow. * @param[out] context Optional, stores error information * @param[in] state The state of the evaluation. * @param[in,out] value The Nix value to force. diff --git a/src/libutil/nix_api_util.h b/src/libutil/nix_api_util.h index 98c837a84..4a7f6c4cd 100644 --- a/src/libutil/nix_api_util.h +++ b/src/libutil/nix_api_util.h @@ -23,7 +23,8 @@ extern "C" { * @brief Dealing with errors from the Nix side * * To handle errors that can be returned from the Nix API, - * a nix_c_context can be passed to any function that potentially returns an error. + * a nix_c_context can be passed to any function that potentially returns an + * error. * * Error information will be stored in this context, and can be retrieved * using nix_err_code and nix_err_msg. From f0afe7f9b9b523c8b03d08314b0334025e8bbef3 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 4 Aug 2023 17:44:34 +0200 Subject: [PATCH 088/168] nix_api_util: throw nix::error instead of new nix::Error for null ctx's --- src/libutil/nix_api_util.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libutil/nix_api_util.cc b/src/libutil/nix_api_util.cc index a5b575a3c..874ccdbb5 100644 --- a/src/libutil/nix_api_util.cc +++ b/src/libutil/nix_api_util.cc @@ -43,7 +43,7 @@ nix_err nix_context_error(nix_c_context *context) { nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg) { if (context == nullptr) { // todo last_err_code - throw new nix::Error("Nix C api error", msg); + throw nix::Error("Nix C api error: %s", msg); } context->last_err_code = err; context->last_err = msg; From c48b9b8a8373202bffd880984b08b76c72adca61 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 4 Aug 2023 17:44:56 +0200 Subject: [PATCH 089/168] nix_api_util: tests --- tests/unit/libutil/nix_api_util.cc | 125 +++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/unit/libutil/nix_api_util.cc diff --git a/tests/unit/libutil/nix_api_util.cc b/tests/unit/libutil/nix_api_util.cc new file mode 100644 index 000000000..26353fe84 --- /dev/null +++ b/tests/unit/libutil/nix_api_util.cc @@ -0,0 +1,125 @@ + +#include "config.hh" +#include "args.hh" +#include "nix_api_util.h" +#include "nix_api_util_internal.h" + +#include + +namespace nixC { + +class nix_api_util_context : public ::testing::Test { +protected: + static void SetUpTestSuite() { + nix_libutil_init(NULL); + } + void SetUp() override { + ctx = nix_c_context_create(); + }; + void TearDown() override { + nix_c_context_free(ctx); + ctx = nullptr; + } + nix_c_context* ctx; +}; + +TEST_F(nix_api_util_context, nix_context_error) { + std::string err_msg_ref; + try { + throw nix::Error("testing error"); + } catch(nix::Error &e) { + err_msg_ref = e.what(); + nix_context_error(ctx); + } + ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR); + ASSERT_EQ(ctx->name, "nix::Error"); + ASSERT_EQ(*ctx->last_err, err_msg_ref); + ASSERT_EQ(ctx->info->msg.str(), "testing error"); + + try { + throw std::runtime_error("testing exception"); + } catch(std::exception &e) { + err_msg_ref = e.what(); + nix_context_error(ctx); + } + ASSERT_EQ(ctx->last_err_code, NIX_ERR_UNKNOWN); + ASSERT_EQ(*ctx->last_err, err_msg_ref); +} + +TEST_F(nix_api_util_context, nix_set_err_msg) { + ASSERT_EQ(ctx->last_err_code, NIX_OK); + nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); + ASSERT_EQ(ctx->last_err_code, NIX_ERR_UNKNOWN); + ASSERT_EQ(*ctx->last_err, "unknown test error"); +} + +TEST(nix_api_util, nix_version_get) { + ASSERT_EQ(std::string(nix_version_get()), PACKAGE_VERSION); +} + +TEST_F(nix_api_util_context, nix_setting_get) { + // todo +} + +TEST_F(nix_api_util_context, nix_setting_set) { + // todo +} + +TEST_F(nix_api_util_context, nix_err_msg) { + // no error + EXPECT_THROW(nix_err_msg(NULL, ctx, NULL), nix::Error); + + // set error + nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); + + // basic usage + std::string err_msg = nix_err_msg(NULL, ctx, NULL); + ASSERT_EQ(err_msg, "unknown test error"); + + // advanced usage + unsigned int sz; + err_msg = nix_err_msg(NULL, ctx, &sz); + ASSERT_EQ(sz, err_msg.size()); +} + +TEST_F(nix_api_util_context, nix_err_info_msg) { + // no error + EXPECT_THROW(nix_err_info_msg(NULL, ctx, NULL, 256), nix::Error); + + try { + throw nix::Error("testing error"); + } catch(...) { + nix_context_error(ctx); + } + char buf[256]; + nix_err_info_msg(NULL, ctx, buf, 256); + ASSERT_EQ(std::string(buf), "testing error"); + + // should overflow + EXPECT_THROW(nix_err_info_msg(NULL, ctx, buf, 1), nix::Error); +} + +TEST_F(nix_api_util_context, nix_err_name) { + // no error + EXPECT_THROW(nix_err_name(NULL, ctx, NULL, 256), nix::Error); + + std::string err_msg_ref; + try { + throw nix::Error("testing error"); + } catch(...) { + nix_context_error(ctx); + } + char err_name[32]; + nix_err_name(NULL, ctx, err_name, 32); + ASSERT_EQ(std::string(err_name), "nix::Error"); + + // overflow + EXPECT_THROW(nix_err_name(NULL, ctx, err_name, 1), nix::Error); +} + +TEST_F(nix_api_util_context, nix_err_code) { + ASSERT_EQ(nix_err_code(ctx), NIX_OK); + nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); + ASSERT_EQ(nix_err_code(ctx), NIX_ERR_UNKNOWN); +} +} From 9cccb8bae0665a311c6d64e21b536c5e3a536115 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 7 Aug 2023 15:09:50 +0200 Subject: [PATCH 090/168] nix_api_expr: always force values before giving them to the user --- src/libexpr/nix_api_expr.cc | 2 ++ src/libexpr/nix_api_expr.h | 6 +++++- src/libexpr/nix_api_value.cc | 5 ++++- src/libexpr/nix_api_value.h | 3 ++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/libexpr/nix_api_expr.cc b/src/libexpr/nix_api_expr.cc index 880030380..a1c6d1acb 100644 --- a/src/libexpr/nix_api_expr.cc +++ b/src/libexpr/nix_api_expr.cc @@ -49,6 +49,7 @@ nix_err nix_expr_eval_from_string(nix_c_context *context, State *state, nix::Expr *parsedExpr = state->state.parseExprFromString( expr, state->state.rootPath(nix::CanonPath(path))); state->state.eval(parsedExpr, *(nix::Value *)value); + state->state.forceValue(*(nix::Value *)value, nix::noPos); } NIXC_CATCH_ERRS } @@ -60,6 +61,7 @@ nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, try { state->state.callFunction(*(nix::Value *)fn, *(nix::Value *)arg, *(nix::Value *)value, nix::noPos); + state->state.forceValue(*(nix::Value *)value, nix::noPos); } NIXC_CATCH_ERRS } diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/nix_api_expr.h index 94eaa5a6c..77632de7c 100644 --- a/src/libexpr/nix_api_expr.h +++ b/src/libexpr/nix_api_expr.h @@ -100,7 +100,11 @@ nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, * The Nix interpreter is lazy, and not-yet-evaluated Values can be * of type NIX_TYPE_THUNK instead of their actual value. * - * This function converts Values into their final type. + * This function converts these Values into their final type. + * + * @note You don't need this function for basic API usage, since all functions + * that return a value call it for you. The only place you will see a + * NIX_TYPE_THUNK is in the primop callback. * * @param[out] context Optional, stores error information * @param[in] state The state of the evaluation. diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index 6e02b3310..ead544a0b 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -201,7 +201,7 @@ ExternalValue *nix_get_external(nix_c_context *context, Value *value) { } Value *nix_get_list_byidx(nix_c_context *context, const Value *value, - unsigned int ix) { + State *state, unsigned int ix) { if (context) context->last_err_code = NIX_OK; try { @@ -209,6 +209,7 @@ Value *nix_get_list_byidx(nix_c_context *context, const Value *value, assert(v.type() == nix::nList); auto *p = v.listElems()[ix]; nix_gc_incref(nullptr, p); + state->state.forceValue(*p, nix::noPos); return (Value *)p; } NIXC_CATCH_ERRS_NULL @@ -225,6 +226,7 @@ Value *nix_get_attr_byname(nix_c_context *context, const Value *value, auto attr = v.attrs->get(s); if (attr) { nix_gc_incref(nullptr, attr->value); + state->state.forceValue(*attr->value, nix::noPos); return attr->value; } nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute"); @@ -258,6 +260,7 @@ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, const nix::Attr &a = (*v.attrs)[i]; *name = ((const std::string &)(state->state.symbols[a.name])).c_str(); nix_gc_incref(nullptr, a.value); + state->state.forceValue(*a.value, nix::noPos); return a.value; } NIXC_CATCH_ERRS_NULL diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index 110dd086f..f47dafa6a 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -178,11 +178,12 @@ ExternalValue *nix_get_external(nix_c_context *context, Value *); * Owned by the GC. Use nix_gc_decref when you're done with the pointer * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect + * @param[in] state nix evaluator state * @param[in] ix list element to get * @return value, NULL in case of errors */ Value *nix_get_list_byidx(nix_c_context *context, const Value *value, - unsigned int ix); + State *state, unsigned int ix); /** @brief Get an attr by name * * Owned by the GC. Use nix_gc_decref when you're done with the pointer From e891aac2e45aeb07f8ebf0304fd465a82aefafd8 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 7 Aug 2023 15:10:04 +0200 Subject: [PATCH 091/168] nix_api_value: add nix_get_attr_name_byidx get attr names without forcing --- src/libexpr/nix_api_value.cc | 12 ++++++++++++ src/libexpr/nix_api_value.h | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index ead544a0b..ceff036c9 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -266,6 +266,18 @@ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, NIXC_CATCH_ERRS_NULL } +const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value, + State *state, unsigned int i) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + const nix::Attr &a = (*v.attrs)[i]; + return ((const std::string &)(state->state.symbols[a.name])).c_str(); + } + NIXC_CATCH_ERRS_NULL +} + nix_err nix_set_bool(nix_c_context *context, Value *value, bool b) { if (context) context->last_err_code = NIX_OK; diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index f47dafa6a..08eb34fc5 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -207,6 +207,8 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value, State *state, const char *name); /** @brief Get an attribute by index in the sorted bindings + * + * Also gives you the name. * * Owned by the GC. Use nix_gc_decref when you're done with the pointer * @param[out] context Optional, stores error information @@ -218,6 +220,20 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value, */ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, State *state, unsigned int i, const char **name); + +/** @brief Get an attribute name by index in the sorted bindings + * + * Useful when you want the name but want to avoid evaluation. + * + * Owned by the nix State + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @param[in] state nix evaluator state + * @param[in] i attribute index + * @return name, NULL in case of errors + */ +const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value, + State *state, unsigned int i); /**@}*/ /** @name Setters */ From 713f10aeaaaf178cbbc85ad88a52e6684f517789 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 7 Aug 2023 15:33:07 +0200 Subject: [PATCH 092/168] nix_api_value: Add nix_register_primop to add builtins --- src/libexpr/nix_api_value.cc | 10 ++++++++++ src/libexpr/nix_api_value.h | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index ceff036c9..dae50352b 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -3,6 +3,7 @@ #include "eval.hh" #include "gc/gc.h" #include "globals.hh" +#include "primops.hh" #include "value.hh" #include "nix_api_expr.h" @@ -55,6 +56,15 @@ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, NIXC_CATCH_ERRS_NULL } +nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp) { + if (context) + context->last_err_code = NIX_OK; + try { + nix::RegisterPrimOp r(std::move(*((nix::PrimOp *)primOp))); + } + NIXC_CATCH_ERRS +} + Value *nix_alloc_value(nix_c_context *context, State *state) { if (context) context->last_err_code = NIX_OK; diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index 08eb34fc5..af1d211a3 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -88,6 +88,20 @@ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *v); */ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, const char *name, const char **args, const char *doc); + +/** @brief add a primop to builtins + * + * Only applies to new States. + * + * Moves your primop into the global + * registry, meaning your input primOp is no longer usable + * (but still possibly subject to garbage collection). + * + * @param[out] context Optional, stores error information + * @return primop, or null in case of errors + * + */ +nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp); /** @} */ // Function prototypes From dc0f7d8f9652ba4fb18096e93fd50bd63ec35ad7 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 7 Aug 2023 15:41:21 +0200 Subject: [PATCH 093/168] initPlugins: run nix_plugin_entry() on dlopen'd plugins Only when it exists. --- src/libstore/globals.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index b9ad8ac18..afb6039f3 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -346,6 +346,12 @@ void initPlugins() dlopen(file.c_str(), RTLD_LAZY | RTLD_LOCAL); if (!handle) throw Error("could not dynamically open plugin file '%s': %s", file, dlerror()); + + /* Older plugins use a statically initialized object to run their code. + Newer plugins can also export nix_plugin_entry() */ + void (*nix_plugin_entry)() = (void (*)())dlsym(handle, "nix_plugin_entry"); + if (nix_plugin_entry) + nix_plugin_entry(); } } From df9401eb4efd63d13392cf9081447d537e6776fa Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 7 Aug 2023 15:54:46 +0200 Subject: [PATCH 094/168] nix_api_store: add nix_init_plugins --- src/libstore/nix_api_store.cc | 9 +++++++++ src/libstore/nix_api_store.h | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/libstore/nix_api_store.cc b/src/libstore/nix_api_store.cc index c81ad49ee..0cc1d1983 100644 --- a/src/libstore/nix_api_store.cc +++ b/src/libstore/nix_api_store.cc @@ -20,6 +20,15 @@ nix_err nix_libstore_init(nix_c_context *context) { NIXC_CATCH_ERRS } +nix_err nix_init_plugins(nix_c_context *context) { + if (context) + context->last_err_code = NIX_OK; + try { + nix::initPlugins(); + } + NIXC_CATCH_ERRS +} + Store *nix_store_open(nix_c_context *context, const char *uri, const char ***params) { if (context) diff --git a/src/libstore/nix_api_store.h b/src/libstore/nix_api_store.h index bc01f0ad2..b15e161b3 100644 --- a/src/libstore/nix_api_store.h +++ b/src/libstore/nix_api_store.h @@ -35,6 +35,17 @@ typedef struct StorePath StorePath; */ nix_err nix_libstore_init(nix_c_context *context); +/** + * @brief Loads plugins specified in the settings + * + * Call this once, after calling your desired init functions and setting + * relevant settings. + * + * @param[out] context Optional, stores error information + * @return NIX_OK if the initialization was successful, an error code otherwise. + */ +nix_err nix_init_plugins(nix_c_context *context); + /** * @brief Open a nix store * @param[out] context Optional, stores error information From e642bbc2a79bfd2339e109cddc8db51f2de07342 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 7 Aug 2023 17:16:58 +0200 Subject: [PATCH 095/168] C API: move to src/lib*/c/ --- Makefile | 3 +++ doc/external-api/doxygen.cfg.in | 6 +++--- local.mk | 2 +- src/libexpr/c/local.mk | 19 +++++++++++++++++++ src/libexpr/c/nix-expr-c.pc.in | 10 ++++++++++ src/libexpr/{ => c}/nix_api_expr.cc | 0 src/libexpr/{ => c}/nix_api_expr.h | 0 src/libexpr/{ => c}/nix_api_expr_internal.h | 0 src/libexpr/{ => c}/nix_api_external.cc | 0 src/libexpr/{ => c}/nix_api_external.h | 0 src/libexpr/{ => c}/nix_api_value.cc | 0 src/libexpr/{ => c}/nix_api_value.h | 0 src/libstore/c/local.mk | 17 +++++++++++++++++ src/libstore/c/nix-store-c.pc.in | 9 +++++++++ src/libstore/{ => c}/nix_api_store.cc | 0 src/libstore/{ => c}/nix_api_store.h | 0 src/libstore/{ => c}/nix_api_store_internal.h | 0 src/libutil/c/local.mk | 15 +++++++++++++++ src/libutil/{ => c}/nix_api_util.cc | 0 src/libutil/{ => c}/nix_api_util.h | 0 src/libutil/{ => c}/nix_api_util_internal.h | 0 tests/unit/libutil/local.mk | 2 +- 22 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/libexpr/c/local.mk create mode 100644 src/libexpr/c/nix-expr-c.pc.in rename src/libexpr/{ => c}/nix_api_expr.cc (100%) rename src/libexpr/{ => c}/nix_api_expr.h (100%) rename src/libexpr/{ => c}/nix_api_expr_internal.h (100%) rename src/libexpr/{ => c}/nix_api_external.cc (100%) rename src/libexpr/{ => c}/nix_api_external.h (100%) rename src/libexpr/{ => c}/nix_api_value.cc (100%) rename src/libexpr/{ => c}/nix_api_value.h (100%) create mode 100644 src/libstore/c/local.mk create mode 100644 src/libstore/c/nix-store-c.pc.in rename src/libstore/{ => c}/nix_api_store.cc (100%) rename src/libstore/{ => c}/nix_api_store.h (100%) rename src/libstore/{ => c}/nix_api_store_internal.h (100%) create mode 100644 src/libutil/c/local.mk rename src/libutil/{ => c}/nix_api_util.cc (100%) rename src/libutil/{ => c}/nix_api_util.h (100%) rename src/libutil/{ => c}/nix_api_util_internal.h (100%) diff --git a/Makefile b/Makefile index 4f60d0d8b..d9efc8154 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,9 @@ makefiles = \ src/libexpr/local.mk \ src/libcmd/local.mk \ src/nix/local.mk \ + src/libutil/c/local.mk \ + src/libstore/c/local.mk \ + src/libexpr/c/local.mk \ src/resolve-system-dependencies/local.mk \ scripts/local.mk \ misc/bash/local.mk \ diff --git a/doc/external-api/doxygen.cfg.in b/doc/external-api/doxygen.cfg.in index 19350b2c6..d78188900 100644 --- a/doc/external-api/doxygen.cfg.in +++ b/doc/external-api/doxygen.cfg.in @@ -36,9 +36,9 @@ GENERATE_LATEX = NO # so they can expand variables despite configure variables. INPUT = \ - src/libutil \ - src/libexpr \ - src/libstore + src/libutil/c \ + src/libexpr/c \ + src/libstore/c FILE_PATTERNS = nix_api_*.h diff --git a/local.mk b/local.mk index f48eb63ee..9a1ed50df 100644 --- a/local.mk +++ b/local.mk @@ -2,7 +2,7 @@ GLOBAL_CXXFLAGS += -Wno-deprecated-declarations -Werror=switch # Allow switch-enum to be overridden for files that do not support it, usually because of dependency headers. ERROR_SWITCH_ENUM = -Werror=switch-enum -$(foreach i, config.h $(wildcard src/lib*/*.hh) $(wildcard src/lib*/*.h), \ +$(foreach i, config.h $(wildcard src/lib*/*.hh) $(wildcard src/lib*/*.h $(filter-out %_internal.h, $(wildcard src/lib*/c/*.h))), \ $(eval $(call install-file-in, $(i), $(includedir)/nix, 0644))) $(GCH): src/libutil/util.hh config.h diff --git a/src/libexpr/c/local.mk b/src/libexpr/c/local.mk new file mode 100644 index 000000000..d2f01c0a9 --- /dev/null +++ b/src/libexpr/c/local.mk @@ -0,0 +1,19 @@ +libraries += libexprc + +libexprc_NAME = libnixexprc + +libexprc_DIR := $(d) + +libexprc_SOURCES := \ + $(wildcard $(d)/*.cc) \ + +libexprc_CXXFLAGS += -I src/libutil -Isrc/libfetchers -I src/libstore -I src/libstorec -I src/libexpr -I src/libutil/c -I src/libstore/c + +libexprc_LIBS = libutil libutilc libstorec libexpr + +libexprc_LDFLAGS += -pthread + +$(eval $(call install-file-in, $(d)/nix-expr-c.pc, $(libdir)/pkgconfig, 0644)) + +libexprc_FORCE_INSTALL := 1 + diff --git a/src/libexpr/c/nix-expr-c.pc.in b/src/libexpr/c/nix-expr-c.pc.in new file mode 100644 index 000000000..897773f20 --- /dev/null +++ b/src/libexpr/c/nix-expr-c.pc.in @@ -0,0 +1,10 @@ +prefix=@prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: Nix +Description: Nix Package Manager - C API +Version: @PACKAGE_VERSION@ +Requires: nix-store-c +Libs: -L${libdir} -lnixexprc +Cflags: -I${includedir}/nix diff --git a/src/libexpr/nix_api_expr.cc b/src/libexpr/c/nix_api_expr.cc similarity index 100% rename from src/libexpr/nix_api_expr.cc rename to src/libexpr/c/nix_api_expr.cc diff --git a/src/libexpr/nix_api_expr.h b/src/libexpr/c/nix_api_expr.h similarity index 100% rename from src/libexpr/nix_api_expr.h rename to src/libexpr/c/nix_api_expr.h diff --git a/src/libexpr/nix_api_expr_internal.h b/src/libexpr/c/nix_api_expr_internal.h similarity index 100% rename from src/libexpr/nix_api_expr_internal.h rename to src/libexpr/c/nix_api_expr_internal.h diff --git a/src/libexpr/nix_api_external.cc b/src/libexpr/c/nix_api_external.cc similarity index 100% rename from src/libexpr/nix_api_external.cc rename to src/libexpr/c/nix_api_external.cc diff --git a/src/libexpr/nix_api_external.h b/src/libexpr/c/nix_api_external.h similarity index 100% rename from src/libexpr/nix_api_external.h rename to src/libexpr/c/nix_api_external.h diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc similarity index 100% rename from src/libexpr/nix_api_value.cc rename to src/libexpr/c/nix_api_value.cc diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/c/nix_api_value.h similarity index 100% rename from src/libexpr/nix_api_value.h rename to src/libexpr/c/nix_api_value.h diff --git a/src/libstore/c/local.mk b/src/libstore/c/local.mk new file mode 100644 index 000000000..35e2bd63d --- /dev/null +++ b/src/libstore/c/local.mk @@ -0,0 +1,17 @@ +libraries += libstorec + +libstorec_NAME = libnixstorec + +libstorec_DIR := $(d) + +libstorec_SOURCES := $(wildcard $(d)/*.cc) + +libstorec_LIBS = libutil libstore libutilc + +libstorec_LDFLAGS += -pthread + +libstorec_CXXFLAGS += -I src/libutil -I src/libstore -I src/libutil/c + +$(eval $(call install-file-in, $(d)/nix-store-c.pc, $(libdir)/pkgconfig, 0644)) + +libstorec_FORCE_INSTALL := 1 diff --git a/src/libstore/c/nix-store-c.pc.in b/src/libstore/c/nix-store-c.pc.in new file mode 100644 index 000000000..563bd2f94 --- /dev/null +++ b/src/libstore/c/nix-store-c.pc.in @@ -0,0 +1,9 @@ +prefix=@prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: Nix +Description: Nix Package Manager - C API +Version: @PACKAGE_VERSION@ +Libs: -L${libdir} -lnixstorec -lnixutilc +Cflags: -I${includedir}/nix diff --git a/src/libstore/nix_api_store.cc b/src/libstore/c/nix_api_store.cc similarity index 100% rename from src/libstore/nix_api_store.cc rename to src/libstore/c/nix_api_store.cc diff --git a/src/libstore/nix_api_store.h b/src/libstore/c/nix_api_store.h similarity index 100% rename from src/libstore/nix_api_store.h rename to src/libstore/c/nix_api_store.h diff --git a/src/libstore/nix_api_store_internal.h b/src/libstore/c/nix_api_store_internal.h similarity index 100% rename from src/libstore/nix_api_store_internal.h rename to src/libstore/c/nix_api_store_internal.h diff --git a/src/libutil/c/local.mk b/src/libutil/c/local.mk new file mode 100644 index 000000000..fe156e7f3 --- /dev/null +++ b/src/libutil/c/local.mk @@ -0,0 +1,15 @@ +libraries += libutilc + +libutilc_NAME = libnixutilc + +libutilc_DIR := $(d) + +libutilc_SOURCES := $(wildcard $(d)/*.cc) + +libutilc_CXXFLAGS += -I src/libutil + +libutilc_LIBS = libutil + +libutilc_LDFLAGS += -pthread + +libutilc_FORCE_INSTALL := 1 diff --git a/src/libutil/nix_api_util.cc b/src/libutil/c/nix_api_util.cc similarity index 100% rename from src/libutil/nix_api_util.cc rename to src/libutil/c/nix_api_util.cc diff --git a/src/libutil/nix_api_util.h b/src/libutil/c/nix_api_util.h similarity index 100% rename from src/libutil/nix_api_util.h rename to src/libutil/c/nix_api_util.h diff --git a/src/libutil/nix_api_util_internal.h b/src/libutil/c/nix_api_util_internal.h similarity index 100% rename from src/libutil/nix_api_util_internal.h rename to src/libutil/c/nix_api_util_internal.h diff --git a/tests/unit/libutil/local.mk b/tests/unit/libutil/local.mk index 536607322..0d0acd4c0 100644 --- a/tests/unit/libutil/local.mk +++ b/tests/unit/libutil/local.mk @@ -22,7 +22,7 @@ libutil-tests_EXTRA_INCLUDES = \ libutil-tests_CXXFLAGS += $(libutil-tests_EXTRA_INCLUDES) -libutil-tests_LIBS = libutil-test-support libutil +libutil-tests_LIBS = libutil-test-support libutil libutilc libutil-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) From 3b41830a9609e8f7c0c92317504a401b091c146e Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 7 Aug 2023 17:54:31 +0200 Subject: [PATCH 096/168] docs/external-api: write main page --- doc/external-api/README.md | 74 +++++++++++++++++++++++++++++++++ doc/external-api/doxygen.cfg.in | 7 +++- 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 doc/external-api/README.md diff --git a/doc/external-api/README.md b/doc/external-api/README.md new file mode 100644 index 000000000..20015e7a9 --- /dev/null +++ b/doc/external-api/README.md @@ -0,0 +1,74 @@ +# Getting started + +There are two ways to interface with nix: embedding it, or as a plugin. Embedding means you link one of the nix libraries in your program and use it from there, while being a plugin means you make a library that gets loaded by the nix evaluator, specified through a configuration option. + +# Embedding the Nix Evaluator + +These examples don't include error handling. +See the [Handling errors](@ref errors) section for more information. + +**main.c:** +```C +#include +#include +#include +#include + +int main() { + nix_libexpr_init(NULL); + + Store* store = nix_store_open(NULL, "dummy://", NULL); + State* state = nix_state_create(NULL, NULL, store); // empty nix path + Value *value = nix_alloc_value(NULL, state); + + nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value); + nix_value_force(NULL, state, value); + printf("nix version: %s\n", nix_get_string(NULL, value)); + + nix_gc_decref(NULL, value); + nix_state_free(state); + nix_store_unref(store); + return 0; +} +``` + +**Usage:** +``` +$ gcc main.c $(pkg-config nix-expr-c --libs --cflags) -o main +$ ./main +nix version 1.2.3 +``` + + +# Writing a Nix Plugin + +**plugin.c:** +```C +#include +#include +#include + +void increment(State* state, int pos, Value** args, Value* v) { + nix_value_force(NULL, state, args[0]); + if (nix_get_type(NULL, args[0]) == NIX_TYPE_INT) { + nix_set_int(NULL, v, nix_get_int(NULL, args[0]) + 1); + } else { + nix_set_null(NULL, v); + } +} + +void nix_plugin_entry() { + const char* args[] = {"n", NULL}; + PrimOp *p = nix_alloc_primop(NULL, increment, 1, "increment", args, "Example nix plugin function: increments an int"); + nix_register_primop(NULL, p); + nix_gc_decref(NULL, p); +} +``` + +**Usage:** +``` +$ gcc plugin.c $(pkg-config nix-expr-c --libs --cflags) -shared -o plugin.so +$ nix --plugin-files ./plugin.so repl +nix-repl> builtins.increment 1 +2 +``` diff --git a/doc/external-api/doxygen.cfg.in b/doc/external-api/doxygen.cfg.in index d78188900..c9f2e4b7b 100644 --- a/doc/external-api/doxygen.cfg.in +++ b/doc/external-api/doxygen.cfg.in @@ -38,9 +38,10 @@ GENERATE_LATEX = NO INPUT = \ src/libutil/c \ src/libexpr/c \ - src/libstore/c + src/libstore/c \ + doc/external-api/README.md -FILE_PATTERNS = nix_api_*.h +FILE_PATTERNS = nix_api_*.h *.md # The INCLUDE_PATH tag can be used to specify one or more directories that # contain include files that are not input files but should be processed by the @@ -52,3 +53,5 @@ INCLUDE_PATH = @RAPIDCHECK_HEADERS@ EXCLUDE_PATTERNS = *_internal.h GENERATE_TREEVIEW = YES OPTIMIZE_OUTPUT_FOR_C = YES + +USE_MDFILE_AS_MAINPAGE = doc/external-api/README.md From 40f5d48d3c74e562a4f031b822bb73bdefdf4144 Mon Sep 17 00:00:00 2001 From: Yorick Date: Mon, 28 Aug 2023 16:20:46 +0200 Subject: [PATCH 097/168] Apply documentation suggestions from code review Co-authored-by: Valentin Gagarin --- doc/external-api/README.md | 30 ++++++++++++++++++++++------ doc/external-api/doxygen.cfg.in | 2 +- src/libexpr/c/nix-expr-c.pc.in | 2 +- src/libexpr/c/nix_api_expr.h | 34 +++++++++++++++++--------------- src/libexpr/c/nix_api_external.h | 2 +- src/libexpr/c/nix_api_value.h | 19 +++++++++--------- src/libstore/c/nix-store-c.pc.in | 2 +- src/libstore/c/nix_api_store.h | 18 ++++++++--------- 8 files changed, 65 insertions(+), 44 deletions(-) diff --git a/doc/external-api/README.md b/doc/external-api/README.md index 20015e7a9..71a181ede 100644 --- a/doc/external-api/README.md +++ b/doc/external-api/README.md @@ -1,11 +1,27 @@ # Getting started -There are two ways to interface with nix: embedding it, or as a plugin. Embedding means you link one of the nix libraries in your program and use it from there, while being a plugin means you make a library that gets loaded by the nix evaluator, specified through a configuration option. +These C bindings are **experimental** at the moment, which means they can still change any time or get removed again, but the plan is to provide a stable external C API to the Nix language and the Nix store. + +The language library allows evaluating Nix expressions and interacting with Nix language values. +The Nix store API is still rudimentary, and only allows initialising and connecting to a store for the Nix language evaluator to interact with. + +Currently there are two ways to interface with the Nix language evaluator programmatically: +1. Embedding the evaluator +2. Writing language plug-ins + +Embedding means you link the Nix C libraries in your program and use them from there. +Adding a plug-in means you make a library that gets loaded by the Nix language evaluator, specified through a configuration option. + +Many of the components and mechanisms involved are not yet documented, therefore please refer to the [Nix source code](https://github.com/NixOS/nix/) for details. +Additions to in-code documentation and the reference manual are highly appreciated. + + +The following examples, for simplicity, don't include error handling. +See the [Handling errors](@ref errors) section for more information. # Embedding the Nix Evaluator -These examples don't include error handling. -See the [Handling errors](@ref errors) section for more information. +In this example we programmatically start the Nix language evaluator with a dummy store (that has no store paths and cannot be written to), and evaluate the Nix expression `builtins.nixVersion`. **main.c:** ```C @@ -18,7 +34,7 @@ int main() { nix_libexpr_init(NULL); Store* store = nix_store_open(NULL, "dummy://", NULL); - State* state = nix_state_create(NULL, NULL, store); // empty nix path + State* state = nix_state_create(NULL, NULL, store); // empty search path (NIX_PATH) Value *value = nix_alloc_value(NULL, state); nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value); @@ -40,7 +56,9 @@ nix version 1.2.3 ``` -# Writing a Nix Plugin +# Writing a Nix language plug-in +In this example we add a custom primitive operation (*primop*) to `builtins`. +It will increment the argument if it is an integer and return `null` otherwise. **plugin.c:** ```C @@ -59,7 +77,7 @@ void increment(State* state, int pos, Value** args, Value* v) { void nix_plugin_entry() { const char* args[] = {"n", NULL}; - PrimOp *p = nix_alloc_primop(NULL, increment, 1, "increment", args, "Example nix plugin function: increments an int"); + PrimOp *p = nix_alloc_primop(NULL, increment, 1, "increment", args, "Example custom built-in function: increments an integer"); nix_register_primop(NULL, p); nix_gc_decref(NULL, p); } diff --git a/doc/external-api/doxygen.cfg.in b/doc/external-api/doxygen.cfg.in index c9f2e4b7b..454514935 100644 --- a/doc/external-api/doxygen.cfg.in +++ b/doc/external-api/doxygen.cfg.in @@ -18,7 +18,7 @@ PROJECT_NUMBER = @PACKAGE_VERSION@ # for a project that appears at the top of each page and should give viewer a # quick idea about the purpose of the project. Keep the description short. -PROJECT_BRIEF = "Nix, the purely functional package manager; stable external interfaces" +PROJECT_BRIEF = "Nix, the purely functional package manager: C API (experimental)" # If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output. # The default value is: YES. diff --git a/src/libexpr/c/nix-expr-c.pc.in b/src/libexpr/c/nix-expr-c.pc.in index 897773f20..06897064d 100644 --- a/src/libexpr/c/nix-expr-c.pc.in +++ b/src/libexpr/c/nix-expr-c.pc.in @@ -3,7 +3,7 @@ libdir=@libdir@ includedir=@includedir@ Name: Nix -Description: Nix Package Manager - C API +Description: Nix Language Evaluator - C API Version: @PACKAGE_VERSION@ Requires: nix-store-c Libs: -L${libdir} -lnixexprc diff --git a/src/libexpr/c/nix_api_expr.h b/src/libexpr/c/nix_api_expr.h index 77632de7c..926d0f7a0 100644 --- a/src/libexpr/c/nix_api_expr.h +++ b/src/libexpr/c/nix_api_expr.h @@ -1,7 +1,7 @@ #ifndef NIX_API_EXPR_H #define NIX_API_EXPR_H /** @defgroup libexpr libexpr - * @brief Bindings to the Nix evaluator + * @brief Bindings to the Nix language evaluator * * Example (without error handling): * @code{.c} @@ -38,17 +38,18 @@ extern "C" { // Type definitions /** - * @brief Represents a nix evaluator state. + * @brief Represents a state of the Nix language evaluator. * - * Multiple can be created for multi-threaded + * Multiple states can be created for multi-threaded * operation. * @struct State + * @see nix_state_create */ typedef struct State State; // nix::EvalState /** - * @brief Represents a nix value. + * @brief Represents a value in the Nix language. * - * Owned by the GC. + * Owned by the garbage collector. * @struct Value * @see value_manip */ @@ -56,7 +57,7 @@ typedef void Value; // nix::Value // Function prototypes /** - * @brief Initializes the Nix expression evaluator. + * @brief Initialize the Nix language evaluator. * * This function should be called before creating a State. * This function can be called multiple times. @@ -73,6 +74,7 @@ nix_err nix_libexpr_init(nix_c_context *context); * @param[in] state The state of the evaluation. * @param[in] expr The Nix expression to parse. * @param[in] path The file path to associate with the expression. + * This is required for expressions that contain relative paths (such as `./.`) that are resolved relative to the given directory. * @param[out] value The result of the evaluation. You should allocate this * yourself. * @return NIX_OK if the evaluation was successful, an error code otherwise. @@ -109,7 +111,7 @@ nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, * @param[out] context Optional, stores error information * @param[in] state The state of the evaluation. * @param[in,out] value The Nix value to force. - * @post values is not of type NIX_TYPE_THUNK + * @post value is not of type NIX_TYPE_THUNK * @return NIX_OK if the force operation was successful, an error code * otherwise. */ @@ -133,10 +135,10 @@ nix_err nix_value_force_deep(nix_c_context *context, State *state, Value *value); /** - * @brief Creates a new Nix state. + * @brief Create a new Nix language evaluator state. * * @param[out] context Optional, stores error information - * @param[in] searchPath The NIX_PATH. + * @param[in] searchPath Array of strings corresponding to entries in NIX_PATH. * @param[in] store The Nix store to use. * @return A new Nix state or NULL on failure. */ @@ -155,28 +157,28 @@ void nix_state_free(State *state); /** @addtogroup GC * @brief Reference counting and garbage collector operations * - * Nix's evaluator uses a garbage collector. To ease C interop, we implement + * The Nix language evaluator uses a garbage collector. To ease C interop, we implement * a reference counting scheme, where objects will be deallocated * when there are no references from the Nix side, and the reference count kept * by the C API reaches `0`. * * Functions returning a garbage-collected object will automatically increase * the refcount for you. You should make sure to call `nix_gc_decref` when - * you're done. + * you're done with a value returned by the evaluator. * @{ */ /** - * @brief Increase the GC refcount. + * @brief Increment the garbage collector reference counter for the given object. * - * The nix C api keeps alive objects by refcounting. - * When you're done with a refcounted pointer, call nix_gc_decref. + * The Nix language evaluator C API keeps track of alive objects by reference counting. + * When you're done with a refcounted pointer, call nix_gc_decref(). * * @param[out] context Optional, stores error information * @param[in] object The object to keep alive */ nix_err nix_gc_incref(nix_c_context *context, const void *object); /** - * @brief Decrease the GC refcount + * @brief Decrement the garbage collector reference counter for the given object * * @param[out] context Optional, stores error information * @param[in] object The object to stop referencing @@ -193,7 +195,7 @@ void nix_gc_now(); /** * @brief Register a callback that gets called when the object is garbage * collected. - * @note objects can only have a single finalizer. This function overwrites + * @note Objects can only have a single finalizer. This function overwrites existing values * silently. * @param[in] obj the object to watch * @param[in] cd the data to pass to the finalizer diff --git a/src/libexpr/c/nix_api_external.h b/src/libexpr/c/nix_api_external.h index 692f8000c..3dc9d79de 100644 --- a/src/libexpr/c/nix_api_external.h +++ b/src/libexpr/c/nix_api_external.h @@ -22,7 +22,7 @@ extern "C" { // cffi start /** - * @brief Represents a string owned by nix. + * @brief Represents a string owned by the Nix language evaluator. * @see nix_set_owned_string */ typedef struct nix_string_return nix_string_return; diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index af1d211a3..a4e643317 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -73,9 +73,10 @@ typedef struct ExternalValue ExternalValue; */ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *v); -/** @brief Allocate a primop +/** @brief Allocate a PrimOp * - * Owned by the GC. Use nix_gc_decref when you're done with the pointer + * Owned by the garbage collector. + * Use nix_gc_decref() when you're done with the returned PrimOp. * * @param[out] context Optional, stores error information * @param[in] fun callback @@ -89,12 +90,12 @@ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *v); PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, const char *name, const char **args, const char *doc); -/** @brief add a primop to builtins +/** @brief add a primop to the `builtins` attribute set * - * Only applies to new States. + * Only applies to States created after this call. * - * Moves your primop into the global - * registry, meaning your input primOp is no longer usable + * Moves your PrimOp into the global evaluator + * registry, meaning your input PrimOp pointer is no longer usable * (but still possibly subject to garbage collection). * * @param[out] context Optional, stores error information @@ -108,7 +109,7 @@ nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp); /** @brief Allocate a Nix value * - * Owned by the GC. Use nix_gc_decref when you're done with the pointer + * Owned by the GC. Use nix_gc_decref() when you're done with the pointer * @param[out] context Optional, stores error information * @param[in] state nix evaluator state * @return value, or null in case of errors @@ -116,7 +117,7 @@ nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp); */ Value *nix_alloc_value(nix_c_context *context, State *state); /** @addtogroup value_manip Manipulating values - * @brief Functions to inspect and change nix Value's + * @brief Functions to inspect and change Nix language values, represented by Value. * @{ */ /** @name Getters @@ -128,7 +129,7 @@ Value *nix_alloc_value(nix_c_context *context, State *state); * @return type of nix value */ ValueType nix_get_type(nix_c_context *context, const Value *value); -/** @brief Get type name of value +/** @brief Get type name of value as defined in the evaluator * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return type name, owned string diff --git a/src/libstore/c/nix-store-c.pc.in b/src/libstore/c/nix-store-c.pc.in index 563bd2f94..de3c7b4c6 100644 --- a/src/libstore/c/nix-store-c.pc.in +++ b/src/libstore/c/nix-store-c.pc.in @@ -3,7 +3,7 @@ libdir=@libdir@ includedir=@includedir@ Name: Nix -Description: Nix Package Manager - C API +Description: Nix Store - C API Version: @PACKAGE_VERSION@ Libs: -L${libdir} -lnixstorec -lnixutilc Cflags: -I${includedir}/nix diff --git a/src/libstore/c/nix_api_store.h b/src/libstore/c/nix_api_store.h index b15e161b3..36d712f01 100644 --- a/src/libstore/c/nix_api_store.h +++ b/src/libstore/c/nix_api_store.h @@ -19,9 +19,9 @@ extern "C" { #endif // cffi start -/** @brief reference to a nix store */ +/** @brief Reference to a Nix store */ typedef struct Store Store; -/** @brief nix store path */ +/** @brief Nix store path */ typedef struct StorePath StorePath; /** @@ -79,9 +79,9 @@ nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest, // returns: owned StorePath* /** - * @brief parse a nix store path into a StorePath + * @brief Parse a Nix store path into a StorePath * - * Don't forget to free this path using nix_store_path_free + * @note Don't forget to free this path using nix_store_path_free()! * @param[out] context Optional, stores error information * @param[in] store nix store reference * @param[in] path Path string to parse, copied @@ -90,7 +90,7 @@ nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest, StorePath *nix_store_parse_path(nix_c_context *context, Store *store, const char *path); -/** @brief Deallocate a nix StorePath +/** @brief Deallocate a StorePath * * Does not fail. * @param[in] p the path to free @@ -98,9 +98,9 @@ StorePath *nix_store_parse_path(nix_c_context *context, Store *store, void nix_store_path_free(StorePath *p); /** - * @brief check if a storepath is valid (exists in the store) + * @brief Check if a StorePath is valid (i.e. that corresponding store object and its closure of references exists in the store) * @param[out] context Optional, stores error information - * @param[in] store nix store reference + * @param[in] store Nix Store reference * @param[in] path Path to check * @return true or false, error info in context */ @@ -109,12 +109,12 @@ bool nix_store_is_valid_path(nix_c_context *context, Store *store, // nix_err nix_store_ensure(Store*, const char*); // nix_err nix_store_build_paths(Store*); /** - * @brief Build a nix store path + * @brief Realise a Nix store path * * Blocking, calls cb once for each built output * * @param[out] context Optional, stores error information - * @param[in] store nix store reference + * @param[in] store Nix Store reference * @param[in] path Path to build * @param[in] userdata data to pass to every callback invocation * @param[in] cb called for every built output From 5d82d6e7336d595ab2a3354547641eadc158c6e7 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Fri, 11 Aug 2023 11:49:10 +0200 Subject: [PATCH 098/168] nix_api: fix missing includes in headers Forward declaration doesn't work here, since we define classes that contain the objects --- src/libexpr/c/nix_api_expr_internal.h | 7 ++----- src/libutil/c/nix_api_util_internal.h | 7 +++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/libexpr/c/nix_api_expr_internal.h b/src/libexpr/c/nix_api_expr_internal.h index 3ee3b18af..bae50cf59 100644 --- a/src/libexpr/c/nix_api_expr_internal.h +++ b/src/libexpr/c/nix_api_expr_internal.h @@ -1,11 +1,8 @@ #ifndef NIX_API_EXPR_INTERNAL_H #define NIX_API_EXPR_INTERNAL_H -// forward declaration -namespace nix { -class EvalState; -class BindingsBuilder; -}; // namespace nix +#include "eval.hh" +#include "attr-set.hh" struct State { nix::EvalState state; diff --git a/src/libutil/c/nix_api_util_internal.h b/src/libutil/c/nix_api_util_internal.h index 9ece28588..013d3bbbb 100644 --- a/src/libutil/c/nix_api_util_internal.h +++ b/src/libutil/c/nix_api_util_internal.h @@ -2,11 +2,10 @@ #define NIX_API_UTIL_INTERNAL_H #include +#include -// forward declaration -namespace nix { -class Error; -}; +#include "error.hh" +#include "nix_api_util.h" struct nix_c_context { nix_err last_err_code = NIX_OK; From 9d380c0f7649922207673fae4fda2e3edf8ee742 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 28 Aug 2023 16:42:59 +0200 Subject: [PATCH 099/168] C API: clarify some documentation --- src/libexpr/c/nix_api_expr.h | 8 +++++--- src/libexpr/c/nix_api_value.h | 12 ++++++------ src/libstore/c/nix_api_store.cc | 6 +++--- src/libstore/c/nix_api_store.h | 8 ++++---- src/libstore/globals.hh | 7 ++++--- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/libexpr/c/nix_api_expr.h b/src/libexpr/c/nix_api_expr.h index 926d0f7a0..a6b902f96 100644 --- a/src/libexpr/c/nix_api_expr.h +++ b/src/libexpr/c/nix_api_expr.h @@ -59,8 +59,9 @@ typedef void Value; // nix::Value /** * @brief Initialize the Nix language evaluator. * - * This function should be called before creating a State. - * This function can be called multiple times. + * This function must be called at least once, + * at some point before constructing a State for the first time. + * This function can be called multiple times, and is idempotent. * * @param[out] context Optional, stores error information * @return NIX_OK if the initialization was successful, an error code otherwise. @@ -106,7 +107,8 @@ nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, * * @note You don't need this function for basic API usage, since all functions * that return a value call it for you. The only place you will see a - * NIX_TYPE_THUNK is in the primop callback. + * NIX_TYPE_THUNK is in the arguments that are passed to a PrimOp function + * you supplied to nix_alloc_primop. * * @param[out] context Optional, stores error information * @param[in] state The state of the evaluation. diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index a4e643317..f4d9c9584 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -66,12 +66,12 @@ typedef struct ExternalValue ExternalValue; */ /** @brief Function pointer for primops * @param[in] state Evaluator state - * @param[in] pos position of function call - * @param[in] args list of arguments - * @param[out] v return value + * @param[in] pos Call position, opaque index into the state's position table. + * @param[in] args list of arguments. Note that these can be thunks and should be forced using nix_value_force before use. + * @param[out] ret return value * @see nix_alloc_primop, nix_set_primop */ -typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *v); +typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *ret); /** @brief Allocate a PrimOp * @@ -80,9 +80,9 @@ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *v); * * @param[out] context Optional, stores error information * @param[in] fun callback - * @param[in] arity expected amount of function arguments + * @param[in] arity expected number of function arguments * @param[in] name function name - * @param[in] args array of argument names + * @param[in] args array of argument names, NULL-terminated * @param[in] doc optional, documentation for this primop * @return primop, or null in case of errors * @see nix_set_primop diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index 0cc1d1983..7b5391034 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -103,7 +103,7 @@ StorePath *nix_store_parse_path(nix_c_context *context, Store *store, nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, void *userdata, - void (*iter)(void *userdata, const char *, + void (*callback)(void *userdata, const char *, const char *)) { if (context) context->last_err_code = NIX_OK; @@ -114,11 +114,11 @@ nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, .outputs = nix::OutputsSpec::All{}, }, }); - if (iter) { + if (callback) { for (auto &[outputName, outputPath] : store->ptr->queryDerivationOutputMap(path->path)) { auto op = store->ptr->printStorePath(outputPath); - iter(userdata, outputName.c_str(), op.c_str()); + callback(userdata, outputName.c_str(), op.c_str()); } } } diff --git a/src/libstore/c/nix_api_store.h b/src/libstore/c/nix_api_store.h index 36d712f01..43ded1860 100644 --- a/src/libstore/c/nix_api_store.h +++ b/src/libstore/c/nix_api_store.h @@ -36,7 +36,7 @@ typedef struct StorePath StorePath; nix_err nix_libstore_init(nix_c_context *context); /** - * @brief Loads plugins specified in the settings + * @brief Loads the plugins specified in Nix's plugin-files setting. * * Call this once, after calling your desired init functions and setting * relevant settings. @@ -111,17 +111,17 @@ bool nix_store_is_valid_path(nix_c_context *context, Store *store, /** * @brief Realise a Nix store path * - * Blocking, calls cb once for each built output + * Blocking, calls callback once for each realisedoutput * * @param[out] context Optional, stores error information * @param[in] store Nix Store reference * @param[in] path Path to build * @param[in] userdata data to pass to every callback invocation - * @param[in] cb called for every built output + * @param[in] callback called for every realised output */ nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, void *userdata, - void (*cb)(void *userdata, const char *outname, + void (*callback)(void *userdata, const char *outname, const char *out)); /** diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index e6acf0a4f..1d54cfe56 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -1156,9 +1156,10 @@ public: this, {}, "plugin-files", R"( A list of plugin files to be loaded by Nix. Each of these files will - be dlopened by Nix, allowing them to affect execution through static - initialization. In particular, these plugins may construct static - instances of RegisterPrimOp to add new primops or constants to the + be dlopened by Nix. If they contain the symbol `nix_plugin_entry()`, + this symbol will be called. Alternatively, they can affect execution + through static initialization. In particular, these plugins may construct + static instances of RegisterPrimOp to add new primops or constants to the expression language, RegisterStoreImplementation to add new store implementations, RegisterCommand to add new subcommands to the `nix` command, and RegisterSetting to add new nix config settings. See the From 91e53de7d3e3a0ac3e8a1c88e79c83c93744dbd8 Mon Sep 17 00:00:00 2001 From: Yorick Date: Mon, 28 Aug 2023 16:44:07 +0200 Subject: [PATCH 100/168] C API: update README example Co-authored-by: Valentin Gagarin --- doc/external-api/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/external-api/README.md b/doc/external-api/README.md index 71a181ede..8b9061df2 100644 --- a/doc/external-api/README.md +++ b/doc/external-api/README.md @@ -39,7 +39,7 @@ int main() { nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value); nix_value_force(NULL, state, value); - printf("nix version: %s\n", nix_get_string(NULL, value)); + printf("Nix version: %s\n", nix_get_string(NULL, value)); nix_gc_decref(NULL, value); nix_state_free(state); @@ -52,7 +52,7 @@ int main() { ``` $ gcc main.c $(pkg-config nix-expr-c --libs --cflags) -o main $ ./main -nix version 1.2.3 +Nix version: 2.17 ``` From e1bb799da9e7a5cef5856952ed35a7bd965ca9c1 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 28 Aug 2023 16:45:02 +0200 Subject: [PATCH 101/168] C API: reformat according to proposed clang-format file --- src/libexpr/c/nix_api_expr.cc | 225 +++---- src/libexpr/c/nix_api_expr.h | 30 +- src/libexpr/c/nix_api_expr_internal.h | 10 +- src/libexpr/c/nix_api_external.cc | 301 ++++----- src/libexpr/c/nix_api_external.h | 179 +++--- src/libexpr/c/nix_api_value.cc | 782 ++++++++++++------------ src/libexpr/c/nix_api_value.h | 110 ++-- src/libstore/c/nix_api_store.cc | 224 +++---- src/libstore/c/nix_api_store.h | 35 +- src/libstore/c/nix_api_store_internal.h | 5 +- src/libutil/c/nix_api_util.cc | 226 +++---- src/libutil/c/nix_api_util.h | 25 +- src/libutil/c/nix_api_util_internal.h | 41 +- 13 files changed, 1115 insertions(+), 1078 deletions(-) diff --git a/src/libexpr/c/nix_api_expr.cc b/src/libexpr/c/nix_api_expr.cc index a1c6d1acb..dc114c777 100644 --- a/src/libexpr/c/nix_api_expr.cc +++ b/src/libexpr/c/nix_api_expr.cc @@ -21,147 +21,158 @@ #include "gc_cpp.h" #endif -nix_err nix_libexpr_init(nix_c_context *context) { - if (context) - context->last_err_code = NIX_OK; - { - auto ret = nix_libutil_init(context); - if (ret != NIX_OK) - return ret; - } - { - auto ret = nix_libstore_init(context); - if (ret != NIX_OK) - return ret; - } - try { - nix::initGC(); - } - NIXC_CATCH_ERRS +nix_err nix_libexpr_init(nix_c_context * context) +{ + if (context) + context->last_err_code = NIX_OK; + { + auto ret = nix_libutil_init(context); + if (ret != NIX_OK) + return ret; + } + { + auto ret = nix_libstore_init(context); + if (ret != NIX_OK) + return ret; + } + try { + nix::initGC(); + } + NIXC_CATCH_ERRS } -nix_err nix_expr_eval_from_string(nix_c_context *context, State *state, - const char *expr, const char *path, - Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - nix::Expr *parsedExpr = state->state.parseExprFromString( - expr, state->state.rootPath(nix::CanonPath(path))); - state->state.eval(parsedExpr, *(nix::Value *)value); - state->state.forceValue(*(nix::Value *)value, nix::noPos); - } - NIXC_CATCH_ERRS +nix_err +nix_expr_eval_from_string(nix_c_context * context, State * state, const char * expr, const char * path, Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + nix::Expr * parsedExpr = state->state.parseExprFromString(expr, state->state.rootPath(nix::CanonPath(path))); + state->state.eval(parsedExpr, *(nix::Value *) value); + state->state.forceValue(*(nix::Value *) value, nix::noPos); + } + NIXC_CATCH_ERRS } -nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, - Value *arg, Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - state->state.callFunction(*(nix::Value *)fn, *(nix::Value *)arg, - *(nix::Value *)value, nix::noPos); - state->state.forceValue(*(nix::Value *)value, nix::noPos); - } - NIXC_CATCH_ERRS +nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value * arg, Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + state->state.callFunction(*(nix::Value *) fn, *(nix::Value *) arg, *(nix::Value *) value, nix::noPos); + state->state.forceValue(*(nix::Value *) value, nix::noPos); + } + NIXC_CATCH_ERRS } -nix_err nix_value_force(nix_c_context *context, State *state, Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - state->state.forceValue(*(nix::Value *)value, nix::noPos); - } - NIXC_CATCH_ERRS +nix_err nix_value_force(nix_c_context * context, State * state, Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + state->state.forceValue(*(nix::Value *) value, nix::noPos); + } + NIXC_CATCH_ERRS } -nix_err nix_value_force_deep(nix_c_context *context, State *state, - Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - state->state.forceValueDeep(*(nix::Value *)value); - } - NIXC_CATCH_ERRS +nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + state->state.forceValueDeep(*(nix::Value *) value); + } + NIXC_CATCH_ERRS } -State *nix_state_create(nix_c_context *context, const char **searchPath_c, - Store *store) { - if (context) - context->last_err_code = NIX_OK; - try { - nix::Strings searchPath; - if (searchPath_c != nullptr) - for (size_t i = 0; searchPath_c[i] != nullptr; i++) - searchPath.push_back(searchPath_c[i]); +State * nix_state_create(nix_c_context * context, const char ** searchPath_c, Store * store) +{ + if (context) + context->last_err_code = NIX_OK; + try { + nix::Strings searchPath; + if (searchPath_c != nullptr) + for (size_t i = 0; searchPath_c[i] != nullptr; i++) + searchPath.push_back(searchPath_c[i]); - return new State{ - nix::EvalState(nix::SearchPath::parse(searchPath), store->ptr)}; - } - NIXC_CATCH_ERRS_NULL + return new State{nix::EvalState(nix::SearchPath::parse(searchPath), store->ptr)}; + } + NIXC_CATCH_ERRS_NULL } -void nix_state_free(State *state) { delete state; } +void nix_state_free(State * state) +{ + delete state; +} #ifdef HAVE_BOEHMGC std::unordered_map< - const void *, unsigned int, std::hash, + const void *, + unsigned int, + std::hash, std::equal_to, - traceable_allocator>> + traceable_allocator>> nix_refcounts; std::mutex nix_refcount_lock; -nix_err nix_gc_incref(nix_c_context *context, const void *p) { - if (context) - context->last_err_code = NIX_OK; - try { - std::scoped_lock lock(nix_refcount_lock); - auto f = nix_refcounts.find(p); - if (f != nix_refcounts.end()) { - f->second++; - } else { - nix_refcounts[p] = 1; +nix_err nix_gc_incref(nix_c_context * context, const void * p) +{ + if (context) + context->last_err_code = NIX_OK; + try { + std::scoped_lock lock(nix_refcount_lock); + auto f = nix_refcounts.find(p); + if (f != nix_refcounts.end()) { + f->second++; + } else { + nix_refcounts[p] = 1; + } } - } - NIXC_CATCH_ERRS + NIXC_CATCH_ERRS } -nix_err nix_gc_decref(nix_c_context *context, const void *p) { +nix_err nix_gc_decref(nix_c_context * context, const void * p) +{ - if (context) - context->last_err_code = NIX_OK; - try { - std::scoped_lock lock(nix_refcount_lock); - auto f = nix_refcounts.find(p); - if (f != nix_refcounts.end()) { - if (--f->second == 0) - nix_refcounts.erase(f); - } else - throw std::runtime_error("nix_gc_decref: object was not referenced"); - } - NIXC_CATCH_ERRS + if (context) + context->last_err_code = NIX_OK; + try { + std::scoped_lock lock(nix_refcount_lock); + auto f = nix_refcounts.find(p); + if (f != nix_refcounts.end()) { + if (--f->second == 0) + nix_refcounts.erase(f); + } else + throw std::runtime_error("nix_gc_decref: object was not referenced"); + } + NIXC_CATCH_ERRS } -void nix_gc_now() { GC_gcollect(); } +void nix_gc_now() +{ + GC_gcollect(); +} #else -void nix_gc_incref(nix_c_context *context, const void *) { - if (context) - context->last_err_code = NIX_OK; - return NIX_OK; +void nix_gc_incref(nix_c_context * context, const void *) +{ + if (context) + context->last_err_code = NIX_OK; + return NIX_OK; } -void nix_gc_decref(nix_c_context *context, const void *) { - if (context) - context->last_err_code = NIX_OK; - return NIX_OK; +void nix_gc_decref(nix_c_context * context, const void *) +{ + if (context) + context->last_err_code = NIX_OK; + return NIX_OK; } void nix_gc_now() {} #endif -void nix_gc_register_finalizer(void *obj, void *cd, - void (*finalizer)(void *obj, void *cd)) { +void nix_gc_register_finalizer(void * obj, void * cd, void (*finalizer)(void * obj, void * cd)) +{ #ifdef HAVE_BOEHMGC - GC_REGISTER_FINALIZER(obj, finalizer, cd, 0, 0); + GC_REGISTER_FINALIZER(obj, finalizer, cd, 0, 0); #endif } diff --git a/src/libexpr/c/nix_api_expr.h b/src/libexpr/c/nix_api_expr.h index a6b902f96..8cc6c916c 100644 --- a/src/libexpr/c/nix_api_expr.h +++ b/src/libexpr/c/nix_api_expr.h @@ -66,7 +66,7 @@ typedef void Value; // nix::Value * @param[out] context Optional, stores error information * @return NIX_OK if the initialization was successful, an error code otherwise. */ -nix_err nix_libexpr_init(nix_c_context *context); +nix_err nix_libexpr_init(nix_c_context * context); /** * @brief Parses and evaluates a Nix expression from a string. @@ -75,14 +75,14 @@ nix_err nix_libexpr_init(nix_c_context *context); * @param[in] state The state of the evaluation. * @param[in] expr The Nix expression to parse. * @param[in] path The file path to associate with the expression. - * This is required for expressions that contain relative paths (such as `./.`) that are resolved relative to the given directory. + * This is required for expressions that contain relative paths (such as `./.`) that are resolved relative to the given + * directory. * @param[out] value The result of the evaluation. You should allocate this * yourself. * @return NIX_OK if the evaluation was successful, an error code otherwise. */ -nix_err nix_expr_eval_from_string(nix_c_context *context, State *state, - const char *expr, const char *path, - Value *value); +nix_err +nix_expr_eval_from_string(nix_c_context * context, State * state, const char * expr, const char * path, Value * value); /** * @brief Calls a Nix function with an argument. @@ -94,8 +94,7 @@ nix_err nix_expr_eval_from_string(nix_c_context *context, State *state, * @param[out] value The result of the function call. * @return NIX_OK if the function call was successful, an error code otherwise. */ -nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, - Value *arg, Value *value); +nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value * arg, Value * value); /** * @brief Forces the evaluation of a Nix value. @@ -117,7 +116,7 @@ nix_err nix_value_call(nix_c_context *context, State *state, Value *fn, * @return NIX_OK if the force operation was successful, an error code * otherwise. */ -nix_err nix_value_force(nix_c_context *context, State *state, Value *value); +nix_err nix_value_force(nix_c_context * context, State * state, Value * value); /** * @brief Forces the deep evaluation of a Nix value. @@ -133,8 +132,7 @@ nix_err nix_value_force(nix_c_context *context, State *state, Value *value); * @return NIX_OK if the deep force operation was successful, an error code * otherwise. */ -nix_err nix_value_force_deep(nix_c_context *context, State *state, - Value *value); +nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * value); /** * @brief Create a new Nix language evaluator state. @@ -144,8 +142,7 @@ nix_err nix_value_force_deep(nix_c_context *context, State *state, * @param[in] store The Nix store to use. * @return A new Nix state or NULL on failure. */ -State *nix_state_create(nix_c_context *context, const char **searchPath, - Store *store); +State * nix_state_create(nix_c_context * context, const char ** searchPath, Store * store); /** * @brief Frees a Nix state. @@ -154,7 +151,7 @@ State *nix_state_create(nix_c_context *context, const char **searchPath, * * @param[in] state The state to free. */ -void nix_state_free(State *state); +void nix_state_free(State * state); /** @addtogroup GC * @brief Reference counting and garbage collector operations @@ -178,14 +175,14 @@ void nix_state_free(State *state); * @param[out] context Optional, stores error information * @param[in] object The object to keep alive */ -nix_err nix_gc_incref(nix_c_context *context, const void *object); +nix_err nix_gc_incref(nix_c_context * context, const void * object); /** * @brief Decrement the garbage collector reference counter for the given object * * @param[out] context Optional, stores error information * @param[in] object The object to stop referencing */ -nix_err nix_gc_decref(nix_c_context *context, const void *object); +nix_err nix_gc_decref(nix_c_context * context, const void * object); /** * @brief Trigger the garbage collector manually @@ -203,8 +200,7 @@ void nix_gc_now(); * @param[in] cd the data to pass to the finalizer * @param[in] finalizer the callback function, called with obj and cd */ -void nix_gc_register_finalizer(void *obj, void *cd, - void (*finalizer)(void *obj, void *cd)); +void nix_gc_register_finalizer(void * obj, void * cd, void (*finalizer)(void * obj, void * cd)); /** @} */ // cffi end diff --git a/src/libexpr/c/nix_api_expr_internal.h b/src/libexpr/c/nix_api_expr_internal.h index bae50cf59..c9906dd3a 100644 --- a/src/libexpr/c/nix_api_expr_internal.h +++ b/src/libexpr/c/nix_api_expr_internal.h @@ -4,12 +4,14 @@ #include "eval.hh" #include "attr-set.hh" -struct State { - nix::EvalState state; +struct State +{ + nix::EvalState state; }; -struct BindingsBuilder { - nix::BindingsBuilder builder; +struct BindingsBuilder +{ + nix::BindingsBuilder builder; }; #endif // NIX_API_EXPR_INTERNAL_H diff --git a/src/libexpr/c/nix_api_external.cc b/src/libexpr/c/nix_api_external.cc index a927a4037..a2d776a47 100644 --- a/src/libexpr/c/nix_api_external.cc +++ b/src/libexpr/c/nix_api_external.cc @@ -20,178 +20,189 @@ #include "gc_cpp.h" #endif -struct nix_string_return { - std::string str; +struct nix_string_return +{ + std::string str; }; -struct nix_printer { - std::ostream &s; +struct nix_printer +{ + std::ostream & s; }; -struct nix_string_context { - nix::NixStringContext &ctx; +struct nix_string_context +{ + nix::NixStringContext & ctx; }; -void nix_set_string_return(nix_string_return *str, const char *c) { - str->str = c; +void nix_set_string_return(nix_string_return * str, const char * c) +{ + str->str = c; } -nix_err nix_external_print(nix_c_context *context, nix_printer *printer, - const char *c) { - if (context) - context->last_err_code = NIX_OK; - try { - printer->s << c; - } - NIXC_CATCH_ERRS +nix_err nix_external_print(nix_c_context * context, nix_printer * printer, const char * c) +{ + if (context) + context->last_err_code = NIX_OK; + try { + printer->s << c; + } + NIXC_CATCH_ERRS } -nix_err nix_external_add_string_context(nix_c_context *context, - nix_string_context *ctx, - const char *c) { - if (context) - context->last_err_code = NIX_OK; - try { - auto r = nix::NixStringContextElem::parse(c); - ctx->ctx.insert(r); - } - NIXC_CATCH_ERRS +nix_err nix_external_add_string_context(nix_c_context * context, nix_string_context * ctx, const char * c) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto r = nix::NixStringContextElem::parse(c); + ctx->ctx.insert(r); + } + NIXC_CATCH_ERRS } -class NixCExternalValue : public nix::ExternalValueBase { - NixCExternalValueDesc &desc; - void *v; +class NixCExternalValue : public nix::ExternalValueBase +{ + NixCExternalValueDesc & desc; + void * v; public: - NixCExternalValue(NixCExternalValueDesc &desc, void *v) : desc(desc), v(v){}; - void *get_ptr() { return v; } - /** - * Print out the value - */ - virtual std::ostream &print(std::ostream &str) const override { - nix_printer p{str}; - desc.print(v, &p); - return str; - } - - /** - * Return a simple string describing the type - */ - virtual std::string showType() const override { - nix_string_return res; - desc.showType(v, &res); - return std::move(res.str); - } - - /** - * Return a string to be used in builtins.typeOf - */ - virtual std::string typeOf() const override { - nix_string_return res; - desc.typeOf(v, &res); - return std::move(res.str); - } - - /** - * Coerce the value to a string. - */ - virtual std::string coerceToString(const nix::Pos &pos, - nix::NixStringContext &context, - bool copyMore, - bool copyToStore) const override { - if (!desc.coerceToString) { - return nix::ExternalValueBase::coerceToString(pos, context, copyMore, - copyToStore); + NixCExternalValue(NixCExternalValueDesc & desc, void * v) + : desc(desc) + , v(v){}; + void * get_ptr() + { + return v; } - nix_string_context ctx{context}; - nix_string_return res{""}; - // todo: pos, errors - desc.coerceToString(v, &ctx, copyMore, copyToStore, &res); - if (res.str.empty()) { - return nix::ExternalValueBase::coerceToString(pos, context, copyMore, - copyToStore); + /** + * Print out the value + */ + virtual std::ostream & print(std::ostream & str) const override + { + nix_printer p{str}; + desc.print(v, &p); + return str; } - return std::move(res.str); - } - /** - * Compare to another value of the same type. - */ - virtual bool operator==(const ExternalValueBase &b) const override { - if (!desc.equal) { - return false; + /** + * Return a simple string describing the type + */ + virtual std::string showType() const override + { + nix_string_return res; + desc.showType(v, &res); + return std::move(res.str); } - auto r = dynamic_cast(&b); - if (!r) - return false; - return desc.equal(v, r->v); - } - /** - * Print the value as JSON. - */ - virtual nlohmann::json - printValueAsJSON(nix::EvalState &state, bool strict, - nix::NixStringContext &context, - bool copyToStore = true) const override { - if (!desc.printValueAsJSON) { - return nix::ExternalValueBase::printValueAsJSON(state, strict, context, - copyToStore); + /** + * Return a string to be used in builtins.typeOf + */ + virtual std::string typeOf() const override + { + nix_string_return res; + desc.typeOf(v, &res); + return std::move(res.str); } - nix_string_context ctx{context}; - nix_string_return res{""}; - desc.printValueAsJSON(v, (State *)&state, strict, &ctx, copyToStore, &res); - if (res.str.empty()) { - return nix::ExternalValueBase::printValueAsJSON(state, strict, context, - copyToStore); - } - return nlohmann::json::parse(res.str); - } - /** - * Print the value as XML. - */ - virtual void printValueAsXML(nix::EvalState &state, bool strict, - bool location, nix::XMLWriter &doc, - nix::NixStringContext &context, - nix::PathSet &drvsSeen, - const nix::PosIdx pos) const override { - if (!desc.printValueAsXML) { - return nix::ExternalValueBase::printValueAsXML( - state, strict, location, doc, context, drvsSeen, pos); + /** + * Coerce the value to a string. + */ + virtual std::string coerceToString( + const nix::Pos & pos, nix::NixStringContext & context, bool copyMore, bool copyToStore) const override + { + if (!desc.coerceToString) { + return nix::ExternalValueBase::coerceToString(pos, context, copyMore, copyToStore); + } + nix_string_context ctx{context}; + nix_string_return res{""}; + // todo: pos, errors + desc.coerceToString(v, &ctx, copyMore, copyToStore, &res); + if (res.str.empty()) { + return nix::ExternalValueBase::coerceToString(pos, context, copyMore, copyToStore); + } + return std::move(res.str); } - nix_string_context ctx{context}; - desc.printValueAsXML(v, (State *)&state, strict, location, &doc, &ctx, - &drvsSeen, *reinterpret_cast(&pos)); - } - virtual ~NixCExternalValue() override{}; + /** + * Compare to another value of the same type. + */ + virtual bool operator==(const ExternalValueBase & b) const override + { + if (!desc.equal) { + return false; + } + auto r = dynamic_cast(&b); + if (!r) + return false; + return desc.equal(v, r->v); + } + + /** + * Print the value as JSON. + */ + virtual nlohmann::json printValueAsJSON( + nix::EvalState & state, bool strict, nix::NixStringContext & context, bool copyToStore = true) const override + { + if (!desc.printValueAsJSON) { + return nix::ExternalValueBase::printValueAsJSON(state, strict, context, copyToStore); + } + nix_string_context ctx{context}; + nix_string_return res{""}; + desc.printValueAsJSON(v, (State *) &state, strict, &ctx, copyToStore, &res); + if (res.str.empty()) { + return nix::ExternalValueBase::printValueAsJSON(state, strict, context, copyToStore); + } + return nlohmann::json::parse(res.str); + } + + /** + * Print the value as XML. + */ + virtual void printValueAsXML( + nix::EvalState & state, + bool strict, + bool location, + nix::XMLWriter & doc, + nix::NixStringContext & context, + nix::PathSet & drvsSeen, + const nix::PosIdx pos) const override + { + if (!desc.printValueAsXML) { + return nix::ExternalValueBase::printValueAsXML(state, strict, location, doc, context, drvsSeen, pos); + } + nix_string_context ctx{context}; + desc.printValueAsXML( + v, (State *) &state, strict, location, &doc, &ctx, &drvsSeen, *reinterpret_cast(&pos)); + } + + virtual ~NixCExternalValue() override{}; }; -ExternalValue *nix_create_external_value(nix_c_context *context, - NixCExternalValueDesc *desc, void *v) { - if (context) - context->last_err_code = NIX_OK; - try { - auto ret = new +ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto ret = new #ifdef HAVE_BOEHMGC - (GC) + (GC) #endif - NixCExternalValue(*desc, v); - nix_gc_incref(nullptr, ret); - return (ExternalValue *)ret; - } - NIXC_CATCH_ERRS_NULL + NixCExternalValue(*desc, v); + nix_gc_incref(nullptr, ret); + return (ExternalValue *) ret; + } + NIXC_CATCH_ERRS_NULL } -void *nix_get_external_value_content(nix_c_context *context, ExternalValue *b) { - if (context) - context->last_err_code = NIX_OK; - try { - auto r = dynamic_cast((nix::ExternalValueBase *)b); - if (r) - return r->get_ptr(); - return nullptr; - } - NIXC_CATCH_ERRS_NULL +void * nix_get_external_value_content(nix_c_context * context, ExternalValue * b) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto r = dynamic_cast((nix::ExternalValueBase *) b); + if (r) + return r->get_ptr(); + return nullptr; + } + NIXC_CATCH_ERRS_NULL } diff --git a/src/libexpr/c/nix_api_external.h b/src/libexpr/c/nix_api_external.h index 3dc9d79de..00eaa4460 100644 --- a/src/libexpr/c/nix_api_external.h +++ b/src/libexpr/c/nix_api_external.h @@ -42,7 +42,7 @@ typedef struct nix_string_context nix_string_context; * @param[out] str the nix_string_return to write to * @param[in] c The string to copy */ -void nix_set_string_return(nix_string_return *str, const char *c); +void nix_set_string_return(nix_string_return * str, const char * c); /** * Print to the nix_printer @@ -52,8 +52,7 @@ void nix_set_string_return(nix_string_return *str, const char *c); * @param[in] str The string to print * @returns NIX_OK if everything worked */ -nix_err nix_external_print(nix_c_context *context, nix_printer *printer, - const char *str); +nix_err nix_external_print(nix_c_context * context, nix_printer * printer, const char * str); /** * Add string context to the nix_string_context object @@ -62,9 +61,7 @@ nix_err nix_external_print(nix_c_context *context, nix_printer *printer, * @param[in] c The context string to add * @returns NIX_OK if everything worked */ -nix_err nix_external_add_string_context(nix_c_context *context, - nix_string_context *string_context, - const char *c); +nix_err nix_external_add_string_context(nix_c_context * context, nix_string_context * string_context, const char * c); /** * @brief Definition for a class of external values @@ -76,89 +73,88 @@ nix_err nix_external_add_string_context(nix_c_context *context, * * @see nix_create_external_value */ -typedef struct NixCExternalValueDesc { - /** - * @brief Called when printing the external value - * - * @param[in] self the void* passed to nix_create_external_value - * @param[out] printer The printer to print to, pass to nix_external_print - */ - void (*print)(void *self, nix_printer *printer); - /** - * @brief Called on :t - * @param[in] self the void* passed to nix_create_external_value - * @param[out] res the return value - */ - void (*showType)(void *self, nix_string_return *res); - /** - * @brief Called on `builtins.typeOf` - * @param self the void* passed to nix_create_external_value - * @param[out] res the return value - */ - void (*typeOf)(void *self, nix_string_return *res); - /** - * @brief Called on "${str}" and builtins.toString. - * - * The latter with coerceMore=true - * Optional, the default is to throw an error. - * @param[in] self the void* passed to nix_create_external_value - * @param[out] c writable string context for the resulting string - * @param[in] coerceMore boolean, try to coerce to strings in more cases - * instead of throwing an error - * @param[in] copyToStore boolean, whether to copy referenced paths to store - * or keep them as-is - * @param[out] res the return value. Not touching this, or setting it to the - * empty string, will make the conversion throw an error. - */ - void (*coerceToString)(void *self, nix_string_context *c, int coerceMore, - int copyToStore, nix_string_return *res); - /** - * @brief Try to compare two external values - * - * Optional, the default is always false. - * If the other object was not a Nix C external value, this comparison will - * also return false - * @param[in] self the void* passed to nix_create_external_value - * @param[in] other the void* passed to the other object's - * nix_create_external_value - * @returns true if the objects are deemed to be equal - */ - int (*equal)(void *self, void *other); - /** - * @brief Convert the external value to json - * - * Optional, the default is to throw an error - * @param[in] self the void* passed to nix_create_external_value - * @param[in] state The evaluator state - * @param[in] strict boolean Whether to force the value before printing - * @param[out] c writable string context for the resulting string - * @param[in] copyToStore whether to copy referenced paths to store or keep - * them as-is - * @param[out] res the return value. Gets parsed as JSON. Not touching this, - * or setting it to the empty string, will make the conversion throw an error. - */ - void (*printValueAsJSON)(void *self, State *, int strict, - nix_string_context *c, bool copyToStore, - nix_string_return *res); - /** - * @brief Convert the external value to XML - * - * Optional, the default is to throw an error - * @todo The mechanisms for this call are incomplete. There are no C - * bindings to work with XML, pathsets and positions. - * @param[in] self the void* passed to nix_create_external_value - * @param[in] state The evaluator state - * @param[in] strict boolean Whether to force the value before printing - * @param[in] location boolean Whether to include position information in the - * xml - * @param[out] doc XML document to output to - * @param[out] c writable string context for the resulting string - * @param[in,out] drvsSeen a path set to avoid duplicating derivations - * @param[in] pos The position of the call. - */ - void (*printValueAsXML)(void *self, State *, int strict, int location, - void *doc, nix_string_context *c, void *drvsSeen, - int pos); +typedef struct NixCExternalValueDesc +{ + /** + * @brief Called when printing the external value + * + * @param[in] self the void* passed to nix_create_external_value + * @param[out] printer The printer to print to, pass to nix_external_print + */ + void (*print)(void * self, nix_printer * printer); + /** + * @brief Called on :t + * @param[in] self the void* passed to nix_create_external_value + * @param[out] res the return value + */ + void (*showType)(void * self, nix_string_return * res); + /** + * @brief Called on `builtins.typeOf` + * @param self the void* passed to nix_create_external_value + * @param[out] res the return value + */ + void (*typeOf)(void * self, nix_string_return * res); + /** + * @brief Called on "${str}" and builtins.toString. + * + * The latter with coerceMore=true + * Optional, the default is to throw an error. + * @param[in] self the void* passed to nix_create_external_value + * @param[out] c writable string context for the resulting string + * @param[in] coerceMore boolean, try to coerce to strings in more cases + * instead of throwing an error + * @param[in] copyToStore boolean, whether to copy referenced paths to store + * or keep them as-is + * @param[out] res the return value. Not touching this, or setting it to the + * empty string, will make the conversion throw an error. + */ + void (*coerceToString)( + void * self, nix_string_context * c, int coerceMore, int copyToStore, nix_string_return * res); + /** + * @brief Try to compare two external values + * + * Optional, the default is always false. + * If the other object was not a Nix C external value, this comparison will + * also return false + * @param[in] self the void* passed to nix_create_external_value + * @param[in] other the void* passed to the other object's + * nix_create_external_value + * @returns true if the objects are deemed to be equal + */ + int (*equal)(void * self, void * other); + /** + * @brief Convert the external value to json + * + * Optional, the default is to throw an error + * @param[in] self the void* passed to nix_create_external_value + * @param[in] state The evaluator state + * @param[in] strict boolean Whether to force the value before printing + * @param[out] c writable string context for the resulting string + * @param[in] copyToStore whether to copy referenced paths to store or keep + * them as-is + * @param[out] res the return value. Gets parsed as JSON. Not touching this, + * or setting it to the empty string, will make the conversion throw an error. + */ + void (*printValueAsJSON)( + void * self, State *, int strict, nix_string_context * c, bool copyToStore, nix_string_return * res); + /** + * @brief Convert the external value to XML + * + * Optional, the default is to throw an error + * @todo The mechanisms for this call are incomplete. There are no C + * bindings to work with XML, pathsets and positions. + * @param[in] self the void* passed to nix_create_external_value + * @param[in] state The evaluator state + * @param[in] strict boolean Whether to force the value before printing + * @param[in] location boolean Whether to include position information in the + * xml + * @param[out] doc XML document to output to + * @param[out] c writable string context for the resulting string + * @param[in,out] drvsSeen a path set to avoid duplicating derivations + * @param[in] pos The position of the call. + */ + void (*printValueAsXML)( + void * self, State *, int strict, int location, void * doc, nix_string_context * c, void * drvsSeen, int pos); } NixCExternalValueDesc; /** @@ -173,8 +169,7 @@ typedef struct NixCExternalValueDesc { * @returns external value, owned by the garbage collector * @see nix_set_external */ -ExternalValue *nix_create_external_value(nix_c_context *context, - NixCExternalValueDesc *desc, void *v); +ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v); /** * @brief Extract the pointer from a nix c external value. @@ -183,7 +178,7 @@ ExternalValue *nix_create_external_value(nix_c_context *context, * @returns The pointer, or null if the external value was not from nix c. * @see nix_get_external */ -void *nix_get_external_value_content(nix_c_context *context, ExternalValue *b); +void * nix_get_external_value_content(nix_c_context * context, ExternalValue * b); // cffi end #ifdef __cplusplus diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index dae50352b..608a625cb 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -18,437 +18,457 @@ #endif // Helper function to throw an exception if value is null -static const nix::Value &check_value_not_null(const Value *value) { - if (!value) { - throw std::runtime_error("Value is null"); - } - return *((const nix::Value *)value); +static const nix::Value & check_value_not_null(const Value * value) +{ + if (!value) { + throw std::runtime_error("Value is null"); + } + return *((const nix::Value *) value); } -static nix::Value &check_value_not_null(Value *value) { - if (!value) { - throw std::runtime_error("Value is null"); - } - return *((nix::Value *)value); +static nix::Value & check_value_not_null(Value * value) +{ + if (!value) { + throw std::runtime_error("Value is null"); + } + return *((nix::Value *) value); } -PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, - const char *name, const char **args, const char *doc) { - if (context) - context->last_err_code = NIX_OK; - try { - auto fun2 = (nix::PrimOpFun)fun; - auto p = new +PrimOp * nix_alloc_primop( + nix_c_context * context, PrimOpFun fun, int arity, const char * name, const char ** args, const char * doc) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto fun2 = (nix::PrimOpFun) fun; + auto p = new #ifdef HAVE_BOEHMGC - (GC) + (GC) #endif - nix::PrimOp{.name = name, - .args = {}, - .arity = (size_t)arity, - .doc = doc, - .fun = fun2}; - if (args) - for (size_t i = 0; args[i]; i++) - p->args.emplace_back(*args); - nix_gc_incref(nullptr, p); - return (PrimOp *)p; - } - NIXC_CATCH_ERRS_NULL -} - -nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp) { - if (context) - context->last_err_code = NIX_OK; - try { - nix::RegisterPrimOp r(std::move(*((nix::PrimOp *)primOp))); - } - NIXC_CATCH_ERRS -} - -Value *nix_alloc_value(nix_c_context *context, State *state) { - if (context) - context->last_err_code = NIX_OK; - try { - Value *res = state->state.allocValue(); - nix_gc_incref(nullptr, res); - return res; - } - NIXC_CATCH_ERRS_NULL -} - -ValueType nix_get_type(nix_c_context *context, const Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - using namespace nix; - switch (v.type()) { - case nThunk: - return NIX_TYPE_THUNK; - case nInt: - return NIX_TYPE_INT; - case nFloat: - return NIX_TYPE_FLOAT; - case nBool: - return NIX_TYPE_BOOL; - case nString: - return NIX_TYPE_STRING; - case nPath: - return NIX_TYPE_PATH; - case nNull: - return NIX_TYPE_NULL; - case nAttrs: - return NIX_TYPE_ATTRS; - case nList: - return NIX_TYPE_LIST; - case nFunction: - return NIX_TYPE_FUNCTION; - case nExternal: - return NIX_TYPE_EXTERNAL; + nix::PrimOp{.name = name, .args = {}, .arity = (size_t) arity, .doc = doc, .fun = fun2}; + if (args) + for (size_t i = 0; args[i]; i++) + p->args.emplace_back(*args); + nix_gc_incref(nullptr, p); + return (PrimOp *) p; } - return NIX_TYPE_NULL; - } - NIXC_CATCH_ERRS_RES(NIX_TYPE_NULL); + NIXC_CATCH_ERRS_NULL } -const char *nix_get_typename(nix_c_context *context, const Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - auto s = nix::showType(v); - return strdup(s.c_str()); - } - NIXC_CATCH_ERRS_NULL -} - -bool nix_get_bool(nix_c_context *context, const Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nBool); - return v.boolean; - } - NIXC_CATCH_ERRS_RES(false); -} - -const char *nix_get_string(nix_c_context *context, const Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nString); - return v.string.s; - } - NIXC_CATCH_ERRS_NULL -} - -const char *nix_get_path_string(nix_c_context *context, const Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nPath); - return v._path; - } - NIXC_CATCH_ERRS_NULL -} - -unsigned int nix_get_list_size(nix_c_context *context, const Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nList); - return v.listSize(); - } - NIXC_CATCH_ERRS_RES(0); -} - -unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nAttrs); - return v.attrs->size(); - } - NIXC_CATCH_ERRS_RES(0); -} - -double nix_get_float(nix_c_context *context, const Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nFloat); - return v.fpoint; - } - NIXC_CATCH_ERRS_RES(NAN); -} - -int64_t nix_get_int(nix_c_context *context, const Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nInt); - return v.integer; - } - NIXC_CATCH_ERRS_RES(0); -} - -ExternalValue *nix_get_external(nix_c_context *context, Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nExternal); - return (ExternalValue *)v.external; - } - NIXC_CATCH_ERRS_NULL; -} - -Value *nix_get_list_byidx(nix_c_context *context, const Value *value, - State *state, unsigned int ix) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nList); - auto *p = v.listElems()[ix]; - nix_gc_incref(nullptr, p); - state->state.forceValue(*p, nix::noPos); - return (Value *)p; - } - NIXC_CATCH_ERRS_NULL -} - -Value *nix_get_attr_byname(nix_c_context *context, const Value *value, - State *state, const char *name) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nAttrs); - nix::Symbol s = state->state.symbols.create(name); - auto attr = v.attrs->get(s); - if (attr) { - nix_gc_incref(nullptr, attr->value); - state->state.forceValue(*attr->value, nix::noPos); - return attr->value; +nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp) +{ + if (context) + context->last_err_code = NIX_OK; + try { + nix::RegisterPrimOp r(std::move(*((nix::PrimOp *) primOp))); } - nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute"); - return nullptr; - } - NIXC_CATCH_ERRS_NULL + NIXC_CATCH_ERRS } -bool nix_has_attr_byname(nix_c_context *context, const Value *value, - State *state, const char *name) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - assert(v.type() == nix::nAttrs); - nix::Symbol s = state->state.symbols.create(name); - auto attr = v.attrs->get(s); - if (attr) - return true; - return false; - } - NIXC_CATCH_ERRS_RES(false); +Value * nix_alloc_value(nix_c_context * context, State * state) +{ + if (context) + context->last_err_code = NIX_OK; + try { + Value * res = state->state.allocValue(); + nix_gc_incref(nullptr, res); + return res; + } + NIXC_CATCH_ERRS_NULL } -Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, - State *state, unsigned int i, const char **name) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - const nix::Attr &a = (*v.attrs)[i]; - *name = ((const std::string &)(state->state.symbols[a.name])).c_str(); - nix_gc_incref(nullptr, a.value); - state->state.forceValue(*a.value, nix::noPos); - return a.value; - } - NIXC_CATCH_ERRS_NULL +ValueType nix_get_type(nix_c_context * context, const Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + using namespace nix; + switch (v.type()) { + case nThunk: + return NIX_TYPE_THUNK; + case nInt: + return NIX_TYPE_INT; + case nFloat: + return NIX_TYPE_FLOAT; + case nBool: + return NIX_TYPE_BOOL; + case nString: + return NIX_TYPE_STRING; + case nPath: + return NIX_TYPE_PATH; + case nNull: + return NIX_TYPE_NULL; + case nAttrs: + return NIX_TYPE_ATTRS; + case nList: + return NIX_TYPE_LIST; + case nFunction: + return NIX_TYPE_FUNCTION; + case nExternal: + return NIX_TYPE_EXTERNAL; + } + return NIX_TYPE_NULL; + } + NIXC_CATCH_ERRS_RES(NIX_TYPE_NULL); } -const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value, - State *state, unsigned int i) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - const nix::Attr &a = (*v.attrs)[i]; - return ((const std::string &)(state->state.symbols[a.name])).c_str(); - } - NIXC_CATCH_ERRS_NULL +const char * nix_get_typename(nix_c_context * context, const Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + auto s = nix::showType(v); + return strdup(s.c_str()); + } + NIXC_CATCH_ERRS_NULL } -nix_err nix_set_bool(nix_c_context *context, Value *value, bool b) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - v.mkBool(b); - } - NIXC_CATCH_ERRS +bool nix_get_bool(nix_c_context * context, const Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nBool); + return v.boolean; + } + NIXC_CATCH_ERRS_RES(false); +} + +const char * nix_get_string(nix_c_context * context, const Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nString); + return v.string.s; + } + NIXC_CATCH_ERRS_NULL +} + +const char * nix_get_path_string(nix_c_context * context, const Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nPath); + return v._path; + } + NIXC_CATCH_ERRS_NULL +} + +unsigned int nix_get_list_size(nix_c_context * context, const Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nList); + return v.listSize(); + } + NIXC_CATCH_ERRS_RES(0); +} + +unsigned int nix_get_attrs_size(nix_c_context * context, const Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nAttrs); + return v.attrs->size(); + } + NIXC_CATCH_ERRS_RES(0); +} + +double nix_get_float(nix_c_context * context, const Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nFloat); + return v.fpoint; + } + NIXC_CATCH_ERRS_RES(NAN); +} + +int64_t nix_get_int(nix_c_context * context, const Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nInt); + return v.integer; + } + NIXC_CATCH_ERRS_RES(0); +} + +ExternalValue * nix_get_external(nix_c_context * context, Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nExternal); + return (ExternalValue *) v.external; + } + NIXC_CATCH_ERRS_NULL; +} + +Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * state, unsigned int ix) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nList); + auto * p = v.listElems()[ix]; + nix_gc_incref(nullptr, p); + state->state.forceValue(*p, nix::noPos); + return (Value *) p; + } + NIXC_CATCH_ERRS_NULL +} + +Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nAttrs); + nix::Symbol s = state->state.symbols.create(name); + auto attr = v.attrs->get(s); + if (attr) { + nix_gc_incref(nullptr, attr->value); + state->state.forceValue(*attr->value, nix::noPos); + return attr->value; + } + nix_set_err_msg(context, NIX_ERR_KEY, "missing attribute"); + return nullptr; + } + NIXC_CATCH_ERRS_NULL +} + +bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + assert(v.type() == nix::nAttrs); + nix::Symbol s = state->state.symbols.create(name); + auto attr = v.attrs->get(s); + if (attr) + return true; + return false; + } + NIXC_CATCH_ERRS_RES(false); +} + +Value * +nix_get_attr_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i, const char ** name) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + const nix::Attr & a = (*v.attrs)[i]; + *name = ((const std::string &) (state->state.symbols[a.name])).c_str(); + nix_gc_incref(nullptr, a.value); + state->state.forceValue(*a.value, nix::noPos); + return a.value; + } + NIXC_CATCH_ERRS_NULL +} + +const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + const nix::Attr & a = (*v.attrs)[i]; + return ((const std::string &) (state->state.symbols[a.name])).c_str(); + } + NIXC_CATCH_ERRS_NULL +} + +nix_err nix_set_bool(nix_c_context * context, Value * value, bool b) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + v.mkBool(b); + } + NIXC_CATCH_ERRS } // todo string context -nix_err nix_set_string(nix_c_context *context, Value *value, const char *str) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - v.mkString(std::string_view(str)); - } - NIXC_CATCH_ERRS +nix_err nix_set_string(nix_c_context * context, Value * value, const char * str) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + v.mkString(std::string_view(str)); + } + NIXC_CATCH_ERRS } -nix_err nix_set_path_string(nix_c_context *context, Value *value, - const char *str) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - v.mkPath(std::string_view(str)); - } - NIXC_CATCH_ERRS +nix_err nix_set_path_string(nix_c_context * context, Value * value, const char * str) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + v.mkPath(std::string_view(str)); + } + NIXC_CATCH_ERRS } -nix_err nix_set_float(nix_c_context *context, Value *value, double d) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - v.mkFloat(d); - } - NIXC_CATCH_ERRS +nix_err nix_set_float(nix_c_context * context, Value * value, double d) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + v.mkFloat(d); + } + NIXC_CATCH_ERRS } -nix_err nix_set_int(nix_c_context *context, Value *value, int64_t i) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - v.mkInt(i); - } - NIXC_CATCH_ERRS +nix_err nix_set_int(nix_c_context * context, Value * value, int64_t i) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + v.mkInt(i); + } + NIXC_CATCH_ERRS } -nix_err nix_set_null(nix_c_context *context, Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - v.mkNull(); - } - NIXC_CATCH_ERRS +nix_err nix_set_null(nix_c_context * context, Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + v.mkNull(); + } + NIXC_CATCH_ERRS } -nix_err nix_set_external(nix_c_context *context, Value *value, - ExternalValue *val) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - auto r = (nix::ExternalValueBase *)val; - v.mkExternal(r); - } - NIXC_CATCH_ERRS +nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * val) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + auto r = (nix::ExternalValueBase *) val; + v.mkExternal(r); + } + NIXC_CATCH_ERRS } -nix_err nix_make_list(nix_c_context *context, State *s, Value *value, - unsigned int size) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - s->state.mkList(v, size); - } - NIXC_CATCH_ERRS +nix_err nix_make_list(nix_c_context * context, State * s, Value * value, unsigned int size) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + s->state.mkList(v, size); + } + NIXC_CATCH_ERRS } -nix_err nix_set_list_byidx(nix_c_context *context, Value *value, - unsigned int ix, Value *elem) { - if (context) - context->last_err_code = NIX_OK; - try { - // todo: assert that this is a list - auto &v = check_value_not_null(value); - auto &e = check_value_not_null(elem); - v.listElems()[ix] = &e; - } - NIXC_CATCH_ERRS +nix_err nix_set_list_byidx(nix_c_context * context, Value * value, unsigned int ix, Value * elem) +{ + if (context) + context->last_err_code = NIX_OK; + try { + // todo: assert that this is a list + auto & v = check_value_not_null(value); + auto & e = check_value_not_null(elem); + v.listElems()[ix] = &e; + } + NIXC_CATCH_ERRS } -nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *p) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - v.mkPrimOp((nix::PrimOp *)p); - } - NIXC_CATCH_ERRS +nix_err nix_set_primop(nix_c_context * context, Value * value, PrimOp * p) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + v.mkPrimOp((nix::PrimOp *) p); + } + NIXC_CATCH_ERRS } -nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - auto &s = check_value_not_null(source); - v = s; - } - NIXC_CATCH_ERRS +nix_err nix_copy_value(nix_c_context * context, Value * value, Value * source) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + auto & s = check_value_not_null(source); + v = s; + } + NIXC_CATCH_ERRS } -nix_err nix_make_attrs(nix_c_context *context, Value *value, - BindingsBuilder *b) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - v.mkAttrs(b->builder); - } - NIXC_CATCH_ERRS +nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder * b) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + v.mkAttrs(b->builder); + } + NIXC_CATCH_ERRS } -BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state, - size_t capacity) { - if (context) - context->last_err_code = NIX_OK; - try { - auto bb = state->state.buildBindings(capacity); - return new +BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, State * state, size_t capacity) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto bb = state->state.buildBindings(capacity); + return new #if HAVE_BOEHMGC - (NoGC) + (NoGC) #endif - BindingsBuilder{std::move(bb)}; - } - NIXC_CATCH_ERRS_NULL + BindingsBuilder{std::move(bb)}; + } + NIXC_CATCH_ERRS_NULL } -nix_err nix_bindings_builder_insert(nix_c_context *context, BindingsBuilder *b, - const char *name, Value *value) { - if (context) - context->last_err_code = NIX_OK; - try { - auto &v = check_value_not_null(value); - nix::Symbol s = b->builder.state.symbols.create(name); - b->builder.insert(s, &v); - } - NIXC_CATCH_ERRS +nix_err nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * b, const char * name, Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto & v = check_value_not_null(value); + nix::Symbol s = b->builder.state.symbols.create(name); + b->builder.insert(s, &v); + } + NIXC_CATCH_ERRS } -void nix_bindings_builder_free(BindingsBuilder *bb) { +void nix_bindings_builder_free(BindingsBuilder * bb) +{ #if HAVE_BOEHMGC - GC_FREE((nix::BindingsBuilder *)bb); + GC_FREE((nix::BindingsBuilder *) bb); #else - delete (nix::BindingsBuilder *)bb; + delete (nix::BindingsBuilder *) bb; #endif } diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index f4d9c9584..21647552b 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -20,17 +20,17 @@ extern "C" { // Type definitions typedef enum { - NIX_TYPE_THUNK, - NIX_TYPE_INT, - NIX_TYPE_FLOAT, - NIX_TYPE_BOOL, - NIX_TYPE_STRING, - NIX_TYPE_PATH, - NIX_TYPE_NULL, - NIX_TYPE_ATTRS, - NIX_TYPE_LIST, - NIX_TYPE_FUNCTION, - NIX_TYPE_EXTERNAL + NIX_TYPE_THUNK, + NIX_TYPE_INT, + NIX_TYPE_FLOAT, + NIX_TYPE_BOOL, + NIX_TYPE_STRING, + NIX_TYPE_PATH, + NIX_TYPE_NULL, + NIX_TYPE_ATTRS, + NIX_TYPE_LIST, + NIX_TYPE_FUNCTION, + NIX_TYPE_EXTERNAL } ValueType; // forward declarations @@ -67,11 +67,12 @@ typedef struct ExternalValue ExternalValue; /** @brief Function pointer for primops * @param[in] state Evaluator state * @param[in] pos Call position, opaque index into the state's position table. - * @param[in] args list of arguments. Note that these can be thunks and should be forced using nix_value_force before use. + * @param[in] args list of arguments. Note that these can be thunks and should be forced using nix_value_force before + * use. * @param[out] ret return value * @see nix_alloc_primop, nix_set_primop */ -typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *ret); +typedef void (*PrimOpFun)(State * state, int pos, Value ** args, Value * ret); /** @brief Allocate a PrimOp * @@ -87,8 +88,8 @@ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *ret); * @return primop, or null in case of errors * @see nix_set_primop */ -PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, - const char *name, const char **args, const char *doc); +PrimOp * nix_alloc_primop( + nix_c_context * context, PrimOpFun fun, int arity, const char * name, const char ** args, const char * doc); /** @brief add a primop to the `builtins` attribute set * @@ -102,7 +103,7 @@ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, * @return primop, or null in case of errors * */ -nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp); +nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp); /** @} */ // Function prototypes @@ -115,7 +116,7 @@ nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp); * @return value, or null in case of errors * */ -Value *nix_alloc_value(nix_c_context *context, State *state); +Value * nix_alloc_value(nix_c_context * context, State * state); /** @addtogroup value_manip Manipulating values * @brief Functions to inspect and change Nix language values, represented by Value. * @{ @@ -128,65 +129,65 @@ Value *nix_alloc_value(nix_c_context *context, State *state); * @param[in] value Nix value to inspect * @return type of nix value */ -ValueType nix_get_type(nix_c_context *context, const Value *value); +ValueType nix_get_type(nix_c_context * context, const Value * value); /** @brief Get type name of value as defined in the evaluator * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return type name, owned string * @todo way to free the result */ -const char *nix_get_typename(nix_c_context *context, const Value *value); +const char * nix_get_typename(nix_c_context * context, const Value * value); /** @brief Get boolean value * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return true or false, error info via context */ -bool nix_get_bool(nix_c_context *context, const Value *value); +bool nix_get_bool(nix_c_context * context, const Value * value); /** @brief Get string * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return string * @return NULL in case of error. */ -const char *nix_get_string(nix_c_context *context, const Value *value); +const char * nix_get_string(nix_c_context * context, const Value * value); /** @brief Get path as string * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return string * @return NULL in case of error. */ -const char *nix_get_path_string(nix_c_context *context, const Value *value); +const char * nix_get_path_string(nix_c_context * context, const Value * value); /** @brief Get the length of a list * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return length of list, error info via context */ -unsigned int nix_get_list_size(nix_c_context *context, const Value *value); +unsigned int nix_get_list_size(nix_c_context * context, const Value * value); /** @brief Get the element count of an attrset * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return attrset element count, error info via context */ -unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value); +unsigned int nix_get_attrs_size(nix_c_context * context, const Value * value); /** @brief Get float value in 64 bits * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return float contents, error info via context */ -double nix_get_float(nix_c_context *context, const Value *value); +double nix_get_float(nix_c_context * context, const Value * value); /** @brief Get int value * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return int contents, error info via context */ -int64_t nix_get_int(nix_c_context *context, const Value *value); +int64_t nix_get_int(nix_c_context * context, const Value * value); /** @brief Get external reference * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return reference to external, NULL in case of error */ -ExternalValue *nix_get_external(nix_c_context *context, Value *); +ExternalValue * nix_get_external(nix_c_context * context, Value *); /** @brief Get the ix'th element of a list * @@ -197,8 +198,7 @@ ExternalValue *nix_get_external(nix_c_context *context, Value *); * @param[in] ix list element to get * @return value, NULL in case of errors */ -Value *nix_get_list_byidx(nix_c_context *context, const Value *value, - State *state, unsigned int ix); +Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * state, unsigned int ix); /** @brief Get an attr by name * * Owned by the GC. Use nix_gc_decref when you're done with the pointer @@ -208,8 +208,7 @@ Value *nix_get_list_byidx(nix_c_context *context, const Value *value, * @param[in] name attribute name * @return value, NULL in case of errors */ -Value *nix_get_attr_byname(nix_c_context *context, const Value *value, - State *state, const char *name); +Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name); /** @brief Check if an attribute name exists on a value * @param[out] context Optional, stores error information @@ -218,8 +217,7 @@ Value *nix_get_attr_byname(nix_c_context *context, const Value *value, * @param[in] name attribute name * @return value, error info via context */ -bool nix_has_attr_byname(nix_c_context *context, const Value *value, - State *state, const char *name); +bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name); /** @brief Get an attribute by index in the sorted bindings * @@ -233,8 +231,8 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value, * @param[out] name will store a pointer to the attribute name * @return value, NULL in case of errors */ -Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, - State *state, unsigned int i, const char **name); +Value * +nix_get_attr_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i, const char ** name); /** @brief Get an attribute name by index in the sorted bindings * @@ -247,8 +245,7 @@ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, * @param[in] i attribute index * @return name, NULL in case of errors */ -const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value, - State *state, unsigned int i); +const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i); /**@}*/ /** @name Setters */ @@ -259,58 +256,55 @@ const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value, * @param[in] b the boolean value * @return error code, NIX_OK on success. */ -nix_err nix_set_bool(nix_c_context *context, Value *value, bool b); +nix_err nix_set_bool(nix_c_context * context, Value * value, bool b); /** @brief Set a string * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] str the string, copied * @return error code, NIX_OK on success. */ -nix_err nix_set_string(nix_c_context *context, Value *value, const char *str); +nix_err nix_set_string(nix_c_context * context, Value * value, const char * str); /** @brief Set a path * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] str the path string, copied * @return error code, NIX_OK on success. */ -nix_err nix_set_path_string(nix_c_context *context, Value *value, - const char *str); +nix_err nix_set_path_string(nix_c_context * context, Value * value, const char * str); /** @brief Set a float * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] d the float, 64-bits * @return error code, NIX_OK on success. */ -nix_err nix_set_float(nix_c_context *context, Value *value, double d); +nix_err nix_set_float(nix_c_context * context, Value * value, double d); /** @brief Set an int * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] i the int * @return error code, NIX_OK on success. */ -nix_err nix_set_int(nix_c_context *context, Value *value, int64_t i); +nix_err nix_set_int(nix_c_context * context, Value * value, int64_t i); /** @brief Set null * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @return error code, NIX_OK on success. */ -nix_err nix_set_null(nix_c_context *context, Value *value); +nix_err nix_set_null(nix_c_context * context, Value * value); /** @brief Set an external value * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] val the external value to set. Will be GC-referenced by the value. * @return error code, NIX_OK on success. */ -nix_err nix_set_external(nix_c_context *context, Value *value, - ExternalValue *val); +nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * val); /** @brief Allocate a list * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] size size of list * @return error code, NIX_OK on success. */ -nix_err nix_make_list(nix_c_context *context, State *s, Value *value, - unsigned int size); +nix_err nix_make_list(nix_c_context * context, State * s, Value * value, unsigned int size); /** @brief Manipulate a list by index * * Don't do this mid-computation. @@ -321,16 +315,14 @@ nix_err nix_make_list(nix_c_context *context, State *s, Value *value, * @param[in] elem the value to set, will be gc-referenced by the value * @return error code, NIX_OK on success. */ -nix_err nix_set_list_byidx(nix_c_context *context, Value *value, - unsigned int ix, Value *elem); +nix_err nix_set_list_byidx(nix_c_context * context, Value * value, unsigned int ix, Value * elem); /** @brief Create an attribute set from a bindings builder * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] b bindings builder to use. Make sure to unref this afterwards. * @return error code, NIX_OK on success. */ -nix_err nix_make_attrs(nix_c_context *context, Value *value, - BindingsBuilder *b); +nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder * b); /** @brief Set primop * @param[out] context Optional, stores error information * @param[out] value Nix value to modify @@ -338,14 +330,14 @@ nix_err nix_make_attrs(nix_c_context *context, Value *value, * @see nix_alloc_primop * @return error code, NIX_OK on success. */ -nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *op); +nix_err nix_set_primop(nix_c_context * context, Value * value, PrimOp * op); /** @brief Copy from another value * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] source value to copy from * @return error code, NIX_OK on success. */ -nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source); +nix_err nix_copy_value(nix_c_context * context, Value * value, Value * source); /**@}*/ /** @brief Create a bindings builder @@ -356,8 +348,7 @@ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source); * @return owned reference to a bindings builder. Make sure to unref when you're done. */ -BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state, - size_t capacity); +BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, State * state, size_t capacity); /** @brief Insert bindings into a builder * @param[out] context Optional, stores error information * @param[in] builder BindingsBuilder to insert into @@ -365,15 +356,14 @@ BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state, * @param[in] value value to give the binding * @return error code, NIX_OK on success. */ -nix_err nix_bindings_builder_insert(nix_c_context *context, - BindingsBuilder *builder, const char *name, - Value *value); +nix_err +nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * builder, const char * name, Value * value); /** @brief Free a bindings builder * * Does not fail. * @param[in] builder the builder to free */ -void nix_bindings_builder_free(BindingsBuilder *builder); +void nix_bindings_builder_free(BindingsBuilder * builder); /**@}*/ // cffi end diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index 7b5391034..4ee97c8a1 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -7,122 +7,132 @@ #include "globals.hh" -struct StorePath { - nix::StorePath path; +struct StorePath +{ + nix::StorePath path; }; -nix_err nix_libstore_init(nix_c_context *context) { - if (context) - context->last_err_code = NIX_OK; - try { - nix::initLibStore(); - } - NIXC_CATCH_ERRS -} - -nix_err nix_init_plugins(nix_c_context *context) { - if (context) - context->last_err_code = NIX_OK; - try { - nix::initPlugins(); - } - NIXC_CATCH_ERRS -} - -Store *nix_store_open(nix_c_context *context, const char *uri, - const char ***params) { - if (context) - context->last_err_code = NIX_OK; - try { - if (!uri) { - return new Store{nix::openStore()}; - } else { - std::string uri_str = uri; - if (!params) - return new Store{nix::openStore(uri_str)}; - - nix::Store::Params params_map; - for (size_t i = 0; params[i] != nullptr; i++) { - params_map[params[i][0]] = params[i][1]; - } - return new Store{nix::openStore(uri_str, params_map)}; +nix_err nix_libstore_init(nix_c_context * context) +{ + if (context) + context->last_err_code = NIX_OK; + try { + nix::initLibStore(); } - } - NIXC_CATCH_ERRS_NULL + NIXC_CATCH_ERRS } -void nix_store_unref(Store *store) { delete store; } - -nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest, - unsigned int n) { - if (context) - context->last_err_code = NIX_OK; - try { - auto res = store->ptr->getUri(); - return nix_export_std_string(context, res, dest, n); - } - NIXC_CATCH_ERRS -} - -nix_err nix_store_get_version(nix_c_context *context, Store *store, char *dest, - unsigned int n) { - if (context) - context->last_err_code = NIX_OK; - try { - auto res = store->ptr->getVersion(); - if (res) { - return nix_export_std_string(context, *res, dest, n); - } else { - return nix_set_err_msg(context, NIX_ERR_UNKNOWN, - "store does not have a version"); +nix_err nix_init_plugins(nix_c_context * context) +{ + if (context) + context->last_err_code = NIX_OK; + try { + nix::initPlugins(); } - } - NIXC_CATCH_ERRS + NIXC_CATCH_ERRS } -bool nix_store_is_valid_path(nix_c_context *context, Store *store, - StorePath *path) { - if (context) - context->last_err_code = NIX_OK; - try { - return store->ptr->isValidPath(path->path); - } - NIXC_CATCH_ERRS_RES(false); -} +Store * nix_store_open(nix_c_context * context, const char * uri, const char *** params) +{ + if (context) + context->last_err_code = NIX_OK; + try { + if (!uri) { + return new Store{nix::openStore()}; + } else { + std::string uri_str = uri; + if (!params) + return new Store{nix::openStore(uri_str)}; -StorePath *nix_store_parse_path(nix_c_context *context, Store *store, - const char *path) { - if (context) - context->last_err_code = NIX_OK; - try { - nix::StorePath s = store->ptr->parseStorePath(path); - return new StorePath{std::move(s)}; - } - NIXC_CATCH_ERRS_NULL -} - -nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, - void *userdata, - void (*callback)(void *userdata, const char *, - const char *)) { - if (context) - context->last_err_code = NIX_OK; - try { - store->ptr->buildPaths({ - nix::DerivedPath::Built{ - .drvPath = path->path, - .outputs = nix::OutputsSpec::All{}, - }, - }); - if (callback) { - for (auto &[outputName, outputPath] : - store->ptr->queryDerivationOutputMap(path->path)) { - auto op = store->ptr->printStorePath(outputPath); - callback(userdata, outputName.c_str(), op.c_str()); - } + nix::Store::Params params_map; + for (size_t i = 0; params[i] != nullptr; i++) { + params_map[params[i][0]] = params[i][1]; + } + return new Store{nix::openStore(uri_str, params_map)}; + } } - } - NIXC_CATCH_ERRS + NIXC_CATCH_ERRS_NULL } -void nix_store_path_free(StorePath *sp) { delete sp; } +void nix_store_unref(Store * store) +{ + delete store; +} + +nix_err nix_store_get_uri(nix_c_context * context, Store * store, char * dest, unsigned int n) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto res = store->ptr->getUri(); + return nix_export_std_string(context, res, dest, n); + } + NIXC_CATCH_ERRS +} + +nix_err nix_store_get_version(nix_c_context * context, Store * store, char * dest, unsigned int n) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto res = store->ptr->getVersion(); + if (res) { + return nix_export_std_string(context, *res, dest, n); + } else { + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "store does not have a version"); + } + } + NIXC_CATCH_ERRS +} + +bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * path) +{ + if (context) + context->last_err_code = NIX_OK; + try { + return store->ptr->isValidPath(path->path); + } + NIXC_CATCH_ERRS_RES(false); +} + +StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const char * path) +{ + if (context) + context->last_err_code = NIX_OK; + try { + nix::StorePath s = store->ptr->parseStorePath(path); + return new StorePath{std::move(s)}; + } + NIXC_CATCH_ERRS_NULL +} + +nix_err nix_store_build( + nix_c_context * context, + Store * store, + StorePath * path, + void * userdata, + void (*callback)(void * userdata, const char *, const char *)) +{ + if (context) + context->last_err_code = NIX_OK; + try { + store->ptr->buildPaths({ + nix::DerivedPath::Built{ + .drvPath = path->path, + .outputs = nix::OutputsSpec::All{}, + }, + }); + if (callback) { + for (auto & [outputName, outputPath] : store->ptr->queryDerivationOutputMap(path->path)) { + auto op = store->ptr->printStorePath(outputPath); + callback(userdata, outputName.c_str(), op.c_str()); + } + } + } + NIXC_CATCH_ERRS +} + +void nix_store_path_free(StorePath * sp) +{ + delete sp; +} diff --git a/src/libstore/c/nix_api_store.h b/src/libstore/c/nix_api_store.h index 43ded1860..91abdb201 100644 --- a/src/libstore/c/nix_api_store.h +++ b/src/libstore/c/nix_api_store.h @@ -33,7 +33,7 @@ typedef struct StorePath StorePath; * @param[out] context Optional, stores error information * @return NIX_OK if the initialization was successful, an error code otherwise. */ -nix_err nix_libstore_init(nix_c_context *context); +nix_err nix_libstore_init(nix_c_context * context); /** * @brief Loads the plugins specified in Nix's plugin-files setting. @@ -44,7 +44,7 @@ nix_err nix_libstore_init(nix_c_context *context); * @param[out] context Optional, stores error information * @return NIX_OK if the initialization was successful, an error code otherwise. */ -nix_err nix_init_plugins(nix_c_context *context); +nix_err nix_init_plugins(nix_c_context * context); /** * @brief Open a nix store @@ -55,7 +55,7 @@ nix_err nix_init_plugins(nix_c_context *context); * @return ref-counted Store pointer, NULL in case of errors * @see nix_store_unref */ -Store *nix_store_open(nix_c_context *, const char *uri, const char ***params); +Store * nix_store_open(nix_c_context *, const char * uri, const char *** params); /** * @brief Unref a nix store @@ -64,7 +64,7 @@ Store *nix_store_open(nix_c_context *, const char *uri, const char ***params); * It'll be closed and deallocated when all references are gone. * @param[in] builder the store to unref */ -void nix_store_unref(Store *store); +void nix_store_unref(Store * store); /** * @brief get the URI of a nix store @@ -74,8 +74,7 @@ void nix_store_unref(Store *store); * @param[in] n Maximum size of the returned string. * @return error code, NIX_OK on success. */ -nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest, - unsigned int n); +nix_err nix_store_get_uri(nix_c_context * context, Store * store, char * dest, unsigned int n); // returns: owned StorePath* /** @@ -87,25 +86,24 @@ nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest, * @param[in] path Path string to parse, copied * @return owned store path, NULL on error */ -StorePath *nix_store_parse_path(nix_c_context *context, Store *store, - const char *path); +StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const char * path); /** @brief Deallocate a StorePath * * Does not fail. * @param[in] p the path to free */ -void nix_store_path_free(StorePath *p); +void nix_store_path_free(StorePath * p); /** - * @brief Check if a StorePath is valid (i.e. that corresponding store object and its closure of references exists in the store) + * @brief Check if a StorePath is valid (i.e. that corresponding store object and its closure of references exists in + * the store) * @param[out] context Optional, stores error information * @param[in] store Nix Store reference * @param[in] path Path to check * @return true or false, error info in context */ -bool nix_store_is_valid_path(nix_c_context *context, Store *store, - StorePath *path); +bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * path); // nix_err nix_store_ensure(Store*, const char*); // nix_err nix_store_build_paths(Store*); /** @@ -119,10 +117,12 @@ bool nix_store_is_valid_path(nix_c_context *context, Store *store, * @param[in] userdata data to pass to every callback invocation * @param[in] callback called for every realised output */ -nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, - void *userdata, - void (*callback)(void *userdata, const char *outname, - const char *out)); +nix_err nix_store_build( + nix_c_context * context, + Store * store, + StorePath * path, + void * userdata, + void (*callback)(void * userdata, const char * outname, const char * out)); /** * @brief get the version of a nix store @@ -132,8 +132,7 @@ nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path, * @param[in] n Maximum size of the returned string. * @return error code, NIX_OK on success. */ -nix_err nix_store_get_version(nix_c_context *, Store *store, char *dest, - unsigned int n); +nix_err nix_store_get_version(nix_c_context *, Store * store, char * dest, unsigned int n); // cffi end #ifdef __cplusplus diff --git a/src/libstore/c/nix_api_store_internal.h b/src/libstore/c/nix_api_store_internal.h index 59524ea8e..ef5edc788 100644 --- a/src/libstore/c/nix_api_store_internal.h +++ b/src/libstore/c/nix_api_store_internal.h @@ -2,7 +2,8 @@ #define NIX_API_STORE_INTERNAL_H #include "store-api.hh" -struct Store { - nix::ref ptr; +struct Store +{ + nix::ref ptr; }; #endif diff --git a/src/libutil/c/nix_api_util.cc b/src/libutil/c/nix_api_util.cc index 874ccdbb5..100e3b21d 100644 --- a/src/libutil/c/nix_api_util.cc +++ b/src/libutil/c/nix_api_util.cc @@ -7,139 +7,145 @@ #include #include -nix_c_context *nix_c_context_create() { return new nix_c_context(); } +nix_c_context * nix_c_context_create() +{ + return new nix_c_context(); +} -void nix_c_context_free(nix_c_context *context) { delete context; } +void nix_c_context_free(nix_c_context * context) +{ + delete context; +} -nix_err nix_context_error(nix_c_context *context) { - if (context == nullptr) { - throw; - } - try { - throw; - } catch (nix::Error &e) { - /* Storing this exception is annoying, take what we need here */ - context->last_err = e.what(); - context->info = e.info(); - int status; - const char *demangled = - abi::__cxa_demangle(typeid(e).name(), 0, 0, &status); - if (demangled) { - context->name = demangled; - // todo: free(demangled); - } else { - context->name = typeid(e).name(); +nix_err nix_context_error(nix_c_context * context) +{ + if (context == nullptr) { + throw; } - context->last_err_code = NIX_ERR_NIX_ERROR; - return context->last_err_code; - } catch (const std::exception &e) { - context->last_err = e.what(); - context->last_err_code = NIX_ERR_UNKNOWN; - return context->last_err_code; - } - // unreachable + try { + throw; + } catch (nix::Error & e) { + /* Storing this exception is annoying, take what we need here */ + context->last_err = e.what(); + context->info = e.info(); + int status; + const char * demangled = abi::__cxa_demangle(typeid(e).name(), 0, 0, &status); + if (demangled) { + context->name = demangled; + // todo: free(demangled); + } else { + context->name = typeid(e).name(); + } + context->last_err_code = NIX_ERR_NIX_ERROR; + return context->last_err_code; + } catch (const std::exception & e) { + context->last_err = e.what(); + context->last_err_code = NIX_ERR_UNKNOWN; + return context->last_err_code; + } + // unreachable } -nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg) { - if (context == nullptr) { - // todo last_err_code - throw nix::Error("Nix C api error: %s", msg); - } - context->last_err_code = err; - context->last_err = msg; - return err; +nix_err nix_set_err_msg(nix_c_context * context, nix_err err, const char * msg) +{ + if (context == nullptr) { + // todo last_err_code + throw nix::Error("Nix C api error: %s", msg); + } + context->last_err_code = err; + context->last_err = msg; + return err; } -const char *nix_version_get() { return PACKAGE_VERSION; } +const char * nix_version_get() +{ + return PACKAGE_VERSION; +} // Implementations -nix_err nix_setting_get(nix_c_context *context, const char *key, char *value, - int n) { - if (context) - context->last_err_code = NIX_OK; - try { - std::map settings; - nix::globalConfig.getSettings(settings); - if (settings.contains(key)) - return nix_export_std_string(context, settings[key].value, value, n); - else { - return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); +nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n) +{ + if (context) + context->last_err_code = NIX_OK; + try { + std::map settings; + nix::globalConfig.getSettings(settings); + if (settings.contains(key)) + return nix_export_std_string(context, settings[key].value, value, n); + else { + return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); + } } - } - NIXC_CATCH_ERRS + NIXC_CATCH_ERRS } -nix_err nix_setting_set(nix_c_context *context, const char *key, - const char *value) { - if (context) - context->last_err_code = NIX_OK; - if (nix::globalConfig.set(key, value)) - return NIX_OK; - else { - return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); - } +nix_err nix_setting_set(nix_c_context * context, const char * key, const char * value) +{ + if (context) + context->last_err_code = NIX_OK; + if (nix::globalConfig.set(key, value)) + return NIX_OK; + else { + return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); + } } -nix_err nix_libutil_init(nix_c_context *context) { - if (context) - context->last_err_code = NIX_OK; - try { - nix::initLibUtil(); - return NIX_OK; - } - NIXC_CATCH_ERRS +nix_err nix_libutil_init(nix_c_context * context) +{ + if (context) + context->last_err_code = NIX_OK; + try { + nix::initLibUtil(); + return NIX_OK; + } + NIXC_CATCH_ERRS } -const char *nix_err_msg(nix_c_context *context, - const nix_c_context *read_context, unsigned int *n) { - if (context) - context->last_err_code = NIX_OK; - if (read_context->last_err) { - if (n) - *n = read_context->last_err->size(); - return read_context->last_err->c_str(); - } - nix_set_err_msg(context, NIX_ERR_UNKNOWN, "No error message"); - return nullptr; +const char * nix_err_msg(nix_c_context * context, const nix_c_context * read_context, unsigned int * n) +{ + if (context) + context->last_err_code = NIX_OK; + if (read_context->last_err) { + if (n) + *n = read_context->last_err->size(); + return read_context->last_err->c_str(); + } + nix_set_err_msg(context, NIX_ERR_UNKNOWN, "No error message"); + return nullptr; } -nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context, - char *value, int n) { - if (context) - context->last_err_code = NIX_OK; - if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { - return nix_set_err_msg(context, NIX_ERR_UNKNOWN, - "Last error was not a nix error"); - } - return nix_export_std_string(context, read_context->name, value, n); +nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n) +{ + if (context) + context->last_err_code = NIX_OK; + if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error"); + } + return nix_export_std_string(context, read_context->name, value, n); } -nix_err nix_err_info_msg(nix_c_context *context, - const nix_c_context *read_context, char *value, - int n) { - if (context) - context->last_err_code = NIX_OK; - if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { - return nix_set_err_msg(context, NIX_ERR_UNKNOWN, - "Last error was not a nix error"); - } - return nix_export_std_string(context, read_context->info->msg.str(), value, - n); +nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n) +{ + if (context) + context->last_err_code = NIX_OK; + if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error"); + } + return nix_export_std_string(context, read_context->info->msg.str(), value, n); } -nix_err nix_err_code(const nix_c_context *read_context) { - return read_context->last_err_code; +nix_err nix_err_code(const nix_c_context * read_context) +{ + return read_context->last_err_code; } // internal -nix_err nix_export_std_string(nix_c_context *context, - const std::string_view str, char *dest, - unsigned int n) { - size_t i = str.copy(dest, n - 1); - dest[i] = 0; - if (i == n - 1) { - return nix_set_err_msg(context, NIX_ERR_OVERFLOW, - "Provided buffer too short"); - } else - return NIX_OK; +nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n) +{ + size_t i = str.copy(dest, n - 1); + dest[i] = 0; + if (i == n - 1) { + return nix_set_err_msg(context, NIX_ERR_OVERFLOW, "Provided buffer too short"); + } else + return NIX_OK; } diff --git a/src/libutil/c/nix_api_util.h b/src/libutil/c/nix_api_util.h index 4a7f6c4cd..de029ba10 100644 --- a/src/libutil/c/nix_api_util.h +++ b/src/libutil/c/nix_api_util.h @@ -127,12 +127,12 @@ typedef struct nix_c_context nix_c_context; * @return allocated nix_c_context, owned by the caller. Free using * `nix_c_context_free`. */ -nix_c_context *nix_c_context_create(); +nix_c_context * nix_c_context_create(); /** * @brief Free a nix_c_context. Does not fail. * @param[out] context The context to free, mandatory. */ -void nix_c_context_free(nix_c_context *context); +void nix_c_context_free(nix_c_context * context); /** * @} */ @@ -147,7 +147,7 @@ void nix_c_context_free(nix_c_context *context); * @return NIX_OK if the initialization is successful, or an error code * otherwise. */ -nix_err nix_libutil_init(nix_c_context *context); +nix_err nix_libutil_init(nix_c_context * context); /** @defgroup settings * @{ @@ -167,8 +167,7 @@ nix_err nix_libutil_init(nix_c_context *context); * provided buffer is too short, or NIX_OK if the setting was retrieved * successfully. */ -nix_err nix_setting_get(nix_c_context *context, const char *key, char *value, - int n); +nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n); /** * @brief Sets a setting in the nix global configuration. @@ -184,8 +183,7 @@ nix_err nix_setting_get(nix_c_context *context, const char *key, char *value, * @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was * set successfully. */ -nix_err nix_setting_set(nix_c_context *context, const char *key, - const char *value); +nix_err nix_setting_set(nix_c_context * context, const char * key, const char * value); /** * @} @@ -198,7 +196,7 @@ nix_err nix_setting_set(nix_c_context *context, const char *key, * Does not fail. * @return A static string representing the version of the nix library. */ -const char *nix_version_get(); +const char * nix_version_get(); /** @addtogroup errors * @{ @@ -217,8 +215,7 @@ const char *nix_version_get(); * @return nullptr if no error message was ever set, * a borrowed pointer to the error message otherwise. */ -const char *nix_err_msg(nix_c_context *context, const nix_c_context *ctx, - unsigned int *n); +const char * nix_err_msg(nix_c_context * context, const nix_c_context * ctx, unsigned int * n); /** * @brief Retrieves the error message from errorInfo in a context. @@ -235,8 +232,7 @@ const char *nix_err_msg(nix_c_context *context, const nix_c_context *ctx, * @param[in] n Maximum size of the returned string. * @return NIX_OK if there were no errors, an error code otherwise. */ -nix_err nix_err_info_msg(nix_c_context *context, - const nix_c_context *read_context, char *value, int n); +nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n); /** * @brief Retrieves the error name from a context. @@ -253,8 +249,7 @@ nix_err nix_err_info_msg(nix_c_context *context, * @param[in] n Maximum size of the returned string. * @return NIX_OK if there were no errors, an error code otherwise. */ -nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context, - char *value, int n); +nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n); /** * @brief Retrieves the most recent error code from a nix_c_context @@ -266,7 +261,7 @@ nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context, * @param[in] read_context the context to retrieve the error message from * @return most recent error code stored in the context. */ -nix_err nix_err_code(const nix_c_context *read_context); +nix_err nix_err_code(const nix_c_context * read_context); /** * @} diff --git a/src/libutil/c/nix_api_util_internal.h b/src/libutil/c/nix_api_util_internal.h index 013d3bbbb..1aaf328c1 100644 --- a/src/libutil/c/nix_api_util_internal.h +++ b/src/libutil/c/nix_api_util_internal.h @@ -7,14 +7,15 @@ #include "error.hh" #include "nix_api_util.h" -struct nix_c_context { - nix_err last_err_code = NIX_OK; - std::optional last_err = {}; - std::optional info = {}; - std::string name = ""; +struct nix_c_context +{ + nix_err last_err_code = NIX_OK; + std::optional last_err = {}; + std::optional info = {}; + std::string name = ""; }; -nix_err nix_context_error(nix_c_context *context); +nix_err nix_context_error(nix_c_context * context); /** * Internal use only. @@ -26,7 +27,7 @@ nix_err nix_context_error(nix_c_context *context); * @param msg The error message to set. * @returns the error code set */ -nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg); +nix_err nix_set_err_msg(nix_c_context * context, nix_err err, const char * msg); /** * Internal use only. @@ -40,21 +41,21 @@ nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg); * @return NIX_OK if there were no errors, NIX_ERR_OVERFLOW if the string length * exceeds `n`. */ -nix_err nix_export_std_string(nix_c_context *context, - const std::string_view str, char *dest, - unsigned int n); +nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n); -#define NIXC_CATCH_ERRS \ - catch (...) { \ - return nix_context_error(context); \ - } \ - return NIX_OK; +#define NIXC_CATCH_ERRS \ + catch (...) \ + { \ + return nix_context_error(context); \ + } \ + return NIX_OK; -#define NIXC_CATCH_ERRS_RES(def) \ - catch (...) { \ - nix_context_error(context); \ - return def; \ - } +#define NIXC_CATCH_ERRS_RES(def) \ + catch (...) \ + { \ + nix_context_error(context); \ + return def; \ + } #define NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_RES(nullptr) #endif // NIX_API_UTIL_INTERNAL_H From 9e423dee11572e6171f33e2645762e6f2bf11980 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 28 Aug 2023 17:27:05 +0200 Subject: [PATCH 102/168] C API: update after rebase --- src/libexpr/c/local.mk | 2 +- src/libstore/c/nix_api_store.cc | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libexpr/c/local.mk b/src/libexpr/c/local.mk index d2f01c0a9..01b03f4d2 100644 --- a/src/libexpr/c/local.mk +++ b/src/libexpr/c/local.mk @@ -9,7 +9,7 @@ libexprc_SOURCES := \ libexprc_CXXFLAGS += -I src/libutil -Isrc/libfetchers -I src/libstore -I src/libstorec -I src/libexpr -I src/libutil/c -I src/libstore/c -libexprc_LIBS = libutil libutilc libstorec libexpr +libexprc_LIBS = libutil libutilc libstore libstorec libexpr libexprc_LDFLAGS += -pthread diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index 4ee97c8a1..496b20534 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -3,6 +3,7 @@ #include "nix_api_util.h" #include "nix_api_util_internal.h" +#include "path.hh" #include "store-api.hh" #include "globals.hh" @@ -118,7 +119,7 @@ nix_err nix_store_build( try { store->ptr->buildPaths({ nix::DerivedPath::Built{ - .drvPath = path->path, + .drvPath = nix::makeConstantStorePathRef(path->path), .outputs = nix::OutputsSpec::All{}, }, }); From 48aa57549d514432d6621c1e29f051951eca2d7f Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 28 Aug 2023 18:20:23 +0200 Subject: [PATCH 103/168] primops: change to std::function, allowing the passing of user data --- src/libexpr/eval.hh | 3 ++- src/libexpr/primops.cc | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index f15d19653..a1b0e58e4 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -17,6 +17,7 @@ #include #include #include +#include namespace nix { @@ -72,7 +73,7 @@ struct PrimOp /** * Implementation of the primop. */ - PrimOpFun fun; + std::function::type> fun; /** * Optional experimental for this to be gated on. diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index db4237130..a619a627a 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -3402,8 +3402,11 @@ static void prim_sort(EvalState & state, const PosIdx pos, Value * * args, Value callFunction. */ /* TODO: (layus) this is absurd. An optimisation like this should be outside the lambda creation */ - if (args[0]->isPrimOp() && args[0]->primOp->fun == prim_lessThan) - return CompareValues(state, noPos, "while evaluating the ordering function passed to builtins.sort")(a, b); + if (args[0]->isPrimOp()) { + auto ptr = args[0]->primOp->fun.target(); + if (ptr && *ptr == prim_lessThan) + return CompareValues(state, noPos, "while evaluating the ordering function passed to builtins.sort")(a, b); + } Value * vs[] = {a, b}; Value vBool; From 3d79f3870926f420560cb63c82e872905ae72766 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 28 Aug 2023 18:20:52 +0200 Subject: [PATCH 104/168] C API: add user_data argument to nix_alloc_primop Also add a helper function for primops, that converts to C argument types (and eventually handles errors) --- src/libexpr/c/nix_api_value.cc | 27 ++++++++++++++++++++++++--- src/libexpr/c/nix_api_value.h | 19 ++++++++++++++----- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index 608a625cb..9a313d8fd 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -34,18 +34,39 @@ static nix::Value & check_value_not_null(Value * value) return *((nix::Value *) value); } +/** + * Helper function to convert calls from nix into C API. + * + * Deals with errors and converts arguments from C++ into C types. + */ +static void nix_c_primop_wrapper( + PrimOpFun f, void * userdata, nix::EvalState & state, const nix::PosIdx pos, nix::Value ** args, nix::Value & v) +{ + f(userdata, (State *) &state, *reinterpret_cast(&pos), (Value **) args, (Value *) &v); +} + PrimOp * nix_alloc_primop( - nix_c_context * context, PrimOpFun fun, int arity, const char * name, const char ** args, const char * doc) + nix_c_context * context, + PrimOpFun fun, + int arity, + const char * name, + const char ** args, + const char * doc, + void * user_data) { if (context) context->last_err_code = NIX_OK; try { - auto fun2 = (nix::PrimOpFun) fun; auto p = new #ifdef HAVE_BOEHMGC (GC) #endif - nix::PrimOp{.name = name, .args = {}, .arity = (size_t) arity, .doc = doc, .fun = fun2}; + nix::PrimOp{ + .name = name, + .args = {}, + .arity = (size_t) arity, + .doc = doc, + .fun = std::bind_front(nix_c_primop_wrapper, fun, user_data)}; if (args) for (size_t i = 0; args[i]; i++) p->args.emplace_back(*args); diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index 21647552b..ffba4c097 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -65,6 +65,7 @@ typedef struct ExternalValue ExternalValue; * @{ */ /** @brief Function pointer for primops + * @param[in] user_data Arbitrary data, passed to nix_alloc_primop and stored. * @param[in] state Evaluator state * @param[in] pos Call position, opaque index into the state's position table. * @param[in] args list of arguments. Note that these can be thunks and should be forced using nix_value_force before @@ -72,7 +73,7 @@ typedef struct ExternalValue ExternalValue; * @param[out] ret return value * @see nix_alloc_primop, nix_set_primop */ -typedef void (*PrimOpFun)(State * state, int pos, Value ** args, Value * ret); +typedef void (*PrimOpFun)(void * user_data, State * state, int pos, Value ** args, Value * ret); /** @brief Allocate a PrimOp * @@ -85,19 +86,27 @@ typedef void (*PrimOpFun)(State * state, int pos, Value ** args, Value * ret); * @param[in] name function name * @param[in] args array of argument names, NULL-terminated * @param[in] doc optional, documentation for this primop + * @param[in] user_data optional, arbitrary data, passed to the function when it's called * @return primop, or null in case of errors * @see nix_set_primop */ PrimOp * nix_alloc_primop( - nix_c_context * context, PrimOpFun fun, int arity, const char * name, const char ** args, const char * doc); + nix_c_context * context, + PrimOpFun fun, + int arity, + const char * name, + const char ** args, + const char * doc, + void * user_data); /** @brief add a primop to the `builtins` attribute set * * Only applies to States created after this call. * - * Moves your PrimOp into the global evaluator - * registry, meaning your input PrimOp pointer is no longer usable - * (but still possibly subject to garbage collection). + * Moves your PrimOp content into the global evaluator + * registry, meaning your input PrimOp pointer is no longer usable. + * You are free to remove your references to it, + * after which it will be garbage collected. * * @param[out] context Optional, stores error information * @return primop, or null in case of errors From ab9250286afa65737503de7019fdf079b3de6e82 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 28 Aug 2023 22:19:28 +0200 Subject: [PATCH 105/168] C API: add a way to throw errors from primops --- doc/external-api/README.md | 8 ++++---- src/libexpr/c/nix_api_value.cc | 7 ++++++- src/libexpr/c/nix_api_value.h | 10 ++++++---- src/libutil/c/nix_api_util.h | 14 ++++++++++++++ src/libutil/c/nix_api_util_internal.h | 12 ------------ 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/doc/external-api/README.md b/doc/external-api/README.md index 8b9061df2..3b802952c 100644 --- a/doc/external-api/README.md +++ b/doc/external-api/README.md @@ -58,7 +58,7 @@ Nix version: 2.17 # Writing a Nix language plug-in In this example we add a custom primitive operation (*primop*) to `builtins`. -It will increment the argument if it is an integer and return `null` otherwise. +It will increment the argument if it is an integer and throw an error otherwise. **plugin.c:** ```C @@ -66,18 +66,18 @@ It will increment the argument if it is an integer and return `null` otherwise. #include #include -void increment(State* state, int pos, Value** args, Value* v) { +void increment(void* user_data, nix_c_context* ctx, State* state, Value** args, Value* v) { nix_value_force(NULL, state, args[0]); if (nix_get_type(NULL, args[0]) == NIX_TYPE_INT) { nix_set_int(NULL, v, nix_get_int(NULL, args[0]) + 1); } else { - nix_set_null(NULL, v); + nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "First argument should be an integer."); } } void nix_plugin_entry() { const char* args[] = {"n", NULL}; - PrimOp *p = nix_alloc_primop(NULL, increment, 1, "increment", args, "Example custom built-in function: increments an integer"); + PrimOp *p = nix_alloc_primop(NULL, increment, 1, "increment", args, "Example custom built-in function: increments an integer", NULL); nix_register_primop(NULL, p); nix_gc_decref(NULL, p); } diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index 9a313d8fd..8b73729a5 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -42,7 +42,12 @@ static nix::Value & check_value_not_null(Value * value) static void nix_c_primop_wrapper( PrimOpFun f, void * userdata, nix::EvalState & state, const nix::PosIdx pos, nix::Value ** args, nix::Value & v) { - f(userdata, (State *) &state, *reinterpret_cast(&pos), (Value **) args, (Value *) &v); + nix_c_context ctx; + f(userdata, &ctx, (State *) &state, (Value **) args, (Value *) &v); + /* TODO: In the future, this should throw different errors depending on the error code */ + if (ctx.last_err_code != NIX_OK) + state.debugThrowLastTrace(nix::Error( + {.msg = nix::hintfmt("Error from builtin function: %s", *ctx.last_err), .errPos = state.positions[pos]})); } PrimOp * nix_alloc_primop( diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index ffba4c097..ca4e83cf4 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -65,15 +65,17 @@ typedef struct ExternalValue ExternalValue; * @{ */ /** @brief Function pointer for primops - * @param[in] user_data Arbitrary data, passed to nix_alloc_primop and stored. + * When you want to return an error, call nix_set_err_msg(context, NIX_ERR_UNKNOWN, "your error message here"). + * + * @param[in] user_data Arbitrary data that was initially supplied to nix_alloc_primop + * @param[out] context Stores error information. * @param[in] state Evaluator state - * @param[in] pos Call position, opaque index into the state's position table. * @param[in] args list of arguments. Note that these can be thunks and should be forced using nix_value_force before * use. * @param[out] ret return value * @see nix_alloc_primop, nix_set_primop */ -typedef void (*PrimOpFun)(void * user_data, State * state, int pos, Value ** args, Value * ret); +typedef void (*PrimOpFun)(void * user_data, nix_c_context * context, State * state, Value ** args, Value * ret); /** @brief Allocate a PrimOp * @@ -86,7 +88,7 @@ typedef void (*PrimOpFun)(void * user_data, State * state, int pos, Value ** arg * @param[in] name function name * @param[in] args array of argument names, NULL-terminated * @param[in] doc optional, documentation for this primop - * @param[in] user_data optional, arbitrary data, passed to the function when it's called + * @param[in] user_data optional, arbitrary data, passed to the callback when it's called * @return primop, or null in case of errors * @see nix_set_primop */ diff --git a/src/libutil/c/nix_api_util.h b/src/libutil/c/nix_api_util.h index de029ba10..c288654fd 100644 --- a/src/libutil/c/nix_api_util.h +++ b/src/libutil/c/nix_api_util.h @@ -263,6 +263,20 @@ nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context */ nix_err nix_err_code(const nix_c_context * read_context); +/** + * @brief Set an error message on a nix context. + * + * This should be used when you want to throw an error from a PrimOp callback. + * + * All other use is internal to the API. + * + * @param context context to write the error message to, or NULL + * @param err The error code to set and return + * @param msg The error message to set. + * @returns the error code set + */ +nix_err nix_set_err_msg(nix_c_context * context, nix_err err, const char * msg); + /** * @} */ diff --git a/src/libutil/c/nix_api_util_internal.h b/src/libutil/c/nix_api_util_internal.h index 1aaf328c1..53c260e35 100644 --- a/src/libutil/c/nix_api_util_internal.h +++ b/src/libutil/c/nix_api_util_internal.h @@ -17,18 +17,6 @@ struct nix_c_context nix_err nix_context_error(nix_c_context * context); -/** - * Internal use only. - * - * Sets the most recent error message. - * - * @param context context to write the error message to, or NULL - * @param err The error code to set and return - * @param msg The error message to set. - * @returns the error code set - */ -nix_err nix_set_err_msg(nix_c_context * context, nix_err err, const char * msg); - /** * Internal use only. * From c6e28d8da238861432b9d1f9010dc7c25841ac78 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Mon, 28 Aug 2023 23:17:43 +0200 Subject: [PATCH 106/168] C API: fix: macos doesn't have std::bind_front --- src/libexpr/c/nix_api_value.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index 8b73729a5..2348b75ed 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -62,6 +62,7 @@ PrimOp * nix_alloc_primop( if (context) context->last_err_code = NIX_OK; try { + using namespace std::placeholders; auto p = new #ifdef HAVE_BOEHMGC (GC) @@ -71,7 +72,7 @@ PrimOp * nix_alloc_primop( .args = {}, .arity = (size_t) arity, .doc = doc, - .fun = std::bind_front(nix_c_primop_wrapper, fun, user_data)}; + .fun = std::bind(nix_c_primop_wrapper, fun, user_data, _1, _2, _3, _4)}; if (args) for (size_t i = 0; args[i]; i++) p->args.emplace_back(*args); From 550af113c6877654ce29e457a09a0ba46169ebbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Tue, 28 Nov 2023 15:05:04 +0100 Subject: [PATCH 107/168] String value refactor Related to https://github.com/NixOS/nix/pull/9047 --- src/libexpr/c/nix_api_value.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index 2348b75ed..d013d5333 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -171,7 +171,7 @@ const char * nix_get_string(nix_c_context * context, const Value * value) try { auto & v = check_value_not_null(value); assert(v.type() == nix::nString); - return v.string.s; + return v.c_str(); } NIXC_CATCH_ERRS_NULL } @@ -183,7 +183,7 @@ const char * nix_get_path_string(nix_c_context * context, const Value * value) try { auto & v = check_value_not_null(value); assert(v.type() == nix::nPath); - return v._path; + return v.path().to_string().c_str(); } NIXC_CATCH_ERRS_NULL } From 46f5d0ee7bb8ecc01dad3a80000ed11fd7c236d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Wed, 6 Dec 2023 15:54:23 +0100 Subject: [PATCH 108/168] Apply suggestions from code review --- src/libexpr/c/nix_api_value.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index d013d5333..b0fb960c6 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -183,7 +183,14 @@ const char * nix_get_path_string(nix_c_context * context, const Value * value) try { auto & v = check_value_not_null(value); assert(v.type() == nix::nPath); - return v.path().to_string().c_str(); + // NOTE (from @yorickvP) + // v._path.path should work but may not be how Eelco intended it. + // Long-term this function should be rewritten to copy some data into a + // user-allocated string. + // We could use v.path().to_string().c_str(), but I'm concerned this + // crashes. Looks like .path() allocates a CanonPath with a copy of the + // string, then it gets the underlying data from that. + return v._path.path; } NIXC_CATCH_ERRS_NULL } From 41f1669deab98db5c954d9bfff174dfc8f20bea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 14 Dec 2023 20:14:58 +0100 Subject: [PATCH 109/168] C API: add tests for libutil and libstore --- src/libstore/c/nix_api_store.cc | 5 -- src/libstore/c/nix_api_store_internal.h | 6 ++ tests/unit/libstore/local.mk | 2 +- tests/unit/libstore/nix_api_store.cc | 70 +++++++++++++++++++ .../libutil-support/tests/nix_api_util.hh | 25 +++++++ tests/unit/libutil/nix_api_util.cc | 50 ++++++++----- 6 files changed, 133 insertions(+), 25 deletions(-) create mode 100644 tests/unit/libstore/nix_api_store.cc create mode 100644 tests/unit/libutil-support/tests/nix_api_util.hh diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index 496b20534..4aff815ff 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -8,11 +8,6 @@ #include "globals.hh" -struct StorePath -{ - nix::StorePath path; -}; - nix_err nix_libstore_init(nix_c_context * context) { if (context) diff --git a/src/libstore/c/nix_api_store_internal.h b/src/libstore/c/nix_api_store_internal.h index ef5edc788..13db0c07c 100644 --- a/src/libstore/c/nix_api_store_internal.h +++ b/src/libstore/c/nix_api_store_internal.h @@ -6,4 +6,10 @@ struct Store { nix::ref ptr; }; + +struct StorePath +{ + nix::StorePath path; +}; + #endif diff --git a/tests/unit/libstore/local.mk b/tests/unit/libstore/local.mk index 960dece89..fe1254487 100644 --- a/tests/unit/libstore/local.mk +++ b/tests/unit/libstore/local.mk @@ -26,6 +26,6 @@ libstore-tests_CXXFLAGS += $(libstore-tests_EXTRA_INCLUDES) libstore-tests_LIBS = \ libstore-test-support libutil-test-support \ - libstore libutil + libstore libstorec libutil libutilc libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc new file mode 100644 index 000000000..3fe55ae93 --- /dev/null +++ b/tests/unit/libstore/nix_api_store.cc @@ -0,0 +1,70 @@ +#include "nix_api_util.h" +#include "nix_api_util_internal.h" +#include "nix_api_store.h" +#include "nix_api_store_internal.h" +#include "tests/nix_api_util.hh" + +#define STORE_DIR "/nix/store/" +#define HASH_PART "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q" +const char * validPath = STORE_DIR HASH_PART "-x"; + +namespace nixC { + +class nix_api_store_test : public nix_api_util_context +{ +public: + void SetUp() override + { + nix_api_util_context::SetUp(); + nix_libstore_init(ctx); + store = nix_store_open(ctx, "dummy://", NULL); + }; + void TearDown() override + { + nix_store_unref(store); + nix_api_util_context::TearDown(); + } + + Store * store; +}; + +TEST_F(nix_api_util_context, nix_libstore_init) +{ + auto ret = nix_libstore_init(ctx); + ASSERT_EQ(NIX_OK, ret); +} + +TEST_F(nix_api_store_test, nix_store_get_uri) +{ + char value[256]; + auto ret = nix_store_get_uri(ctx, store, value, 256); + ASSERT_EQ(NIX_OK, ret); + ASSERT_STREQ("dummy", value); +} + +TEST_F(nix_api_store_test, InvalidPathFails) +{ + nix_store_parse_path(ctx, store, "invalid-path"); + ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR); +} + +TEST_F(nix_api_store_test, ReturnsValidStorePath) +{ + StorePath * result = nix_store_parse_path(ctx, store, validPath); + ASSERT_NE(result, nullptr); + ASSERT_STREQ("x", result->path.name().data()); + ASSERT_STREQ(HASH_PART "-x", result->path.to_string().data()); +} + +TEST_F(nix_api_store_test, SetsLastErrCodeToNixOk) +{ + nix_store_parse_path(ctx, store, validPath); + ASSERT_EQ(ctx->last_err_code, NIX_OK); +} + +TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull) +{ + ASSERT_NO_THROW(nix_store_parse_path(nullptr, store, validPath)); +} + +} diff --git a/tests/unit/libutil-support/tests/nix_api_util.hh b/tests/unit/libutil-support/tests/nix_api_util.hh new file mode 100644 index 000000000..f2ee58da2 --- /dev/null +++ b/tests/unit/libutil-support/tests/nix_api_util.hh @@ -0,0 +1,25 @@ +#pragma once +///@file +#include "nix_api_util.h" + +#include + + +class nix_api_util_context : public ::testing::Test +{ +protected: + static void SetUpTestSuite() + { + nix_libutil_init(NULL); + } + void SetUp() override + { + ctx = nix_c_context_create(); + }; + void TearDown() override + { + nix_c_context_free(ctx); + ctx = nullptr; + } + nix_c_context * ctx; +}; diff --git a/tests/unit/libutil/nix_api_util.cc b/tests/unit/libutil/nix_api_util.cc index 26353fe84..8cc2b6616 100644 --- a/tests/unit/libutil/nix_api_util.cc +++ b/tests/unit/libutil/nix_api_util.cc @@ -3,26 +3,12 @@ #include "args.hh" #include "nix_api_util.h" #include "nix_api_util_internal.h" +#include "tests/nix_api_util.hh" #include namespace nixC { -class nix_api_util_context : public ::testing::Test { -protected: - static void SetUpTestSuite() { - nix_libutil_init(NULL); - } - void SetUp() override { - ctx = nix_c_context_create(); - }; - void TearDown() override { - nix_c_context_free(ctx); - ctx = nullptr; - } - nix_c_context* ctx; -}; - TEST_F(nix_api_util_context, nix_context_error) { std::string err_msg_ref; try { @@ -57,12 +43,38 @@ TEST(nix_api_util, nix_version_get) { ASSERT_EQ(std::string(nix_version_get()), PACKAGE_VERSION); } -TEST_F(nix_api_util_context, nix_setting_get) { - // todo +struct MySettings : nix::Config +{ + nix::Setting settingSet{this, "empty", "setting-name", "Description"}; +}; + +MySettings mySettings; +static nix::GlobalConfig::Register rs(&mySettings); + +TEST_F(nix_api_util_context, nix_setting_get) +{ + ASSERT_EQ(ctx->last_err_code, NIX_OK); + char value[256]; + nix_err result = nix_setting_get(ctx, "invalid-key", value, 256); + ASSERT_EQ(result, NIX_ERR_KEY); + + result = nix_setting_get(ctx, "setting-name", value, 256); + ASSERT_EQ(result, NIX_OK); + ASSERT_STREQ("empty", value); } -TEST_F(nix_api_util_context, nix_setting_set) { - // todo +TEST_F(nix_api_util_context, nix_setting_set) +{ + nix_err result = nix_setting_set(ctx, "invalid-key", "new-value"); + ASSERT_EQ(result, NIX_ERR_KEY); + + result = nix_setting_set(ctx, "setting-name", "new-value"); + ASSERT_EQ(result, NIX_OK); + + char value[256]; + result = nix_setting_get(ctx, "setting-name", value, 256); + ASSERT_EQ(result, NIX_OK); + ASSERT_STREQ("new-value", value); } TEST_F(nix_api_util_context, nix_err_msg) { From 55601963b3c4f40b4bdac42a4531fd3177c93935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Fri, 15 Dec 2023 00:26:45 +0100 Subject: [PATCH 110/168] C API: fix documentation build --- doc/external-api/local.mk | 6 +++--- flake.nix | 7 +++++++ package.nix | 16 ++++++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/doc/external-api/local.mk b/doc/external-api/local.mk index a0f6e26fc..aa501198b 100644 --- a/doc/external-api/local.mk +++ b/doc/external-api/local.mk @@ -1,19 +1,19 @@ .PHONY: external-api-html -ifeq ($(internal_api_docs), yes) +ifeq ($(external_api_docs), yes) $(docdir)/external-api/html/index.html $(docdir)/external-api/latex: $(d)/doxygen.cfg mkdir -p $(docdir)/external-api { cat $< ; echo "OUTPUT_DIRECTORY=$(docdir)/external-api" ; } | doxygen - -# Generate the HTML API docs for Nix's unstable internal interfaces. +# Generate the HTML API docs for Nix's unstable external interfaces. external-api-html: $(docdir)/external-api/html/index.html else # Make a nicer error message external-api-html: - @echo "Internal API docs are disabled. Configure with '--enable-external-api-docs', or avoid calling 'make external-api-html'." + @echo "External API docs are disabled. Configure with '--enable-external-api-docs', or avoid calling 'make external-api-html'." @exit 1 endif diff --git a/flake.nix b/flake.nix index 89b928e83..be4e68783 100644 --- a/flake.nix +++ b/flake.nix @@ -285,6 +285,13 @@ enableInternalAPIDocs = true; }; + # API docs for Nix's C bindings. + external-api-docs = nixpkgsFor.x86_64-linux.native.callPackage ./package.nix { + inherit fileset; + doBuild = false; + enableExternalAPIDocs = true; + }; + # System tests. tests = import ./tests/nixos { inherit lib nixpkgs nixpkgsFor; } // { diff --git a/package.nix b/package.nix index a334ae48c..af4ca9b46 100644 --- a/package.nix +++ b/package.nix @@ -5,6 +5,7 @@ , autoreconfHook , aws-sdk-cpp , boehmgc +, buildPackages , nlohmann_json , bison , boost @@ -91,9 +92,10 @@ # - readline , readlineFlavor ? if stdenv.hostPlatform.isWindows then "readline" else "editline" -# Whether to build the internal API docs, can be done separately from +# Whether to build the internal/external API docs, can be done separately from # everything else. , enableInternalAPIDocs ? false +, enableExternalAPIDocs ? false # Whether to install unit tests. This is useful when cross compiling # since we cannot run them natively during the build, but can do so @@ -199,7 +201,7 @@ in { ++ lib.optional doBuild "dev" # If we are doing just build or just docs, the one thing will use # "out". We only need additional outputs if we are doing both. - ++ lib.optional (doBuild && (enableManual || enableInternalAPIDocs)) "doc" + ++ lib.optional (doBuild && (enableManual || enableInternalAPIDocs || enableExternalAPIDocs)) "doc" ++ lib.optional installUnitTests "check"; nativeBuildInputs = [ @@ -221,7 +223,7 @@ in { ] ++ lib.optionals (doInstallCheck || enableManual) [ jq # Also for custom mdBook preprocessor. ] ++ lib.optional stdenv.hostPlatform.isLinux util-linux - ++ lib.optional enableInternalAPIDocs doxygen + ++ lib.optional (enableInternalAPIDocs || enableExternalAPIDocs) doxygen ; buildInputs = lib.optionals doBuild [ @@ -285,6 +287,7 @@ in { (lib.enableFeature buildUnitTests "unit-tests") (lib.enableFeature doInstallCheck "functional-tests") (lib.enableFeature enableInternalAPIDocs "internal-api-docs") + (lib.enableFeature enableExternalAPIDocs "external-api-docs") (lib.enableFeature enableManual "doc-gen") (lib.enableFeature enableGC "gc") (lib.enableFeature enableMarkdown "markdown") @@ -309,7 +312,8 @@ in { makeFlags = "profiledir=$(out)/etc/profile.d PRECOMPILE_HEADERS=1"; installTargets = lib.optional doBuild "install" - ++ lib.optional enableInternalAPIDocs "internal-api-html"; + ++ lib.optional enableInternalAPIDocs "internal-api-html" + ++ lib.optional enableExternalAPIDocs "external-api-html"; installFlags = "sysconfdir=$(out)/etc"; @@ -336,6 +340,10 @@ in { '' + lib.optionalString enableInternalAPIDocs '' mkdir -p ''${!outputDoc}/nix-support echo "doc internal-api-docs $out/share/doc/nix/internal-api/html" >> ''${!outputDoc}/nix-support/hydra-build-products + '' + + lib.optionalString enableExternalAPIDocs '' + mkdir -p ''${!outputDoc}/nix-support + echo "doc external-api-docs $out/share/doc/nix/external-api/html" >> ''${!outputDoc}/nix-support/hydra-build-products ''; # So the check output gets links for DLLs in the out output. From ac3a9c6605d43bb808e3ae864302141867051be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Wed, 3 Jan 2024 19:10:43 +0100 Subject: [PATCH 111/168] C API: add nix_api_expr tests --- tests/unit/libexpr/local.mk | 2 +- tests/unit/libexpr/nix_api_expr.cc | 82 +++++++++++++++++++ .../libstore-support/tests/nix_api_store.hh | 40 +++++++++ tests/unit/libstore/nix_api_store.cc | 31 +++---- .../libutil-support/tests/nix_api_util.hh | 8 +- 5 files changed, 139 insertions(+), 24 deletions(-) create mode 100644 tests/unit/libexpr/nix_api_expr.cc create mode 100644 tests/unit/libstore-support/tests/nix_api_store.hh diff --git a/tests/unit/libexpr/local.mk b/tests/unit/libexpr/local.mk index eda65508d..0a7b28436 100644 --- a/tests/unit/libexpr/local.mk +++ b/tests/unit/libexpr/local.mk @@ -32,6 +32,6 @@ libexpr-tests_CXXFLAGS += $(libexpr-tests_EXTRA_INCLUDES) libexpr-tests_LIBS = \ libexpr-test-support libstore-test-support libutils-test-support \ - libexpr libfetchers libstore libutil + libexpr libexprc libfetchers libstore libstorec libutil libutilc libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc new file mode 100644 index 000000000..03de4547a --- /dev/null +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -0,0 +1,82 @@ +#include "nix_api_store.h" +#include "nix_api_store_internal.h" +#include "nix_api_util.h" +#include "nix_api_util_internal.h" +#include "nix_api_expr.h" +#include "nix_api_value.h" + +#include "tests/nix_api_store.hh" + +#include + +namespace nixC { + +class nix_api_expr_test : public nix_api_store_test +{ +public: + nix_api_expr_test() + { + state = nix_state_create(nullptr, nullptr, store); + value = nix_alloc_value(nullptr, state); + } + ~nix_api_expr_test() + { + nix_gc_decref(nullptr, value); + nix_state_free(state); + } + + State * state; + Value * value; +}; + +TEST_F(nix_api_expr_test, nix_expr_eval_from_string) +{ + nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value); + nix_value_force(nullptr, state, value); + auto result = nix_get_string(nullptr, value); + + ASSERT_STREQ(PACKAGE_VERSION, result); +} + +TEST_F(nix_api_expr_test, nix_expr_eval_add_numbers) +{ + nix_expr_eval_from_string(nullptr, state, "1 + 1", ".", value); + nix_value_force(nullptr, state, value); + auto result = nix_get_int(nullptr, value); + + ASSERT_EQ(2, result); +} + +TEST_F(nix_api_expr_test, nix_expr_eval_drv) +{ + auto expr = R"(derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; })"; + nix_expr_eval_from_string(nullptr, state, expr, ".", value); + nix_value_force(nullptr, state, value); + + ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(nullptr, value)); + + auto stateFn = nix_state_create(nullptr, nullptr, store); + auto valueFn = nix_alloc_value(nullptr, state); + nix_expr_eval_from_string(nullptr, stateFn, "builtins.toString", ".", valueFn); + + ASSERT_EQ(NIX_TYPE_FUNCTION, nix_get_type(nullptr, valueFn)); + + auto stateResult = nix_state_create(nullptr, nullptr, store); + auto valueResult = nix_alloc_value(nullptr, stateResult); + + nix_value_call(ctx, stateResult, valueFn, value, valueResult); + nix_value_force(nullptr, stateResult, valueResult); + + auto p = nix_get_string(nullptr, valueResult); + + ASSERT_STREQ("/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname", p); + + // Clean up + nix_gc_decref(nullptr, valueFn); + nix_state_free(stateFn); + + nix_gc_decref(nullptr, valueResult); + nix_state_free(stateResult); +} + +} diff --git a/tests/unit/libstore-support/tests/nix_api_store.hh b/tests/unit/libstore-support/tests/nix_api_store.hh new file mode 100644 index 000000000..e762a3ca8 --- /dev/null +++ b/tests/unit/libstore-support/tests/nix_api_store.hh @@ -0,0 +1,40 @@ +#pragma once +///@file +#include "tests/nix_api_util.hh" + +#include "nix_api_store.h" +#include "nix_api_store_internal.h" + +#include +#include + +namespace fs = std::filesystem; + +class nix_api_store_test : public nix_api_util_context +{ +public: + nix_api_store_test() + { + nix_libstore_init(ctx); + + auto tmpl = nix::getEnv("TMPDIR").value_or("/tmp") + "/tests_nix-store.XXXXXX"; + nixStoreDir = mkdtemp((char *) tmpl.c_str()); + + // Options documented in `nix help-stores` + const char * p1[] = {"root", nixStoreDir.c_str()}; + const char ** params[] = {p1, nullptr}; + store = nix_store_open(ctx, "local", params); + }; + ~nix_api_store_test() override + { + nix_store_unref(store); + + for (auto & path : fs::recursive_directory_iterator(nixStoreDir)) { + fs::permissions(path, fs::perms::owner_all); + } + fs::remove_all(nixStoreDir); + } + + Store * store; + std::string nixStoreDir; +}; diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc index 3fe55ae93..764cd0d88 100644 --- a/tests/unit/libstore/nix_api_store.cc +++ b/tests/unit/libstore/nix_api_store.cc @@ -2,7 +2,8 @@ #include "nix_api_util_internal.h" #include "nix_api_store.h" #include "nix_api_store_internal.h" -#include "tests/nix_api_util.hh" + +#include "tests/nix_api_store.hh" #define STORE_DIR "/nix/store/" #define HASH_PART "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q" @@ -10,24 +11,6 @@ const char * validPath = STORE_DIR HASH_PART "-x"; namespace nixC { -class nix_api_store_test : public nix_api_util_context -{ -public: - void SetUp() override - { - nix_api_util_context::SetUp(); - nix_libstore_init(ctx); - store = nix_store_open(ctx, "dummy://", NULL); - }; - void TearDown() override - { - nix_store_unref(store); - nix_api_util_context::TearDown(); - } - - Store * store; -}; - TEST_F(nix_api_util_context, nix_libstore_init) { auto ret = nix_libstore_init(ctx); @@ -39,7 +22,7 @@ TEST_F(nix_api_store_test, nix_store_get_uri) char value[256]; auto ret = nix_store_get_uri(ctx, store, value, 256); ASSERT_EQ(NIX_OK, ret); - ASSERT_STREQ("dummy", value); + ASSERT_STREQ("local", value); } TEST_F(nix_api_store_test, InvalidPathFails) @@ -67,4 +50,12 @@ TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull) ASSERT_NO_THROW(nix_store_parse_path(nullptr, store, validPath)); } +TEST_F(nix_api_store_test, get_version) +{ + char value[256]; + auto ret = nix_store_get_version(ctx, store, value, 256); + ASSERT_EQ(NIX_OK, ret); + ASSERT_STREQ(PACKAGE_VERSION, value); +} + } diff --git a/tests/unit/libutil-support/tests/nix_api_util.hh b/tests/unit/libutil-support/tests/nix_api_util.hh index f2ee58da2..b007ac4b1 100644 --- a/tests/unit/libutil-support/tests/nix_api_util.hh +++ b/tests/unit/libutil-support/tests/nix_api_util.hh @@ -4,7 +4,6 @@ #include - class nix_api_util_context : public ::testing::Test { protected: @@ -12,14 +11,17 @@ protected: { nix_libutil_init(NULL); } - void SetUp() override + + nix_api_util_context() { ctx = nix_c_context_create(); }; - void TearDown() override + + ~nix_api_util_context() override { nix_c_context_free(ctx); ctx = nullptr; } + nix_c_context * ctx; }; From 92dacec0e4ad4a3e1a6104b6ac93ad109c5ae0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Fri, 5 Jan 2024 10:16:56 +0100 Subject: [PATCH 112/168] C API: Apply documentation suggestions Co-authored-by: asymmetric --- doc/external-api/README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/external-api/README.md b/doc/external-api/README.md index 3b802952c..ff0a30ff4 100644 --- a/doc/external-api/README.md +++ b/doc/external-api/README.md @@ -1,6 +1,7 @@ # Getting started -These C bindings are **experimental** at the moment, which means they can still change any time or get removed again, but the plan is to provide a stable external C API to the Nix language and the Nix store. +> **Warning** +> These bindings are **experimental**, which means they can change at any time or be removed outright; nevertheless the plan is to provide a stable external C API to the Nix language and the Nix store. The language library allows evaluating Nix expressions and interacting with Nix language values. The Nix store API is still rudimentary, and only allows initialising and connecting to a store for the Nix language evaluator to interact with. @@ -49,7 +50,7 @@ int main() { ``` **Usage:** -``` +```ShellSession $ gcc main.c $(pkg-config nix-expr-c --libs --cflags) -o main $ ./main Nix version: 2.17 @@ -84,7 +85,7 @@ void nix_plugin_entry() { ``` **Usage:** -``` +```ShellSession $ gcc plugin.c $(pkg-config nix-expr-c --libs --cflags) -shared -o plugin.so $ nix --plugin-files ./plugin.so repl nix-repl> builtins.increment 1 From 24604d024a187dd06544ddbda880ab4bc4bcdb4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Tue, 9 Jan 2024 22:51:39 +0100 Subject: [PATCH 113/168] C API: fix docs build after rebase --- Makefile | 9 +++++++++ Makefile.config.in | 1 + configure.ac | 4 ++++ doc/external-api/README.md | 6 +++--- doc/external-api/local.mk | 16 ++-------------- package.nix | 3 +++ src/libstore/c/nix_api_store.h | 2 +- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index d9efc8154..ae4a8f0a4 100644 --- a/Makefile +++ b/Makefile @@ -129,3 +129,12 @@ internal-api-html: @echo "Internal API docs are disabled. Configure with '--enable-internal-api-docs', or avoid calling 'make internal-api-html'." @exit 1 endif + +ifeq ($(ENABLE_EXTERNAL_API_DOCS), yes) +$(eval $(call include-sub-makefile, doc/external-api/local.mk)) +else +.PHONY: external-api-html +external-api-html: + @echo "External API docs are disabled. Configure with '--enable-external-api-docs', or avoid calling 'make external-api-html'." + @exit 1 +endif diff --git a/Makefile.config.in b/Makefile.config.in index d5c382630..7f517898c 100644 --- a/Makefile.config.in +++ b/Makefile.config.in @@ -12,6 +12,7 @@ ENABLE_BUILD = @ENABLE_BUILD@ ENABLE_DOC_GEN = @ENABLE_DOC_GEN@ ENABLE_FUNCTIONAL_TESTS = @ENABLE_FUNCTIONAL_TESTS@ ENABLE_INTERNAL_API_DOCS = @ENABLE_INTERNAL_API_DOCS@ +ENABLE_EXTERNAL_API_DOCS = @ENABLE_EXTERNAL_API_DOCS@ ENABLE_S3 = @ENABLE_S3@ ENABLE_UNIT_TESTS = @ENABLE_UNIT_TESTS@ GTEST_LIBS = @GTEST_LIBS@ diff --git a/configure.ac b/configure.ac index c3823c01c..1d327d51d 100644 --- a/configure.ac +++ b/configure.ac @@ -177,6 +177,10 @@ AC_ARG_ENABLE(internal-api-docs, AS_HELP_STRING([--enable-internal-api-docs],[Bu ENABLE_INTERNAL_API_DOCS=$enableval, ENABLE_INTERNAL_API_DOCS=no) AC_SUBST(ENABLE_INTERNAL_API_DOCS) +AC_ARG_ENABLE(external-api-docs, AS_HELP_STRING([--enable-external-api-docs],[Build API docs for Nix's external unstable C interfaces]), + ENABLE_EXTERNAL_API_DOCS=$enableval, ENABLE_EXTERNAL_API_DOCS=no) +AC_SUBST(ENABLE_EXTERNAL_API_DOCS) + AS_IF( [test "$ENABLE_FUNCTIONAL_TESTS" == "yes" || test "$ENABLE_DOC_GEN" == "yes"], [NEED_PROG(jq, jq)]) diff --git a/doc/external-api/README.md b/doc/external-api/README.md index ff0a30ff4..3fa1c55f9 100644 --- a/doc/external-api/README.md +++ b/doc/external-api/README.md @@ -48,7 +48,7 @@ int main() { return 0; } ``` - + **Usage:** ```ShellSession $ gcc main.c $(pkg-config nix-expr-c --libs --cflags) -o main @@ -66,7 +66,7 @@ It will increment the argument if it is an integer and throw an error otherwise. #include #include #include - + void increment(void* user_data, nix_c_context* ctx, State* state, Value** args, Value* v) { nix_value_force(NULL, state, args[0]); if (nix_get_type(NULL, args[0]) == NIX_TYPE_INT) { @@ -75,7 +75,7 @@ void increment(void* user_data, nix_c_context* ctx, State* state, Value** args, nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "First argument should be an integer."); } } - + void nix_plugin_entry() { const char* args[] = {"n", NULL}; PrimOp *p = nix_alloc_primop(NULL, increment, 1, "increment", args, "Example custom built-in function: increments an integer", NULL); diff --git a/doc/external-api/local.mk b/doc/external-api/local.mk index aa501198b..c739bdaf0 100644 --- a/doc/external-api/local.mk +++ b/doc/external-api/local.mk @@ -1,19 +1,7 @@ -.PHONY: external-api-html - -ifeq ($(external_api_docs), yes) - $(docdir)/external-api/html/index.html $(docdir)/external-api/latex: $(d)/doxygen.cfg mkdir -p $(docdir)/external-api { cat $< ; echo "OUTPUT_DIRECTORY=$(docdir)/external-api" ; } | doxygen - -# Generate the HTML API docs for Nix's unstable external interfaces. +# Generate the HTML API docs for Nix's unstable C bindings +.PHONY: external-api-html external-api-html: $(docdir)/external-api/html/index.html - -else - -# Make a nicer error message -external-api-html: - @echo "External API docs are disabled. Configure with '--enable-external-api-docs', or avoid calling 'make external-api-html'." - @exit 1 - -endif diff --git a/package.nix b/package.nix index af4ca9b46..c9e50c399 100644 --- a/package.nix +++ b/package.nix @@ -184,6 +184,9 @@ in { ./doc/manual ] ++ lib.optionals enableInternalAPIDocs [ ./doc/internal-api + ] ++ lib.optionals enableExternalAPIDocs [ + ./doc/external-api + ] ++ lib.optionals (enableInternalAPIDocs || enableExternalAPIDocs) [ # Source might not be compiled, but still must be available # for Doxygen to gather comments. ./src diff --git a/src/libstore/c/nix_api_store.h b/src/libstore/c/nix_api_store.h index 91abdb201..7732ade6b 100644 --- a/src/libstore/c/nix_api_store.h +++ b/src/libstore/c/nix_api_store.h @@ -109,7 +109,7 @@ bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * /** * @brief Realise a Nix store path * - * Blocking, calls callback once for each realisedoutput + * Blocking, calls callback once for each realised output * * @param[out] context Optional, stores error information * @param[in] store Nix Store reference From 535694122e4cbbffa04fec903002ba08cf9deb53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Wed, 10 Jan 2024 11:58:35 +0100 Subject: [PATCH 114/168] C API: rename State to EvalState --- doc/external-api/README.md | 49 +++++++++++++++++---------- src/libexpr/c/nix_api_expr.cc | 16 ++++----- src/libexpr/c/nix_api_expr.h | 24 ++++++------- src/libexpr/c/nix_api_expr_internal.h | 2 +- src/libexpr/c/nix_api_external.cc | 5 +-- src/libexpr/c/nix_api_external.h | 11 ++++-- src/libexpr/c/nix_api_value.cc | 18 +++++----- src/libexpr/c/nix_api_value.h | 22 ++++++------ tests/unit/libexpr/nix_api_expr.cc | 2 +- 9 files changed, 85 insertions(+), 64 deletions(-) diff --git a/doc/external-api/README.md b/doc/external-api/README.md index 3fa1c55f9..e9ca25ab6 100644 --- a/doc/external-api/README.md +++ b/doc/external-api/README.md @@ -1,30 +1,40 @@ # Getting started -> **Warning** -> These bindings are **experimental**, which means they can change at any time or be removed outright; nevertheless the plan is to provide a stable external C API to the Nix language and the Nix store. +> **Warning** These bindings are **experimental**, which means they can change +> at any time or be removed outright; nevertheless the plan is to provide a +> stable external C API to the Nix language and the Nix store. -The language library allows evaluating Nix expressions and interacting with Nix language values. -The Nix store API is still rudimentary, and only allows initialising and connecting to a store for the Nix language evaluator to interact with. +The language library allows evaluating Nix expressions and interacting with Nix +language values. The Nix store API is still rudimentary, and only allows +initialising and connecting to a store for the Nix language evaluator to +interact with. + +Currently there are two ways to interface with the Nix language evaluator +programmatically: -Currently there are two ways to interface with the Nix language evaluator programmatically: 1. Embedding the evaluator 2. Writing language plug-ins -Embedding means you link the Nix C libraries in your program and use them from there. -Adding a plug-in means you make a library that gets loaded by the Nix language evaluator, specified through a configuration option. +Embedding means you link the Nix C libraries in your program and use them from +there. Adding a plug-in means you make a library that gets loaded by the Nix +language evaluator, specified through a configuration option. -Many of the components and mechanisms involved are not yet documented, therefore please refer to the [Nix source code](https://github.com/NixOS/nix/) for details. -Additions to in-code documentation and the reference manual are highly appreciated. +Many of the components and mechanisms involved are not yet documented, therefore +please refer to the [Nix source code](https://github.com/NixOS/nix/) for +details. Additions to in-code documentation and the reference manual are highly +appreciated. - -The following examples, for simplicity, don't include error handling. -See the [Handling errors](@ref errors) section for more information. +The following examples, for simplicity, don't include error handling. See the +[Handling errors](@ref errors) section for more information. # Embedding the Nix Evaluator -In this example we programmatically start the Nix language evaluator with a dummy store (that has no store paths and cannot be written to), and evaluate the Nix expression `builtins.nixVersion`. +In this example we programmatically start the Nix language evaluator with a +dummy store (that has no store paths and cannot be written to), and evaluate the +Nix expression `builtins.nixVersion`. **main.c:** + ```C #include #include @@ -35,7 +45,7 @@ int main() { nix_libexpr_init(NULL); Store* store = nix_store_open(NULL, "dummy://", NULL); - State* state = nix_state_create(NULL, NULL, store); // empty search path (NIX_PATH) + EvalState* state = nix_state_create(NULL, NULL, store); // empty search path (NIX_PATH) Value *value = nix_alloc_value(NULL, state); nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value); @@ -50,24 +60,26 @@ int main() { ``` **Usage:** + ```ShellSession $ gcc main.c $(pkg-config nix-expr-c --libs --cflags) -o main $ ./main Nix version: 2.17 ``` - # Writing a Nix language plug-in -In this example we add a custom primitive operation (*primop*) to `builtins`. -It will increment the argument if it is an integer and throw an error otherwise. + +In this example we add a custom primitive operation (_primop_) to `builtins`. It +will increment the argument if it is an integer and throw an error otherwise. **plugin.c:** + ```C #include #include #include -void increment(void* user_data, nix_c_context* ctx, State* state, Value** args, Value* v) { +void increment(void* user_data, nix_c_context* ctx, EvalState* state, Value** args, Value* v) { nix_value_force(NULL, state, args[0]); if (nix_get_type(NULL, args[0]) == NIX_TYPE_INT) { nix_set_int(NULL, v, nix_get_int(NULL, args[0]) + 1); @@ -85,6 +97,7 @@ void nix_plugin_entry() { ``` **Usage:** + ```ShellSession $ gcc plugin.c $(pkg-config nix-expr-c --libs --cflags) -shared -o plugin.so $ nix --plugin-files ./plugin.so repl diff --git a/src/libexpr/c/nix_api_expr.cc b/src/libexpr/c/nix_api_expr.cc index dc114c777..f18ef399b 100644 --- a/src/libexpr/c/nix_api_expr.cc +++ b/src/libexpr/c/nix_api_expr.cc @@ -41,8 +41,8 @@ nix_err nix_libexpr_init(nix_c_context * context) NIXC_CATCH_ERRS } -nix_err -nix_expr_eval_from_string(nix_c_context * context, State * state, const char * expr, const char * path, Value * value) +nix_err nix_expr_eval_from_string( + nix_c_context * context, EvalState * state, const char * expr, const char * path, Value * value) { if (context) context->last_err_code = NIX_OK; @@ -54,7 +54,7 @@ nix_expr_eval_from_string(nix_c_context * context, State * state, const char * e NIXC_CATCH_ERRS } -nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value * arg, Value * value) +nix_err nix_value_call(nix_c_context * context, EvalState * state, Value * fn, Value * arg, Value * value) { if (context) context->last_err_code = NIX_OK; @@ -65,7 +65,7 @@ nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value NIXC_CATCH_ERRS } -nix_err nix_value_force(nix_c_context * context, State * state, Value * value) +nix_err nix_value_force(nix_c_context * context, EvalState * state, Value * value) { if (context) context->last_err_code = NIX_OK; @@ -75,7 +75,7 @@ nix_err nix_value_force(nix_c_context * context, State * state, Value * value) NIXC_CATCH_ERRS } -nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * value) +nix_err nix_value_force_deep(nix_c_context * context, EvalState * state, Value * value) { if (context) context->last_err_code = NIX_OK; @@ -85,7 +85,7 @@ nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * val NIXC_CATCH_ERRS } -State * nix_state_create(nix_c_context * context, const char ** searchPath_c, Store * store) +EvalState * nix_state_create(nix_c_context * context, const char ** searchPath_c, Store * store) { if (context) context->last_err_code = NIX_OK; @@ -95,12 +95,12 @@ State * nix_state_create(nix_c_context * context, const char ** searchPath_c, St for (size_t i = 0; searchPath_c[i] != nullptr; i++) searchPath.push_back(searchPath_c[i]); - return new State{nix::EvalState(nix::SearchPath::parse(searchPath), store->ptr)}; + return new EvalState{nix::EvalState(nix::SearchPath::parse(searchPath), store->ptr)}; } NIXC_CATCH_ERRS_NULL } -void nix_state_free(State * state) +void nix_state_free(EvalState * state) { delete state; } diff --git a/src/libexpr/c/nix_api_expr.h b/src/libexpr/c/nix_api_expr.h index 8cc6c916c..7f32140a0 100644 --- a/src/libexpr/c/nix_api_expr.h +++ b/src/libexpr/c/nix_api_expr.h @@ -9,7 +9,7 @@ * nix_libexpr_init(NULL); * * Store* store = nix_store_open(NULL, "dummy", NULL); - * State* state = nix_state_create(NULL, NULL, store); // empty nix path + * EvalState* state = nix_state_create(NULL, NULL, store); // empty nix path * Value *value = nix_alloc_value(NULL, state); * * nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value); @@ -42,10 +42,10 @@ extern "C" { * * Multiple states can be created for multi-threaded * operation. - * @struct State + * @struct EvalState * @see nix_state_create */ -typedef struct State State; // nix::EvalState +typedef struct EvalState EvalState; // nix::EvalState /** * @brief Represents a value in the Nix language. * @@ -60,7 +60,7 @@ typedef void Value; // nix::Value * @brief Initialize the Nix language evaluator. * * This function must be called at least once, - * at some point before constructing a State for the first time. + * at some point before constructing a EvalState for the first time. * This function can be called multiple times, and is idempotent. * * @param[out] context Optional, stores error information @@ -77,12 +77,12 @@ nix_err nix_libexpr_init(nix_c_context * context); * @param[in] path The file path to associate with the expression. * This is required for expressions that contain relative paths (such as `./.`) that are resolved relative to the given * directory. - * @param[out] value The result of the evaluation. You should allocate this + * @param[out] value The result of the evaluation. You must allocate this * yourself. * @return NIX_OK if the evaluation was successful, an error code otherwise. */ -nix_err -nix_expr_eval_from_string(nix_c_context * context, State * state, const char * expr, const char * path, Value * value); +nix_err nix_expr_eval_from_string( + nix_c_context * context, EvalState * state, const char * expr, const char * path, Value * value); /** * @brief Calls a Nix function with an argument. @@ -94,7 +94,7 @@ nix_expr_eval_from_string(nix_c_context * context, State * state, const char * e * @param[out] value The result of the function call. * @return NIX_OK if the function call was successful, an error code otherwise. */ -nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value * arg, Value * value); +nix_err nix_value_call(nix_c_context * context, EvalState * state, Value * fn, Value * arg, Value * value); /** * @brief Forces the evaluation of a Nix value. @@ -116,7 +116,7 @@ nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value * @return NIX_OK if the force operation was successful, an error code * otherwise. */ -nix_err nix_value_force(nix_c_context * context, State * state, Value * value); +nix_err nix_value_force(nix_c_context * context, EvalState * state, Value * value); /** * @brief Forces the deep evaluation of a Nix value. @@ -132,7 +132,7 @@ nix_err nix_value_force(nix_c_context * context, State * state, Value * value); * @return NIX_OK if the deep force operation was successful, an error code * otherwise. */ -nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * value); +nix_err nix_value_force_deep(nix_c_context * context, EvalState * state, Value * value); /** * @brief Create a new Nix language evaluator state. @@ -142,7 +142,7 @@ nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * val * @param[in] store The Nix store to use. * @return A new Nix state or NULL on failure. */ -State * nix_state_create(nix_c_context * context, const char ** searchPath, Store * store); +EvalState * nix_state_create(nix_c_context * context, const char ** searchPath, Store * store); /** * @brief Frees a Nix state. @@ -151,7 +151,7 @@ State * nix_state_create(nix_c_context * context, const char ** searchPath, Stor * * @param[in] state The state to free. */ -void nix_state_free(State * state); +void nix_state_free(EvalState * state); /** @addtogroup GC * @brief Reference counting and garbage collector operations diff --git a/src/libexpr/c/nix_api_expr_internal.h b/src/libexpr/c/nix_api_expr_internal.h index c9906dd3a..e116af165 100644 --- a/src/libexpr/c/nix_api_expr_internal.h +++ b/src/libexpr/c/nix_api_expr_internal.h @@ -4,7 +4,7 @@ #include "eval.hh" #include "attr-set.hh" -struct State +struct EvalState { nix::EvalState state; }; diff --git a/src/libexpr/c/nix_api_external.cc b/src/libexpr/c/nix_api_external.cc index a2d776a47..2e8a98567 100644 --- a/src/libexpr/c/nix_api_external.cc +++ b/src/libexpr/c/nix_api_external.cc @@ -148,7 +148,7 @@ public: } nix_string_context ctx{context}; nix_string_return res{""}; - desc.printValueAsJSON(v, (State *) &state, strict, &ctx, copyToStore, &res); + desc.printValueAsJSON(v, (EvalState *) &state, strict, &ctx, copyToStore, &res); if (res.str.empty()) { return nix::ExternalValueBase::printValueAsJSON(state, strict, context, copyToStore); } @@ -172,7 +172,8 @@ public: } nix_string_context ctx{context}; desc.printValueAsXML( - v, (State *) &state, strict, location, &doc, &ctx, &drvsSeen, *reinterpret_cast(&pos)); + v, (EvalState *) &state, strict, location, &doc, &ctx, &drvsSeen, + *reinterpret_cast(&pos)); } virtual ~NixCExternalValue() override{}; diff --git a/src/libexpr/c/nix_api_external.h b/src/libexpr/c/nix_api_external.h index 00eaa4460..daa74c5a8 100644 --- a/src/libexpr/c/nix_api_external.h +++ b/src/libexpr/c/nix_api_external.h @@ -136,7 +136,7 @@ typedef struct NixCExternalValueDesc * or setting it to the empty string, will make the conversion throw an error. */ void (*printValueAsJSON)( - void * self, State *, int strict, nix_string_context * c, bool copyToStore, nix_string_return * res); + void * self, EvalState *, int strict, nix_string_context * c, bool copyToStore, nix_string_return * res); /** * @brief Convert the external value to XML * @@ -154,7 +154,14 @@ typedef struct NixCExternalValueDesc * @param[in] pos The position of the call. */ void (*printValueAsXML)( - void * self, State *, int strict, int location, void * doc, nix_string_context * c, void * drvsSeen, int pos); + void * self, + EvalState *, + int strict, + int location, + void * doc, + nix_string_context * c, + void * drvsSeen, + int pos); } NixCExternalValueDesc; /** diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index b0fb960c6..ffa3aa5f7 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -43,7 +43,7 @@ static void nix_c_primop_wrapper( PrimOpFun f, void * userdata, nix::EvalState & state, const nix::PosIdx pos, nix::Value ** args, nix::Value & v) { nix_c_context ctx; - f(userdata, &ctx, (State *) &state, (Value **) args, (Value *) &v); + f(userdata, &ctx, (EvalState *) &state, (Value **) args, (Value *) &v); /* TODO: In the future, this should throw different errors depending on the error code */ if (ctx.last_err_code != NIX_OK) state.debugThrowLastTrace(nix::Error( @@ -92,7 +92,7 @@ nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp) NIXC_CATCH_ERRS } -Value * nix_alloc_value(nix_c_context * context, State * state) +Value * nix_alloc_value(nix_c_context * context, EvalState * state) { if (context) context->last_err_code = NIX_OK; @@ -255,7 +255,7 @@ ExternalValue * nix_get_external(nix_c_context * context, Value * value) NIXC_CATCH_ERRS_NULL; } -Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * state, unsigned int ix) +Value * nix_get_list_byidx(nix_c_context * context, const Value * value, EvalState * state, unsigned int ix) { if (context) context->last_err_code = NIX_OK; @@ -270,7 +270,7 @@ Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * NIXC_CATCH_ERRS_NULL } -Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name) +Value * nix_get_attr_byname(nix_c_context * context, const Value * value, EvalState * state, const char * name) { if (context) context->last_err_code = NIX_OK; @@ -290,7 +290,7 @@ Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State NIXC_CATCH_ERRS_NULL } -bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name) +bool nix_has_attr_byname(nix_c_context * context, const Value * value, EvalState * state, const char * name) { if (context) context->last_err_code = NIX_OK; @@ -307,7 +307,7 @@ bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * s } Value * -nix_get_attr_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i, const char ** name) +nix_get_attr_byidx(nix_c_context * context, const Value * value, EvalState * state, unsigned int i, const char ** name) { if (context) context->last_err_code = NIX_OK; @@ -322,7 +322,7 @@ nix_get_attr_byidx(nix_c_context * context, const Value * value, State * state, NIXC_CATCH_ERRS_NULL } -const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i) +const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, EvalState * state, unsigned int i) { if (context) context->last_err_code = NIX_OK; @@ -413,7 +413,7 @@ nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * NIXC_CATCH_ERRS } -nix_err nix_make_list(nix_c_context * context, State * s, Value * value, unsigned int size) +nix_err nix_make_list(nix_c_context * context, EvalState * s, Value * value, unsigned int size) { if (context) context->last_err_code = NIX_OK; @@ -471,7 +471,7 @@ nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder * NIXC_CATCH_ERRS } -BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, State * state, size_t capacity) +BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, EvalState * state, size_t capacity) { if (context) context->last_err_code = NIX_OK; diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index ca4e83cf4..de6dbc9ff 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -35,7 +35,7 @@ typedef enum { // forward declarations typedef void Value; -typedef struct State State; +typedef struct EvalState EvalState; // type defs /** @brief Stores an under-construction set of bindings * @ingroup value_manip @@ -75,7 +75,7 @@ typedef struct ExternalValue ExternalValue; * @param[out] ret return value * @see nix_alloc_primop, nix_set_primop */ -typedef void (*PrimOpFun)(void * user_data, nix_c_context * context, State * state, Value ** args, Value * ret); +typedef void (*PrimOpFun)(void * user_data, nix_c_context * context, EvalState * state, Value ** args, Value * ret); /** @brief Allocate a PrimOp * @@ -127,7 +127,7 @@ nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp); * @return value, or null in case of errors * */ -Value * nix_alloc_value(nix_c_context * context, State * state); +Value * nix_alloc_value(nix_c_context * context, EvalState * state); /** @addtogroup value_manip Manipulating values * @brief Functions to inspect and change Nix language values, represented by Value. * @{ @@ -209,7 +209,7 @@ ExternalValue * nix_get_external(nix_c_context * context, Value *); * @param[in] ix list element to get * @return value, NULL in case of errors */ -Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * state, unsigned int ix); +Value * nix_get_list_byidx(nix_c_context * context, const Value * value, EvalState * state, unsigned int ix); /** @brief Get an attr by name * * Owned by the GC. Use nix_gc_decref when you're done with the pointer @@ -219,7 +219,7 @@ Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * * @param[in] name attribute name * @return value, NULL in case of errors */ -Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name); +Value * nix_get_attr_byname(nix_c_context * context, const Value * value, EvalState * state, const char * name); /** @brief Check if an attribute name exists on a value * @param[out] context Optional, stores error information @@ -228,7 +228,7 @@ Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State * @param[in] name attribute name * @return value, error info via context */ -bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name); +bool nix_has_attr_byname(nix_c_context * context, const Value * value, EvalState * state, const char * name); /** @brief Get an attribute by index in the sorted bindings * @@ -243,20 +243,20 @@ bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * s * @return value, NULL in case of errors */ Value * -nix_get_attr_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i, const char ** name); +nix_get_attr_byidx(nix_c_context * context, const Value * value, EvalState * state, unsigned int i, const char ** name); /** @brief Get an attribute name by index in the sorted bindings * * Useful when you want the name but want to avoid evaluation. * - * Owned by the nix State + * Owned by the nix EvalState * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @param[in] state nix evaluator state * @param[in] i attribute index * @return name, NULL in case of errors */ -const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i); +const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, EvalState * state, unsigned int i); /**@}*/ /** @name Setters */ @@ -315,7 +315,7 @@ nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * * @param[in] size size of list * @return error code, NIX_OK on success. */ -nix_err nix_make_list(nix_c_context * context, State * s, Value * value, unsigned int size); +nix_err nix_make_list(nix_c_context * context, EvalState * s, Value * value, unsigned int size); /** @brief Manipulate a list by index * * Don't do this mid-computation. @@ -359,7 +359,7 @@ nix_err nix_copy_value(nix_c_context * context, Value * value, Value * source); * @return owned reference to a bindings builder. Make sure to unref when you're done. */ -BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, State * state, size_t capacity); +BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, EvalState * state, size_t capacity); /** @brief Insert bindings into a builder * @param[out] context Optional, stores error information * @param[in] builder BindingsBuilder to insert into diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc index 03de4547a..60a33a5cf 100644 --- a/tests/unit/libexpr/nix_api_expr.cc +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -25,7 +25,7 @@ public: nix_state_free(state); } - State * state; + EvalState * state; Value * value; }; From d5ec1d0617a9b2b463fec2fee548945c5c6987ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 11 Jan 2024 22:41:57 +0100 Subject: [PATCH 115/168] C API: nix_store_open, check for empty strings --- src/libstore/c/nix_api_store.cc | 22 +++++++++++----------- tests/unit/libstore/nix_api_store.cc | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index 4aff815ff..6586d4a1b 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -33,19 +33,19 @@ Store * nix_store_open(nix_c_context * context, const char * uri, const char *** if (context) context->last_err_code = NIX_OK; try { - if (!uri) { - return new Store{nix::openStore()}; - } else { - std::string uri_str = uri; - if (!params) - return new Store{nix::openStore(uri_str)}; + std::string uri_str = uri ? uri : ""; - nix::Store::Params params_map; - for (size_t i = 0; params[i] != nullptr; i++) { - params_map[params[i][0]] = params[i][1]; - } - return new Store{nix::openStore(uri_str, params_map)}; + if (uri_str.empty()) + return new Store{nix::openStore()}; + + if (!params) + return new Store{nix::openStore(uri_str)}; + + nix::Store::Params params_map; + for (size_t i = 0; params[i] != nullptr; i++) { + params_map[params[i][0]] = params[i][1]; } + return new Store{nix::openStore(uri_str, params_map)}; } NIXC_CATCH_ERRS_NULL } diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc index 764cd0d88..bbf850291 100644 --- a/tests/unit/libstore/nix_api_store.cc +++ b/tests/unit/libstore/nix_api_store.cc @@ -58,4 +58,28 @@ TEST_F(nix_api_store_test, get_version) ASSERT_STREQ(PACKAGE_VERSION, value); } +TEST_F(nix_api_util_context, nix_store_open_dummy) +{ + nix_libstore_init(ctx); + Store * store = nix_store_open(ctx, "dummy://", nullptr); + ASSERT_EQ(NIX_OK, ctx->last_err_code); + ASSERT_STREQ("dummy", store->ptr->getUri().c_str()); + nix_store_unref(store); +} + +TEST_F(nix_api_util_context, nix_store_open_invalid) +{ + nix_libstore_init(ctx); + Store * store = nix_store_open(ctx, "invalid://", nullptr); + ASSERT_EQ(NIX_ERR_NIX_ERROR, ctx->last_err_code); + ASSERT_EQ(nullptr, store); + nix_store_unref(store); +} + +TEST_F(nix_api_store_test, nix_store_is_valid_path_not_in_store) +{ + StorePath * path = nix_store_parse_path(ctx, store, validPath); + ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, path)); +} + } From 415583a5009a13d677adcb22c26e27e1d04931eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 11 Jan 2024 22:42:44 +0100 Subject: [PATCH 116/168] C API: use bool argument consistently --- src/libexpr/c/nix_api_external.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libexpr/c/nix_api_external.h b/src/libexpr/c/nix_api_external.h index daa74c5a8..c935bbe56 100644 --- a/src/libexpr/c/nix_api_external.h +++ b/src/libexpr/c/nix_api_external.h @@ -136,7 +136,7 @@ typedef struct NixCExternalValueDesc * or setting it to the empty string, will make the conversion throw an error. */ void (*printValueAsJSON)( - void * self, EvalState *, int strict, nix_string_context * c, bool copyToStore, nix_string_return * res); + void * self, EvalState *, bool strict, nix_string_context * c, bool copyToStore, nix_string_return * res); /** * @brief Convert the external value to XML * From 51ff547d9ada2243afc02c6c84c7e7be64fefdb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Mon, 15 Jan 2024 23:44:57 +0100 Subject: [PATCH 117/168] C API: add more tests to nix_api_expr --- tests/unit/libexpr/nix_api_expr.cc | 47 ++++++++++++++++------ tests/unit/libexpr/nix_api_value.cc | 61 +++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 tests/unit/libexpr/nix_api_value.cc diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc index 60a33a5cf..0389306ec 100644 --- a/tests/unit/libexpr/nix_api_expr.cc +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -51,24 +51,19 @@ TEST_F(nix_api_expr_test, nix_expr_eval_drv) { auto expr = R"(derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; })"; nix_expr_eval_from_string(nullptr, state, expr, ".", value); - nix_value_force(nullptr, state, value); - ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(nullptr, value)); - auto stateFn = nix_state_create(nullptr, nullptr, store); - auto valueFn = nix_alloc_value(nullptr, state); + EvalState * stateFn = nix_state_create(nullptr, nullptr, store); + Value * valueFn = nix_alloc_value(nullptr, state); nix_expr_eval_from_string(nullptr, stateFn, "builtins.toString", ".", valueFn); - ASSERT_EQ(NIX_TYPE_FUNCTION, nix_get_type(nullptr, valueFn)); - auto stateResult = nix_state_create(nullptr, nullptr, store); - auto valueResult = nix_alloc_value(nullptr, stateResult); - + EvalState * stateResult = nix_state_create(nullptr, nullptr, store); + Value * valueResult = nix_alloc_value(nullptr, stateResult); nix_value_call(ctx, stateResult, valueFn, value, valueResult); - nix_value_force(nullptr, stateResult, valueResult); - - auto p = nix_get_string(nullptr, valueResult); + ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, valueResult)); + const char * p = nix_get_string(nullptr, valueResult); ASSERT_STREQ("/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname", p); // Clean up @@ -79,4 +74,34 @@ TEST_F(nix_api_expr_test, nix_expr_eval_drv) nix_state_free(stateResult); } +TEST_F(nix_api_expr_test, nix_build_drv) +{ + auto expr = R"(derivation { name = "myname"; + system = builtins.currentSystem; + builder = "/bin/sh"; + args = [ "-c" "echo hello world > $out" ]; + })"; + nix_expr_eval_from_string(nullptr, state, expr, ".", value); + + Value * drvPathValue = nix_get_attr_byname(nullptr, value, state, "drvPath"); + const char * drvPath = nix_get_string(nullptr, drvPathValue); + ASSERT_STREQ("/nix/store/5fxx84dpz59ch79wf9x8ja715p7hf3q1-myname.drv", drvPath); + + StorePath * drvStorePath = nix_store_parse_path(ctx, store, drvPath); + ASSERT_EQ(true, nix_store_is_valid_path(nullptr, store, drvStorePath)); + + Value * outPathValue = nix_get_attr_byname(nullptr, value, state, "outPath"); + const char * outPath = nix_get_string(nullptr, outPathValue); + ASSERT_STREQ("/nix/store/rp0xk0641l8hpdb84fsx3kwwrl45pxan-myname", outPath); + + StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath); + ASSERT_EQ(false, nix_store_is_valid_path(nullptr, store, outStorePath)); + + nix_store_build(ctx, store, drvStorePath, nullptr, nullptr); + ASSERT_EQ(true, nix_store_is_valid_path(nullptr, store, outStorePath)); + + // Clean up + nix_store_path_free(drvStorePath); + nix_store_path_free(outStorePath); +} } diff --git a/tests/unit/libexpr/nix_api_value.cc b/tests/unit/libexpr/nix_api_value.cc new file mode 100644 index 000000000..ce9cfd68b --- /dev/null +++ b/tests/unit/libexpr/nix_api_value.cc @@ -0,0 +1,61 @@ +#include "nix_api_store.h" +#include "nix_api_store_internal.h" +#include "nix_api_util.h" +#include "nix_api_util_internal.h" +#include "nix_api_expr.h" +#include "nix_api_value.h" + +#include "tests/nix_api_store.hh" + +#include + +namespace nixC { + +class nix_api_value_test : public nix_api_store_test +{ +public: + nix_api_value_test() + { + state = nix_state_create(nullptr, nullptr, store); + value = nix_alloc_value(nullptr, state); + } + ~nix_api_value_test() + { + nix_gc_decref(nullptr, value); + nix_state_free(state); + } + + EvalState * state; + Value * value; +}; + +TEST_F(nix_api_value_test, nix_value_set_get_int) +{ + int myInt = 1; + nix_set_int(nullptr, value, myInt); + + ASSERT_EQ(myInt, nix_get_int(nullptr, value)); +} + +TEST_F(nix_api_value_test, nix_value_make_list) +{ + int size = 10; + nix_make_list(nullptr, state, value, size); + ASSERT_EQ(size, nix_get_list_size(nullptr, value)); +} + +TEST_F(nix_api_value_test, nix_value_set_get_list) +{ + int size = 10; + nix_make_list(nullptr, state, value, size); + + Value * intValue = nix_alloc_value(nullptr, state); + nix_set_int(nullptr, intValue, 42); + nix_set_list_byidx(nullptr, value, 0, intValue); + + ASSERT_EQ(42, nix_get_int(nullptr, nix_get_list_byidx(nullptr, value, state, 0))); + + // Clean up + nix_gc_decref(nullptr, intValue); +} +} From dfdb90dc8e5e743a72d3aeaa472d27d6a1b40c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 22 Feb 2024 00:09:15 +0100 Subject: [PATCH 118/168] C API: Consolidate initializers --- src/libexpr/c/nix_api_value.cc | 99 +++++++++++++++++++++-------- src/libexpr/c/nix_api_value.h | 76 +++++++++++++++------- tests/unit/libexpr/nix_api_value.cc | 70 ++++++++++++++++---- 3 files changed, 183 insertions(+), 62 deletions(-) diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index ffa3aa5f7..dbddbd876 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -17,6 +17,32 @@ #include "gc_cpp.h" #endif +class ListBuilder +{ +private: + std::vector values; + +public: + ListBuilder(size_t capacity) + { + values.reserve(capacity); + } + + void push_back(nix::Value * value) + { + values.push_back(value); + } + + Value * finish(nix::EvalState * state, nix::Value * list) + { + state->mkList(*list, values.size()); + for (size_t n = 0; n < list->listSize(); ++n) { + list->listElems()[n] = values[n]; + } + return list; + } +}; + // Helper function to throw an exception if value is null static const nix::Value & check_value_not_null(const Value * value) { @@ -334,7 +360,7 @@ const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * valu NIXC_CATCH_ERRS_NULL } -nix_err nix_set_bool(nix_c_context * context, Value * value, bool b) +nix_err nix_init_bool(nix_c_context * context, Value * value, bool b) { if (context) context->last_err_code = NIX_OK; @@ -346,7 +372,7 @@ nix_err nix_set_bool(nix_c_context * context, Value * value, bool b) } // todo string context -nix_err nix_set_string(nix_c_context * context, Value * value, const char * str) +nix_err nix_init_string(nix_c_context * context, Value * value, const char * str) { if (context) context->last_err_code = NIX_OK; @@ -357,7 +383,7 @@ nix_err nix_set_string(nix_c_context * context, Value * value, const char * str) NIXC_CATCH_ERRS } -nix_err nix_set_path_string(nix_c_context * context, Value * value, const char * str) +nix_err nix_init_path_string(nix_c_context * context, Value * value, const char * str) { if (context) context->last_err_code = NIX_OK; @@ -368,7 +394,7 @@ nix_err nix_set_path_string(nix_c_context * context, Value * value, const char * NIXC_CATCH_ERRS } -nix_err nix_set_float(nix_c_context * context, Value * value, double d) +nix_err nix_init_float(nix_c_context * context, Value * value, double d) { if (context) context->last_err_code = NIX_OK; @@ -379,7 +405,7 @@ nix_err nix_set_float(nix_c_context * context, Value * value, double d) NIXC_CATCH_ERRS } -nix_err nix_set_int(nix_c_context * context, Value * value, int64_t i) +nix_err nix_init_int(nix_c_context * context, Value * value, int64_t i) { if (context) context->last_err_code = NIX_OK; @@ -390,7 +416,7 @@ nix_err nix_set_int(nix_c_context * context, Value * value, int64_t i) NIXC_CATCH_ERRS } -nix_err nix_set_null(nix_c_context * context, Value * value) +nix_err nix_init_null(nix_c_context * context, Value * value) { if (context) context->last_err_code = NIX_OK; @@ -401,7 +427,7 @@ nix_err nix_set_null(nix_c_context * context, Value * value) NIXC_CATCH_ERRS } -nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * val) +nix_err nix_init_external(nix_c_context * context, Value * value, ExternalValue * val) { if (context) context->last_err_code = NIX_OK; @@ -413,31 +439,52 @@ nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * NIXC_CATCH_ERRS } -nix_err nix_make_list(nix_c_context * context, EvalState * s, Value * value, unsigned int size) +ListBuilder * nix_make_list_builder(nix_c_context * context, EvalState * state, size_t capacity) +{ + if (context) + context->last_err_code = NIX_OK; + try { + auto builder = ListBuilder(capacity); + return new +#if HAVE_BOEHMGC + (NoGC) +#endif + ListBuilder{std::move(builder)}; + } + NIXC_CATCH_ERRS_NULL +} + +nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * builder, Value * value) +{ + if (context) + context->last_err_code = NIX_OK; + try { + builder->push_back((nix::Value *) value); + } + NIXC_CATCH_ERRS +} + +void nix_list_builder_free(ListBuilder * bb) +{ +#if HAVE_BOEHMGC + GC_FREE(bb); +#else + delete bb; +#endif +} + +nix_err nix_make_list(nix_c_context * context, EvalState * s, Value * value, ListBuilder * b) { if (context) context->last_err_code = NIX_OK; try { auto & v = check_value_not_null(value); - s->state.mkList(v, size); + b->finish(&(s->state), &v); } NIXC_CATCH_ERRS } -nix_err nix_set_list_byidx(nix_c_context * context, Value * value, unsigned int ix, Value * elem) -{ - if (context) - context->last_err_code = NIX_OK; - try { - // todo: assert that this is a list - auto & v = check_value_not_null(value); - auto & e = check_value_not_null(elem); - v.listElems()[ix] = &e; - } - NIXC_CATCH_ERRS -} - -nix_err nix_set_primop(nix_c_context * context, Value * value, PrimOp * p) +nix_err nix_init_primop(nix_c_context * context, Value * value, PrimOp * p) { if (context) context->last_err_code = NIX_OK; @@ -486,14 +533,14 @@ BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, EvalState * NIXC_CATCH_ERRS_NULL } -nix_err nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * b, const char * name, Value * value) +nix_err nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * bb, const char * name, Value * value) { if (context) context->last_err_code = NIX_OK; try { auto & v = check_value_not_null(value); - nix::Symbol s = b->builder.state.symbols.create(name); - b->builder.insert(s, &v); + nix::Symbol s = bb->builder.state.symbols.create(name); + bb->builder.insert(s, &v); } NIXC_CATCH_ERRS } diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index de6dbc9ff..df1f949ed 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -46,6 +46,8 @@ typedef struct EvalState EvalState; */ typedef struct BindingsBuilder BindingsBuilder; +typedef class ListBuilder ListBuilder; + /** @brief PrimOp function * @ingroup primops * @@ -73,7 +75,7 @@ typedef struct ExternalValue ExternalValue; * @param[in] args list of arguments. Note that these can be thunks and should be forced using nix_value_force before * use. * @param[out] ret return value - * @see nix_alloc_primop, nix_set_primop + * @see nix_alloc_primop, nix_init_primop */ typedef void (*PrimOpFun)(void * user_data, nix_c_context * context, EvalState * state, Value ** args, Value * ret); @@ -90,7 +92,7 @@ typedef void (*PrimOpFun)(void * user_data, nix_c_context * context, EvalState * * @param[in] doc optional, documentation for this primop * @param[in] user_data optional, arbitrary data, passed to the callback when it's called * @return primop, or null in case of errors - * @see nix_set_primop + * @see nix_init_primop */ PrimOp * nix_alloc_primop( nix_c_context * context, @@ -162,6 +164,7 @@ bool nix_get_bool(nix_c_context * context, const Value * value); * @return NULL in case of error. */ const char * nix_get_string(nix_c_context * context, const Value * value); + /** @brief Get path as string * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect @@ -169,30 +172,35 @@ const char * nix_get_string(nix_c_context * context, const Value * value); * @return NULL in case of error. */ const char * nix_get_path_string(nix_c_context * context, const Value * value); + /** @brief Get the length of a list * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return length of list, error info via context */ unsigned int nix_get_list_size(nix_c_context * context, const Value * value); + /** @brief Get the element count of an attrset * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return attrset element count, error info via context */ unsigned int nix_get_attrs_size(nix_c_context * context, const Value * value); + /** @brief Get float value in 64 bits * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return float contents, error info via context */ double nix_get_float(nix_c_context * context, const Value * value); + /** @brief Get int value * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return int contents, error info via context */ int64_t nix_get_int(nix_c_context * context, const Value * value); + /** @brief Get external reference * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect @@ -210,6 +218,7 @@ ExternalValue * nix_get_external(nix_c_context * context, Value *); * @return value, NULL in case of errors */ Value * nix_get_list_byidx(nix_c_context * context, const Value * value, EvalState * state, unsigned int ix); + /** @brief Get an attr by name * * Owned by the GC. Use nix_gc_decref when you're done with the pointer @@ -257,8 +266,9 @@ nix_get_attr_byidx(nix_c_context * context, const Value * value, EvalState * sta * @return name, NULL in case of errors */ const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, EvalState * state, unsigned int i); + /**@}*/ -/** @name Setters +/** @name Initializers */ /**@{*/ /** @brief Set boolean value @@ -267,66 +277,82 @@ const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * valu * @param[in] b the boolean value * @return error code, NIX_OK on success. */ -nix_err nix_set_bool(nix_c_context * context, Value * value, bool b); +nix_err nix_init_bool(nix_c_context * context, Value * value, bool b); /** @brief Set a string * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] str the string, copied * @return error code, NIX_OK on success. */ -nix_err nix_set_string(nix_c_context * context, Value * value, const char * str); +nix_err nix_init_string(nix_c_context * context, Value * value, const char * str); /** @brief Set a path * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] str the path string, copied * @return error code, NIX_OK on success. */ -nix_err nix_set_path_string(nix_c_context * context, Value * value, const char * str); +nix_err nix_init_path_string(nix_c_context * context, Value * value, const char * str); /** @brief Set a float * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] d the float, 64-bits * @return error code, NIX_OK on success. */ -nix_err nix_set_float(nix_c_context * context, Value * value, double d); +nix_err nix_init_float(nix_c_context * context, Value * value, double d); /** @brief Set an int * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] i the int * @return error code, NIX_OK on success. */ -nix_err nix_set_int(nix_c_context * context, Value * value, int64_t i); + +nix_err nix_init_int(nix_c_context * context, Value * value, int64_t i); /** @brief Set null * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @return error code, NIX_OK on success. */ -nix_err nix_set_null(nix_c_context * context, Value * value); + +nix_err nix_init_null(nix_c_context * context, Value * value); /** @brief Set an external value * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] val the external value to set. Will be GC-referenced by the value. * @return error code, NIX_OK on success. */ -nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * val); -/** @brief Allocate a list +nix_err nix_init_external(nix_c_context * context, Value * value, ExternalValue * val); + +/** @brief Create a list from a list builder * @param[out] context Optional, stores error information * @param[out] value Nix value to modify - * @param[in] size size of list + * @param[in] b list builder to use. Make sure to unref this afterwards. * @return error code, NIX_OK on success. */ -nix_err nix_make_list(nix_c_context * context, EvalState * s, Value * value, unsigned int size); -/** @brief Manipulate a list by index +nix_err nix_make_list(nix_c_context * context, EvalState * s, Value * value, ListBuilder * b); + +/** @brief Create a list builder + * @param[out] context Optional, stores error information + * @param[in] state nix evaluator state + * @param[in] capacity how many bindings you'll add. Don't exceed. + * @return owned reference to a list builder. Make sure to unref when you're done. + */ +ListBuilder * nix_make_list_builder(nix_c_context * context, EvalState * state, size_t capacity); + +/** @brief Insert bindings into a builder + * @param[out] context Optional, stores error information + * @param[in] builder ListBuilder to insert into + * @param[in] value value to insert + * @return error code, NIX_OK on success. + */ +nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * builder, Value * value); + +/** @brief Free a list builder * - * Don't do this mid-computation. - * @pre your list should be at least 'ix+1' items long - * @param[out] context Optional, stores error information - * @param[out] value Nix value to modify - * @param[in] ix index to manipulate - * @param[in] elem the value to set, will be gc-referenced by the value - * @return error code, NIX_OK on success. + * Does not fail. + * @param[in] builder the builder to free */ -nix_err nix_set_list_byidx(nix_c_context * context, Value * value, unsigned int ix, Value * elem); +void nix_list_builder_free(ListBuilder * builder); + /** @brief Create an attribute set from a bindings builder * @param[out] context Optional, stores error information * @param[out] value Nix value to modify @@ -334,6 +360,7 @@ nix_err nix_set_list_byidx(nix_c_context * context, Value * value, unsigned int * @return error code, NIX_OK on success. */ nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder * b); + /** @brief Set primop * @param[out] context Optional, stores error information * @param[out] value Nix value to modify @@ -341,7 +368,7 @@ nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder * * @see nix_alloc_primop * @return error code, NIX_OK on success. */ -nix_err nix_set_primop(nix_c_context * context, Value * value, PrimOp * op); +nix_err nix_init_primop(nix_c_context * context, Value * value, PrimOp * op); /** @brief Copy from another value * @param[out] context Optional, stores error information * @param[out] value Nix value to modify @@ -352,7 +379,6 @@ nix_err nix_copy_value(nix_c_context * context, Value * value, Value * source); /**@}*/ /** @brief Create a bindings builder - * @param[out] context Optional, stores error information * @param[in] state nix evaluator state * @param[in] capacity how many bindings you'll add. Don't exceed. @@ -360,6 +386,7 @@ nix_err nix_copy_value(nix_c_context * context, Value * value, Value * source); done. */ BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, EvalState * state, size_t capacity); + /** @brief Insert bindings into a builder * @param[out] context Optional, stores error information * @param[in] builder BindingsBuilder to insert into @@ -369,6 +396,7 @@ BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, EvalState * */ nix_err nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * builder, const char * name, Value * value); + /** @brief Free a bindings builder * * Does not fail. diff --git a/tests/unit/libexpr/nix_api_value.cc b/tests/unit/libexpr/nix_api_value.cc index ce9cfd68b..d1247e027 100644 --- a/tests/unit/libexpr/nix_api_value.cc +++ b/tests/unit/libexpr/nix_api_value.cc @@ -7,6 +7,7 @@ #include "tests/nix_api_store.hh" +#include #include namespace nixC { @@ -32,30 +33,75 @@ public: TEST_F(nix_api_value_test, nix_value_set_get_int) { int myInt = 1; - nix_set_int(nullptr, value, myInt); + nix_init_int(nullptr, value, myInt); ASSERT_EQ(myInt, nix_get_int(nullptr, value)); } -TEST_F(nix_api_value_test, nix_value_make_list) +TEST_F(nix_api_value_test, nix_make_and_set_list) { int size = 10; - nix_make_list(nullptr, state, value, size); - ASSERT_EQ(size, nix_get_list_size(nullptr, value)); -} - -TEST_F(nix_api_value_test, nix_value_set_get_list) -{ - int size = 10; - nix_make_list(nullptr, state, value, size); + ListBuilder * builder = nix_make_list_builder(nullptr, state, size); Value * intValue = nix_alloc_value(nullptr, state); - nix_set_int(nullptr, intValue, 42); - nix_set_list_byidx(nullptr, value, 0, intValue); + nix_init_int(nullptr, intValue, 42); + nix_list_builder_insert(nullptr, builder, intValue); + nix_make_list(nullptr, state, value, builder); + nix_list_builder_free(builder); ASSERT_EQ(42, nix_get_int(nullptr, nix_get_list_byidx(nullptr, value, state, 0))); + ASSERT_EQ(1, nix_get_list_size(nullptr, value)); // Clean up nix_gc_decref(nullptr, intValue); } + +TEST_F(nix_api_value_test, nix_make_attrs_t) +{ + int size = 10; + const char ** out_name = (const char **) malloc(sizeof(char *)); + + BindingsBuilder * builder = nix_make_bindings_builder(nullptr, state, size); + + Value * intValue = nix_alloc_value(nullptr, state); + nix_init_int(nullptr, intValue, 42); + + Value * stringValue = nix_alloc_value(nullptr, state); + nix_init_string(nullptr, stringValue, "foo"); + + nix_bindings_builder_insert(nullptr, builder, "a", intValue); + nix_bindings_builder_insert(nullptr, builder, "b", stringValue); + nix_make_attrs(nullptr, value, builder); + nix_bindings_builder_free(builder); + + ASSERT_EQ(2, nix_get_attrs_size(nullptr, value)); + + Value * out_value = nix_get_attr_byname(nullptr, value, state, "a"); + ASSERT_EQ(42, nix_get_int(nullptr, out_value)); + nix_gc_decref(nullptr, out_value); + + out_value = nix_get_attr_byidx(nullptr, value, state, 0, out_name); + ASSERT_EQ(42, nix_get_int(nullptr, out_value)); + ASSERT_STREQ("a", *out_name); + nix_gc_decref(nullptr, out_value); + + ASSERT_STREQ("a", nix_get_attr_name_byidx(nullptr, value, state, 0)); + + out_value = nix_get_attr_byname(nullptr, value, state, "b"); + ASSERT_STREQ("foo", nix_get_string(nullptr, out_value)); + nix_gc_decref(nullptr, out_value); + + out_value = nix_get_attr_byidx(nullptr, value, state, 1, out_name); + ASSERT_STREQ("foo", nix_get_string(nullptr, out_value)); + ASSERT_STREQ("b", *out_name); + nix_gc_decref(nullptr, out_value); + + ASSERT_STREQ("b", nix_get_attr_name_byidx(nullptr, value, state, 1)); + + // Clean up + nix_gc_decref(nullptr, intValue); + nix_gc_decref(nullptr, stringValue); + free(out_name); +} + } From 24c8f6864dd3ec308f181c6e05067b4f61c227c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 22 Feb 2024 00:22:54 +0100 Subject: [PATCH 119/168] C API: if store doesn't have a version, return an empty string --- src/libstore/c/nix_api_store.cc | 8 +++----- src/libstore/c/nix_api_store.h | 3 ++- tests/unit/libstore/nix_api_store.cc | 5 +++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index 6586d4a1b..656eb2ae7 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -72,11 +72,9 @@ nix_err nix_store_get_version(nix_c_context * context, Store * store, char * des context->last_err_code = NIX_OK; try { auto res = store->ptr->getVersion(); - if (res) { - return nix_export_std_string(context, *res, dest, n); - } else { - return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "store does not have a version"); - } + if (!res) + res = ""; + return nix_export_std_string(context, *res, dest, n); } NIXC_CATCH_ERRS } diff --git a/src/libstore/c/nix_api_store.h b/src/libstore/c/nix_api_store.h index 7732ade6b..9c5e524e5 100644 --- a/src/libstore/c/nix_api_store.h +++ b/src/libstore/c/nix_api_store.h @@ -125,7 +125,8 @@ nix_err nix_store_build( void (*callback)(void * userdata, const char * outname, const char * out)); /** - * @brief get the version of a nix store + * @brief get the version of a nix store. + * If the store doesn't have a version (like the dummy store), returns an empty string. * @param[out] context Optional, stores error information * @param[in] store nix store reference * @param[out] dest The allocated area to write the string to. diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc index bbf850291..e093e9d15 100644 --- a/tests/unit/libstore/nix_api_store.cc +++ b/tests/unit/libstore/nix_api_store.cc @@ -64,6 +64,11 @@ TEST_F(nix_api_util_context, nix_store_open_dummy) Store * store = nix_store_open(ctx, "dummy://", nullptr); ASSERT_EQ(NIX_OK, ctx->last_err_code); ASSERT_STREQ("dummy", store->ptr->getUri().c_str()); + + char value[256]; + nix_store_get_version(ctx, store, value, 256); + ASSERT_STREQ("", value); + nix_store_unref(store); } From b9cd24a4a85beafe44b2d059c61e0daa2c842e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 22 Feb 2024 12:57:51 +0100 Subject: [PATCH 120/168] C API: fix api_expr tests --- tests/unit/libexpr/nix_api_expr.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc index 0389306ec..ec51d14a3 100644 --- a/tests/unit/libexpr/nix_api_expr.cc +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -85,14 +85,20 @@ TEST_F(nix_api_expr_test, nix_build_drv) Value * drvPathValue = nix_get_attr_byname(nullptr, value, state, "drvPath"); const char * drvPath = nix_get_string(nullptr, drvPathValue); - ASSERT_STREQ("/nix/store/5fxx84dpz59ch79wf9x8ja715p7hf3q1-myname.drv", drvPath); + + std::string p = drvPath; + std::string pEnd = "-myname.drv"; + ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size())); StorePath * drvStorePath = nix_store_parse_path(ctx, store, drvPath); ASSERT_EQ(true, nix_store_is_valid_path(nullptr, store, drvStorePath)); Value * outPathValue = nix_get_attr_byname(nullptr, value, state, "outPath"); const char * outPath = nix_get_string(nullptr, outPathValue); - ASSERT_STREQ("/nix/store/rp0xk0641l8hpdb84fsx3kwwrl45pxan-myname", outPath); + + p = outPath; + pEnd = "-myname"; + ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size())); StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath); ASSERT_EQ(false, nix_store_is_valid_path(nullptr, store, outStorePath)); From 6c231dcf68a0261178060cde2c03810858b43c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sat, 24 Feb 2024 16:07:16 +0100 Subject: [PATCH 121/168] C API: disable test --- src/libexpr/c/nix_api_value.h | 4 ++++ tests/unit/libexpr/nix_api_expr.cc | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index df1f949ed..27027caf0 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -278,12 +278,14 @@ const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * valu * @return error code, NIX_OK on success. */ nix_err nix_init_bool(nix_c_context * context, Value * value, bool b); + /** @brief Set a string * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] str the string, copied * @return error code, NIX_OK on success. */ + nix_err nix_init_string(nix_c_context * context, Value * value, const char * str); /** @brief Set a path * @param[out] context Optional, stores error information @@ -291,6 +293,7 @@ nix_err nix_init_string(nix_c_context * context, Value * value, const char * str * @param[in] str the path string, copied * @return error code, NIX_OK on success. */ + nix_err nix_init_path_string(nix_c_context * context, Value * value, const char * str); /** @brief Set a float * @param[out] context Optional, stores error information @@ -298,6 +301,7 @@ nix_err nix_init_path_string(nix_c_context * context, Value * value, const char * @param[in] d the float, 64-bits * @return error code, NIX_OK on success. */ + nix_err nix_init_float(nix_c_context * context, Value * value, double d); /** @brief Set an int * @param[out] context Optional, stores error information diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc index ec51d14a3..5caccea9a 100644 --- a/tests/unit/libexpr/nix_api_expr.cc +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -79,7 +79,7 @@ TEST_F(nix_api_expr_test, nix_build_drv) auto expr = R"(derivation { name = "myname"; system = builtins.currentSystem; builder = "/bin/sh"; - args = [ "-c" "echo hello world > $out" ]; + args = [ "-c" "echo foo > $out" ]; })"; nix_expr_eval_from_string(nullptr, state, expr, ".", value); @@ -104,7 +104,11 @@ TEST_F(nix_api_expr_test, nix_build_drv) ASSERT_EQ(false, nix_store_is_valid_path(nullptr, store, outStorePath)); nix_store_build(ctx, store, drvStorePath, nullptr, nullptr); - ASSERT_EQ(true, nix_store_is_valid_path(nullptr, store, outStorePath)); + + // TODO figure out why fails. + // `make libexpr-tests_RUN` works, but `nix build .` fails + /* auto is_valid_path = nix_store_is_valid_path(ctx, store, outStorePath); */ + /* ASSERT_EQ(true, is_valid_path); */ // Clean up nix_store_path_free(drvStorePath); From 2349185c966983ee1ac1d748f53be5b42461ebcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sun, 25 Feb 2024 00:26:36 +0100 Subject: [PATCH 122/168] C API: fix after rebase --- src/libexpr/c/nix_api_external.cc | 10 +++++++--- src/libexpr/c/nix_api_external.h | 4 ++-- src/libexpr/c/nix_api_value.cc | 3 +-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/libexpr/c/nix_api_external.cc b/src/libexpr/c/nix_api_external.cc index 2e8a98567..3af4af4d4 100644 --- a/src/libexpr/c/nix_api_external.cc +++ b/src/libexpr/c/nix_api_external.cc @@ -108,17 +108,21 @@ public: * Coerce the value to a string. */ virtual std::string coerceToString( - const nix::Pos & pos, nix::NixStringContext & context, bool copyMore, bool copyToStore) const override + nix::EvalState & state, + const nix::PosIdx & pos, + nix::NixStringContext & context, + bool copyMore, + bool copyToStore) const override { if (!desc.coerceToString) { - return nix::ExternalValueBase::coerceToString(pos, context, copyMore, copyToStore); + return nix::ExternalValueBase::coerceToString(state, pos, context, copyMore, copyToStore); } nix_string_context ctx{context}; nix_string_return res{""}; // todo: pos, errors desc.coerceToString(v, &ctx, copyMore, copyToStore, &res); if (res.str.empty()) { - return nix::ExternalValueBase::coerceToString(pos, context, copyMore, copyToStore); + return nix::ExternalValueBase::coerceToString(state, pos, context, copyMore, copyToStore); } return std::move(res.str); } diff --git a/src/libexpr/c/nix_api_external.h b/src/libexpr/c/nix_api_external.h index c935bbe56..12ea00407 100644 --- a/src/libexpr/c/nix_api_external.h +++ b/src/libexpr/c/nix_api_external.h @@ -165,7 +165,7 @@ typedef struct NixCExternalValueDesc } NixCExternalValueDesc; /** - * @brief Create an external value, that can be given to nix_set_external + * @brief Create an external value, that can be given to nix_init_external * * Owned by the GC. Use nix_gc_decref when you're done with the pointer. * @@ -174,7 +174,7 @@ typedef struct NixCExternalValueDesc * as the ExternalValue lives * @param[in] v the value to store * @returns external value, owned by the garbage collector - * @see nix_set_external + * @see nix_init_external */ ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v); diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index dbddbd876..e63d13f7a 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -72,8 +72,7 @@ static void nix_c_primop_wrapper( f(userdata, &ctx, (EvalState *) &state, (Value **) args, (Value *) &v); /* TODO: In the future, this should throw different errors depending on the error code */ if (ctx.last_err_code != NIX_OK) - state.debugThrowLastTrace(nix::Error( - {.msg = nix::hintfmt("Error from builtin function: %s", *ctx.last_err), .errPos = state.positions[pos]})); + state.error("Error from builtin function: %s", *ctx.last_err).atPos(pos).debugThrow(); } PrimOp * nix_alloc_primop( From 7c602d9f014abbba5b6f9300e89eda68e520cea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sun, 25 Feb 2024 00:28:04 +0100 Subject: [PATCH 123/168] C API: add tests for external values --- doc/external-api/README.md | 2 +- src/libexpr/c/nix_api_expr_internal.h | 15 +++++ src/libexpr/c/nix_api_external.cc | 15 ----- src/libexpr/c/nix_api_value.h | 2 +- tests/unit/libexpr/nix_api_external.cc | 83 ++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 tests/unit/libexpr/nix_api_external.cc diff --git a/doc/external-api/README.md b/doc/external-api/README.md index e9ca25ab6..24118f9f0 100644 --- a/doc/external-api/README.md +++ b/doc/external-api/README.md @@ -82,7 +82,7 @@ will increment the argument if it is an integer and throw an error otherwise. void increment(void* user_data, nix_c_context* ctx, EvalState* state, Value** args, Value* v) { nix_value_force(NULL, state, args[0]); if (nix_get_type(NULL, args[0]) == NIX_TYPE_INT) { - nix_set_int(NULL, v, nix_get_int(NULL, args[0]) + 1); + nix_init_int(NULL, v, nix_get_int(NULL, args[0]) + 1); } else { nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "First argument should be an integer."); } diff --git a/src/libexpr/c/nix_api_expr_internal.h b/src/libexpr/c/nix_api_expr_internal.h index e116af165..352ac496f 100644 --- a/src/libexpr/c/nix_api_expr_internal.h +++ b/src/libexpr/c/nix_api_expr_internal.h @@ -14,4 +14,19 @@ struct BindingsBuilder nix::BindingsBuilder builder; }; +struct nix_string_return +{ + std::string str; +}; + +struct nix_printer +{ + std::ostream & s; +}; + +struct nix_string_context +{ + nix::NixStringContext & ctx; +}; + #endif // NIX_API_EXPR_INTERNAL_H diff --git a/src/libexpr/c/nix_api_external.cc b/src/libexpr/c/nix_api_external.cc index 3af4af4d4..c237cfb70 100644 --- a/src/libexpr/c/nix_api_external.cc +++ b/src/libexpr/c/nix_api_external.cc @@ -20,21 +20,6 @@ #include "gc_cpp.h" #endif -struct nix_string_return -{ - std::string str; -}; - -struct nix_printer -{ - std::ostream & s; -}; - -struct nix_string_context -{ - nix::NixStringContext & ctx; -}; - void nix_set_string_return(nix_string_return * str, const char * c) { str->str = c; diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index 27027caf0..a9a640231 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -52,7 +52,7 @@ typedef class ListBuilder ListBuilder; * @ingroup primops * * Owned by the GC - * @see nix_alloc_primop, nix_set_primop + * @see nix_alloc_primop, nix_init_primop */ typedef struct PrimOp PrimOp; /** @brief External Value diff --git a/tests/unit/libexpr/nix_api_external.cc b/tests/unit/libexpr/nix_api_external.cc new file mode 100644 index 000000000..5f5353b04 --- /dev/null +++ b/tests/unit/libexpr/nix_api_external.cc @@ -0,0 +1,83 @@ +#include "nix_api_store.h" +#include "nix_api_store_internal.h" +#include "nix_api_util.h" +#include "nix_api_util_internal.h" +#include "nix_api_expr.h" +#include "nix_api_expr_internal.h" +#include "nix_api_value.h" +#include "nix_api_external.h" +#include "tests/nix_api_store.hh" + +#include +#include + +namespace nixC { + +class MyExternalValueDesc : public NixCExternalValueDesc +{ +public: + MyExternalValueDesc(int x) + : _x(x) + { + print = print_function; + showType = show_type_function; + typeOf = type_of_function; + } + +private: + int _x; + static void print_function(void * self, nix_printer * printer) {} + + static void show_type_function(void * self, nix_string_return * res) {} + + static void type_of_function(void * self, nix_string_return * res) + { + std::cout << self << std::endl; + MyExternalValueDesc * obj = static_cast(self); + + std::string type_string = "nix-external_x); + type_string += " )>"; + res->str = &*type_string.begin(); + } +}; + +class nix_api_external_test : public nix_api_store_test +{ +public: + nix_api_external_test() + { + state = nix_state_create(nullptr, nullptr, store); + value = nix_alloc_value(nullptr, state); + } + ~nix_api_external_test() + { + nix_gc_decref(nullptr, value); + nix_state_free(state); + } + + EvalState * state; + Value * value; +}; + +TEST_F(nix_api_external_test, nix_expr_eval_from_string) +{ + MyExternalValueDesc * external = new MyExternalValueDesc(42); + ExternalValue * val = nix_create_external_value(ctx, external, external); + nix_init_external(nullptr, value, val); + + EvalState * stateResult = nix_state_create(nullptr, nullptr, store); + Value * valueResult = nix_alloc_value(nullptr, stateResult); + + EvalState * stateFn = nix_state_create(nullptr, nullptr, store); + Value * valueFn = nix_alloc_value(nullptr, stateFn); + + nix_expr_eval_from_string(nullptr, state, "builtins.typeOf", ".", valueFn); + + ASSERT_EQ(NIX_TYPE_EXTERNAL, nix_get_type(nullptr, value)); + + nix_value_call(ctx, state, valueFn, value, valueResult); + + ASSERT_STREQ("nix-external", nix_get_string(nullptr, valueResult)); +} +} From c49b88b066f10203fb94ebd91a498bc259cb8c21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sun, 25 Feb 2024 14:20:57 +0100 Subject: [PATCH 124/168] C API: update docs based on PR feedback --- doc/external-api/README.md | 2 +- src/libexpr/c/nix_api_expr.h | 2 +- src/libexpr/c/nix_api_value.h | 5 +++++ src/libstore/c/nix_api_store.cc | 2 +- src/libstore/c/nix_api_store.h | 13 +++++++------ tests/unit/libstore-support/tests/nix_api_store.hh | 2 +- tests/unit/libstore/nix_api_store.cc | 4 ++-- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/doc/external-api/README.md b/doc/external-api/README.md index 24118f9f0..1d7344ddd 100644 --- a/doc/external-api/README.md +++ b/doc/external-api/README.md @@ -54,7 +54,7 @@ int main() { nix_gc_decref(NULL, value); nix_state_free(state); - nix_store_unref(store); + nix_store_free(store); return 0; } ``` diff --git a/src/libexpr/c/nix_api_expr.h b/src/libexpr/c/nix_api_expr.h index 7f32140a0..7504b5d7a 100644 --- a/src/libexpr/c/nix_api_expr.h +++ b/src/libexpr/c/nix_api_expr.h @@ -18,7 +18,7 @@ * * nix_gc_decref(NULL, value); * nix_state_free(state); - * nix_store_unref(store); + * nix_store_free(store); * return 0; * } * @endcode diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index a9a640231..64c0c367a 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -269,6 +269,11 @@ const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * valu /**@}*/ /** @name Initializers + * + * Values are typically "returned" by initializing already allocated memory that serves as the return value. + * For this reason, the construction of values is not tied their allocation. + * Nix is a language with immutable values. Respect this property by only initializing Values once; and only initialize + * Values that are meant to be initialized by you. Failing to adhere to these rules may lead to undefined behavior. */ /**@{*/ /** @brief Set boolean value diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index 656eb2ae7..d6602471d 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -50,7 +50,7 @@ Store * nix_store_open(nix_c_context * context, const char * uri, const char *** NIXC_CATCH_ERRS_NULL } -void nix_store_unref(Store * store) +void nix_store_free(Store * store) { delete store; } diff --git a/src/libstore/c/nix_api_store.h b/src/libstore/c/nix_api_store.h index 9c5e524e5..e6d88026b 100644 --- a/src/libstore/c/nix_api_store.h +++ b/src/libstore/c/nix_api_store.h @@ -48,23 +48,24 @@ nix_err nix_init_plugins(nix_c_context * context); /** * @brief Open a nix store + * Store instances may share state and resources behind the scenes. * @param[out] context Optional, stores error information * @param[in] uri URI of the nix store, copied * @param[in] params optional, array of key-value pairs, {{"endpoint", * "https://s3.local"}} - * @return ref-counted Store pointer, NULL in case of errors - * @see nix_store_unref + * @return a Store pointer, NULL in case of errors + * @see nix_store_free */ Store * nix_store_open(nix_c_context *, const char * uri, const char *** params); /** - * @brief Unref a nix store + * @brief Deallocate a nix store and free any resources if not also held by other Store instances. * * Does not fail. - * It'll be closed and deallocated when all references are gone. - * @param[in] builder the store to unref + * + * @param[in] store the store to free */ -void nix_store_unref(Store * store); +void nix_store_free(Store * store); /** * @brief get the URI of a nix store diff --git a/tests/unit/libstore-support/tests/nix_api_store.hh b/tests/unit/libstore-support/tests/nix_api_store.hh index e762a3ca8..34d467d49 100644 --- a/tests/unit/libstore-support/tests/nix_api_store.hh +++ b/tests/unit/libstore-support/tests/nix_api_store.hh @@ -27,7 +27,7 @@ public: }; ~nix_api_store_test() override { - nix_store_unref(store); + nix_store_free(store); for (auto & path : fs::recursive_directory_iterator(nixStoreDir)) { fs::permissions(path, fs::perms::owner_all); diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc index e093e9d15..54daf927a 100644 --- a/tests/unit/libstore/nix_api_store.cc +++ b/tests/unit/libstore/nix_api_store.cc @@ -69,7 +69,7 @@ TEST_F(nix_api_util_context, nix_store_open_dummy) nix_store_get_version(ctx, store, value, 256); ASSERT_STREQ("", value); - nix_store_unref(store); + nix_store_free(store); } TEST_F(nix_api_util_context, nix_store_open_invalid) @@ -78,7 +78,7 @@ TEST_F(nix_api_util_context, nix_store_open_invalid) Store * store = nix_store_open(ctx, "invalid://", nullptr); ASSERT_EQ(NIX_ERR_NIX_ERROR, ctx->last_err_code); ASSERT_EQ(nullptr, store); - nix_store_unref(store); + nix_store_free(store); } TEST_F(nix_api_store_test, nix_store_is_valid_path_not_in_store) From 693e8ec8fefa78aec72b2f5fd44842226e52d526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sun, 25 Feb 2024 14:41:35 +0100 Subject: [PATCH 125/168] C API: unify makefile after rebase --- Makefile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index ae4a8f0a4..306f9ed19 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,10 @@ ifeq ($(ENABLE_INTERNAL_API_DOCS), yes) makefiles-late += doc/internal-api/local.mk endif +ifeq ($(ENABLE_EXTERNAL_API_DOCS), yes) +makefiles-late += doc/external-api/local.mk +endif + # Miscellaneous global Flags OPTIMIZE = 1 @@ -130,9 +134,7 @@ internal-api-html: @exit 1 endif -ifeq ($(ENABLE_EXTERNAL_API_DOCS), yes) -$(eval $(call include-sub-makefile, doc/external-api/local.mk)) -else +ifneq ($(ENABLE_EXTERNAL_API_DOCS), yes) .PHONY: external-api-html external-api-html: @echo "External API docs are disabled. Configure with '--enable-external-api-docs', or avoid calling 'make external-api-html'." From 2e1dbbe307199442d7975958664fd57fc37eee70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sun, 25 Feb 2024 18:14:32 +0100 Subject: [PATCH 126/168] C API: refactor test support --- .../libexpr-support/tests/nix_api_expr.hh | 29 +++++++++++++++++++ tests/unit/libexpr/nix_api_expr.cc | 20 +------------ tests/unit/libexpr/nix_api_external.cc | 23 ++------------- tests/unit/libexpr/nix_api_value.cc | 26 +++-------------- .../libstore-support/tests/nix_api_store.hh | 24 ++++++++++----- .../libutil-support/tests/nix_api_util.hh | 7 ++--- 6 files changed, 55 insertions(+), 74 deletions(-) create mode 100644 tests/unit/libexpr-support/tests/nix_api_expr.hh diff --git a/tests/unit/libexpr-support/tests/nix_api_expr.hh b/tests/unit/libexpr-support/tests/nix_api_expr.hh new file mode 100644 index 000000000..f63c03319 --- /dev/null +++ b/tests/unit/libexpr-support/tests/nix_api_expr.hh @@ -0,0 +1,29 @@ +#pragma once +///@file +#include "nix_api_expr.h" +#include "nix_api_value.h" +#include "tests/nix_api_store.hh" + +#include + +namespace nixC { +class nix_api_expr_test : public nix_api_store_test +{ +protected: + + nix_api_expr_test() + { + nix_libexpr_init(ctx); + state = nix_state_create(nullptr, nullptr, store); + value = nix_alloc_value(nullptr, state); + } + ~nix_api_expr_test() + { + nix_gc_decref(nullptr, value); + nix_state_free(state); + } + + EvalState * state; + Value * value; +}; +} diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc index 5caccea9a..d14e90097 100644 --- a/tests/unit/libexpr/nix_api_expr.cc +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -5,30 +5,12 @@ #include "nix_api_expr.h" #include "nix_api_value.h" -#include "tests/nix_api_store.hh" +#include "tests/nix_api_expr.hh" #include namespace nixC { -class nix_api_expr_test : public nix_api_store_test -{ -public: - nix_api_expr_test() - { - state = nix_state_create(nullptr, nullptr, store); - value = nix_alloc_value(nullptr, state); - } - ~nix_api_expr_test() - { - nix_gc_decref(nullptr, value); - nix_state_free(state); - } - - EvalState * state; - Value * value; -}; - TEST_F(nix_api_expr_test, nix_expr_eval_from_string) { nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value); diff --git a/tests/unit/libexpr/nix_api_external.cc b/tests/unit/libexpr/nix_api_external.cc index 5f5353b04..1f190d9c8 100644 --- a/tests/unit/libexpr/nix_api_external.cc +++ b/tests/unit/libexpr/nix_api_external.cc @@ -6,9 +6,8 @@ #include "nix_api_expr_internal.h" #include "nix_api_value.h" #include "nix_api_external.h" -#include "tests/nix_api_store.hh" +#include "tests/nix_api_expr.hh" -#include #include namespace nixC { @@ -42,25 +41,7 @@ private: } }; -class nix_api_external_test : public nix_api_store_test -{ -public: - nix_api_external_test() - { - state = nix_state_create(nullptr, nullptr, store); - value = nix_alloc_value(nullptr, state); - } - ~nix_api_external_test() - { - nix_gc_decref(nullptr, value); - nix_state_free(state); - } - - EvalState * state; - Value * value; -}; - -TEST_F(nix_api_external_test, nix_expr_eval_from_string) +TEST_F(nix_api_expr_test, nix_expr_eval_external) { MyExternalValueDesc * external = new MyExternalValueDesc(42); ExternalValue * val = nix_create_external_value(ctx, external, external); diff --git a/tests/unit/libexpr/nix_api_value.cc b/tests/unit/libexpr/nix_api_value.cc index d1247e027..abed456f7 100644 --- a/tests/unit/libexpr/nix_api_value.cc +++ b/tests/unit/libexpr/nix_api_value.cc @@ -5,32 +5,14 @@ #include "nix_api_expr.h" #include "nix_api_value.h" -#include "tests/nix_api_store.hh" +#include "tests/nix_api_expr.hh" #include #include namespace nixC { -class nix_api_value_test : public nix_api_store_test -{ -public: - nix_api_value_test() - { - state = nix_state_create(nullptr, nullptr, store); - value = nix_alloc_value(nullptr, state); - } - ~nix_api_value_test() - { - nix_gc_decref(nullptr, value); - nix_state_free(state); - } - - EvalState * state; - Value * value; -}; - -TEST_F(nix_api_value_test, nix_value_set_get_int) +TEST_F(nix_api_expr_test, nix_value_set_get_int) { int myInt = 1; nix_init_int(nullptr, value, myInt); @@ -38,7 +20,7 @@ TEST_F(nix_api_value_test, nix_value_set_get_int) ASSERT_EQ(myInt, nix_get_int(nullptr, value)); } -TEST_F(nix_api_value_test, nix_make_and_set_list) +TEST_F(nix_api_expr_test, nix_build_and_init_list) { int size = 10; ListBuilder * builder = nix_make_list_builder(nullptr, state, size); @@ -56,7 +38,7 @@ TEST_F(nix_api_value_test, nix_make_and_set_list) nix_gc_decref(nullptr, intValue); } -TEST_F(nix_api_value_test, nix_make_attrs_t) +TEST_F(nix_api_expr_test, nix_build_and_init_attr) { int size = 10; const char ** out_name = (const char **) malloc(sizeof(char *)); diff --git a/tests/unit/libstore-support/tests/nix_api_store.hh b/tests/unit/libstore-support/tests/nix_api_store.hh index 34d467d49..4608dd90d 100644 --- a/tests/unit/libstore-support/tests/nix_api_store.hh +++ b/tests/unit/libstore-support/tests/nix_api_store.hh @@ -10,21 +10,16 @@ namespace fs = std::filesystem; +namespace nixC { class nix_api_store_test : public nix_api_util_context { public: nix_api_store_test() { nix_libstore_init(ctx); - - auto tmpl = nix::getEnv("TMPDIR").value_or("/tmp") + "/tests_nix-store.XXXXXX"; - nixStoreDir = mkdtemp((char *) tmpl.c_str()); - - // Options documented in `nix help-stores` - const char * p1[] = {"root", nixStoreDir.c_str()}; - const char ** params[] = {p1, nullptr}; - store = nix_store_open(ctx, "local", params); + init_local_store(); }; + ~nix_api_store_test() override { nix_store_free(store); @@ -37,4 +32,17 @@ public: Store * store; std::string nixStoreDir; + +protected: + void init_local_store() + { + auto tmpl = nix::getEnv("TMPDIR").value_or("/tmp") + "/tests_nix-store.XXXXXX"; + nixStoreDir = mkdtemp((char *) tmpl.c_str()); + + // Options documented in `nix help-stores` + const char * p1[] = {"root", nixStoreDir.c_str()}; + const char ** params[] = {p1, nullptr}; + store = nix_store_open(ctx, "local", params); + } }; +} diff --git a/tests/unit/libutil-support/tests/nix_api_util.hh b/tests/unit/libutil-support/tests/nix_api_util.hh index b007ac4b1..314ec70de 100644 --- a/tests/unit/libutil-support/tests/nix_api_util.hh +++ b/tests/unit/libutil-support/tests/nix_api_util.hh @@ -4,17 +4,15 @@ #include +namespace nixC { class nix_api_util_context : public ::testing::Test { protected: - static void SetUpTestSuite() - { - nix_libutil_init(NULL); - } nix_api_util_context() { ctx = nix_c_context_create(); + nix_libutil_init(ctx); }; ~nix_api_util_context() override @@ -25,3 +23,4 @@ protected: nix_c_context * ctx; }; +} From 1093ab64a24aec3032a0642b27522520c970d898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Sun, 25 Feb 2024 21:21:05 +0100 Subject: [PATCH 127/168] C API: add more tests --- src/libexpr/c/nix_api_value.cc | 4 +- src/libexpr/c/nix_api_value.h | 9 +++-- src/libexpr/eval.cc | 5 --- tests/unit/libexpr/nix_api_external.cc | 1 - tests/unit/libexpr/nix_api_value.cc | 56 ++++++++++++++++++++++++++ tests/unit/libutil/nix_api_util.cc | 37 ++++++++++------- 6 files changed, 86 insertions(+), 26 deletions(-) diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index e63d13f7a..740751beb 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -382,13 +382,13 @@ nix_err nix_init_string(nix_c_context * context, Value * value, const char * str NIXC_CATCH_ERRS } -nix_err nix_init_path_string(nix_c_context * context, Value * value, const char * str) +nix_err nix_init_path_string(nix_c_context * context, EvalState * s, Value * value, const char * str) { if (context) context->last_err_code = NIX_OK; try { auto & v = check_value_not_null(value); - v.mkPath(std::string_view(str)); + v.mkPath(s->state.rootPath(nix::CanonPath(str))); } NIXC_CATCH_ERRS } diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index 64c0c367a..e3e937e37 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -129,6 +129,7 @@ nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp); * @return value, or null in case of errors * */ + Value * nix_alloc_value(nix_c_context * context, EvalState * state); /** @addtogroup value_manip Manipulating values * @brief Functions to inspect and change Nix language values, represented by Value. @@ -142,6 +143,7 @@ Value * nix_alloc_value(nix_c_context * context, EvalState * state); * @param[in] value Nix value to inspect * @return type of nix value */ + ValueType nix_get_type(nix_c_context * context, const Value * value); /** @brief Get type name of value as defined in the evaluator * @param[out] context Optional, stores error information @@ -149,6 +151,7 @@ ValueType nix_get_type(nix_c_context * context, const Value * value); * @return type name, owned string * @todo way to free the result */ + const char * nix_get_typename(nix_c_context * context, const Value * value); /** @brief Get boolean value @@ -290,24 +293,24 @@ nix_err nix_init_bool(nix_c_context * context, Value * value, bool b); * @param[in] str the string, copied * @return error code, NIX_OK on success. */ - nix_err nix_init_string(nix_c_context * context, Value * value, const char * str); + /** @brief Set a path * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] str the path string, copied * @return error code, NIX_OK on success. */ +nix_err nix_init_path_string(nix_c_context * context, EvalState * s, Value * value, const char * str); -nix_err nix_init_path_string(nix_c_context * context, Value * value, const char * str); /** @brief Set a float * @param[out] context Optional, stores error information * @param[out] value Nix value to modify * @param[in] d the float, 64-bits * @return error code, NIX_OK on success. */ - nix_err nix_init_float(nix_c_context * context, Value * value, double d); + /** @brief Set an int * @param[out] context Optional, stores error information * @param[out] value Nix value to modify diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index a93e531b6..794451d82 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -897,11 +897,6 @@ void Value::mkStringMove(const char * s, const NixStringContext & context) copyContextToValue(*this, context); } -void Value::mkPath(std::string_view path) -{ - mkPath(makeImmutableString(path)); -} - void Value::mkPath(const SourcePath & path) { mkPath(&*path.accessor, makeImmutableString(path.path.abs())); diff --git a/tests/unit/libexpr/nix_api_external.cc b/tests/unit/libexpr/nix_api_external.cc index 1f190d9c8..68766fd47 100644 --- a/tests/unit/libexpr/nix_api_external.cc +++ b/tests/unit/libexpr/nix_api_external.cc @@ -31,7 +31,6 @@ private: static void type_of_function(void * self, nix_string_return * res) { - std::cout << self << std::endl; MyExternalValueDesc * obj = static_cast(self); std::string type_string = "nix-externallast_err, err_msg_ref); } -TEST_F(nix_api_util_context, nix_set_err_msg) { +TEST_F(nix_api_util_context, nix_set_err_msg) +{ ASSERT_EQ(ctx->last_err_code, NIX_OK); nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); ASSERT_EQ(ctx->last_err_code, NIX_ERR_UNKNOWN); ASSERT_EQ(*ctx->last_err, "unknown test error"); } -TEST(nix_api_util, nix_version_get) { +TEST(nix_api_util, nix_version_get) +{ ASSERT_EQ(std::string(nix_version_get()), PACKAGE_VERSION); } @@ -77,9 +80,10 @@ TEST_F(nix_api_util_context, nix_setting_set) ASSERT_STREQ("new-value", value); } -TEST_F(nix_api_util_context, nix_err_msg) { +TEST_F(nix_api_util_context, nix_err_msg) +{ // no error - EXPECT_THROW(nix_err_msg(NULL, ctx, NULL), nix::Error); + EXPECT_THROW(nix_err_msg(nullptr, ctx, NULL), nix::Error); // set error nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); @@ -90,46 +94,49 @@ TEST_F(nix_api_util_context, nix_err_msg) { // advanced usage unsigned int sz; - err_msg = nix_err_msg(NULL, ctx, &sz); + err_msg = nix_err_msg(nix_c_context_create(), ctx, &sz); ASSERT_EQ(sz, err_msg.size()); } -TEST_F(nix_api_util_context, nix_err_info_msg) { +TEST_F(nix_api_util_context, nix_err_info_msg) +{ // no error EXPECT_THROW(nix_err_info_msg(NULL, ctx, NULL, 256), nix::Error); try { throw nix::Error("testing error"); - } catch(...) { + } catch (...) { nix_context_error(ctx); } char buf[256]; - nix_err_info_msg(NULL, ctx, buf, 256); + nix_err_info_msg(nix_c_context_create(), ctx, buf, 256); ASSERT_EQ(std::string(buf), "testing error"); // should overflow EXPECT_THROW(nix_err_info_msg(NULL, ctx, buf, 1), nix::Error); } -TEST_F(nix_api_util_context, nix_err_name) { +TEST_F(nix_api_util_context, nix_err_name) +{ // no error EXPECT_THROW(nix_err_name(NULL, ctx, NULL, 256), nix::Error); std::string err_msg_ref; try { throw nix::Error("testing error"); - } catch(...) { + } catch (...) { nix_context_error(ctx); } char err_name[32]; - nix_err_name(NULL, ctx, err_name, 32); + nix_err_name(nix_c_context_create(), ctx, err_name, 32); ASSERT_EQ(std::string(err_name), "nix::Error"); // overflow EXPECT_THROW(nix_err_name(NULL, ctx, err_name, 1), nix::Error); } -TEST_F(nix_api_util_context, nix_err_code) { +TEST_F(nix_api_util_context, nix_err_code) +{ ASSERT_EQ(nix_err_code(ctx), NIX_OK); nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); ASSERT_EQ(nix_err_code(ctx), NIX_ERR_UNKNOWN); From 34d15e8f2fb0d653474d5cadc6725086649cfdf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Tue, 27 Feb 2024 18:09:38 +0100 Subject: [PATCH 128/168] C API: rename nix_store_build -> nix_store_realise --- src/libstore/c/nix_api_store.cc | 2 +- src/libstore/c/nix_api_store.h | 2 +- tests/unit/libexpr/nix_api_expr.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index d6602471d..c997019f9 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -100,7 +100,7 @@ StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const c NIXC_CATCH_ERRS_NULL } -nix_err nix_store_build( +nix_err nix_store_realise( nix_c_context * context, Store * store, StorePath * path, diff --git a/src/libstore/c/nix_api_store.h b/src/libstore/c/nix_api_store.h index e6d88026b..28544fa90 100644 --- a/src/libstore/c/nix_api_store.h +++ b/src/libstore/c/nix_api_store.h @@ -118,7 +118,7 @@ bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * * @param[in] userdata data to pass to every callback invocation * @param[in] callback called for every realised output */ -nix_err nix_store_build( +nix_err nix_store_realise( nix_c_context * context, Store * store, StorePath * path, diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc index d14e90097..103156744 100644 --- a/tests/unit/libexpr/nix_api_expr.cc +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -85,7 +85,7 @@ TEST_F(nix_api_expr_test, nix_build_drv) StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath); ASSERT_EQ(false, nix_store_is_valid_path(nullptr, store, outStorePath)); - nix_store_build(ctx, store, drvStorePath, nullptr, nullptr); + nix_store_realise(ctx, store, drvStorePath, nullptr, nullptr); // TODO figure out why fails. // `make libexpr-tests_RUN` works, but `nix build .` fails From 1a574c6c6051d20a2a737606cf0f4df581f024ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Tue, 27 Feb 2024 22:08:00 +0100 Subject: [PATCH 129/168] C API: refactor ListBuilder --- src/libexpr/c/nix_api_expr_internal.h | 32 ++++++++++++++++++++++++ src/libexpr/c/nix_api_value.cc | 36 ++++----------------------- src/libexpr/c/nix_api_value.h | 17 +++++++++---- tests/unit/libexpr/nix_api_value.cc | 2 +- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/libexpr/c/nix_api_expr_internal.h b/src/libexpr/c/nix_api_expr_internal.h index 352ac496f..2b066ecff 100644 --- a/src/libexpr/c/nix_api_expr_internal.h +++ b/src/libexpr/c/nix_api_expr_internal.h @@ -3,6 +3,33 @@ #include "eval.hh" #include "attr-set.hh" +#include "nix_api_value.h" + +class CListBuilder +{ +private: + std::vector values; + +public: + CListBuilder(size_t capacity) + { + values.reserve(capacity); + } + + void push_back(nix::Value * value) + { + values.push_back(value); + } + + Value * finish(nix::EvalState * state, nix::Value * list) + { + state->mkList(*list, values.size()); + for (size_t n = 0; n < list->listSize(); ++n) { + list->listElems()[n] = values[n]; + } + return list; + } +}; struct EvalState { @@ -14,6 +41,11 @@ struct BindingsBuilder nix::BindingsBuilder builder; }; +struct ListBuilder +{ + CListBuilder builder; +}; + struct nix_string_return { std::string str; diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr/c/nix_api_value.cc index 740751beb..1faf05611 100644 --- a/src/libexpr/c/nix_api_value.cc +++ b/src/libexpr/c/nix_api_value.cc @@ -17,32 +17,6 @@ #include "gc_cpp.h" #endif -class ListBuilder -{ -private: - std::vector values; - -public: - ListBuilder(size_t capacity) - { - values.reserve(capacity); - } - - void push_back(nix::Value * value) - { - values.push_back(value); - } - - Value * finish(nix::EvalState * state, nix::Value * list) - { - state->mkList(*list, values.size()); - for (size_t n = 0; n < list->listSize(); ++n) { - list->listElems()[n] = values[n]; - } - return list; - } -}; - // Helper function to throw an exception if value is null static const nix::Value & check_value_not_null(const Value * value) { @@ -443,7 +417,7 @@ ListBuilder * nix_make_list_builder(nix_c_context * context, EvalState * state, if (context) context->last_err_code = NIX_OK; try { - auto builder = ListBuilder(capacity); + auto builder = CListBuilder(capacity); return new #if HAVE_BOEHMGC (NoGC) @@ -453,12 +427,12 @@ ListBuilder * nix_make_list_builder(nix_c_context * context, EvalState * state, NIXC_CATCH_ERRS_NULL } -nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * builder, Value * value) +nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * list_builder, Value * value) { if (context) context->last_err_code = NIX_OK; try { - builder->push_back((nix::Value *) value); + list_builder->builder.push_back((nix::Value *) value); } NIXC_CATCH_ERRS } @@ -472,13 +446,13 @@ void nix_list_builder_free(ListBuilder * bb) #endif } -nix_err nix_make_list(nix_c_context * context, EvalState * s, Value * value, ListBuilder * b) +nix_err nix_make_list(nix_c_context * context, EvalState * s, ListBuilder * list_builder, Value * value) { if (context) context->last_err_code = NIX_OK; try { auto & v = check_value_not_null(value); - b->finish(&(s->state), &v); + list_builder->builder.finish(&(s->state), &v); } NIXC_CATCH_ERRS } diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr/c/nix_api_value.h index e3e937e37..b7115c27d 100644 --- a/src/libexpr/c/nix_api_value.h +++ b/src/libexpr/c/nix_api_value.h @@ -46,7 +46,14 @@ typedef struct EvalState EvalState; */ typedef struct BindingsBuilder BindingsBuilder; -typedef class ListBuilder ListBuilder; +/** @brief Stores an under-construction list + * @ingroup value_manip + * + * Do not reuse. + * @see nix_make_list_builder, nix_list_builder_free, nix_make_list + * @see nix_list_builder_insert + */ +typedef struct ListBuilder ListBuilder; /** @brief PrimOp function * @ingroup primops @@ -336,11 +343,11 @@ nix_err nix_init_external(nix_c_context * context, Value * value, ExternalValue /** @brief Create a list from a list builder * @param[out] context Optional, stores error information + * @param[in] list_builder list builder to use. Make sure to unref this afterwards. * @param[out] value Nix value to modify - * @param[in] b list builder to use. Make sure to unref this afterwards. * @return error code, NIX_OK on success. */ -nix_err nix_make_list(nix_c_context * context, EvalState * s, Value * value, ListBuilder * b); +nix_err nix_make_list(nix_c_context * context, EvalState * s, ListBuilder * list_builder, Value * value); /** @brief Create a list builder * @param[out] context Optional, stores error information @@ -352,11 +359,11 @@ ListBuilder * nix_make_list_builder(nix_c_context * context, EvalState * state, /** @brief Insert bindings into a builder * @param[out] context Optional, stores error information - * @param[in] builder ListBuilder to insert into + * @param[in] list_builder ListBuilder to insert into * @param[in] value value to insert * @return error code, NIX_OK on success. */ -nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * builder, Value * value); +nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * list_builder, Value * value); /** @brief Free a list builder * diff --git a/tests/unit/libexpr/nix_api_value.cc b/tests/unit/libexpr/nix_api_value.cc index 2f98297b3..ac28526c8 100644 --- a/tests/unit/libexpr/nix_api_value.cc +++ b/tests/unit/libexpr/nix_api_value.cc @@ -78,7 +78,7 @@ TEST_F(nix_api_expr_test, nix_build_and_init_list) Value * intValue = nix_alloc_value(nullptr, state); nix_init_int(nullptr, intValue, 42); nix_list_builder_insert(nullptr, builder, intValue); - nix_make_list(nullptr, state, value, builder); + nix_make_list(nullptr, state, builder, value); nix_list_builder_free(builder); ASSERT_EQ(42, nix_get_int(nullptr, nix_get_list_byidx(nullptr, value, state, 0))); From 31fbb24329851d4747d64319f62da9c7e77ead35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 29 Feb 2024 16:32:49 +0100 Subject: [PATCH 130/168] C API: refactor nix_store_realise --- src/libstore/c/nix_api_store.cc | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index c997019f9..199f5526a 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -5,6 +5,7 @@ #include "path.hh" #include "store-api.hh" +#include "build-result.hh" #include "globals.hh" @@ -110,16 +111,19 @@ nix_err nix_store_realise( if (context) context->last_err_code = NIX_OK; try { - store->ptr->buildPaths({ - nix::DerivedPath::Built{ - .drvPath = nix::makeConstantStorePathRef(path->path), - .outputs = nix::OutputsSpec::All{}, - }, - }); + + const std::vector paths{nix::DerivedPath::Built{ + .drvPath = nix::makeConstantStorePathRef(path->path), .outputs = nix::OutputsSpec::All{}}}; + + const auto nixStore = store->ptr; + auto results = nixStore->buildPathsWithResults(paths, nix::bmNormal, nixStore); + if (callback) { - for (auto & [outputName, outputPath] : store->ptr->queryDerivationOutputMap(path->path)) { - auto op = store->ptr->printStorePath(outputPath); - callback(userdata, outputName.c_str(), op.c_str()); + for (const auto & result : results) { + for (const auto & [outputName, realisation] : result.builtOutputs) { + auto op = store->ptr->printStorePath(realisation.outPath); + callback(userdata, outputName.c_str(), op.c_str()); + } } } } From 940ff6535c293cc3e78f99b806ef55b54eb2a7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 29 Feb 2024 18:33:07 +0100 Subject: [PATCH 131/168] C API: update libstore tests --- tests/unit/libexpr/nix_api_expr.cc | 19 ++++++++++--------- .../libstore-support/tests/nix_api_store.hh | 16 +++++++++++----- tests/unit/libstore/nix_api_store.cc | 18 ++++++++---------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc index 103156744..9d54a62f8 100644 --- a/tests/unit/libexpr/nix_api_expr.cc +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -45,8 +45,9 @@ TEST_F(nix_api_expr_test, nix_expr_eval_drv) nix_value_call(ctx, stateResult, valueFn, value, valueResult); ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, valueResult)); - const char * p = nix_get_string(nullptr, valueResult); - ASSERT_STREQ("/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname", p); + std::string p = nix_get_string(nullptr, valueResult); + std::string pEnd = "-myname"; + ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size())); // Clean up nix_gc_decref(nullptr, valueFn); @@ -73,22 +74,22 @@ TEST_F(nix_api_expr_test, nix_build_drv) ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size())); StorePath * drvStorePath = nix_store_parse_path(ctx, store, drvPath); - ASSERT_EQ(true, nix_store_is_valid_path(nullptr, store, drvStorePath)); + ASSERT_EQ(true, nix_store_is_valid_path(ctx, store, drvStorePath)); - Value * outPathValue = nix_get_attr_byname(nullptr, value, state, "outPath"); - const char * outPath = nix_get_string(nullptr, outPathValue); + Value * outPathValue = nix_get_attr_byname(ctx, value, state, "outPath"); + const char * outPath = nix_get_string(ctx, outPathValue); p = outPath; pEnd = "-myname"; ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size())); + ASSERT_EQ(true, drvStorePath->path.isDerivation()); StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath); - ASSERT_EQ(false, nix_store_is_valid_path(nullptr, store, outStorePath)); - - nix_store_realise(ctx, store, drvStorePath, nullptr, nullptr); + ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, outStorePath)); // TODO figure out why fails. - // `make libexpr-tests_RUN` works, but `nix build .` fails + // `make libexpr-tests_RUN` works, but `nix build .` enters an infinite loop + /* nix_store_realise(ctx, store, drvStorePath, nullptr, nullptr); */ /* auto is_valid_path = nix_store_is_valid_path(ctx, store, outStorePath); */ /* ASSERT_EQ(true, is_valid_path); */ diff --git a/tests/unit/libstore-support/tests/nix_api_store.hh b/tests/unit/libstore-support/tests/nix_api_store.hh index 4608dd90d..a8b60fbc3 100644 --- a/tests/unit/libstore-support/tests/nix_api_store.hh +++ b/tests/unit/libstore-support/tests/nix_api_store.hh @@ -24,24 +24,30 @@ public: { nix_store_free(store); - for (auto & path : fs::recursive_directory_iterator(nixStoreDir)) { + for (auto & path : fs::recursive_directory_iterator(nixDir)) { fs::permissions(path, fs::perms::owner_all); } - fs::remove_all(nixStoreDir); + fs::remove_all(nixDir); } Store * store; + std::string nixDir; std::string nixStoreDir; protected: void init_local_store() { auto tmpl = nix::getEnv("TMPDIR").value_or("/tmp") + "/tests_nix-store.XXXXXX"; - nixStoreDir = mkdtemp((char *) tmpl.c_str()); + nixDir = mkdtemp((char *) tmpl.c_str()); + nixStoreDir = nixDir + "/my_nix_store"; // Options documented in `nix help-stores` - const char * p1[] = {"root", nixStoreDir.c_str()}; - const char ** params[] = {p1, nullptr}; + const char * p1[] = {"store", nixStoreDir.c_str()}; + const char * p2[] = {"state", (new std::string(nixDir + "/my_state"))->c_str()}; + const char * p3[] = {"log", (new std::string(nixDir + "/my_log"))->c_str()}; + + const char ** params[] = {p1, p2, p3, nullptr}; + store = nix_store_open(ctx, "local", params); } }; diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc index 54daf927a..dac7fa910 100644 --- a/tests/unit/libstore/nix_api_store.cc +++ b/tests/unit/libstore/nix_api_store.cc @@ -5,12 +5,10 @@ #include "tests/nix_api_store.hh" -#define STORE_DIR "/nix/store/" -#define HASH_PART "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q" -const char * validPath = STORE_DIR HASH_PART "-x"; - namespace nixC { +std::string PATH_SUFFIX = "/g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-name"; + TEST_F(nix_api_util_context, nix_libstore_init) { auto ret = nix_libstore_init(ctx); @@ -33,21 +31,21 @@ TEST_F(nix_api_store_test, InvalidPathFails) TEST_F(nix_api_store_test, ReturnsValidStorePath) { - StorePath * result = nix_store_parse_path(ctx, store, validPath); + StorePath * result = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str()); ASSERT_NE(result, nullptr); - ASSERT_STREQ("x", result->path.name().data()); - ASSERT_STREQ(HASH_PART "-x", result->path.to_string().data()); + ASSERT_STREQ("name", result->path.name().data()); + ASSERT_STREQ(PATH_SUFFIX.substr(1).c_str(), result->path.to_string().data()); } TEST_F(nix_api_store_test, SetsLastErrCodeToNixOk) { - nix_store_parse_path(ctx, store, validPath); + nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str()); ASSERT_EQ(ctx->last_err_code, NIX_OK); } TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull) { - ASSERT_NO_THROW(nix_store_parse_path(nullptr, store, validPath)); + ASSERT_NO_THROW(nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str())); } TEST_F(nix_api_store_test, get_version) @@ -83,7 +81,7 @@ TEST_F(nix_api_util_context, nix_store_open_invalid) TEST_F(nix_api_store_test, nix_store_is_valid_path_not_in_store) { - StorePath * path = nix_store_parse_path(ctx, store, validPath); + StorePath * path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str()); ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, path)); } From d96b52bd8bd1b07377fe10633acd121e696cdee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Wed, 27 Mar 2024 17:50:36 +0100 Subject: [PATCH 132/168] C api: nix_export_std_string -> nix_observe_string --- src/libstore/c/nix_api_store.cc | 10 ++--- src/libstore/c/nix_api_store.h | 14 ++++--- src/libutil/c/nix_api_util.cc | 27 ++++++------ src/libutil/c/nix_api_util.h | 37 ++++++++++------ src/libutil/c/nix_api_util_internal.h | 14 +++---- tests/unit/libstore/nix_api_store.cc | 23 ++++++---- .../libutil-support/tests/nix_api_util.hh | 1 + tests/unit/libutil/nix_api_util.cc | 42 ++++++++++--------- 8 files changed, 92 insertions(+), 76 deletions(-) diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index 199f5526a..93e1626a1 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -56,26 +56,24 @@ void nix_store_free(Store * store) delete store; } -nix_err nix_store_get_uri(nix_c_context * context, Store * store, char * dest, unsigned int n) +nix_err nix_store_get_uri(nix_c_context * context, Store * store, void * callback, void * user_data) { if (context) context->last_err_code = NIX_OK; try { auto res = store->ptr->getUri(); - return nix_export_std_string(context, res, dest, n); + return call_nix_observe_string(res, callback, user_data); } NIXC_CATCH_ERRS } -nix_err nix_store_get_version(nix_c_context * context, Store * store, char * dest, unsigned int n) +nix_err nix_store_get_version(nix_c_context * context, Store * store, void * callback, void * user_data) { if (context) context->last_err_code = NIX_OK; try { auto res = store->ptr->getVersion(); - if (!res) - res = ""; - return nix_export_std_string(context, *res, dest, n); + return call_nix_observe_string(res.value_or(""), callback, user_data); } NIXC_CATCH_ERRS } diff --git a/src/libstore/c/nix_api_store.h b/src/libstore/c/nix_api_store.h index 28544fa90..25175de44 100644 --- a/src/libstore/c/nix_api_store.h +++ b/src/libstore/c/nix_api_store.h @@ -71,11 +71,12 @@ void nix_store_free(Store * store); * @brief get the URI of a nix store * @param[out] context Optional, stores error information * @param[in] store nix store reference - * @param[out] dest The allocated area to write the string to. - * @param[in] n Maximum size of the returned string. + * @param[in] callback Called with the URI. + * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. + * @see nix_observe_string * @return error code, NIX_OK on success. */ -nix_err nix_store_get_uri(nix_c_context * context, Store * store, char * dest, unsigned int n); +nix_err nix_store_get_uri(nix_c_context * context, Store * store, void * callback, void * user_data); // returns: owned StorePath* /** @@ -130,11 +131,12 @@ nix_err nix_store_realise( * If the store doesn't have a version (like the dummy store), returns an empty string. * @param[out] context Optional, stores error information * @param[in] store nix store reference - * @param[out] dest The allocated area to write the string to. - * @param[in] n Maximum size of the returned string. + * @param[in] callback Called with the version. + * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. + * @see nix_observe_string * @return error code, NIX_OK on success. */ -nix_err nix_store_get_version(nix_c_context *, Store * store, char * dest, unsigned int n); +nix_err nix_store_get_version(nix_c_context * context, Store * store, void * callback, void * user_data); // cffi end #ifdef __cplusplus diff --git a/src/libutil/c/nix_api_util.cc b/src/libutil/c/nix_api_util.cc index 100e3b21d..ed542059d 100644 --- a/src/libutil/c/nix_api_util.cc +++ b/src/libutil/c/nix_api_util.cc @@ -63,16 +63,17 @@ const char * nix_version_get() } // Implementations -nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n) + +nix_err nix_setting_get(nix_c_context * context, const char * key, void * callback, void * user_data) { if (context) context->last_err_code = NIX_OK; try { std::map settings; nix::globalConfig.getSettings(settings); - if (settings.contains(key)) - return nix_export_std_string(context, settings[key].value, value, n); - else { + if (settings.contains(key)) { + return call_nix_observe_string(settings[key].value, callback, user_data); + } else { return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); } } @@ -114,24 +115,24 @@ const char * nix_err_msg(nix_c_context * context, const nix_c_context * read_con return nullptr; } -nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n) +nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data) { if (context) context->last_err_code = NIX_OK; if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error"); } - return nix_export_std_string(context, read_context->name, value, n); + return call_nix_observe_string(read_context->name, callback, user_data); } -nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n) +nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data) { if (context) context->last_err_code = NIX_OK; if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error"); } - return nix_export_std_string(context, read_context->info->msg.str(), value, n); + return call_nix_observe_string(read_context->info->msg.str(), callback, user_data); } nix_err nix_err_code(const nix_c_context * read_context) @@ -140,12 +141,8 @@ nix_err nix_err_code(const nix_c_context * read_context) } // internal -nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n) +nix_err call_nix_observe_string(const std::string str, void * callback, void * user_data) { - size_t i = str.copy(dest, n - 1); - dest[i] = 0; - if (i == n - 1) { - return nix_set_err_msg(context, NIX_ERR_OVERFLOW, "Provided buffer too short"); - } else - return NIX_OK; + ((nix_observe_string) callback)(str.c_str(), str.size(), user_data); + return NIX_OK; } diff --git a/src/libutil/c/nix_api_util.h b/src/libutil/c/nix_api_util.h index c288654fd..fc6dc8655 100644 --- a/src/libutil/c/nix_api_util.h +++ b/src/libutil/c/nix_api_util.h @@ -119,6 +119,15 @@ typedef int nix_err; */ typedef struct nix_c_context nix_c_context; +/** + * @brief Called to get the value of a string owned by Nix. + * + * @param[in] start the string to copy. + * @param[in] n the string length. + * @param[in] user_data optional, arbitrary data, passed to the nix_observe_string when it's called. + */ +typedef void (*nix_observe_string)(const char * start, unsigned int n, void * user_data); + // Function prototypes /** @@ -160,14 +169,13 @@ nix_err nix_libutil_init(nix_c_context * context); * * @param[out] context optional, Stores error information * @param[in] key The key of the setting to retrieve. - * @param[out] value A pointer to a buffer where the value of the setting will - * be stored. - * @param[in] n The size of the buffer pointed to by value. - * @return NIX_ERR_KEY if the setting is unknown, NIX_ERR_OVERFLOW if the - * provided buffer is too short, or NIX_OK if the setting was retrieved + * @param[in] callback Called with the setting value. + * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. + * @see nix_observe_string + * @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was retrieved * successfully. */ -nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n); +nix_err nix_setting_get(nix_c_context * context, const char * key, void * callback, void * user_data); /** * @brief Sets a setting in the nix global configuration. @@ -227,12 +235,14 @@ const char * nix_err_msg(nix_c_context * context, const nix_c_context * ctx, uns * * @param[out] context optional, the context to store errors in if this function * fails - * @param[in] read_context the context to retrieve the error message from - * @param[out] value The allocated area to write the error string to. - * @param[in] n Maximum size of the returned string. + * @param[in] read_context the context to retrieve the error message from. + * @param[in] callback Called with the error message. + * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. + * @see nix_observe_string * @return NIX_OK if there were no errors, an error code otherwise. */ -nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n); +nix_err +nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data); /** * @brief Retrieves the error name from a context. @@ -245,11 +255,12 @@ nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_con * @param context optional, the context to store errors in if this function * fails * @param[in] read_context the context to retrieve the error message from - * @param[out] value The allocated area to write the error string to. - * @param[in] n Maximum size of the returned string. + * @param[in] callback Called with the error name. + * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. + * @see nix_observe_string * @return NIX_OK if there were no errors, an error code otherwise. */ -nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n); +nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data); /** * @brief Retrieves the most recent error code from a nix_c_context diff --git a/src/libutil/c/nix_api_util_internal.h b/src/libutil/c/nix_api_util_internal.h index 53c260e35..f91d8c118 100644 --- a/src/libutil/c/nix_api_util_internal.h +++ b/src/libutil/c/nix_api_util_internal.h @@ -20,16 +20,16 @@ nix_err nix_context_error(nix_c_context * context); /** * Internal use only. * - * Export a std::string across the C api boundary + * Helper to invoke nix_observe_string * @param context optional, the context to store errors in if this function * fails - * @param str The string to export - * @param value The allocated area to write the string to. - * @param n Maximum size of the returned string. - * @return NIX_OK if there were no errors, NIX_ERR_OVERFLOW if the string length - * exceeds `n`. + * @param str The string to observe + * @param callback Called with the observed string. + * @param user_data optional, arbitrary data, passed to the callback when it's called. + * @return NIX_OK if there were no errors. + * @see nix_observe_string */ -nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n); +nix_err call_nix_observe_string(const std::string str, void * callback, void * user_data); #define NIXC_CATCH_ERRS \ catch (...) \ diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc index dac7fa910..a31d66a4c 100644 --- a/tests/unit/libstore/nix_api_store.cc +++ b/tests/unit/libstore/nix_api_store.cc @@ -7,6 +7,11 @@ namespace nixC { +void observe_string_cb(const char * start, unsigned int n, std::string * user_data) +{ + *user_data = std::string(start); +} + std::string PATH_SUFFIX = "/g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-name"; TEST_F(nix_api_util_context, nix_libstore_init) @@ -17,10 +22,10 @@ TEST_F(nix_api_util_context, nix_libstore_init) TEST_F(nix_api_store_test, nix_store_get_uri) { - char value[256]; - auto ret = nix_store_get_uri(ctx, store, value, 256); + std::string str; + auto ret = nix_store_get_uri(ctx, store, (void *) observe_string_cb, &str); ASSERT_EQ(NIX_OK, ret); - ASSERT_STREQ("local", value); + ASSERT_STREQ("local", str.c_str()); } TEST_F(nix_api_store_test, InvalidPathFails) @@ -50,10 +55,10 @@ TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull) TEST_F(nix_api_store_test, get_version) { - char value[256]; - auto ret = nix_store_get_version(ctx, store, value, 256); + std::string str; + auto ret = nix_store_get_version(ctx, store, (void *) observe_string_cb, &str); ASSERT_EQ(NIX_OK, ret); - ASSERT_STREQ(PACKAGE_VERSION, value); + ASSERT_STREQ(PACKAGE_VERSION, str.c_str()); } TEST_F(nix_api_util_context, nix_store_open_dummy) @@ -63,9 +68,9 @@ TEST_F(nix_api_util_context, nix_store_open_dummy) ASSERT_EQ(NIX_OK, ctx->last_err_code); ASSERT_STREQ("dummy", store->ptr->getUri().c_str()); - char value[256]; - nix_store_get_version(ctx, store, value, 256); - ASSERT_STREQ("", value); + std::string str; + nix_store_get_version(ctx, store, (void *) observe_string_cb, &str); + ASSERT_STREQ("", str.c_str()); nix_store_free(store); } diff --git a/tests/unit/libutil-support/tests/nix_api_util.hh b/tests/unit/libutil-support/tests/nix_api_util.hh index 314ec70de..0dfb38f7b 100644 --- a/tests/unit/libutil-support/tests/nix_api_util.hh +++ b/tests/unit/libutil-support/tests/nix_api_util.hh @@ -5,6 +5,7 @@ #include namespace nixC { + class nix_api_util_context : public ::testing::Test { protected: diff --git a/tests/unit/libutil/nix_api_util.cc b/tests/unit/libutil/nix_api_util.cc index a3ec5e0a1..20e46637c 100644 --- a/tests/unit/libutil/nix_api_util.cc +++ b/tests/unit/libutil/nix_api_util.cc @@ -9,6 +9,11 @@ namespace nixC { +void observe_string_cb(const char * start, unsigned int n, std::string * user_data) +{ + *user_data = std::string(start); +} + TEST_F(nix_api_util_context, nix_context_error) { std::string err_msg_ref; @@ -57,13 +62,13 @@ static nix::GlobalConfig::Register rs(&mySettings); TEST_F(nix_api_util_context, nix_setting_get) { ASSERT_EQ(ctx->last_err_code, NIX_OK); - char value[256]; - nix_err result = nix_setting_get(ctx, "invalid-key", value, 256); + std::string setting_value; + nix_err result = nix_setting_get(ctx, "invalid-key", (void *) observe_string_cb, &setting_value); ASSERT_EQ(result, NIX_ERR_KEY); - result = nix_setting_get(ctx, "setting-name", value, 256); + result = nix_setting_get(ctx, "setting-name", (void *) observe_string_cb, &setting_value); ASSERT_EQ(result, NIX_OK); - ASSERT_STREQ("empty", value); + ASSERT_STREQ("empty", setting_value.c_str()); } TEST_F(nix_api_util_context, nix_setting_set) @@ -74,10 +79,10 @@ TEST_F(nix_api_util_context, nix_setting_set) result = nix_setting_set(ctx, "setting-name", "new-value"); ASSERT_EQ(result, NIX_OK); - char value[256]; - result = nix_setting_get(ctx, "setting-name", value, 256); + std::string setting_value; + result = nix_setting_get(ctx, "setting-name", (void *) observe_string_cb, &setting_value); ASSERT_EQ(result, NIX_OK); - ASSERT_STREQ("new-value", value); + ASSERT_STREQ("new-value", setting_value.c_str()); } TEST_F(nix_api_util_context, nix_err_msg) @@ -100,26 +105,26 @@ TEST_F(nix_api_util_context, nix_err_msg) TEST_F(nix_api_util_context, nix_err_info_msg) { + std::string err_info; + // no error - EXPECT_THROW(nix_err_info_msg(NULL, ctx, NULL, 256), nix::Error); + EXPECT_THROW(nix_err_info_msg(NULL, ctx, (void *) observe_string_cb, &err_info), nix::Error); try { throw nix::Error("testing error"); } catch (...) { nix_context_error(ctx); } - char buf[256]; - nix_err_info_msg(nix_c_context_create(), ctx, buf, 256); - ASSERT_EQ(std::string(buf), "testing error"); - - // should overflow - EXPECT_THROW(nix_err_info_msg(NULL, ctx, buf, 1), nix::Error); + nix_err_info_msg(nix_c_context_create(), ctx, (void *) observe_string_cb, &err_info); + ASSERT_STREQ("testing error", err_info.c_str()); } TEST_F(nix_api_util_context, nix_err_name) { + std::string err_name; + // no error - EXPECT_THROW(nix_err_name(NULL, ctx, NULL, 256), nix::Error); + EXPECT_THROW(nix_err_name(NULL, ctx, (void *) observe_string_cb, &err_name), nix::Error); std::string err_msg_ref; try { @@ -127,12 +132,8 @@ TEST_F(nix_api_util_context, nix_err_name) } catch (...) { nix_context_error(ctx); } - char err_name[32]; - nix_err_name(nix_c_context_create(), ctx, err_name, 32); + nix_err_name(nix_c_context_create(), ctx, (void *) observe_string_cb, &err_name); ASSERT_EQ(std::string(err_name), "nix::Error"); - - // overflow - EXPECT_THROW(nix_err_name(NULL, ctx, err_name, 1), nix::Error); } TEST_F(nix_api_util_context, nix_err_code) @@ -141,4 +142,5 @@ TEST_F(nix_api_util_context, nix_err_code) nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error"); ASSERT_EQ(nix_err_code(ctx), NIX_ERR_UNKNOWN); } + } From c57de60522c3f2d493b6c013072b5c2b0dea3f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 28 Mar 2024 19:00:04 +0100 Subject: [PATCH 133/168] C API: Keep the structure flat See https://github.com/NixOS/nix/pull/10329 --- Makefile | 6 ++--- doc/external-api/doxygen.cfg.in | 6 ++--- local.mk | 2 +- src/libexpr-c/local.mk | 25 +++++++++++++++++++ src/{libexpr/c => libexpr-c}/nix-expr-c.pc.in | 0 src/{libexpr/c => libexpr-c}/nix_api_expr.cc | 0 src/{libexpr/c => libexpr-c}/nix_api_expr.h | 0 .../c => libexpr-c}/nix_api_expr_internal.h | 0 .../c => libexpr-c}/nix_api_external.cc | 0 .../c => libexpr-c}/nix_api_external.h | 0 src/{libexpr/c => libexpr-c}/nix_api_value.cc | 0 src/{libexpr/c => libexpr-c}/nix_api_value.h | 0 src/libexpr/c/local.mk | 19 -------------- src/{libstore/c => libstore-c}/local.mk | 6 ++++- .../c => libstore-c}/nix-store-c.pc.in | 0 .../c => libstore-c}/nix_api_store.cc | 0 .../c => libstore-c}/nix_api_store.h | 0 .../c => libstore-c}/nix_api_store_internal.h | 0 src/{libutil/c => libutil-c}/local.mk | 5 +++- src/{libutil/c => libutil-c}/nix_api_util.cc | 0 src/{libutil/c => libutil-c}/nix_api_util.h | 0 .../c => libutil-c}/nix_api_util_internal.h | 0 tests/unit/libexpr/local.mk | 5 +++- tests/unit/libstore/local.mk | 4 ++- tests/unit/libutil/local.mk | 3 ++- tests/unit/libutil/nix_api_util.cc | 1 - 26 files changed, 50 insertions(+), 32 deletions(-) create mode 100644 src/libexpr-c/local.mk rename src/{libexpr/c => libexpr-c}/nix-expr-c.pc.in (100%) rename src/{libexpr/c => libexpr-c}/nix_api_expr.cc (100%) rename src/{libexpr/c => libexpr-c}/nix_api_expr.h (100%) rename src/{libexpr/c => libexpr-c}/nix_api_expr_internal.h (100%) rename src/{libexpr/c => libexpr-c}/nix_api_external.cc (100%) rename src/{libexpr/c => libexpr-c}/nix_api_external.h (100%) rename src/{libexpr/c => libexpr-c}/nix_api_value.cc (100%) rename src/{libexpr/c => libexpr-c}/nix_api_value.h (100%) delete mode 100644 src/libexpr/c/local.mk rename src/{libstore/c => libstore-c}/local.mk (57%) rename src/{libstore/c => libstore-c}/nix-store-c.pc.in (100%) rename src/{libstore/c => libstore-c}/nix_api_store.cc (100%) rename src/{libstore/c => libstore-c}/nix_api_store.h (100%) rename src/{libstore/c => libstore-c}/nix_api_store_internal.h (100%) rename src/{libutil/c => libutil-c}/local.mk (53%) rename src/{libutil/c => libutil-c}/nix_api_util.cc (100%) rename src/{libutil/c => libutil-c}/nix_api_util.h (100%) rename src/{libutil/c => libutil-c}/nix_api_util_internal.h (100%) diff --git a/Makefile b/Makefile index 306f9ed19..788ed3571 100644 --- a/Makefile +++ b/Makefile @@ -18,9 +18,9 @@ makefiles = \ src/libexpr/local.mk \ src/libcmd/local.mk \ src/nix/local.mk \ - src/libutil/c/local.mk \ - src/libstore/c/local.mk \ - src/libexpr/c/local.mk \ + src/libutil-c/local.mk \ + src/libstore-c/local.mk \ + src/libexpr-c/local.mk \ src/resolve-system-dependencies/local.mk \ scripts/local.mk \ misc/bash/local.mk \ diff --git a/doc/external-api/doxygen.cfg.in b/doc/external-api/doxygen.cfg.in index 454514935..cd8b4989b 100644 --- a/doc/external-api/doxygen.cfg.in +++ b/doc/external-api/doxygen.cfg.in @@ -36,9 +36,9 @@ GENERATE_LATEX = NO # so they can expand variables despite configure variables. INPUT = \ - src/libutil/c \ - src/libexpr/c \ - src/libstore/c \ + src/libutil-c \ + src/libexpr-c \ + src/libstore-c \ doc/external-api/README.md FILE_PATTERNS = nix_api_*.h *.md diff --git a/local.mk b/local.mk index 9a1ed50df..69ef02f08 100644 --- a/local.mk +++ b/local.mk @@ -2,7 +2,7 @@ GLOBAL_CXXFLAGS += -Wno-deprecated-declarations -Werror=switch # Allow switch-enum to be overridden for files that do not support it, usually because of dependency headers. ERROR_SWITCH_ENUM = -Werror=switch-enum -$(foreach i, config.h $(wildcard src/lib*/*.hh) $(wildcard src/lib*/*.h $(filter-out %_internal.h, $(wildcard src/lib*/c/*.h))), \ +$(foreach i, config.h $(wildcard src/lib*/*.hh) $(wildcard src/lib*/*.h $(filter-out %_internal.h, $(wildcard src/lib*c/*.h))), \ $(eval $(call install-file-in, $(i), $(includedir)/nix, 0644))) $(GCH): src/libutil/util.hh config.h diff --git a/src/libexpr-c/local.mk b/src/libexpr-c/local.mk new file mode 100644 index 000000000..ce5d321d6 --- /dev/null +++ b/src/libexpr-c/local.mk @@ -0,0 +1,25 @@ +libraries += libexprc + +libexprc_NAME = libnixexprc + +libexprc_DIR := $(d) + +libexprc_SOURCES := \ + $(wildcard $(d)/*.cc) \ + +# Not just for this library itself, but also for downstream libraries using this library + +INCLUDE_libexprc := -I $(d) +libexprc_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libutilc) \ + $(INCLUDE_libfetchers) \ + $(INCLUDE_libstore) $(INCLUDE_libstorec) \ + $(INCLUDE_libexpr) $(INCLUDE_libexprc) + +libexprc_LIBS = libutil libutilc libstore libstorec libexpr + +libexprc_LDFLAGS += -pthread + +$(eval $(call install-file-in, $(d)/nix-expr-c.pc, $(libdir)/pkgconfig, 0644)) + +libexprc_FORCE_INSTALL := 1 + diff --git a/src/libexpr/c/nix-expr-c.pc.in b/src/libexpr-c/nix-expr-c.pc.in similarity index 100% rename from src/libexpr/c/nix-expr-c.pc.in rename to src/libexpr-c/nix-expr-c.pc.in diff --git a/src/libexpr/c/nix_api_expr.cc b/src/libexpr-c/nix_api_expr.cc similarity index 100% rename from src/libexpr/c/nix_api_expr.cc rename to src/libexpr-c/nix_api_expr.cc diff --git a/src/libexpr/c/nix_api_expr.h b/src/libexpr-c/nix_api_expr.h similarity index 100% rename from src/libexpr/c/nix_api_expr.h rename to src/libexpr-c/nix_api_expr.h diff --git a/src/libexpr/c/nix_api_expr_internal.h b/src/libexpr-c/nix_api_expr_internal.h similarity index 100% rename from src/libexpr/c/nix_api_expr_internal.h rename to src/libexpr-c/nix_api_expr_internal.h diff --git a/src/libexpr/c/nix_api_external.cc b/src/libexpr-c/nix_api_external.cc similarity index 100% rename from src/libexpr/c/nix_api_external.cc rename to src/libexpr-c/nix_api_external.cc diff --git a/src/libexpr/c/nix_api_external.h b/src/libexpr-c/nix_api_external.h similarity index 100% rename from src/libexpr/c/nix_api_external.h rename to src/libexpr-c/nix_api_external.h diff --git a/src/libexpr/c/nix_api_value.cc b/src/libexpr-c/nix_api_value.cc similarity index 100% rename from src/libexpr/c/nix_api_value.cc rename to src/libexpr-c/nix_api_value.cc diff --git a/src/libexpr/c/nix_api_value.h b/src/libexpr-c/nix_api_value.h similarity index 100% rename from src/libexpr/c/nix_api_value.h rename to src/libexpr-c/nix_api_value.h diff --git a/src/libexpr/c/local.mk b/src/libexpr/c/local.mk deleted file mode 100644 index 01b03f4d2..000000000 --- a/src/libexpr/c/local.mk +++ /dev/null @@ -1,19 +0,0 @@ -libraries += libexprc - -libexprc_NAME = libnixexprc - -libexprc_DIR := $(d) - -libexprc_SOURCES := \ - $(wildcard $(d)/*.cc) \ - -libexprc_CXXFLAGS += -I src/libutil -Isrc/libfetchers -I src/libstore -I src/libstorec -I src/libexpr -I src/libutil/c -I src/libstore/c - -libexprc_LIBS = libutil libutilc libstore libstorec libexpr - -libexprc_LDFLAGS += -pthread - -$(eval $(call install-file-in, $(d)/nix-expr-c.pc, $(libdir)/pkgconfig, 0644)) - -libexprc_FORCE_INSTALL := 1 - diff --git a/src/libstore/c/local.mk b/src/libstore-c/local.mk similarity index 57% rename from src/libstore/c/local.mk rename to src/libstore-c/local.mk index 35e2bd63d..36a8e77a4 100644 --- a/src/libstore/c/local.mk +++ b/src/libstore-c/local.mk @@ -10,7 +10,11 @@ libstorec_LIBS = libutil libstore libutilc libstorec_LDFLAGS += -pthread -libstorec_CXXFLAGS += -I src/libutil -I src/libstore -I src/libutil/c +# Not just for this library itself, but also for downstream libraries using this library + +INCLUDE_libstorec := -I $(d) +libstorec_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libutilc) \ + $(INCLUDE_libstore) $(INCLUDE_libstorec) $(eval $(call install-file-in, $(d)/nix-store-c.pc, $(libdir)/pkgconfig, 0644)) diff --git a/src/libstore/c/nix-store-c.pc.in b/src/libstore-c/nix-store-c.pc.in similarity index 100% rename from src/libstore/c/nix-store-c.pc.in rename to src/libstore-c/nix-store-c.pc.in diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc similarity index 100% rename from src/libstore/c/nix_api_store.cc rename to src/libstore-c/nix_api_store.cc diff --git a/src/libstore/c/nix_api_store.h b/src/libstore-c/nix_api_store.h similarity index 100% rename from src/libstore/c/nix_api_store.h rename to src/libstore-c/nix_api_store.h diff --git a/src/libstore/c/nix_api_store_internal.h b/src/libstore-c/nix_api_store_internal.h similarity index 100% rename from src/libstore/c/nix_api_store_internal.h rename to src/libstore-c/nix_api_store_internal.h diff --git a/src/libutil/c/local.mk b/src/libutil-c/local.mk similarity index 53% rename from src/libutil/c/local.mk rename to src/libutil-c/local.mk index fe156e7f3..342dc2d8b 100644 --- a/src/libutil/c/local.mk +++ b/src/libutil-c/local.mk @@ -6,7 +6,10 @@ libutilc_DIR := $(d) libutilc_SOURCES := $(wildcard $(d)/*.cc) -libutilc_CXXFLAGS += -I src/libutil +# Not just for this library itself, but also for downstream libraries using this library + +INCLUDE_libutilc := -I $(d) +libutilc_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libutilc) libutilc_LIBS = libutil diff --git a/src/libutil/c/nix_api_util.cc b/src/libutil-c/nix_api_util.cc similarity index 100% rename from src/libutil/c/nix_api_util.cc rename to src/libutil-c/nix_api_util.cc diff --git a/src/libutil/c/nix_api_util.h b/src/libutil-c/nix_api_util.h similarity index 100% rename from src/libutil/c/nix_api_util.h rename to src/libutil-c/nix_api_util.h diff --git a/src/libutil/c/nix_api_util_internal.h b/src/libutil-c/nix_api_util_internal.h similarity index 100% rename from src/libutil/c/nix_api_util_internal.h rename to src/libutil-c/nix_api_util_internal.h diff --git a/tests/unit/libexpr/local.mk b/tests/unit/libexpr/local.mk index 0a7b28436..8df1a5207 100644 --- a/tests/unit/libexpr/local.mk +++ b/tests/unit/libexpr/local.mk @@ -24,9 +24,12 @@ libexpr-tests_EXTRA_INCLUDES = \ -I tests/unit/libstore-support \ -I tests/unit/libutil-support \ $(INCLUDE_libexpr) \ + $(INCLUDE_libexprc) \ $(INCLUDE_libfetchers) \ $(INCLUDE_libstore) \ - $(INCLUDE_libutil) + $(INCLUDE_libstorec) \ + $(INCLUDE_libutil) \ + $(INCLUDE_libutilc) libexpr-tests_CXXFLAGS += $(libexpr-tests_EXTRA_INCLUDES) diff --git a/tests/unit/libstore/local.mk b/tests/unit/libstore/local.mk index fe1254487..b8f895fad 100644 --- a/tests/unit/libstore/local.mk +++ b/tests/unit/libstore/local.mk @@ -20,7 +20,9 @@ libstore-tests_EXTRA_INCLUDES = \ -I tests/unit/libstore-support \ -I tests/unit/libutil-support \ $(INCLUDE_libstore) \ - $(INCLUDE_libutil) + $(INCLUDE_libstorec) \ + $(INCLUDE_libutil) \ + $(INCLUDE_libutilc) libstore-tests_CXXFLAGS += $(libstore-tests_EXTRA_INCLUDES) diff --git a/tests/unit/libutil/local.mk b/tests/unit/libutil/local.mk index 0d0acd4c0..39b4c0782 100644 --- a/tests/unit/libutil/local.mk +++ b/tests/unit/libutil/local.mk @@ -18,7 +18,8 @@ libutil-tests_SOURCES := $(wildcard $(d)/*.cc) libutil-tests_EXTRA_INCLUDES = \ -I tests/unit/libutil-support \ - $(INCLUDE_libutil) + $(INCLUDE_libutil) \ + $(INCLUDE_libutilc) libutil-tests_CXXFLAGS += $(libutil-tests_EXTRA_INCLUDES) diff --git a/tests/unit/libutil/nix_api_util.cc b/tests/unit/libutil/nix_api_util.cc index 20e46637c..09f3f3e05 100644 --- a/tests/unit/libutil/nix_api_util.cc +++ b/tests/unit/libutil/nix_api_util.cc @@ -1,4 +1,3 @@ - #include "config.hh" #include "args.hh" #include "nix_api_util.h" From 925a8fda6e2709a1cae2f5684bd7f5e91d9375d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 28 Mar 2024 19:02:01 +0100 Subject: [PATCH 134/168] C API: Use new ListBuilder helper See https://github.com/NixOS/nix/pull/10251 --- src/libexpr-c/nix_api_expr_internal.h | 28 +-------------------------- src/libexpr-c/nix_api_value.cc | 16 ++++++++------- src/libexpr-c/nix_api_value.h | 5 +++-- tests/unit/libexpr/nix_api_value.cc | 5 +++-- 4 files changed, 16 insertions(+), 38 deletions(-) diff --git a/src/libexpr-c/nix_api_expr_internal.h b/src/libexpr-c/nix_api_expr_internal.h index 2b066ecff..b50a51347 100644 --- a/src/libexpr-c/nix_api_expr_internal.h +++ b/src/libexpr-c/nix_api_expr_internal.h @@ -5,32 +5,6 @@ #include "attr-set.hh" #include "nix_api_value.h" -class CListBuilder -{ -private: - std::vector values; - -public: - CListBuilder(size_t capacity) - { - values.reserve(capacity); - } - - void push_back(nix::Value * value) - { - values.push_back(value); - } - - Value * finish(nix::EvalState * state, nix::Value * list) - { - state->mkList(*list, values.size()); - for (size_t n = 0; n < list->listSize(); ++n) { - list->listElems()[n] = values[n]; - } - return list; - } -}; - struct EvalState { nix::EvalState state; @@ -43,7 +17,7 @@ struct BindingsBuilder struct ListBuilder { - CListBuilder builder; + nix::ListBuilder builder; }; struct nix_string_return diff --git a/src/libexpr-c/nix_api_value.cc b/src/libexpr-c/nix_api_value.cc index 1faf05611..26ae194d9 100644 --- a/src/libexpr-c/nix_api_value.cc +++ b/src/libexpr-c/nix_api_value.cc @@ -263,7 +263,8 @@ Value * nix_get_list_byidx(nix_c_context * context, const Value * value, EvalSta assert(v.type() == nix::nList); auto * p = v.listElems()[ix]; nix_gc_incref(nullptr, p); - state->state.forceValue(*p, nix::noPos); + if (p != nullptr) + state->state.forceValue(*p, nix::noPos); return (Value *) p; } NIXC_CATCH_ERRS_NULL @@ -417,7 +418,7 @@ ListBuilder * nix_make_list_builder(nix_c_context * context, EvalState * state, if (context) context->last_err_code = NIX_OK; try { - auto builder = CListBuilder(capacity); + auto builder = state->state.buildList(capacity); return new #if HAVE_BOEHMGC (NoGC) @@ -427,20 +428,21 @@ ListBuilder * nix_make_list_builder(nix_c_context * context, EvalState * state, NIXC_CATCH_ERRS_NULL } -nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * list_builder, Value * value) +nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * list_builder, unsigned int index, Value * value) { if (context) context->last_err_code = NIX_OK; try { - list_builder->builder.push_back((nix::Value *) value); + auto & e = check_value_not_null(value); + list_builder->builder[index] = &e; } NIXC_CATCH_ERRS } -void nix_list_builder_free(ListBuilder * bb) +void nix_list_builder_free(ListBuilder * list_builder) { #if HAVE_BOEHMGC - GC_FREE(bb); + GC_FREE(list_builder); #else delete bb; #endif @@ -452,7 +454,7 @@ nix_err nix_make_list(nix_c_context * context, EvalState * s, ListBuilder * list context->last_err_code = NIX_OK; try { auto & v = check_value_not_null(value); - list_builder->builder.finish(&(s->state), &v); + v.mkList(list_builder->builder); } NIXC_CATCH_ERRS } diff --git a/src/libexpr-c/nix_api_value.h b/src/libexpr-c/nix_api_value.h index b7115c27d..d80414fe8 100644 --- a/src/libexpr-c/nix_api_value.h +++ b/src/libexpr-c/nix_api_value.h @@ -360,17 +360,18 @@ ListBuilder * nix_make_list_builder(nix_c_context * context, EvalState * state, /** @brief Insert bindings into a builder * @param[out] context Optional, stores error information * @param[in] list_builder ListBuilder to insert into + * @param[in] index index to manipulate * @param[in] value value to insert * @return error code, NIX_OK on success. */ -nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * list_builder, Value * value); +nix_err nix_list_builder_insert(nix_c_context * context, ListBuilder * list_builder, unsigned int index, Value * value); /** @brief Free a list builder * * Does not fail. * @param[in] builder the builder to free */ -void nix_list_builder_free(ListBuilder * builder); +void nix_list_builder_free(ListBuilder * list_builder); /** @brief Create an attribute set from a bindings builder * @param[out] context Optional, stores error information diff --git a/tests/unit/libexpr/nix_api_value.cc b/tests/unit/libexpr/nix_api_value.cc index ac28526c8..fd7c91a7d 100644 --- a/tests/unit/libexpr/nix_api_value.cc +++ b/tests/unit/libexpr/nix_api_value.cc @@ -77,12 +77,13 @@ TEST_F(nix_api_expr_test, nix_build_and_init_list) Value * intValue = nix_alloc_value(nullptr, state); nix_init_int(nullptr, intValue, 42); - nix_list_builder_insert(nullptr, builder, intValue); + nix_list_builder_insert(nullptr, builder, 0, intValue); nix_make_list(nullptr, state, builder, value); nix_list_builder_free(builder); ASSERT_EQ(42, nix_get_int(nullptr, nix_get_list_byidx(nullptr, value, state, 0))); - ASSERT_EQ(1, nix_get_list_size(nullptr, value)); + ASSERT_EQ(nullptr, nix_get_list_byidx(nullptr, value, state, 1)); + ASSERT_EQ(10, nix_get_list_size(nullptr, value)); ASSERT_STREQ("a list", nix_get_typename(nullptr, value)); ASSERT_EQ(NIX_TYPE_LIST, nix_get_type(nullptr, value)); From 061140fc8fbf6f9dd8e89c8843a8879e0d88b95e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 28 Mar 2024 19:38:12 +0100 Subject: [PATCH 135/168] C API: remove unused argument --- src/libexpr-c/nix_api_value.cc | 2 +- src/libexpr-c/nix_api_value.h | 2 +- tests/unit/libexpr/nix_api_value.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libexpr-c/nix_api_value.cc b/src/libexpr-c/nix_api_value.cc index 26ae194d9..93e7db5c0 100644 --- a/src/libexpr-c/nix_api_value.cc +++ b/src/libexpr-c/nix_api_value.cc @@ -448,7 +448,7 @@ void nix_list_builder_free(ListBuilder * list_builder) #endif } -nix_err nix_make_list(nix_c_context * context, EvalState * s, ListBuilder * list_builder, Value * value) +nix_err nix_make_list(nix_c_context * context, ListBuilder * list_builder, Value * value) { if (context) context->last_err_code = NIX_OK; diff --git a/src/libexpr-c/nix_api_value.h b/src/libexpr-c/nix_api_value.h index d80414fe8..c42581278 100644 --- a/src/libexpr-c/nix_api_value.h +++ b/src/libexpr-c/nix_api_value.h @@ -347,7 +347,7 @@ nix_err nix_init_external(nix_c_context * context, Value * value, ExternalValue * @param[out] value Nix value to modify * @return error code, NIX_OK on success. */ -nix_err nix_make_list(nix_c_context * context, EvalState * s, ListBuilder * list_builder, Value * value); +nix_err nix_make_list(nix_c_context * context, ListBuilder * list_builder, Value * value); /** @brief Create a list builder * @param[out] context Optional, stores error information diff --git a/tests/unit/libexpr/nix_api_value.cc b/tests/unit/libexpr/nix_api_value.cc index fd7c91a7d..20c874f22 100644 --- a/tests/unit/libexpr/nix_api_value.cc +++ b/tests/unit/libexpr/nix_api_value.cc @@ -78,7 +78,7 @@ TEST_F(nix_api_expr_test, nix_build_and_init_list) Value * intValue = nix_alloc_value(nullptr, state); nix_init_int(nullptr, intValue, 42); nix_list_builder_insert(nullptr, builder, 0, intValue); - nix_make_list(nullptr, state, builder, value); + nix_make_list(nullptr, builder, value); nix_list_builder_free(builder); ASSERT_EQ(42, nix_get_int(nullptr, nix_get_list_byidx(nullptr, value, state, 0))); From 2bb609bce278fc817608dfb68516b5296a24d2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Fri, 29 Mar 2024 10:01:16 +0100 Subject: [PATCH 136/168] C API: rename nix_observe_string -> nix_get_string_callback --- src/libstore-c/nix_api_store.cc | 4 ++-- src/libstore-c/nix_api_store.h | 4 ++-- src/libutil-c/nix_api_util.cc | 10 +++++----- src/libutil-c/nix_api_util.h | 10 +++++----- src/libutil-c/nix_api_util_internal.h | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index 93e1626a1..d80ba332e 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -62,7 +62,7 @@ nix_err nix_store_get_uri(nix_c_context * context, Store * store, void * callbac context->last_err_code = NIX_OK; try { auto res = store->ptr->getUri(); - return call_nix_observe_string(res, callback, user_data); + return call_nix_get_string_callback(res, callback, user_data); } NIXC_CATCH_ERRS } @@ -73,7 +73,7 @@ nix_err nix_store_get_version(nix_c_context * context, Store * store, void * cal context->last_err_code = NIX_OK; try { auto res = store->ptr->getVersion(); - return call_nix_observe_string(res.value_or(""), callback, user_data); + return call_nix_get_string_callback(res.value_or(""), callback, user_data); } NIXC_CATCH_ERRS } diff --git a/src/libstore-c/nix_api_store.h b/src/libstore-c/nix_api_store.h index 25175de44..1309f99b7 100644 --- a/src/libstore-c/nix_api_store.h +++ b/src/libstore-c/nix_api_store.h @@ -73,7 +73,7 @@ void nix_store_free(Store * store); * @param[in] store nix store reference * @param[in] callback Called with the URI. * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. - * @see nix_observe_string + * @see nix_get_string_callback * @return error code, NIX_OK on success. */ nix_err nix_store_get_uri(nix_c_context * context, Store * store, void * callback, void * user_data); @@ -133,7 +133,7 @@ nix_err nix_store_realise( * @param[in] store nix store reference * @param[in] callback Called with the version. * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. - * @see nix_observe_string + * @see nix_get_string_callback * @return error code, NIX_OK on success. */ nix_err nix_store_get_version(nix_c_context * context, Store * store, void * callback, void * user_data); diff --git a/src/libutil-c/nix_api_util.cc b/src/libutil-c/nix_api_util.cc index ed542059d..8d0f7ac38 100644 --- a/src/libutil-c/nix_api_util.cc +++ b/src/libutil-c/nix_api_util.cc @@ -72,7 +72,7 @@ nix_err nix_setting_get(nix_c_context * context, const char * key, void * callba std::map settings; nix::globalConfig.getSettings(settings); if (settings.contains(key)) { - return call_nix_observe_string(settings[key].value, callback, user_data); + return call_nix_get_string_callback(settings[key].value, callback, user_data); } else { return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found"); } @@ -122,7 +122,7 @@ nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error"); } - return call_nix_observe_string(read_context->name, callback, user_data); + return call_nix_get_string_callback(read_context->name, callback, user_data); } nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data) @@ -132,7 +132,7 @@ nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_con if (read_context->last_err_code != NIX_ERR_NIX_ERROR) { return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error"); } - return call_nix_observe_string(read_context->info->msg.str(), callback, user_data); + return call_nix_get_string_callback(read_context->info->msg.str(), callback, user_data); } nix_err nix_err_code(const nix_c_context * read_context) @@ -141,8 +141,8 @@ nix_err nix_err_code(const nix_c_context * read_context) } // internal -nix_err call_nix_observe_string(const std::string str, void * callback, void * user_data) +nix_err call_nix_get_string_callback(const std::string str, void * callback, void * user_data) { - ((nix_observe_string) callback)(str.c_str(), str.size(), user_data); + ((nix_get_string_callback) callback)(str.c_str(), str.size(), user_data); return NIX_OK; } diff --git a/src/libutil-c/nix_api_util.h b/src/libutil-c/nix_api_util.h index fc6dc8655..cb506ca90 100644 --- a/src/libutil-c/nix_api_util.h +++ b/src/libutil-c/nix_api_util.h @@ -124,9 +124,9 @@ typedef struct nix_c_context nix_c_context; * * @param[in] start the string to copy. * @param[in] n the string length. - * @param[in] user_data optional, arbitrary data, passed to the nix_observe_string when it's called. + * @param[in] user_data optional, arbitrary data, passed to the nix_get_string_callback when it's called. */ -typedef void (*nix_observe_string)(const char * start, unsigned int n, void * user_data); +typedef void (*nix_get_string_callback)(const char * start, unsigned int n, void * user_data); // Function prototypes @@ -171,7 +171,7 @@ nix_err nix_libutil_init(nix_c_context * context); * @param[in] key The key of the setting to retrieve. * @param[in] callback Called with the setting value. * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. - * @see nix_observe_string + * @see nix_get_string_callback * @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was retrieved * successfully. */ @@ -238,7 +238,7 @@ const char * nix_err_msg(nix_c_context * context, const nix_c_context * ctx, uns * @param[in] read_context the context to retrieve the error message from. * @param[in] callback Called with the error message. * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. - * @see nix_observe_string + * @see nix_get_string_callback * @return NIX_OK if there were no errors, an error code otherwise. */ nix_err @@ -257,7 +257,7 @@ nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, vo * @param[in] read_context the context to retrieve the error message from * @param[in] callback Called with the error name. * @param[in] user_data optional, arbitrary data, passed to the callback when it's called. - * @see nix_observe_string + * @see nix_get_string_callback * @return NIX_OK if there were no errors, an error code otherwise. */ nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data); diff --git a/src/libutil-c/nix_api_util_internal.h b/src/libutil-c/nix_api_util_internal.h index f91d8c118..6e8eac020 100644 --- a/src/libutil-c/nix_api_util_internal.h +++ b/src/libutil-c/nix_api_util_internal.h @@ -20,16 +20,16 @@ nix_err nix_context_error(nix_c_context * context); /** * Internal use only. * - * Helper to invoke nix_observe_string + * Helper to invoke nix_get_string_callback * @param context optional, the context to store errors in if this function * fails * @param str The string to observe * @param callback Called with the observed string. * @param user_data optional, arbitrary data, passed to the callback when it's called. * @return NIX_OK if there were no errors. - * @see nix_observe_string + * @see nix_get_string_callback */ -nix_err call_nix_observe_string(const std::string str, void * callback, void * user_data); +nix_err call_nix_get_string_callback(const std::string str, void * callback, void * user_data); #define NIXC_CATCH_ERRS \ catch (...) \ From 2d84433a3b573c846103eb85ece9fdb77ed5fa4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Fri, 29 Mar 2024 10:05:21 +0100 Subject: [PATCH 137/168] C API: update documentation --- doc/external-api/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/external-api/README.md b/doc/external-api/README.md index 1d7344ddd..8a6f1c085 100644 --- a/doc/external-api/README.md +++ b/doc/external-api/README.md @@ -41,6 +41,8 @@ Nix expression `builtins.nixVersion`. #include #include +// NOTE: This example lacks all error handling. Production code must check for +// errors, as some return values will be undefined. int main() { nix_libexpr_init(NULL); From dffc22f30fd5f25861809bc5d9ae8405390035df Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 29 Mar 2024 12:51:14 +0100 Subject: [PATCH 138/168] Rename local-store.sh -> chroot-store.sh --- tests/functional/{local-store.sh => chroot-store.sh} | 0 tests/functional/local.mk | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/functional/{local-store.sh => chroot-store.sh} (100%) diff --git a/tests/functional/local-store.sh b/tests/functional/chroot-store.sh similarity index 100% rename from tests/functional/local-store.sh rename to tests/functional/chroot-store.sh diff --git a/tests/functional/local.mk b/tests/functional/local.mk index 8bb8e3600..ca9837d32 100644 --- a/tests/functional/local.mk +++ b/tests/functional/local.mk @@ -83,7 +83,7 @@ nix_tests = \ export.sh \ config.sh \ add.sh \ - local-store.sh \ + chroot-store.sh \ filter-source.sh \ misc.sh \ dump-db.sh \ From 89307728649cd96cb82fba739c014c1e78a6fc31 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 29 Mar 2024 13:19:36 +0100 Subject: [PATCH 139/168] Add regression test for #10331, #10267 --- tests/functional/chroot-store.sh | 41 +++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/tests/functional/chroot-store.sh b/tests/functional/chroot-store.sh index f7c8eb3f1..9e589d04b 100644 --- a/tests/functional/chroot-store.sh +++ b/tests/functional/chroot-store.sh @@ -1,22 +1,45 @@ source common.sh -cd $TEST_ROOT +echo example > $TEST_ROOT/example.txt +mkdir -p $TEST_ROOT/x -echo example > example.txt -mkdir -p ./x +export NIX_STORE_DIR=/nix2/store -NIX_STORE_DIR=$TEST_ROOT/x +CORRECT_PATH=$(cd $TEST_ROOT && nix-store --store ./x --add example.txt) -CORRECT_PATH=$(nix-store --store ./x --add example.txt) +[[ $CORRECT_PATH =~ ^/nix2/store/.*-example.txt$ ]] -PATH1=$(nix path-info --store ./x $CORRECT_PATH) +PATH1=$(cd $TEST_ROOT && nix path-info --store ./x $CORRECT_PATH) [ $CORRECT_PATH == $PATH1 ] -PATH2=$(nix path-info --store "$PWD/x" $CORRECT_PATH) +PATH2=$(nix path-info --store "$TEST_ROOT/x" $CORRECT_PATH) [ $CORRECT_PATH == $PATH2 ] -PATH3=$(nix path-info --store "local?root=$PWD/x" $CORRECT_PATH) +PATH3=$(nix path-info --store "local?root=$TEST_ROOT/x" $CORRECT_PATH) [ $CORRECT_PATH == $PATH3 ] # Ensure store info trusted works with local store -nix --store ./x store info --json | jq -e '.trusted' +nix --store $TEST_ROOT/x store info --json | jq -e '.trusted' + +# Test building in a chroot store. +if canUseSandbox; then + + flakeDir=$TEST_ROOT/flake + mkdir -p $flakeDir + + cat > $flakeDir/flake.nix < Date: Wed, 27 Mar 2024 14:49:45 +0100 Subject: [PATCH 140/168] Add trust-tarballs-from-git-forges setting If enabled, GitHub flakerefs don't require a content hash, a Git revision is enough. Fixes #10297. --- src/libfetchers/fetch-settings.hh | 17 ++++++++++++++++- src/libfetchers/github.cc | 4 +++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/libfetchers/fetch-settings.hh b/src/libfetchers/fetch-settings.hh index f095963a8..d085f0d82 100644 --- a/src/libfetchers/fetch-settings.hh +++ b/src/libfetchers/fetch-settings.hh @@ -78,7 +78,6 @@ struct FetchSettings : public Config )", {}, true, Xp::Flakes}; - Setting useRegistries{this, true, "use-registries", "Whether to use flake registries to resolve flake references.", {}, true, Xp::Flakes}; @@ -94,6 +93,22 @@ struct FetchSettings : public Config empty, the summary is generated based on the action performed. )", {}, true, Xp::Flakes}; + + Setting trustTarballsFromGitForges{ + this, true, "trust-tarballs-from-git-forges", + R"( + If enabled (the default), Nix will consider tarballs from + GitHub and similar Git forges to be locked if a Git revision + is specified, + e.g. `github:NixOS/patchelf/7c2f768bf9601268a4e71c2ebe91e2011918a70f`. + This requires Nix to trust that the provider will return the + correct contents for the specified Git revision. + + If disabled, such tarballs are only considered locked if a + `narHash` attribute is specified, + e.g. `github:NixOS/patchelf/7c2f768bf9601268a4e71c2ebe91e2011918a70f?narHash=sha256-PPXqKY2hJng4DBVE0I4xshv/vGLUskL7jl53roB8UdU%3D`. + )"}; + }; // FIXME: don't use a global variable. diff --git a/src/libfetchers/github.cc b/src/libfetchers/github.cc index 8100afe4d..60e323464 100644 --- a/src/libfetchers/github.cc +++ b/src/libfetchers/github.cc @@ -294,7 +294,9 @@ struct GitArchiveInputScheme : InputScheme Git revision alone, we also require a NAR hash for locking. FIXME: in the future, we may want to require a Git tree hash instead of a NAR hash. */ - return input.getRev().has_value() && input.getNarHash().has_value(); + return input.getRev().has_value() + && (fetchSettings.trustTarballsFromGitForges || + input.getNarHash().has_value()); } std::optional experimentalFeature() const override From 00ce36fafe175ba607522a9c9d549a604ed00522 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 27 Mar 2024 15:53:11 +0100 Subject: [PATCH 141/168] Add test --- tests/nixos/github-flakes.nix | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/nixos/github-flakes.nix b/tests/nixos/github-flakes.nix index 6f8a5b9d8..221045009 100644 --- a/tests/nixos/github-flakes.nix +++ b/tests/nixos/github-flakes.nix @@ -187,9 +187,14 @@ in client.succeed("nix flake metadata nixpkgs --tarball-ttl 0 >&2") # Test fetchTree on a github URL. - hash = client.succeed(f"nix eval --raw --expr '(fetchTree {info['url']}).narHash'") + hash = client.succeed(f"nix eval --no-trust-tarballs-from-git-forges --raw --expr '(fetchTree {info['url']}).narHash'") assert hash == info['locked']['narHash'] + # Fetching without a narHash should succeed if trust-github is set and fail otherwise. + client.succeed(f"nix eval --raw --expr 'builtins.fetchTree github:github:fancy-enterprise/private-flake/{info['revision']}'") + out = client.fail(f"nix eval --no-trust-tarballs-from-git-forges --raw --expr 'builtins.fetchTree github:github:fancy-enterprise/private-flake/{info['revision']}' 2>&1") + assert "will not fetch unlocked input" in out, "--no-trust-tarballs-from-git-forges did not fail with the expected error" + # Shut down the web server. The flake should be cached on the client. github.succeed("systemctl stop httpd.service") From a09d0e19c11a9b92cfca3898ccf8c32a04a7973c Mon Sep 17 00:00:00 2001 From: Jonathan De Troye Date: Fri, 29 Mar 2024 08:57:41 -0400 Subject: [PATCH 142/168] flakes: Reword doc/manual/rl-next/remove-repl-flake.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com> --- doc/manual/rl-next/remove-repl-flake.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/doc/manual/rl-next/remove-repl-flake.md b/doc/manual/rl-next/remove-repl-flake.md index a4ddeaf59..23298e2ed 100644 --- a/doc/manual/rl-next/remove-repl-flake.md +++ b/doc/manual/rl-next/remove-repl-flake.md @@ -5,8 +5,4 @@ issues: 10103 prs: 10299 --- -This PR removes the repl-flake feature that was adopted to provide a migration path when changing the behavior of `nix repl`. Moving forward this command will behave more like the rest of the modern cli. - -- Removes any repl-flake references. -- Removes the parts of `applyDefaultInstallables` that are no longer needed in repl.cc. -- Fix/Add any tests. +The `repl-flake` experimental feature has been removed. The `nix repl` command now works like the rest of the new CLI in that `nix repl {path}` now tries to load a flake at `{path}` (or fails if the `flakes` experimental feature isn't enabled).* From 926fbadcc30a4614b5f5a3d18a6f4096914f97da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Fri, 29 Mar 2024 14:00:19 +0100 Subject: [PATCH 143/168] C API: add more tests --- src/libexpr-c/nix_api_value.cc | 2 +- src/libexpr-c/nix_api_value.h | 6 +- .../libexpr-support/tests/nix_api_expr.hh | 2 + tests/unit/libexpr/nix_api_external.cc | 2 +- tests/unit/libexpr/nix_api_value.cc | 172 +++++++++++------- 5 files changed, 112 insertions(+), 72 deletions(-) diff --git a/src/libexpr-c/nix_api_value.cc b/src/libexpr-c/nix_api_value.cc index 93e7db5c0..80e853b87 100644 --- a/src/libexpr-c/nix_api_value.cc +++ b/src/libexpr-c/nix_api_value.cc @@ -227,7 +227,7 @@ double nix_get_float(nix_c_context * context, const Value * value) assert(v.type() == nix::nFloat); return v.fpoint; } - NIXC_CATCH_ERRS_RES(NAN); + NIXC_CATCH_ERRS_RES(0.0); } int64_t nix_get_int(nix_c_context * context, const Value * value) diff --git a/src/libexpr-c/nix_api_value.h b/src/libexpr-c/nix_api_value.h index c42581278..42218188c 100644 --- a/src/libexpr-c/nix_api_value.h +++ b/src/libexpr-c/nix_api_value.h @@ -136,8 +136,8 @@ nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp); * @return value, or null in case of errors * */ - Value * nix_alloc_value(nix_c_context * context, EvalState * state); + /** @addtogroup value_manip Manipulating values * @brief Functions to inspect and change Nix language values, represented by Value. * @{ @@ -150,15 +150,14 @@ Value * nix_alloc_value(nix_c_context * context, EvalState * state); * @param[in] value Nix value to inspect * @return type of nix value */ - ValueType nix_get_type(nix_c_context * context, const Value * value); + /** @brief Get type name of value as defined in the evaluator * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect * @return type name, owned string * @todo way to free the result */ - const char * nix_get_typename(nix_c_context * context, const Value * value); /** @brief Get boolean value @@ -167,6 +166,7 @@ const char * nix_get_typename(nix_c_context * context, const Value * value); * @return true or false, error info via context */ bool nix_get_bool(nix_c_context * context, const Value * value); + /** @brief Get string * @param[out] context Optional, stores error information * @param[in] value Nix value to inspect diff --git a/tests/unit/libexpr-support/tests/nix_api_expr.hh b/tests/unit/libexpr-support/tests/nix_api_expr.hh index f63c03319..d1840d034 100644 --- a/tests/unit/libexpr-support/tests/nix_api_expr.hh +++ b/tests/unit/libexpr-support/tests/nix_api_expr.hh @@ -7,6 +7,7 @@ #include namespace nixC { + class nix_api_expr_test : public nix_api_store_test { protected: @@ -26,4 +27,5 @@ protected: EvalState * state; Value * value; }; + } diff --git a/tests/unit/libexpr/nix_api_external.cc b/tests/unit/libexpr/nix_api_external.cc index 68766fd47..7e2caed1b 100644 --- a/tests/unit/libexpr/nix_api_external.cc +++ b/tests/unit/libexpr/nix_api_external.cc @@ -44,7 +44,7 @@ TEST_F(nix_api_expr_test, nix_expr_eval_external) { MyExternalValueDesc * external = new MyExternalValueDesc(42); ExternalValue * val = nix_create_external_value(ctx, external, external); - nix_init_external(nullptr, value, val); + nix_init_external(ctx, value, val); EvalState * stateResult = nix_state_create(nullptr, nullptr, store); Value * valueResult = nix_alloc_value(nullptr, stateResult); diff --git a/tests/unit/libexpr/nix_api_value.cc b/tests/unit/libexpr/nix_api_value.cc index 20c874f22..726960638 100644 --- a/tests/unit/libexpr/nix_api_value.cc +++ b/tests/unit/libexpr/nix_api_value.cc @@ -14,132 +14,170 @@ namespace nixC { TEST_F(nix_api_expr_test, nix_value_set_get_int) { - int myInt = 1; - nix_init_int(nullptr, value, myInt); + ASSERT_EQ(0, nix_get_int(ctx, nullptr)); + ASSERT_DEATH(nix_get_int(ctx, value), ""); - ASSERT_EQ(myInt, nix_get_int(nullptr, value)); - ASSERT_STREQ("an integer", nix_get_typename(nullptr, value)); - ASSERT_EQ(NIX_TYPE_INT, nix_get_type(nullptr, value)); + int myInt = 1; + nix_init_int(ctx, value, myInt); + + ASSERT_EQ(myInt, nix_get_int(ctx, value)); + ASSERT_STREQ("an integer", nix_get_typename(ctx, value)); + ASSERT_EQ(NIX_TYPE_INT, nix_get_type(ctx, value)); } TEST_F(nix_api_expr_test, nix_value_set_get_float) { - float myDouble = 1.0; - nix_init_float(nullptr, value, myDouble); + ASSERT_FLOAT_EQ(0.0, nix_get_float(ctx, nullptr)); + ASSERT_DEATH(nix_get_float(ctx, value), ""); - ASSERT_EQ(myDouble, nix_get_float(nullptr, value)); - ASSERT_STREQ("a float", nix_get_typename(nullptr, value)); - ASSERT_EQ(NIX_TYPE_FLOAT, nix_get_type(nullptr, value)); + float myDouble = 1.0; + nix_init_float(ctx, value, myDouble); + + ASSERT_FLOAT_EQ(myDouble, nix_get_float(ctx, value)); + ASSERT_STREQ("a float", nix_get_typename(ctx, value)); + ASSERT_EQ(NIX_TYPE_FLOAT, nix_get_type(ctx, value)); } TEST_F(nix_api_expr_test, nix_value_set_get_bool) { - bool myBool = true; - nix_init_bool(nullptr, value, myBool); + ASSERT_EQ(false, nix_get_bool(ctx, nullptr)); + ASSERT_DEATH(nix_get_bool(ctx, value), ""); - ASSERT_EQ(myBool, nix_get_bool(nullptr, value)); - ASSERT_STREQ("a Boolean", nix_get_typename(nullptr, value)); - ASSERT_EQ(NIX_TYPE_BOOL, nix_get_type(nullptr, value)); + bool myBool = true; + nix_init_bool(ctx, value, myBool); + + ASSERT_EQ(myBool, nix_get_bool(ctx, value)); + ASSERT_STREQ("a Boolean", nix_get_typename(ctx, value)); + ASSERT_EQ(NIX_TYPE_BOOL, nix_get_type(ctx, value)); } TEST_F(nix_api_expr_test, nix_value_set_get_string) { - const char * myString = "some string"; - nix_init_string(nullptr, value, myString); + ASSERT_EQ(nullptr, nix_get_string(ctx, nullptr)); + ASSERT_DEATH(nix_get_string(ctx, value), ""); - ASSERT_STREQ(myString, nix_get_string(nullptr, value)); - ASSERT_STREQ("a string", nix_get_typename(nullptr, value)); - ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, value)); + const char * myString = "some string"; + nix_init_string(ctx, value, myString); + + ASSERT_STREQ(myString, nix_get_string(ctx, value)); + ASSERT_STREQ("a string", nix_get_typename(ctx, value)); + ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(ctx, value)); } TEST_F(nix_api_expr_test, nix_value_set_get_null) { - nix_init_null(nullptr, value); + ASSERT_DEATH(nix_get_typename(ctx, value), ""); - ASSERT_STREQ("null", nix_get_typename(nullptr, value)); - ASSERT_EQ(NIX_TYPE_NULL, nix_get_type(nullptr, value)); + nix_init_null(ctx, value); + + ASSERT_STREQ("null", nix_get_typename(ctx, value)); + ASSERT_EQ(NIX_TYPE_NULL, nix_get_type(ctx, value)); } TEST_F(nix_api_expr_test, nix_value_set_get_path) { - const char * p = "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname"; - nix_init_path_string(nullptr, state, value, p); + ASSERT_EQ(nullptr, nix_get_path_string(ctx, nullptr)); + ASSERT_DEATH(nix_get_path_string(ctx, value), ""); - ASSERT_STREQ(p, nix_get_path_string(nullptr, value)); - ASSERT_STREQ("a path", nix_get_typename(nullptr, value)); - ASSERT_EQ(NIX_TYPE_PATH, nix_get_type(nullptr, value)); + const char * p = "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname"; + nix_init_path_string(ctx, state, value, p); + + ASSERT_STREQ(p, nix_get_path_string(ctx, value)); + ASSERT_STREQ("a path", nix_get_typename(ctx, value)); + ASSERT_EQ(NIX_TYPE_PATH, nix_get_type(ctx, value)); } TEST_F(nix_api_expr_test, nix_build_and_init_list) { - int size = 10; - ListBuilder * builder = nix_make_list_builder(nullptr, state, size); + ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, nullptr, state, 0)); + ASSERT_EQ(0, nix_get_list_size(ctx, nullptr)); - Value * intValue = nix_alloc_value(nullptr, state); - nix_init_int(nullptr, intValue, 42); - nix_list_builder_insert(nullptr, builder, 0, intValue); - nix_make_list(nullptr, builder, value); + ASSERT_DEATH(nix_get_list_byidx(ctx, value, state, 0), ""); + ASSERT_DEATH(nix_get_list_size(ctx, value), ""); + + int size = 10; + ListBuilder * builder = nix_make_list_builder(ctx, state, size); + + Value * intValue = nix_alloc_value(ctx, state); + nix_init_int(ctx, intValue, 42); + nix_list_builder_insert(ctx, builder, 0, intValue); + nix_make_list(ctx, builder, value); nix_list_builder_free(builder); - ASSERT_EQ(42, nix_get_int(nullptr, nix_get_list_byidx(nullptr, value, state, 0))); - ASSERT_EQ(nullptr, nix_get_list_byidx(nullptr, value, state, 1)); - ASSERT_EQ(10, nix_get_list_size(nullptr, value)); + ASSERT_EQ(42, nix_get_int(ctx, nix_get_list_byidx(ctx, value, state, 0))); + ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, value, state, 1)); + ASSERT_EQ(10, nix_get_list_size(ctx, value)); - ASSERT_STREQ("a list", nix_get_typename(nullptr, value)); - ASSERT_EQ(NIX_TYPE_LIST, nix_get_type(nullptr, value)); + ASSERT_STREQ("a list", nix_get_typename(ctx, value)); + ASSERT_EQ(NIX_TYPE_LIST, nix_get_type(ctx, value)); // Clean up - nix_gc_decref(nullptr, intValue); + nix_gc_decref(ctx, intValue); } TEST_F(nix_api_expr_test, nix_build_and_init_attr) { + ASSERT_EQ(nullptr, nix_get_attr_byname(ctx, nullptr, state, 0)); + ASSERT_EQ(nullptr, nix_get_attr_byidx(ctx, nullptr, state, 0, nullptr)); + ASSERT_EQ(nullptr, nix_get_attr_name_byidx(ctx, nullptr, state, 0)); + ASSERT_EQ(0, nix_get_attrs_size(ctx, nullptr)); + ASSERT_EQ(false, nix_has_attr_byname(ctx, nullptr, state, "no-value")); + + ASSERT_DEATH(nix_get_attr_byname(ctx, value, state, 0), ""); + ASSERT_DEATH(nix_get_attr_byidx(ctx, value, state, 0, nullptr), ""); + ASSERT_DEATH(nix_get_attr_name_byidx(ctx, value, state, 0), ""); + ASSERT_DEATH(nix_get_attrs_size(ctx, value), ""); + ASSERT_DEATH(nix_has_attr_byname(ctx, value, state, "no-value"), ""); + int size = 10; const char ** out_name = (const char **) malloc(sizeof(char *)); - BindingsBuilder * builder = nix_make_bindings_builder(nullptr, state, size); + BindingsBuilder * builder = nix_make_bindings_builder(ctx, state, size); - Value * intValue = nix_alloc_value(nullptr, state); - nix_init_int(nullptr, intValue, 42); + Value * intValue = nix_alloc_value(ctx, state); + nix_init_int(ctx, intValue, 42); - Value * stringValue = nix_alloc_value(nullptr, state); - nix_init_string(nullptr, stringValue, "foo"); + Value * stringValue = nix_alloc_value(ctx, state); + nix_init_string(ctx, stringValue, "foo"); - nix_bindings_builder_insert(nullptr, builder, "a", intValue); - nix_bindings_builder_insert(nullptr, builder, "b", stringValue); - nix_make_attrs(nullptr, value, builder); + nix_bindings_builder_insert(ctx, builder, "a", intValue); + nix_bindings_builder_insert(ctx, builder, "b", stringValue); + nix_make_attrs(ctx, value, builder); nix_bindings_builder_free(builder); - ASSERT_EQ(2, nix_get_attrs_size(nullptr, value)); + ASSERT_EQ(2, nix_get_attrs_size(ctx, value)); - Value * out_value = nix_get_attr_byname(nullptr, value, state, "a"); - ASSERT_EQ(42, nix_get_int(nullptr, out_value)); - nix_gc_decref(nullptr, out_value); + Value * out_value = nix_get_attr_byname(ctx, value, state, "a"); + ASSERT_EQ(42, nix_get_int(ctx, out_value)); + nix_gc_decref(ctx, out_value); - out_value = nix_get_attr_byidx(nullptr, value, state, 0, out_name); - ASSERT_EQ(42, nix_get_int(nullptr, out_value)); + out_value = nix_get_attr_byidx(ctx, value, state, 0, out_name); + ASSERT_EQ(42, nix_get_int(ctx, out_value)); ASSERT_STREQ("a", *out_name); + nix_gc_decref(ctx, out_value); + + ASSERT_STREQ("a", nix_get_attr_name_byidx(ctx, value, state, 0)); + + ASSERT_EQ(true, nix_has_attr_byname(ctx, value, state, "b")); + ASSERT_EQ(false, nix_has_attr_byname(ctx, value, state, "no-value")); + + out_value = nix_get_attr_byname(ctx, value, state, "b"); + ASSERT_STREQ("foo", nix_get_string(ctx, out_value)); nix_gc_decref(nullptr, out_value); - ASSERT_STREQ("a", nix_get_attr_name_byidx(nullptr, value, state, 0)); - - out_value = nix_get_attr_byname(nullptr, value, state, "b"); - ASSERT_STREQ("foo", nix_get_string(nullptr, out_value)); - nix_gc_decref(nullptr, out_value); - - out_value = nix_get_attr_byidx(nullptr, value, state, 1, out_name); - ASSERT_STREQ("foo", nix_get_string(nullptr, out_value)); + out_value = nix_get_attr_byidx(ctx, value, state, 1, out_name); + ASSERT_STREQ("foo", nix_get_string(ctx, out_value)); ASSERT_STREQ("b", *out_name); nix_gc_decref(nullptr, out_value); - ASSERT_STREQ("b", nix_get_attr_name_byidx(nullptr, value, state, 1)); + ASSERT_STREQ("b", nix_get_attr_name_byidx(ctx, value, state, 1)); - ASSERT_STREQ("a set", nix_get_typename(nullptr, value)); - ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(nullptr, value)); + ASSERT_STREQ("a set", nix_get_typename(ctx, value)); + ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(ctx, value)); // Clean up - nix_gc_decref(nullptr, intValue); - nix_gc_decref(nullptr, stringValue); + nix_gc_decref(ctx, intValue); + nix_gc_decref(ctx, stringValue); free(out_name); } From 0be87c023f9758889dd2dbb045f6c64f24a92883 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Fri, 29 Mar 2024 16:31:48 +0000 Subject: [PATCH 144/168] Fix "include" directive in config files --- src/libutil/config.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libutil/config.cc b/src/libutil/config.cc index 617c2ec89..efde8591b 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -124,7 +124,7 @@ static void applyConfigInner(const std::string & contents, const std::string & p auto p = absPath(tokens[1], dirOf(path)); if (pathExists(p)) { try { - std::string includedContents = readFile(path); + std::string includedContents = readFile(p); applyConfigInner(includedContents, p, parsedContents); } catch (SystemError &) { // TODO: Do we actually want to ignore this? Or is it better to fail? From 555181c3fd6c6ee4417e093abf556a319e720d3c Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 29 Mar 2024 15:30:43 -0400 Subject: [PATCH 145/168] `throwExceptionSelfCheck` throw `Error` `SysError` is not appropriate because there is no (Unix) syscall involved. The catch block in `initLibUtil` is already for `Error` and still works. --- src/libutil/error.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libutil/error.cc b/src/libutil/error.cc index 036e19e26..fd4f4efd1 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -16,9 +16,10 @@ void BaseError::addTrace(std::shared_ptr && e, HintFmt hint, TracePrint pri err.traces.push_front(Trace { .pos = std::move(e), .hint = hint, .print = print }); } -void throwExceptionSelfCheck(){ +void throwExceptionSelfCheck() +{ // This is meant to be caught in initLibUtil() - throw SysError("C++ exception handling is broken. This would appear to be a problem with the way Nix was compiled and/or linked and/or loaded."); + throw Error("C++ exception handling is broken. This would appear to be a problem with the way Nix was compiled and/or linked and/or loaded."); } // c++ std::exception descendants must have a 'const char* what()' function. From 8be347afcac8006967921e4738215d98405f6d75 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 29 Mar 2024 15:21:10 -0400 Subject: [PATCH 146/168] Factor out `nix::maybeLstat` This function is nice for more than `PosixSourceAccessor`. We can make a few things simpler with it. Note that the error logic slightly changes in some of the call sites, in that we also count `ENOTDIR` and not just `ENOENT` as not having the file, but that should be fine. --- src/libstore/build/local-derivation-goal.cc | 24 ++++++++++----------- src/libstore/builtins/buildenv.cc | 18 +++++++--------- src/libutil/file-system.cc | 22 +++++++++++++------ src/libutil/file-system.hh | 1 + src/libutil/posix-source-accessor.cc | 8 +------ 5 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index d52ebf064..914fffd16 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -2053,13 +2053,13 @@ void LocalDerivationGoal::runChild() i.first, i.second.source); std::string path = i.first; - struct stat st; - if (lstat(path.c_str(), &st)) { - if (i.second.optional && errno == ENOENT) + auto optSt = maybeLstat(path.c_str()); + if (!optSt) { + if (i.second.optional) continue; - throw SysError("getting attributes of path '%s", path); + throw SysError("getting attributes of required path '%s", path); } - if (S_ISDIR(st.st_mode)) + if (S_ISDIR(optSt->st_mode)) sandboxProfile += fmt("\t(subpath \"%s\")\n", path); else sandboxProfile += fmt("\t(literal \"%s\")\n", path); @@ -2271,14 +2271,12 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs() continue; } - struct stat st; - if (lstat(actualPath.c_str(), &st) == -1) { - if (errno == ENOENT) - throw BuildError( - "builder for '%s' failed to produce output path for output '%s' at '%s'", - worker.store.printStorePath(drvPath), outputName, actualPath); - throw SysError("getting attributes of path '%s'", actualPath); - } + auto optSt = maybeLstat(actualPath.c_str()); + if (!optSt) + throw BuildError( + "builder for '%s' failed to produce output path for output '%s' at '%s'", + worker.store.printStorePath(drvPath), outputName, actualPath); + struct stat & st = *optSt; #ifndef __CYGWIN__ /* Check that the output is not group or world writable, as diff --git a/src/libstore/builtins/buildenv.cc b/src/libstore/builtins/buildenv.cc index 1ed7b39cc..31a6b32f1 100644 --- a/src/libstore/builtins/buildenv.cc +++ b/src/libstore/builtins/buildenv.cc @@ -64,9 +64,9 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir, continue; else if (S_ISDIR(srcSt.st_mode)) { - struct stat dstSt; - auto res = lstat(dstFile.c_str(), &dstSt); - if (res == 0) { + auto dstStOpt = maybeLstat(dstFile.c_str()); + if (dstStOpt) { + auto & dstSt = *dstStOpt; if (S_ISDIR(dstSt.st_mode)) { createLinks(state, srcFile, dstFile, priority); continue; @@ -82,14 +82,13 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir, createLinks(state, srcFile, dstFile, priority); continue; } - } else if (errno != ENOENT) - throw SysError("getting status of '%1%'", dstFile); + } } else { - struct stat dstSt; - auto res = lstat(dstFile.c_str(), &dstSt); - if (res == 0) { + auto dstStOpt = maybeLstat(dstFile.c_str()); + if (dstStOpt) { + auto & dstSt = *dstStOpt; if (S_ISLNK(dstSt.st_mode)) { auto prevPriority = state.priorities[dstFile]; if (prevPriority == priority) @@ -104,8 +103,7 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir, throw SysError("unlinking '%1%'", dstFile); } else if (S_ISDIR(dstSt.st_mode)) throw Error("collision between non-directory '%1%' and directory '%2%'", srcFile, dstFile); - } else if (errno != ENOENT) - throw SysError("getting status of '%1%'", dstFile); + } } createSymlink(srcFile, dstFile); diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index c0e268e9d..89d309731 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -174,15 +174,23 @@ struct stat lstat(const Path & path) } +std::optional maybeLstat(const Path & path) +{ + std::optional st{std::in_place}; + if (lstat(path.c_str(), &*st)) + { + if (errno == ENOENT || errno == ENOTDIR) + st.reset(); + else + throw SysError("getting status of '%s'", path); + } + return st; +} + + bool pathExists(const Path & path) { - int res; - struct stat st; - res = lstat(path.c_str(), &st); - if (!res) return true; - if (errno != ENOENT && errno != ENOTDIR) - throw SysError("getting status of %1%", path); - return false; + return maybeLstat(path).has_value(); } bool pathAccessible(const Path & path) diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index 9d565c881..dd071e1af 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -84,6 +84,7 @@ bool isDirOrInDir(std::string_view path, std::string_view dir); */ struct stat stat(const Path & path); struct stat lstat(const Path & path); +std::optional maybeLstat(const Path & path); /** * @return true iff the given path exists. diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index 41c2db59a..8039d4b80 100644 --- a/src/libutil/posix-source-accessor.cc +++ b/src/libutil/posix-source-accessor.cc @@ -97,13 +97,7 @@ std::optional PosixSourceAccessor::cachedLstat(const CanonPath & pa if (i != cache->end()) return i->second; } - std::optional st{std::in_place}; - if (::lstat(absPath.c_str(), &*st)) { - if (errno == ENOENT || errno == ENOTDIR) - st.reset(); - else - throw SysError("getting status of '%s'", showPath(path)); - } + auto st = nix::maybeLstat(absPath.c_str()); auto cache(_cache.lock()); if (cache->size() >= 16384) cache->clear(); From e4d9b207c20f2051be060a8fd32efe69dae78a80 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 29 Mar 2024 15:40:56 -0400 Subject: [PATCH 147/168] Factor out `isRootUser` function --- src/libstore/globals.cc | 3 +-- src/libstore/globals.hh | 3 ++- src/libstore/local-store.cc | 7 ++++--- src/libstore/lock.cc | 5 +++-- src/libstore/profiles.cc | 4 ++-- src/libstore/store-api.cc | 2 +- src/libutil/users.cc | 5 +++++ src/libutil/users.hh | 6 ++++++ src/nix-build/nix-build.cc | 6 ++++-- src/nix-env/nix-env.cc | 2 +- src/nix/main.cc | 2 +- 11 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index fa0938d7b..306e98e2d 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -2,7 +2,6 @@ #include "current-process.hh" #include "archive.hh" #include "args.hh" -#include "users.hh" #include "abstract-setting-to-json.hh" #include "compute-levels.hh" @@ -57,7 +56,7 @@ Settings::Settings() , nixManDir(canonPath(NIX_MAN_DIR)) , nixDaemonSocketFile(canonPath(getEnvNonEmpty("NIX_DAEMON_SOCKET_PATH").value_or(nixStateDir + DEFAULT_SOCKET_PATH))) { - buildUsersGroup = getuid() == 0 ? "nixbld" : ""; + buildUsersGroup = isRootUser() ? "nixbld" : ""; allowSymlinkedStore = getEnv("NIX_IGNORE_SYMLINK_STORE") == "1"; auto sslOverride = getEnv("NIX_SSL_CERT_FILE").value_or(getEnv("SSL_CERT_FILE").value_or("")); diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index e6acf0a4f..b8958898d 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -5,6 +5,7 @@ #include "config.hh" #include "environment-variables.hh" #include "experimental-features.hh" +#include "users.hh" #include #include @@ -665,7 +666,7 @@ public: Setting sandboxFallback{this, true, "sandbox-fallback", "Whether to disable sandboxing when the kernel doesn't allow it."}; - Setting requireDropSupplementaryGroups{this, getuid() == 0, "require-drop-supplementary-groups", + Setting requireDropSupplementaryGroups{this, isRootUser(), "require-drop-supplementary-groups", R"( Following the principle of least privilege, Nix will attempt to drop supplementary groups when building with sandboxing. diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 1bbeaa912..914f0cbe3 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -16,6 +16,7 @@ #include "posix-fs-canonicalise.hh" #include "posix-source-accessor.hh" #include "keys.hh" +#include "users.hh" #include #include @@ -223,7 +224,7 @@ LocalStore::LocalStore(const Params & params) /* Optionally, create directories and set permissions for a multi-user install. */ - if (getuid() == 0 && settings.buildUsersGroup != "") { + if (isRootUser() && settings.buildUsersGroup != "") { mode_t perm = 01775; struct group * gr = getgrnam(settings.buildUsersGroup.get().c_str()); @@ -573,7 +574,7 @@ void LocalStore::openDB(State & state, bool create) void LocalStore::makeStoreWritable() { #if __linux__ - if (getuid() != 0) return; + if (!isRootUser()) return; /* Check if /nix/store is on a read-only mount. */ struct statvfs stat; if (statvfs(realStoreDir.get().c_str(), &stat) != 0) @@ -1570,7 +1571,7 @@ static void makeMutable(const Path & path) /* Upgrade from schema 6 (Nix 0.15) to schema 7 (Nix >= 1.3). */ void LocalStore::upgradeStore7() { - if (getuid() != 0) return; + if (!isRootUser()) return; printInfo("removing immutable bits from the Nix store (this may take a while)..."); makeMutable(realStoreDir); } diff --git a/src/libstore/lock.cc b/src/libstore/lock.cc index 87f55ce49..023c74e34 100644 --- a/src/libstore/lock.cc +++ b/src/libstore/lock.cc @@ -2,6 +2,7 @@ #include "file-system.hh" #include "globals.hh" #include "pathlocks.hh" +#include "users.hh" #include #include @@ -192,10 +193,10 @@ std::unique_ptr acquireUserLock(uid_t nrIds, bool useUserNamespace) bool useBuildUsers() { #if __linux__ - static bool b = (settings.buildUsersGroup != "" || settings.autoAllocateUids) && getuid() == 0; + static bool b = (settings.buildUsersGroup != "" || settings.autoAllocateUids) && isRootUser(); return b; #elif __APPLE__ - static bool b = settings.buildUsersGroup != "" && getuid() == 0; + static bool b = settings.buildUsersGroup != "" && isRootUser(); return b; #else return false; diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index e8b88693d..73d3976f4 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -308,7 +308,7 @@ std::string optimisticLockProfile(const Path & profile) Path profilesDir() { auto profileRoot = - (getuid() == 0) + isRootUser() ? rootProfilesDir() : createNixStateDir() + "/profiles"; createDirs(profileRoot); @@ -332,7 +332,7 @@ Path getDefaultProfile() // Backwards compatibiliy measure: Make root's profile available as // `.../default` as it's what NixOS and most of the init scripts expect Path globalProfileLink = settings.nixStateDir + "/profiles/default"; - if (getuid() == 0 && !pathExists(globalProfileLink)) { + if (isRootUser() && !pathExists(globalProfileLink)) { replaceSymlink(profile, globalProfileLink); } return absPath(readLink(profileLink), dirOf(profileLink)); diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 4356296d4..62403e633 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -1307,7 +1307,7 @@ std::shared_ptr openFromNonUri(const std::string & uri, const Store::Para #if __linux__ else if (!pathExists(stateDir) && params.empty() - && getuid() != 0 + && !isRootUser() && !getEnv("NIX_STORE_DIR").has_value() && !getEnv("NIX_STATE_DIR").has_value()) { diff --git a/src/libutil/users.cc b/src/libutil/users.cc index 95a641322..8cb3db434 100644 --- a/src/libutil/users.cc +++ b/src/libutil/users.cc @@ -113,4 +113,9 @@ std::string expandTilde(std::string_view path) return std::string(path); } + +bool isRootUser() { + return getuid() == 0; +} + } diff --git a/src/libutil/users.hh b/src/libutil/users.hh index cecbb8bfb..449e5bbe9 100644 --- a/src/libutil/users.hh +++ b/src/libutil/users.hh @@ -55,4 +55,10 @@ Path createNixStateDir(); */ std::string expandTilde(std::string_view path); + +/** + * Is the current user UID 0 on Unix? + */ +bool isRootUser(); + } diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 35eef5b83..8276be8e8 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -24,6 +24,7 @@ #include "common-eval-args.hh" #include "attr-path.hh" #include "legacy.hh" +#include "users.hh" using namespace nix; using namespace std::string_literals; @@ -572,8 +573,9 @@ static void main_nix_build(int argc, char * * argv) "BASH=%5%; " "set +e; " R"s([ -n "$PS1" -a -z "$NIX_SHELL_PRESERVE_PROMPT" ] && )s" + - (getuid() == 0 ? R"s(PS1='\n\[\033[1;31m\][nix-shell:\w]\$\[\033[0m\] '; )s" - : R"s(PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '; )s") + + (isRootUser() + ? R"s(PS1='\n\[\033[1;31m\][nix-shell:\w]\$\[\033[0m\] '; )s" + : R"s(PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '; )s") + "if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; " "unset NIX_ENFORCE_PURITY; " "shopt -u nullglob; " diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 9288d89a1..177344044 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -1414,7 +1414,7 @@ static int main_nix_env(int argc, char * * argv) replaceSymlink( defaultChannelsDir(), nixExprPath + "/channels"); - if (getuid() != 0) + if (!isRootUser()) replaceSymlink( rootChannelsDir(), nixExprPath + "/channels_root"); diff --git a/src/nix/main.cc b/src/nix/main.cc index 22f9e7931..36256f3d0 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -348,7 +348,7 @@ void mainWrapped(int argc, char * * argv) initGC(); #if __linux__ - if (getuid() == 0) { + if (isRootUser()) { try { saveMountNamespace(); if (unshare(CLONE_NEWNS) == -1) From 6d9bafb3b8fa04a4056cb96ce3eb91feeaaec62f Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Sat, 30 Mar 2024 01:29:22 +0300 Subject: [PATCH 148/168] nfc(libutil): reformat files Run clang-format on compression.{cc,hh} and tarfile{cc,hh}. This way follow-up patches will be formatted properly and have easier to read diffs. --- src/libutil/compression.cc | 68 ++++++++++++++++++++++---------------- src/libutil/compression.hh | 5 +-- src/libutil/tarfile.cc | 36 ++++++++++---------- src/libutil/tarfile.hh | 3 +- 4 files changed, 63 insertions(+), 49 deletions(-) diff --git a/src/libutil/compression.cc b/src/libutil/compression.cc index d06f1f87b..d00303dce 100644 --- a/src/libutil/compression.cc +++ b/src/libutil/compression.cc @@ -12,8 +12,6 @@ #include #include -#include - namespace nix { static const int COMPRESSION_LEVEL_DEFAULT = -1; @@ -40,20 +38,24 @@ struct ArchiveDecompressionSource : Source { std::unique_ptr archive = 0; Source & src; - ArchiveDecompressionSource(Source & src) : src(src) {} + ArchiveDecompressionSource(Source & src) + : src(src) + { + } ~ArchiveDecompressionSource() override {} - size_t read(char * data, size_t len) override { + size_t read(char * data, size_t len) override + { struct archive_entry * ae; if (!archive) { archive = std::make_unique(src, true); - this->archive->check(archive_read_next_header(this->archive->archive, &ae), - "failed to read header (%s)"); + this->archive->check(archive_read_next_header(this->archive->archive, &ae), "failed to read header (%s)"); if (archive_filter_count(this->archive->archive) < 2) { throw CompressionError("input compression not recognized"); } } ssize_t result = archive_read_data(this->archive->archive, data, len); - if (result > 0) return result; + if (result > 0) + return result; if (result == 0) { throw EndOfFile("reached end of compressed file"); } @@ -67,16 +69,19 @@ struct ArchiveCompressionSink : CompressionSink Sink & nextSink; struct archive * archive; - ArchiveCompressionSink(Sink & nextSink, std::string format, bool parallel, int level = COMPRESSION_LEVEL_DEFAULT) : nextSink(nextSink) + ArchiveCompressionSink(Sink & nextSink, std::string format, bool parallel, int level = COMPRESSION_LEVEL_DEFAULT) + : nextSink(nextSink) { archive = archive_write_new(); - if (!archive) throw Error("failed to initialize libarchive"); + if (!archive) + throw Error("failed to initialize libarchive"); check(archive_write_add_filter_by_name(archive, format.c_str()), "couldn't initialize compression (%s)"); check(archive_write_set_format_raw(archive)); if (parallel) check(archive_write_set_filter_option(archive, format.c_str(), "threads", "0")); if (level != COMPRESSION_LEVEL_DEFAULT) - check(archive_write_set_filter_option(archive, format.c_str(), "compression-level", std::to_string(level).c_str())); + check(archive_write_set_filter_option( + archive, format.c_str(), "compression-level", std::to_string(level).c_str())); // disable internal buffering check(archive_write_set_bytes_per_block(archive, 0)); // disable output padding @@ -86,7 +91,8 @@ struct ArchiveCompressionSink : CompressionSink ~ArchiveCompressionSink() override { - if (archive) archive_write_free(archive); + if (archive) + archive_write_free(archive); } void finish() override @@ -106,7 +112,8 @@ struct ArchiveCompressionSink : CompressionSink void writeUnbuffered(std::string_view data) override { ssize_t result = archive_write_data(archive, data.data(), data.length()); - if (result <= 0) check(result); + if (result <= 0) + check(result); } private: @@ -130,13 +137,20 @@ private: struct NoneSink : CompressionSink { Sink & nextSink; - NoneSink(Sink & nextSink, int level = COMPRESSION_LEVEL_DEFAULT) : nextSink(nextSink) + NoneSink(Sink & nextSink, int level = COMPRESSION_LEVEL_DEFAULT) + : nextSink(nextSink) { if (level != COMPRESSION_LEVEL_DEFAULT) warn("requested compression level '%d' not supported by compression method 'none'", level); } - void finish() override { flush(); } - void writeUnbuffered(std::string_view data) override { nextSink(data); } + void finish() override + { + flush(); + } + void writeUnbuffered(std::string_view data) override + { + nextSink(data); + } }; struct BrotliDecompressionSink : ChunkedCompressionSink @@ -145,7 +159,8 @@ struct BrotliDecompressionSink : ChunkedCompressionSink BrotliDecoderState * state; bool finished = false; - BrotliDecompressionSink(Sink & nextSink) : nextSink(nextSink) + BrotliDecompressionSink(Sink & nextSink) + : nextSink(nextSink) { state = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr); if (!state) @@ -173,10 +188,7 @@ struct BrotliDecompressionSink : ChunkedCompressionSink while (!finished && (!data.data() || avail_in)) { checkInterrupt(); - if (!BrotliDecoderDecompressStream(state, - &avail_in, &next_in, - &avail_out, &next_out, - nullptr)) + if (!BrotliDecoderDecompressStream(state, &avail_in, &next_in, &avail_out, &next_out, nullptr)) throw CompressionError("error while decompressing brotli file"); if (avail_out < sizeof(outbuf) || avail_in == 0) { @@ -219,7 +231,8 @@ struct BrotliCompressionSink : ChunkedCompressionSink BrotliEncoderState * state; bool finished = false; - BrotliCompressionSink(Sink & nextSink) : nextSink(nextSink) + BrotliCompressionSink(Sink & nextSink) + : nextSink(nextSink) { state = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); if (!state) @@ -247,11 +260,9 @@ struct BrotliCompressionSink : ChunkedCompressionSink while (!finished && (!data.data() || avail_in)) { checkInterrupt(); - if (!BrotliEncoderCompressStream(state, - data.data() ? BROTLI_OPERATION_PROCESS : BROTLI_OPERATION_FINISH, - &avail_in, &next_in, - &avail_out, &next_out, - nullptr)) + if (!BrotliEncoderCompressStream( + state, data.data() ? BROTLI_OPERATION_PROCESS : BROTLI_OPERATION_FINISH, &avail_in, &next_in, + &avail_out, &next_out, nullptr)) throw CompressionError("error while compressing brotli compression"); if (avail_out < sizeof(outbuf) || avail_in == 0) { @@ -267,9 +278,8 @@ struct BrotliCompressionSink : ChunkedCompressionSink ref makeCompressionSink(const std::string & method, Sink & nextSink, const bool parallel, int level) { - std::vector la_supports = { - "bzip2", "compress", "grzip", "gzip", "lrzip", "lz4", "lzip", "lzma", "lzop", "xz", "zstd" - }; + std::vector la_supports = {"bzip2", "compress", "grzip", "gzip", "lrzip", "lz4", + "lzip", "lzma", "lzop", "xz", "zstd"}; if (std::find(la_supports.begin(), la_supports.end(), method) != la_supports.end()) { return make_ref(nextSink, method, parallel, level); } diff --git a/src/libutil/compression.hh b/src/libutil/compression.hh index 4e53a7b3c..e0c531b1f 100644 --- a/src/libutil/compression.hh +++ b/src/libutil/compression.hh @@ -11,7 +11,7 @@ namespace nix { struct CompressionSink : BufferedSink, FinishSink { - using BufferedSink::operator (); + using BufferedSink::operator(); using BufferedSink::writeUnbuffered; using FinishSink::finish; }; @@ -22,7 +22,8 @@ std::unique_ptr makeDecompressionSink(const std::string & method, Si std::string compress(const std::string & method, std::string_view in, const bool parallel = false, int level = -1); -ref makeCompressionSink(const std::string & method, Sink & nextSink, const bool parallel = false, int level = -1); +ref +makeCompressionSink(const std::string & method, Sink & nextSink, const bool parallel = false, int level = -1); MakeError(UnknownCompressionMethod, Error); diff --git a/src/libutil/tarfile.cc b/src/libutil/tarfile.cc index 3bb6694f8..4a11195c4 100644 --- a/src/libutil/tarfile.cc +++ b/src/libutil/tarfile.cc @@ -12,7 +12,7 @@ static int callback_open(struct archive *, void * self) return ARCHIVE_OK; } -static ssize_t callback_read(struct archive * archive, void * _self, const void * * buffer) +static ssize_t callback_read(struct archive * archive, void * _self, const void ** buffer) { auto self = (TarArchive *) _self; *buffer = self->buffer.data(); @@ -40,7 +40,8 @@ void TarArchive::check(int err, const std::string & reason) throw Error(reason, archive_error_string(this->archive)); } -TarArchive::TarArchive(Source & source, bool raw) : buffer(65536) +TarArchive::TarArchive(Source & source, bool raw) + : buffer(65536) { this->archive = archive_read_new(); this->source = &source; @@ -54,10 +55,11 @@ TarArchive::TarArchive(Source & source, bool raw) : buffer(65536) archive_read_support_format_empty(archive); } archive_read_set_option(archive, NULL, "mac-ext", NULL); - check(archive_read_open(archive, (void *)this, callback_open, callback_read, callback_close), "Failed to open archive (%s)"); + check( + archive_read_open(archive, (void *) this, callback_open, callback_read, callback_close), + "Failed to open archive (%s)"); } - TarArchive::TarArchive(const Path & path) { this->archive = archive_read_new(); @@ -75,19 +77,19 @@ void TarArchive::close() TarArchive::~TarArchive() { - if (this->archive) archive_read_free(this->archive); + if (this->archive) + archive_read_free(this->archive); } static void extract_archive(TarArchive & archive, const Path & destDir) { - int flags = ARCHIVE_EXTRACT_TIME - | ARCHIVE_EXTRACT_SECURE_SYMLINKS - | ARCHIVE_EXTRACT_SECURE_NODOTDOT; + int flags = ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_SECURE_SYMLINKS | ARCHIVE_EXTRACT_SECURE_NODOTDOT; for (;;) { struct archive_entry * entry; int r = archive_read_next_header(archive.archive, &entry); - if (r == ARCHIVE_EOF) break; + if (r == ARCHIVE_EOF) + break; auto name = archive_entry_pathname(entry); if (!name) throw Error("cannot get archive member name: %s", archive_error_string(archive.archive)); @@ -96,18 +98,16 @@ static void extract_archive(TarArchive & archive, const Path & destDir) else archive.check(r); - archive_entry_copy_pathname(entry, - (destDir + "/" + name).c_str()); + archive_entry_copy_pathname(entry, (destDir + "/" + name).c_str()); // sources can and do contain dirs with no rx bits if (archive_entry_filetype(entry) == AE_IFDIR && (archive_entry_mode(entry) & 0500) != 0500) archive_entry_set_mode(entry, archive_entry_mode(entry) | 0500); // Patch hardlink path - const char *original_hardlink = archive_entry_hardlink(entry); + const char * original_hardlink = archive_entry_hardlink(entry); if (original_hardlink) { - archive_entry_copy_hardlink(entry, - (destDir + "/" + original_hardlink).c_str()); + archive_entry_copy_hardlink(entry, (destDir + "/" + original_hardlink).c_str()); } archive.check(archive_read_extract(archive.archive, entry, flags)); @@ -140,7 +140,8 @@ time_t unpackTarfileToSink(TarArchive & archive, FileSystemObjectSink & parseSin // FIXME: merge with extract_archive struct archive_entry * entry; int r = archive_read_next_header(archive.archive, &entry); - if (r == ARCHIVE_EOF) break; + if (r == ARCHIVE_EOF) + break; auto path = archive_entry_pathname(entry); if (!path) throw Error("cannot get archive member name: %s", archive_error_string(archive.archive)); @@ -167,8 +168,9 @@ time_t unpackTarfileToSink(TarArchive & archive, FileSystemObjectSink & parseSin auto n = archive_read_data(archive.archive, buf.data(), buf.size()); if (n < 0) throw Error("cannot read file '%s' from tarball", path); - if (n == 0) break; - crf(std::string_view { + if (n == 0) + break; + crf(std::string_view{ (const char *) buf.data(), (size_t) n, }); diff --git a/src/libutil/tarfile.hh b/src/libutil/tarfile.hh index 6a9c42149..200bb8f8f 100644 --- a/src/libutil/tarfile.hh +++ b/src/libutil/tarfile.hh @@ -7,7 +7,8 @@ namespace nix { -struct TarArchive { +struct TarArchive +{ struct archive * archive; Source * source; std::vector buffer; From 500683a94903bf2472c5306fe42a02ea76f621dd Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Sat, 30 Mar 2024 01:29:29 +0300 Subject: [PATCH 149/168] fix(libutil): apply only the specified filter to decompress archive This patch makes `makeDecompressionSink` strip only a single layer of compression specified via method. This fixes erroneous decompression of doubly-compressed NARs fetched with curl. --- src/libutil/compression.cc | 10 ++++--- src/libutil/tarfile.cc | 57 +++++++++++++++++++++++++++++--------- src/libutil/tarfile.hh | 16 +++++++++-- 3 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/libutil/compression.cc b/src/libutil/compression.cc index d00303dce..d17401f27 100644 --- a/src/libutil/compression.cc +++ b/src/libutil/compression.cc @@ -38,8 +38,10 @@ struct ArchiveDecompressionSource : Source { std::unique_ptr archive = 0; Source & src; - ArchiveDecompressionSource(Source & src) + std::optional compressionMethod; + ArchiveDecompressionSource(Source & src, std::optional compressionMethod = std::nullopt) : src(src) + , compressionMethod(std::move(compressionMethod)) { } ~ArchiveDecompressionSource() override {} @@ -47,7 +49,7 @@ struct ArchiveDecompressionSource : Source { struct archive_entry * ae; if (!archive) { - archive = std::make_unique(src, true); + archive = std::make_unique(src, /*raw*/ true, compressionMethod); this->archive->check(archive_read_next_header(this->archive->archive, &ae), "failed to read header (%s)"); if (archive_filter_count(this->archive->archive) < 2) { throw CompressionError("input compression not recognized"); @@ -218,8 +220,8 @@ std::unique_ptr makeDecompressionSink(const std::string & method, Si else if (method == "br") return std::make_unique(nextSink); else - return sourceToSink([&](Source & source) { - auto decompressionSource = std::make_unique(source); + return sourceToSink([method, &nextSink](Source & source) { + auto decompressionSource = std::make_unique(source, method); decompressionSource->drainInto(nextSink); }); } diff --git a/src/libutil/tarfile.cc b/src/libutil/tarfile.cc index 4a11195c4..6bb2bd2f3 100644 --- a/src/libutil/tarfile.cc +++ b/src/libutil/tarfile.cc @@ -1,18 +1,21 @@ #include #include +#include "finally.hh" #include "serialise.hh" #include "tarfile.hh" #include "file-system.hh" namespace nix { -static int callback_open(struct archive *, void * self) +namespace { + +int callback_open(struct archive *, void * self) { return ARCHIVE_OK; } -static ssize_t callback_read(struct archive * archive, void * _self, const void ** buffer) +ssize_t callback_read(struct archive * archive, void * _self, const void ** buffer) { auto self = (TarArchive *) _self; *buffer = self->buffer.data(); @@ -27,33 +30,61 @@ static ssize_t callback_read(struct archive * archive, void * _self, const void } } -static int callback_close(struct archive *, void * self) +int callback_close(struct archive *, void * self) { return ARCHIVE_OK; } -void TarArchive::check(int err, const std::string & reason) +void checkLibArchive(archive * archive, int err, const std::string & reason) { if (err == ARCHIVE_EOF) throw EndOfFile("reached end of archive"); else if (err != ARCHIVE_OK) - throw Error(reason, archive_error_string(this->archive)); + throw Error(reason, archive_error_string(archive)); } -TarArchive::TarArchive(Source & source, bool raw) - : buffer(65536) +constexpr auto defaultBufferSize = std::size_t{65536}; +} + +void TarArchive::check(int err, const std::string & reason) { - this->archive = archive_read_new(); - this->source = &source; + checkLibArchive(archive, err, reason); +} + +/// @brief Get filter_code from its name. +/// +/// libarchive does not provide a convenience function like archive_write_add_filter_by_name but for reading. +/// Instead it's necessary to use this kludge to convert method -> code and +/// then use archive_read_support_filter_by_code. Arguably this is better than +/// hand-rolling the equivalent function that is better implemented in libarchive. +int getArchiveFilterCodeByName(const std::string & method) +{ + auto * ar = archive_write_new(); + auto cleanup = Finally{[&ar]() { checkLibArchive(ar, archive_write_close(ar), "failed to close archive: %s"); }}; + auto err = archive_write_add_filter_by_name(ar, method.c_str()); + checkLibArchive(ar, err, "failed to get libarchive filter by name: %s"); + auto code = archive_filter_code(ar, 0); + return code; +} + +TarArchive::TarArchive(Source & source, bool raw, std::optional compression_method) + : archive{archive_read_new()} + , source{&source} + , buffer(defaultBufferSize) +{ + if (!compression_method) { + archive_read_support_filter_all(archive); + } else { + archive_read_support_filter_by_code(archive, getArchiveFilterCodeByName(*compression_method)); + } if (!raw) { - archive_read_support_filter_all(archive); archive_read_support_format_all(archive); } else { - archive_read_support_filter_all(archive); archive_read_support_format_raw(archive); archive_read_support_format_empty(archive); } + archive_read_set_option(archive, NULL, "mac-ext", NULL); check( archive_read_open(archive, (void *) this, callback_open, callback_read, callback_close), @@ -61,9 +92,9 @@ TarArchive::TarArchive(Source & source, bool raw) } TarArchive::TarArchive(const Path & path) + : archive{archive_read_new()} + , buffer(defaultBufferSize) { - this->archive = archive_read_new(); - archive_read_support_filter_all(archive); archive_read_support_format_all(archive); archive_read_set_option(archive, NULL, "mac-ext", NULL); diff --git a/src/libutil/tarfile.hh b/src/libutil/tarfile.hh index 200bb8f8f..705d211e4 100644 --- a/src/libutil/tarfile.hh +++ b/src/libutil/tarfile.hh @@ -15,18 +15,28 @@ struct TarArchive void check(int err, const std::string & reason = "failed to extract archive (%s)"); - TarArchive(Source & source, bool raw = false); + explicit TarArchive(const Path & path); - TarArchive(const Path & path); + /// @brief Create a generic archive from source. + /// @param source - Input byte stream. + /// @param raw - Whether to enable raw file support. For more info look in docs: + /// https://manpages.debian.org/stretch/libarchive-dev/archive_read_format.3.en.html + /// @param compression_method - Primary compression method to use. std::nullopt means 'all'. + TarArchive(Source & source, bool raw = false, std::optional compression_method = std::nullopt); - /// disable copy constructor + /// Disable copy constructor. Explicitly default move assignment/constructor. TarArchive(const TarArchive &) = delete; + TarArchive & operator=(const TarArchive &) = delete; + TarArchive(TarArchive &&) = default; + TarArchive & operator=(TarArchive &&) = default; void close(); ~TarArchive(); }; +int getArchiveFilterCodeByName(const std::string & method); + void unpackTarfile(Source & source, const Path & destDir); void unpackTarfile(const Path & tarFile, const Path & destDir); From a66b5a1526dd1e1cd1a56c6cd1b6c9bc20b6d8b4 Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Sat, 30 Mar 2024 01:29:34 +0300 Subject: [PATCH 150/168] test(nixos): add integration test for doubly-compressed content Add an integration test with a compressing proxy (nginx is used). This test verifies that fetched archives do not get decompressed excessively. --- tests/nixos/default.nix | 2 + tests/nixos/gzip-content-encoding.nix | 71 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tests/nixos/gzip-content-encoding.nix diff --git a/tests/nixos/default.nix b/tests/nixos/default.nix index 98de31e13..627728424 100644 --- a/tests/nixos/default.nix +++ b/tests/nixos/default.nix @@ -158,4 +158,6 @@ in fetch-git = runNixOSTestFor "x86_64-linux" ./fetch-git; ca-fd-leak = runNixOSTestFor "x86_64-linux" ./ca-fd-leak; + + gzip-content-encoding = runNixOSTestFor "x86_64-linux" ./gzip-content-encoding.nix; } diff --git a/tests/nixos/gzip-content-encoding.nix b/tests/nixos/gzip-content-encoding.nix new file mode 100644 index 000000000..a5a0033fd --- /dev/null +++ b/tests/nixos/gzip-content-encoding.nix @@ -0,0 +1,71 @@ +# Test that compressed files fetched from server with compressed responses +# do not get excessively decompressed. +# E.g. fetching a zstd compressed tarball from a server, +# which compresses the response with `Content-Encoding: gzip`. +# The expected result is that the fetched file is a zstd archive. + +{ lib, config, ... }: + +let + pkgs = config.nodes.machine.nixpkgs.pkgs; + + ztdCompressedFile = pkgs.stdenv.mkDerivation { + name = "dummy-zstd-compressed-archive"; + dontUnpack = true; + nativeBuildInputs = with pkgs; [ zstd ]; + buildPhase = '' + mkdir archive + for _ in {1..100}; do echo "lorem" > archive/file1; done + for _ in {1..100}; do echo "ipsum" > archive/file2; done + tar --zstd -cf archive.tar.zst archive + ''; + installPhase = '' + install -Dm 644 -T archive.tar.zst $out/share/archive + ''; + }; + + fileCmd = "${pkgs.file}/bin/file"; +in + +{ + name = "gzip-content-encoding"; + + nodes = + { machine = + { config, pkgs, ... }: + { networking.firewall.allowedTCPPorts = [ 80 ]; + + services.nginx.enable = true; + services.nginx.virtualHosts."localhost" = + { root = "${ztdCompressedFile}/share/"; + # Make sure that nginx really tries to compress the + # file on the fly with no regard to size/mime. + # http://nginx.org/en/docs/http/ngx_http_gzip_module.html + extraConfig = '' + gzip on; + gzip_types *; + gzip_proxied any; + gzip_min_length 0; + ''; + }; + virtualisation.writableStore = true; + virtualisation.additionalPaths = with pkgs; [ file ]; + nix.settings.substituters = lib.mkForce [ ]; + }; + }; + + # Check that when nix-prefetch-url is used with a zst tarball it does not get decompressed. + testScript = { nodes }: '' + # fmt: off + start_all() + + machine.wait_for_unit("nginx.service") + machine.succeed(""" + # Make sure that the file is properly compressed as the test would be meaningless otherwise + curl --compressed -v http://localhost/archive |& tr -s ' ' |& grep --ignore-case 'content-encoding: gzip' + archive_path=$(nix-prefetch-url http://localhost/archive --print-path | tail -n1) + [[ $(${fileCmd} --brief --mime-type $archive_path) == "application/zstd" ]] + tar --zstd -xf $archive_path + """) + ''; +} From 3752bbef28899bc05e2e144fae5dcf37d99f86b5 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sat, 30 Mar 2024 10:39:25 -0400 Subject: [PATCH 151/168] Document `maybeLstat` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com> --- src/libutil/file-system.hh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index dd071e1af..06a993829 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -84,6 +84,10 @@ bool isDirOrInDir(std::string_view path, std::string_view dir); */ struct stat stat(const Path & path); struct stat lstat(const Path & path); +/** + * `lstat` the given path if it exists. + * @return std::nullopt if the path doesn't exist, or an optional containing the result of `lstat` otherwise + */ std::optional maybeLstat(const Path & path); /** From 39c554aad4bb76ff31b8a3072d6b8882297d0d9f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 1 Apr 2024 22:32:29 +0000 Subject: [PATCH 152/168] build(deps): bump zeebe-io/backport-action from 2.4.1 to 2.5.0 Bumps [zeebe-io/backport-action](https://github.com/zeebe-io/backport-action) from 2.4.1 to 2.5.0. - [Release notes](https://github.com/zeebe-io/backport-action/releases) - [Commits](https://github.com/zeebe-io/backport-action/compare/v2.4.1...v2.5.0) --- updated-dependencies: - dependency-name: zeebe-io/backport-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/backport.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml index 5b75704b5..8f83b913c 100644 --- a/.github/workflows/backport.yml +++ b/.github/workflows/backport.yml @@ -21,7 +21,7 @@ jobs: fetch-depth: 0 - name: Create backport PRs # should be kept in sync with `version` - uses: zeebe-io/backport-action@v2.4.1 + uses: zeebe-io/backport-action@v2.5.0 with: # Config README: https://github.com/zeebe-io/backport-action#backport-action github_token: ${{ secrets.GITHUB_TOKEN }} From 29239a282964a946ca79d825bfea781e2cb7cd02 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 1 Apr 2024 22:32:31 +0000 Subject: [PATCH 153/168] build(deps): bump actions/checkout from 3 to 4 Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v3...v4) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 818e81ec8..2b8eac49d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -163,7 +163,7 @@ jobs: vm_tests: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: DeterminateSystems/nix-installer-action@main - uses: DeterminateSystems/magic-nix-cache-action@main - run: nix build -L .#hydraJobs.tests.githubFlakes .#hydraJobs.tests.tarballFlakes From 852391765dd132c74ce3dc63b8ee07c6112b097c Mon Sep 17 00:00:00 2001 From: John Ericson Date: Wed, 27 Mar 2024 12:01:07 -0400 Subject: [PATCH 154/168] Add unix (and linux) dirs In the Nix commit, platform-specific sources will go here. --- local.mk | 5 +++++ src/libcmd/local.mk | 2 +- src/libexpr/local.mk | 2 +- src/libfetchers/local.mk | 6 ++++++ src/libmain/local.mk | 7 ++++++- src/libstore/local.mk | 6 ++++++ src/libutil/local.mk | 12 ++++++++++++ src/nix/local.mk | 14 ++++++++++++-- src/resolve-system-dependencies/local.mk | 2 +- 9 files changed, 50 insertions(+), 6 deletions(-) diff --git a/local.mk b/local.mk index 7e3c77a65..408a82327 100644 --- a/local.mk +++ b/local.mk @@ -5,6 +5,11 @@ ERROR_SWITCH_ENUM = -Werror=switch-enum $(foreach i, config.h $(wildcard src/lib*/*.hh), \ $(eval $(call install-file-in, $(i), $(includedir)/nix, 0644))) +ifdef HOST_UNIX + $(foreach i, $(wildcard src/lib*/unix/*.hh), \ + $(eval $(call install-file-in, $(i), $(includedir)/nix, 0644))) +endif + $(GCH): src/libutil/util.hh config.h GCH_CXXFLAGS = $(INCLUDE_libutil) diff --git a/src/libcmd/local.mk b/src/libcmd/local.mk index 7a7c46ee2..9aa33a9d3 100644 --- a/src/libcmd/local.mk +++ b/src/libcmd/local.mk @@ -6,7 +6,7 @@ libcmd_DIR := $(d) libcmd_SOURCES := $(wildcard $(d)/*.cc) -libcmd_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) $(INCLUDE_libexpr) -I src/libmain +libcmd_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) $(INCLUDE_libexpr) $(INCLUDE_libmain) libcmd_LDFLAGS = $(EDITLINE_LIBS) $(LOWDOWN_LIBS) $(THREAD_LDFLAGS) diff --git a/src/libexpr/local.mk b/src/libexpr/local.mk index 17f793ec3..ecadc5e5d 100644 --- a/src/libexpr/local.mk +++ b/src/libexpr/local.mk @@ -15,7 +15,7 @@ libexpr_SOURCES := \ INCLUDE_libexpr := -I $(d) -libexpr_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) -I src/libmain $(INCLUDE_libexpr) +libexpr_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) $(INCLUDE_libmain) $(INCLUDE_libexpr) libexpr_LIBS = libutil libstore libfetchers diff --git a/src/libfetchers/local.mk b/src/libfetchers/local.mk index e229a0993..0fef1466b 100644 --- a/src/libfetchers/local.mk +++ b/src/libfetchers/local.mk @@ -5,10 +5,16 @@ libfetchers_NAME = libnixfetchers libfetchers_DIR := $(d) libfetchers_SOURCES := $(wildcard $(d)/*.cc) +ifdef HOST_UNIX + libfetchers_SOURCES += $(wildcard $(d)/unix/*.cc) +endif # Not just for this library itself, but also for downstream libraries using this library INCLUDE_libfetchers := -I $(d) +ifdef HOST_UNIX + INCLUDE_libfetchers += -I $(d)/unix +endif libfetchers_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) diff --git a/src/libmain/local.mk b/src/libmain/local.mk index fde28a820..d41c49dd7 100644 --- a/src/libmain/local.mk +++ b/src/libmain/local.mk @@ -5,8 +5,13 @@ libmain_NAME = libnixmain libmain_DIR := $(d) libmain_SOURCES := $(wildcard $(d)/*.cc) +ifdef HOST_UNIX + libmain_SOURCES += $(wildcard $(d)/unix/*.cc) +endif -libmain_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) +INCLUDE_libmain := -I $(d) + +libmain_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libmain) libmain_LDFLAGS += $(OPENSSL_LIBS) diff --git a/src/libstore/local.mk b/src/libstore/local.mk index 7eaa317e5..ccb7aeee2 100644 --- a/src/libstore/local.mk +++ b/src/libstore/local.mk @@ -5,6 +5,9 @@ libstore_NAME = libnixstore libstore_DIR := $(d) libstore_SOURCES := $(wildcard $(d)/*.cc $(d)/builtins/*.cc $(d)/build/*.cc) +ifdef HOST_UNIX + libstore_SOURCES += $(wildcard $(d)/unix/*.cc) +endif libstore_LIBS = libutil @@ -30,6 +33,9 @@ endif # Not just for this library itself, but also for downstream libraries using this library INCLUDE_libstore := -I $(d) -I $(d)/build +ifdef HOST_UNIX + INCLUDE_libstore += -I $(d)/unix +endif libstore_CXXFLAGS += \ $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libstore) \ diff --git a/src/libutil/local.mk b/src/libutil/local.mk index 4ffb6bd9d..9773ef64f 100644 --- a/src/libutil/local.mk +++ b/src/libutil/local.mk @@ -5,10 +5,22 @@ libutil_NAME = libnixutil libutil_DIR := $(d) libutil_SOURCES := $(wildcard $(d)/*.cc $(d)/signature/*.cc) +ifdef HOST_UNIX + libutil_SOURCES += $(wildcard $(d)/unix/*.cc) +endif +ifdef HOST_LINUX + libutil_SOURCES += $(wildcard $(d)/linux/*.cc) +endif # Not just for this library itself, but also for downstream libraries using this library INCLUDE_libutil := -I $(d) +ifdef HOST_UNIX + INCLUDE_libutil += -I $(d)/unix +endif +ifdef HOST_LINUX + INCLUDE_libutil += -I $(d)/linux +endif libutil_CXXFLAGS += $(INCLUDE_libutil) libutil_LDFLAGS += $(THREAD_LDFLAGS) $(LIBCURL_LIBS) $(SODIUM_LIBS) $(OPENSSL_LIBS) $(LIBBROTLI_LIBS) $(LIBARCHIVE_LIBS) $(BOOST_LDFLAGS) -lboost_context diff --git a/src/nix/local.mk b/src/nix/local.mk index 55544b564..9f6f31b3a 100644 --- a/src/nix/local.mk +++ b/src/nix/local.mk @@ -12,9 +12,19 @@ nix_SOURCES := \ $(wildcard src/nix-daemon/*.cc) \ $(wildcard src/nix-env/*.cc) \ $(wildcard src/nix-instantiate/*.cc) \ - $(wildcard src/nix-store/*.cc) \ + $(wildcard src/nix-store/*.cc) -nix_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) $(INCLUDE_libexpr) -I src/libmain -I src/libcmd -I doc/manual +ifdef HOST_UNIX +nix_SOURCES += \ + $(wildcard $(d)/unix/*.cc) +endif + +INCLUDE_nix := -I $(d) +ifdef HOST_UNIX + INCLUDE_nix += -I $(d)/unix +endif + +nix_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libfetchers) $(INCLUDE_libexpr) $(INCLUDE_libmain) -I src/libcmd -I doc/manual $(INCLUDE_nix) nix_LIBS = libexpr libmain libfetchers libstore libutil libcmd diff --git a/src/resolve-system-dependencies/local.mk b/src/resolve-system-dependencies/local.mk index f28fdab3b..e138c4080 100644 --- a/src/resolve-system-dependencies/local.mk +++ b/src/resolve-system-dependencies/local.mk @@ -6,7 +6,7 @@ resolve-system-dependencies_DIR := $(d) resolve-system-dependencies_INSTALL_DIR := $(libexecdir)/nix -resolve-system-dependencies_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) -I src/libmain +resolve-system-dependencies_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libmain) resolve-system-dependencies_LIBS := libstore libmain libutil From 02fa20622f34e67bc551f4eb5b7c4d387641b9a6 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 29 Mar 2024 15:59:14 -0400 Subject: [PATCH 155/168] Start factoring out Unix assumptions This splits files and adds new identifiers in preperation for supporting windows, but no Windows-specific code is actually added yet. Co-authored-by: Robert Hensing --- src/libfetchers/{ => unix}/git.cc | 0 src/libfetchers/{ => unix}/mercurial.cc | 0 src/libstore/build/local-derivation-goal.cc | 26 +-- src/libstore/filetransfer.cc | 7 +- src/libutil/current-process.cc | 4 +- src/libutil/environment-variables.cc | 14 -- src/libutil/file-descriptor.cc | 186 +++---------------- src/libutil/file-descriptor.hh | 58 ++++-- src/libutil/{ => linux}/namespaces.cc | 20 +- src/libutil/{ => linux}/namespaces.hh | 4 - src/libutil/logging.cc | 5 +- src/libutil/serialise.hh | 16 +- src/libutil/unix/environment-variables.cc | 21 +++ src/libutil/unix/file-descriptor.cc | 155 ++++++++++++++++ src/libutil/{ => unix}/monitor-fd.hh | 0 src/libutil/{ => unix}/processes.cc | 0 src/libutil/{ => unix}/processes.hh | 0 src/libutil/{ => unix}/signals.cc | 0 src/libutil/{ => unix}/signals.hh | 0 src/libutil/{ => unix}/unix-domain-socket.cc | 0 src/libutil/{ => unix}/unix-domain-socket.hh | 0 src/libutil/unix/users.cc | 66 +++++++ src/libutil/users.cc | 60 ------ src/libutil/util.cc | 1 - src/nix/main.cc | 5 +- src/nix/{ => unix}/daemon.cc | 0 src/nix/{ => unix}/daemon.md | 0 src/nix/{ => unix}/fmt.cc | 0 src/nix/{ => unix}/fmt.md | 0 src/nix/{ => unix}/run.cc | 0 src/nix/{ => unix}/run.hh | 0 src/nix/{ => unix}/run.md | 0 src/nix/{ => unix}/upgrade-nix.cc | 0 src/nix/{ => unix}/upgrade-nix.md | 0 34 files changed, 352 insertions(+), 296 deletions(-) rename src/libfetchers/{ => unix}/git.cc (100%) rename src/libfetchers/{ => unix}/mercurial.cc (100%) rename src/libutil/{ => linux}/namespaces.cc (95%) rename src/libutil/{ => linux}/namespaces.hh (96%) create mode 100644 src/libutil/unix/environment-variables.cc create mode 100644 src/libutil/unix/file-descriptor.cc rename src/libutil/{ => unix}/monitor-fd.hh (100%) rename src/libutil/{ => unix}/processes.cc (100%) rename src/libutil/{ => unix}/processes.hh (100%) rename src/libutil/{ => unix}/signals.cc (100%) rename src/libutil/{ => unix}/signals.hh (100%) rename src/libutil/{ => unix}/unix-domain-socket.cc (100%) rename src/libutil/{ => unix}/unix-domain-socket.hh (100%) create mode 100644 src/libutil/unix/users.cc rename src/nix/{ => unix}/daemon.cc (100%) rename src/nix/{ => unix}/daemon.md (100%) rename src/nix/{ => unix}/fmt.cc (100%) rename src/nix/{ => unix}/fmt.md (100%) rename src/nix/{ => unix}/run.cc (100%) rename src/nix/{ => unix}/run.hh (100%) rename src/nix/{ => unix}/run.md (100%) rename src/nix/{ => unix}/upgrade-nix.cc (100%) rename src/nix/{ => unix}/upgrade-nix.md (100%) diff --git a/src/libfetchers/git.cc b/src/libfetchers/unix/git.cc similarity index 100% rename from src/libfetchers/git.cc rename to src/libfetchers/unix/git.cc diff --git a/src/libfetchers/mercurial.cc b/src/libfetchers/unix/mercurial.cc similarity index 100% rename from src/libfetchers/mercurial.cc rename to src/libfetchers/unix/mercurial.cc diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index 914fffd16..f8794e783 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -17,7 +17,6 @@ #include "cgroup.hh" #include "personality.hh" #include "current-process.hh" -#include "namespaces.hh" #include "child.hh" #include "unix-domain-socket.hh" #include "posix-fs-canonicalise.hh" @@ -40,18 +39,19 @@ /* Includes required for chroot support. */ #if __linux__ -#include -#include -#include -#include -#include -#include -#include -#include -#if HAVE_SECCOMP -#include -#endif -#define pivot_root(new_root, put_old) (syscall(SYS_pivot_root, new_root, put_old)) +# include +# include +# include +# include +# include +# include +# include +# include +# include "namespaces.hh" +# if HAVE_SECCOMP +# include +# endif +# define pivot_root(new_root, put_old) (syscall(SYS_pivot_root, new_root, put_old)) #endif #if __APPLE__ diff --git a/src/libstore/filetransfer.cc b/src/libstore/filetransfer.cc index bab21bf51..0d5379d25 100644 --- a/src/libstore/filetransfer.cc +++ b/src/libstore/filetransfer.cc @@ -1,5 +1,4 @@ #include "filetransfer.hh" -#include "namespaces.hh" #include "globals.hh" #include "store-api.hh" #include "s3.hh" @@ -12,6 +11,10 @@ #include #endif +#if __linux__ +# include "namespaces.hh" +#endif + #include #include @@ -568,7 +571,9 @@ struct curlFileTransfer : public FileTransfer stopWorkerThread(); }); + #if __linux__ unshareFilesystem(); + #endif std::map> items; diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index f80f43ef0..c13e6d567 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -2,7 +2,6 @@ #include #include "current-process.hh" -#include "namespaces.hh" #include "util.hh" #include "finally.hh" #include "file-system.hh" @@ -17,6 +16,7 @@ # include # include # include "cgroup.hh" +# include "namespaces.hh" #endif #include @@ -84,7 +84,9 @@ void restoreProcessContext(bool restoreMounts) { restoreSignals(); if (restoreMounts) { + #if __linux__ restoreMountNamespace(); + #endif } if (savedStackSize) { diff --git a/src/libutil/environment-variables.cc b/src/libutil/environment-variables.cc index 6618d7872..7f4bb2d00 100644 --- a/src/libutil/environment-variables.cc +++ b/src/libutil/environment-variables.cc @@ -32,18 +32,4 @@ std::map getEnv() return env; } - -void clearEnv() -{ - for (auto & name : getEnv()) - unsetenv(name.first.c_str()); -} - -void replaceEnv(const std::map & newEnv) -{ - clearEnv(); - for (auto & newEnvVar : newEnv) - setenv(newEnvVar.first.c_str(), newEnvVar.second.c_str(), 1); -} - } diff --git a/src/libutil/file-descriptor.cc b/src/libutil/file-descriptor.cc index 55d57e29b..95cbb8537 100644 --- a/src/libutil/file-descriptor.cc +++ b/src/libutil/file-descriptor.cc @@ -8,74 +8,14 @@ namespace nix { -std::string readFile(int fd) -{ - struct stat st; - if (fstat(fd, &st) == -1) - throw SysError("statting file"); - - return drainFD(fd, true, st.st_size); -} - - -void readFull(int fd, char * buf, size_t count) -{ - while (count) { - checkInterrupt(); - ssize_t res = read(fd, buf, count); - if (res == -1) { - if (errno == EINTR) continue; - throw SysError("reading from file"); - } - if (res == 0) throw EndOfFile("unexpected end-of-file"); - count -= res; - buf += res; - } -} - - -void writeFull(int fd, std::string_view s, bool allowInterrupts) -{ - while (!s.empty()) { - if (allowInterrupts) checkInterrupt(); - ssize_t res = write(fd, s.data(), s.size()); - if (res == -1 && errno != EINTR) - throw SysError("writing to file"); - if (res > 0) - s.remove_prefix(res); - } -} - - -std::string readLine(int fd) -{ - std::string s; - while (1) { - checkInterrupt(); - char ch; - // FIXME: inefficient - ssize_t rd = read(fd, &ch, 1); - if (rd == -1) { - if (errno != EINTR) - throw SysError("reading a line"); - } else if (rd == 0) - throw EndOfFile("unexpected EOF reading a line"); - else { - if (ch == '\n') return s; - s += ch; - } - } -} - - -void writeLine(int fd, std::string s) +void writeLine(Descriptor fd, std::string s) { s += '\n'; writeFull(fd, s); } -std::string drainFD(int fd, bool block, const size_t reserveSize) +std::string drainFD(Descriptor fd, bool block, const size_t reserveSize) { // the parser needs two extra bytes to append terminating characters, other users will // not care very much about the extra memory. @@ -85,50 +25,18 @@ std::string drainFD(int fd, bool block, const size_t reserveSize) } -void drainFD(int fd, Sink & sink, bool block) -{ - // silence GCC maybe-uninitialized warning in finally - int saved = 0; - - if (!block) { - saved = fcntl(fd, F_GETFL); - if (fcntl(fd, F_SETFL, saved | O_NONBLOCK) == -1) - throw SysError("making file descriptor non-blocking"); - } - - Finally finally([&] { - if (!block) { - if (fcntl(fd, F_SETFL, saved) == -1) - throw SysError("making file descriptor blocking"); - } - }); - - std::vector buf(64 * 1024); - while (1) { - checkInterrupt(); - ssize_t rd = read(fd, buf.data(), buf.size()); - if (rd == -1) { - if (!block && (errno == EAGAIN || errno == EWOULDBLOCK)) - break; - if (errno != EINTR) - throw SysError("reading from file"); - } - else if (rd == 0) break; - else sink({reinterpret_cast(buf.data()), size_t(rd)}); - } -} - ////////////////////////////////////////////////////////////////////// -AutoCloseFD::AutoCloseFD() : fd{-1} {} + +AutoCloseFD::AutoCloseFD() : fd{INVALID_DESCRIPTOR} {} -AutoCloseFD::AutoCloseFD(int fd) : fd{fd} {} +AutoCloseFD::AutoCloseFD(Descriptor fd) : fd{fd} {} AutoCloseFD::AutoCloseFD(AutoCloseFD && that) : fd{that.fd} { - that.fd = -1; + that.fd = INVALID_DESCRIPTOR; } @@ -136,7 +44,7 @@ AutoCloseFD & AutoCloseFD::operator =(AutoCloseFD && that) { close(); fd = that.fd; - that.fd = -1; + that.fd = INVALID_DESCRIPTOR; return *this; } @@ -151,7 +59,7 @@ AutoCloseFD::~AutoCloseFD() } -int AutoCloseFD::get() const +Descriptor AutoCloseFD::get() const { return fd; } @@ -159,56 +67,46 @@ int AutoCloseFD::get() const void AutoCloseFD::close() { - if (fd != -1) { - if (::close(fd) == -1) + if (fd != INVALID_DESCRIPTOR) { + if(::close(fd) == -1) /* This should never happen. */ throw SysError("closing file descriptor %1%", fd); - fd = -1; + fd = INVALID_DESCRIPTOR; } } void AutoCloseFD::fsync() { - if (fd != -1) { - int result; + if (fd != INVALID_DESCRIPTOR) { + int result; + result = #if __APPLE__ - result = ::fcntl(fd, F_FULLFSYNC); + ::fcntl(fd, F_FULLFSYNC) #else - result = ::fsync(fd); + ::fsync(fd) #endif - if (result == -1) - throw SysError("fsync file descriptor %1%", fd); - } + ; + if (result == -1) + throw SysError("fsync file descriptor %1%", fd); + } } AutoCloseFD::operator bool() const { - return fd != -1; + return fd != INVALID_DESCRIPTOR; } -int AutoCloseFD::release() +Descriptor AutoCloseFD::release() { - int oldFD = fd; - fd = -1; + Descriptor oldFD = fd; + fd = INVALID_DESCRIPTOR; return oldFD; } -void Pipe::create() -{ - int fds[2]; -#if HAVE_PIPE2 - if (pipe2(fds, O_CLOEXEC) != 0) throw SysError("creating pipe"); -#else - if (pipe(fds) != 0) throw SysError("creating pipe"); - closeOnExec(fds[0]); - closeOnExec(fds[1]); -#endif - readSide = fds[0]; - writeSide = fds[1]; -} +////////////////////////////////////////////////////////////////////// void Pipe::close() @@ -217,38 +115,4 @@ void Pipe::close() writeSide.close(); } -////////////////////////////////////////////////////////////////////// - -void closeMostFDs(const std::set & exceptions) -{ -#if __linux__ - try { - for (auto & s : readDirectory("/proc/self/fd")) { - auto fd = std::stoi(s.name); - if (!exceptions.count(fd)) { - debug("closing leaked FD %d", fd); - close(fd); - } - } - return; - } catch (SystemError &) { - } -#endif - - int maxFD = 0; - maxFD = sysconf(_SC_OPEN_MAX); - for (int fd = 0; fd < maxFD; ++fd) - if (!exceptions.count(fd)) - close(fd); /* ignore result */ -} - - -void closeOnExec(int fd) -{ - int prev; - if ((prev = fcntl(fd, F_GETFD, 0)) == -1 || - fcntl(fd, F_SETFD, prev | FD_CLOEXEC) == -1) - throw SysError("setting close-on-exec flag"); -} - } diff --git a/src/libutil/file-descriptor.hh b/src/libutil/file-descriptor.hh index 80ec86135..719e1e444 100644 --- a/src/libutil/file-descriptor.hh +++ b/src/libutil/file-descriptor.hh @@ -9,53 +9,85 @@ namespace nix { struct Sink; struct Source; +/** + * Operating System capability + */ +typedef int Descriptor; + +const Descriptor INVALID_DESCRIPTOR = -1; + +/** + * Convert a native `Descriptor` to a POSIX file descriptor + * + * This is a no-op except on Windows. + */ +static inline Descriptor toDescriptor(int fd) +{ + return fd; +} + +/** + * Convert a POSIX file descriptor to a native `Descriptor` + * + * This is a no-op except on Windows. + */ +static inline int fromDescriptor(Descriptor fd, int flags) +{ + return fd; +} + /** * Read the contents of a resource into a string. */ -std::string readFile(int fd); +std::string readFile(Descriptor fd); /** * Wrappers arount read()/write() that read/write exactly the * requested number of bytes. */ -void readFull(int fd, char * buf, size_t count); +void readFull(Descriptor fd, char * buf, size_t count); -void writeFull(int fd, std::string_view s, bool allowInterrupts = true); +void writeFull(Descriptor fd, std::string_view s, bool allowInterrupts = true); /** * Read a line from a file descriptor. */ -std::string readLine(int fd); +std::string readLine(Descriptor fd); /** * Write a line to a file descriptor. */ -void writeLine(int fd, std::string s); +void writeLine(Descriptor fd, std::string s); /** * Read a file descriptor until EOF occurs. */ -std::string drainFD(int fd, bool block = true, const size_t reserveSize=0); +std::string drainFD(Descriptor fd, bool block = true, const size_t reserveSize=0); -void drainFD(int fd, Sink & sink, bool block = true); +void drainFD(Descriptor fd, Sink & sink, bool block = true); + +[[gnu::always_inline]] +inline Descriptor getStandardOut() { + return STDOUT_FILENO; +} /** * Automatic cleanup of resources. */ class AutoCloseFD { - int fd; + Descriptor fd; public: AutoCloseFD(); - AutoCloseFD(int fd); + AutoCloseFD(Descriptor fd); AutoCloseFD(const AutoCloseFD & fd) = delete; AutoCloseFD(AutoCloseFD&& fd); ~AutoCloseFD(); AutoCloseFD& operator =(const AutoCloseFD & fd) = delete; AutoCloseFD& operator =(AutoCloseFD&& fd); - int get() const; + Descriptor get() const; explicit operator bool() const; - int release(); + Descriptor release(); void close(); void fsync(); }; @@ -72,12 +104,12 @@ public: * Close all file descriptors except those listed in the given set. * Good practice in child processes. */ -void closeMostFDs(const std::set & exceptions); +void closeMostFDs(const std::set & exceptions); /** * Set the close-on-exec flag for the given file descriptor. */ -void closeOnExec(int fd); +void closeOnExec(Descriptor fd); MakeError(EndOfFile, Error); diff --git a/src/libutil/namespaces.cc b/src/libutil/linux/namespaces.cc similarity index 95% rename from src/libutil/namespaces.cc rename to src/libutil/linux/namespaces.cc index a789b321e..f8289ef39 100644 --- a/src/libutil/namespaces.cc +++ b/src/libutil/linux/namespaces.cc @@ -5,18 +5,14 @@ #include "processes.hh" #include "signals.hh" -#if __linux__ -# include -# include -# include "cgroup.hh" -#endif +#include +#include +#include "cgroup.hh" #include namespace nix { -#if __linux__ - bool userNamespacesSupported() { static auto res = [&]() -> bool @@ -101,19 +97,14 @@ bool mountAndPidNamespacesSupported() return res; } -#endif - ////////////////////////////////////////////////////////////////////// -#if __linux__ static AutoCloseFD fdSavedMountNamespace; static AutoCloseFD fdSavedRoot; -#endif void saveMountNamespace() { -#if __linux__ static std::once_flag done; std::call_once(done, []() { fdSavedMountNamespace = open("/proc/self/ns/mnt", O_RDONLY); @@ -122,12 +113,10 @@ void saveMountNamespace() fdSavedRoot = open("/proc/self/root", O_RDONLY); }); -#endif } void restoreMountNamespace() { -#if __linux__ try { auto savedCwd = absPath("."); @@ -146,15 +135,12 @@ void restoreMountNamespace() } catch (Error & e) { debug(e.msg()); } -#endif } void unshareFilesystem() { -#ifdef __linux__ if (unshare(CLONE_FS) != 0 && errno != EPERM) throw SysError("unsharing filesystem state in download thread"); -#endif } } diff --git a/src/libutil/namespaces.hh b/src/libutil/linux/namespaces.hh similarity index 96% rename from src/libutil/namespaces.hh rename to src/libutil/linux/namespaces.hh index 7e4e921a8..ef3c9123f 100644 --- a/src/libutil/namespaces.hh +++ b/src/libutil/linux/namespaces.hh @@ -26,12 +26,8 @@ void restoreMountNamespace(); */ void unshareFilesystem(); -#if __linux__ - bool userNamespacesSupported(); bool mountAndPidNamespacesSupported(); -#endif - } diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 83db492ca..5024c6081 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -37,8 +37,9 @@ void Logger::warn(const std::string & msg) void Logger::writeToStdout(std::string_view s) { - writeFull(STDOUT_FILENO, s); - writeFull(STDOUT_FILENO, "\n"); + Descriptor standard_out = getStandardOut(); + writeFull(standard_out, s); + writeFull(standard_out, "\n"); } class SimpleLogger : public Logger diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh index d9522566f..6249ddaf5 100644 --- a/src/libutil/serialise.hh +++ b/src/libutil/serialise.hh @@ -119,18 +119,18 @@ protected: */ struct FdSink : BufferedSink { - int fd; + Descriptor fd; size_t written = 0; - FdSink() : fd(-1) { } - FdSink(int fd) : fd(fd) { } + FdSink() : fd(INVALID_DESCRIPTOR) { } + FdSink(Descriptor fd) : fd(fd) { } FdSink(FdSink&&) = default; FdSink & operator=(FdSink && s) { flush(); fd = s.fd; - s.fd = -1; + s.fd = INVALID_DESCRIPTOR; written = s.written; return *this; } @@ -151,18 +151,18 @@ private: */ struct FdSource : BufferedSource { - int fd; + Descriptor fd; size_t read = 0; BackedStringView endOfFileError{"unexpected end-of-file"}; - FdSource() : fd(-1) { } - FdSource(int fd) : fd(fd) { } + FdSource() : fd(INVALID_DESCRIPTOR) { } + FdSource(Descriptor fd) : fd(fd) { } FdSource(FdSource &&) = default; FdSource & operator=(FdSource && s) { fd = s.fd; - s.fd = -1; + s.fd = INVALID_DESCRIPTOR; read = s.read; return *this; } diff --git a/src/libutil/unix/environment-variables.cc b/src/libutil/unix/environment-variables.cc new file mode 100644 index 000000000..c72880896 --- /dev/null +++ b/src/libutil/unix/environment-variables.cc @@ -0,0 +1,21 @@ +#include "util.hh" +#include "environment-variables.hh" + +extern char * * environ __attribute__((weak)); + +namespace nix { + +void clearEnv() +{ + for (auto & name : getEnv()) + unsetenv(name.first.c_str()); +} + +void replaceEnv(const std::map & newEnv) +{ + clearEnv(); + for (auto & newEnvVar : newEnv) + setenv(newEnvVar.first.c_str(), newEnvVar.second.c_str(), 1); +} + +} diff --git a/src/libutil/unix/file-descriptor.cc b/src/libutil/unix/file-descriptor.cc new file mode 100644 index 000000000..27c8d821b --- /dev/null +++ b/src/libutil/unix/file-descriptor.cc @@ -0,0 +1,155 @@ +#include "file-system.hh" +#include "signals.hh" +#include "finally.hh" +#include "serialise.hh" + +#include +#include + +namespace nix { + +std::string readFile(int fd) +{ + struct stat st; + if (fstat(fd, &st) == -1) + throw SysError("statting file"); + + return drainFD(fd, true, st.st_size); +} + + +void readFull(int fd, char * buf, size_t count) +{ + while (count) { + checkInterrupt(); + ssize_t res = read(fd, buf, count); + if (res == -1) { + if (errno == EINTR) continue; + throw SysError("reading from file"); + } + if (res == 0) throw EndOfFile("unexpected end-of-file"); + count -= res; + buf += res; + } +} + + +void writeFull(int fd, std::string_view s, bool allowInterrupts) +{ + while (!s.empty()) { + if (allowInterrupts) checkInterrupt(); + ssize_t res = write(fd, s.data(), s.size()); + if (res == -1 && errno != EINTR) + throw SysError("writing to file"); + if (res > 0) + s.remove_prefix(res); + } +} + + +std::string readLine(int fd) +{ + std::string s; + while (1) { + checkInterrupt(); + char ch; + // FIXME: inefficient + ssize_t rd = read(fd, &ch, 1); + if (rd == -1) { + if (errno != EINTR) + throw SysError("reading a line"); + } else if (rd == 0) + throw EndOfFile("unexpected EOF reading a line"); + else { + if (ch == '\n') return s; + s += ch; + } + } +} + + +void drainFD(int fd, Sink & sink, bool block) +{ + // silence GCC maybe-uninitialized warning in finally + int saved = 0; + + if (!block) { + saved = fcntl(fd, F_GETFL); + if (fcntl(fd, F_SETFL, saved | O_NONBLOCK) == -1) + throw SysError("making file descriptor non-blocking"); + } + + Finally finally([&]() { + if (!block) { + if (fcntl(fd, F_SETFL, saved) == -1) + throw SysError("making file descriptor blocking"); + } + }); + + std::vector buf(64 * 1024); + while (1) { + checkInterrupt(); + ssize_t rd = read(fd, buf.data(), buf.size()); + if (rd == -1) { + if (!block && (errno == EAGAIN || errno == EWOULDBLOCK)) + break; + if (errno != EINTR) + throw SysError("reading from file"); + } + else if (rd == 0) break; + else sink({reinterpret_cast(buf.data()), (size_t) rd}); + } +} + +////////////////////////////////////////////////////////////////////// + +void Pipe::create() +{ + int fds[2]; +#if HAVE_PIPE2 + if (pipe2(fds, O_CLOEXEC) != 0) throw SysError("creating pipe"); +#else + if (pipe(fds) != 0) throw SysError("creating pipe"); + closeOnExec(fds[0]); + closeOnExec(fds[1]); +#endif + readSide = fds[0]; + writeSide = fds[1]; +} + + +////////////////////////////////////////////////////////////////////// + +void closeMostFDs(const std::set & exceptions) +{ +#if __linux__ + try { + for (auto & s : readDirectory("/proc/self/fd")) { + auto fd = std::stoi(s.name); + if (!exceptions.count(fd)) { + debug("closing leaked FD %d", fd); + close(fd); + } + } + return; + } catch (SysError &) { + } +#endif + + int maxFD = 0; + maxFD = sysconf(_SC_OPEN_MAX); + for (int fd = 0; fd < maxFD; ++fd) + if (!exceptions.count(fd)) + close(fd); /* ignore result */ +} + + +void closeOnExec(int fd) +{ + int prev; + if ((prev = fcntl(fd, F_GETFD, 0)) == -1 || + fcntl(fd, F_SETFD, prev | FD_CLOEXEC) == -1) + throw SysError("setting close-on-exec flag"); +} + +} diff --git a/src/libutil/monitor-fd.hh b/src/libutil/unix/monitor-fd.hh similarity index 100% rename from src/libutil/monitor-fd.hh rename to src/libutil/unix/monitor-fd.hh diff --git a/src/libutil/processes.cc b/src/libutil/unix/processes.cc similarity index 100% rename from src/libutil/processes.cc rename to src/libutil/unix/processes.cc diff --git a/src/libutil/processes.hh b/src/libutil/unix/processes.hh similarity index 100% rename from src/libutil/processes.hh rename to src/libutil/unix/processes.hh diff --git a/src/libutil/signals.cc b/src/libutil/unix/signals.cc similarity index 100% rename from src/libutil/signals.cc rename to src/libutil/unix/signals.cc diff --git a/src/libutil/signals.hh b/src/libutil/unix/signals.hh similarity index 100% rename from src/libutil/signals.hh rename to src/libutil/unix/signals.hh diff --git a/src/libutil/unix-domain-socket.cc b/src/libutil/unix/unix-domain-socket.cc similarity index 100% rename from src/libutil/unix-domain-socket.cc rename to src/libutil/unix/unix-domain-socket.cc diff --git a/src/libutil/unix-domain-socket.hh b/src/libutil/unix/unix-domain-socket.hh similarity index 100% rename from src/libutil/unix-domain-socket.hh rename to src/libutil/unix/unix-domain-socket.hh diff --git a/src/libutil/unix/users.cc b/src/libutil/unix/users.cc new file mode 100644 index 000000000..58063a953 --- /dev/null +++ b/src/libutil/unix/users.cc @@ -0,0 +1,66 @@ +#include "util.hh" +#include "users.hh" +#include "environment-variables.hh" +#include "file-system.hh" + +#include +#include +#include + +namespace nix { + +std::string getUserName() +{ + auto pw = getpwuid(geteuid()); + std::string name = pw ? pw->pw_name : getEnv("USER").value_or(""); + if (name.empty()) + throw Error("cannot figure out user name"); + return name; +} + +Path getHomeOf(uid_t userId) +{ + std::vector buf(16384); + struct passwd pwbuf; + struct passwd * pw; + if (getpwuid_r(userId, &pwbuf, buf.data(), buf.size(), &pw) != 0 + || !pw || !pw->pw_dir || !pw->pw_dir[0]) + throw Error("cannot determine user's home directory"); + return pw->pw_dir; +} + +Path getHome() +{ + static Path homeDir = []() + { + std::optional unownedUserHomeDir = {}; + auto homeDir = getEnv("HOME"); + if (homeDir) { + // Only use $HOME if doesn't exist or is owned by the current user. + struct stat st; + int result = stat(homeDir->c_str(), &st); + if (result != 0) { + if (errno != ENOENT) { + warn("couldn't stat $HOME ('%s') for reason other than not existing ('%d'), falling back to the one defined in the 'passwd' file", *homeDir, errno); + homeDir.reset(); + } + } else if (st.st_uid != geteuid()) { + unownedUserHomeDir.swap(homeDir); + } + } + if (!homeDir) { + homeDir = getHomeOf(geteuid()); + if (unownedUserHomeDir.has_value() && unownedUserHomeDir != homeDir) { + warn("$HOME ('%s') is not owned by you, falling back to the one defined in the 'passwd' file ('%s')", *unownedUserHomeDir, *homeDir); + } + } + return *homeDir; + }(); + return homeDir; +} + +bool isRootUser() { + return getuid() == 0; +} + +} diff --git a/src/libutil/users.cc b/src/libutil/users.cc index 8cb3db434..d546e364f 100644 --- a/src/libutil/users.cc +++ b/src/libutil/users.cc @@ -3,63 +3,8 @@ #include "environment-variables.hh" #include "file-system.hh" -#include -#include -#include - namespace nix { -std::string getUserName() -{ - auto pw = getpwuid(geteuid()); - std::string name = pw ? pw->pw_name : getEnv("USER").value_or(""); - if (name.empty()) - throw Error("cannot figure out user name"); - return name; -} - -Path getHomeOf(uid_t userId) -{ - std::vector buf(16384); - struct passwd pwbuf; - struct passwd * pw; - if (getpwuid_r(userId, &pwbuf, buf.data(), buf.size(), &pw) != 0 - || !pw || !pw->pw_dir || !pw->pw_dir[0]) - throw Error("cannot determine user's home directory"); - return pw->pw_dir; -} - -Path getHome() -{ - static Path homeDir = []() - { - std::optional unownedUserHomeDir = {}; - auto homeDir = getEnv("HOME"); - if (homeDir) { - // Only use $HOME if doesn't exist or is owned by the current user. - struct stat st; - int result = stat(homeDir->c_str(), &st); - if (result != 0) { - if (errno != ENOENT) { - warn("couldn't stat $HOME ('%s') for reason other than not existing ('%d'), falling back to the one defined in the 'passwd' file", *homeDir, errno); - homeDir.reset(); - } - } else if (st.st_uid != geteuid()) { - unownedUserHomeDir.swap(homeDir); - } - } - if (!homeDir) { - homeDir = getHomeOf(geteuid()); - if (unownedUserHomeDir.has_value() && unownedUserHomeDir != homeDir) { - warn("$HOME ('%s') is not owned by you, falling back to the one defined in the 'passwd' file ('%s')", *unownedUserHomeDir, *homeDir); - } - } - return *homeDir; - }(); - return homeDir; -} - - Path getCacheDir() { auto cacheDir = getEnv("XDG_CACHE_HOME"); @@ -113,9 +58,4 @@ std::string expandTilde(std::string_view path) return std::string(path); } - -bool isRootUser() { - return getuid() == 0; -} - } diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 06124bf15..103ce4232 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -4,7 +4,6 @@ #include #include #include -#include #include #include diff --git a/src/nix/main.cc b/src/nix/main.cc index 36256f3d0..25f81e48b 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -2,7 +2,6 @@ #include "args/root.hh" #include "current-process.hh" -#include "namespaces.hh" #include "command.hh" #include "common-args.hh" #include "eval.hh" @@ -27,6 +26,10 @@ #include +#if __linux__ +# include "namespaces.hh" +#endif + extern std::string chrootHelperName; void chrootHelper(int argc, char * * argv); diff --git a/src/nix/daemon.cc b/src/nix/unix/daemon.cc similarity index 100% rename from src/nix/daemon.cc rename to src/nix/unix/daemon.cc diff --git a/src/nix/daemon.md b/src/nix/unix/daemon.md similarity index 100% rename from src/nix/daemon.md rename to src/nix/unix/daemon.md diff --git a/src/nix/fmt.cc b/src/nix/unix/fmt.cc similarity index 100% rename from src/nix/fmt.cc rename to src/nix/unix/fmt.cc diff --git a/src/nix/fmt.md b/src/nix/unix/fmt.md similarity index 100% rename from src/nix/fmt.md rename to src/nix/unix/fmt.md diff --git a/src/nix/run.cc b/src/nix/unix/run.cc similarity index 100% rename from src/nix/run.cc rename to src/nix/unix/run.cc diff --git a/src/nix/run.hh b/src/nix/unix/run.hh similarity index 100% rename from src/nix/run.hh rename to src/nix/unix/run.hh diff --git a/src/nix/run.md b/src/nix/unix/run.md similarity index 100% rename from src/nix/run.md rename to src/nix/unix/run.md diff --git a/src/nix/upgrade-nix.cc b/src/nix/unix/upgrade-nix.cc similarity index 100% rename from src/nix/upgrade-nix.cc rename to src/nix/unix/upgrade-nix.cc diff --git a/src/nix/upgrade-nix.md b/src/nix/unix/upgrade-nix.md similarity index 100% rename from src/nix/upgrade-nix.md rename to src/nix/unix/upgrade-nix.md From ba0bd8fae63e4f38cd735d8586f86a7a6e7b363a Mon Sep 17 00:00:00 2001 From: Cameron Dart Date: Tue, 2 Apr 2024 18:10:36 -0700 Subject: [PATCH 156/168] Add functional tests for include directive in nix config file --- tests/functional/config.sh | 12 +++++++++++- tests/functional/config/extra-config.conf | 1 + tests/functional/config/nix-with-bang-include.conf | 2 ++ tests/functional/config/nix-with-include.conf | 2 ++ 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/functional/config/extra-config.conf create mode 100644 tests/functional/config/nix-with-bang-include.conf create mode 100644 tests/functional/config/nix-with-include.conf diff --git a/tests/functional/config.sh b/tests/functional/config.sh index 324fe95bd..efdf2a958 100644 --- a/tests/functional/config.sh +++ b/tests/functional/config.sh @@ -43,6 +43,16 @@ export NIX_USER_CONF_FILES=$here/config/nix-with-substituters.conf var=$(nix config show | grep '^substituters =' | cut -d '=' -f 2 | xargs) [[ $var == https://example.com ]] +# Test that we can include a file. +export NIX_USER_CONF_FILES=$here/config/nix-with-include.conf +var=$(nix config show | grep '^allowed-uris =' | cut -d '=' -f 2 | xargs) +[[ $var == https://github.com/NixOS/nix ]] + +# Test that we can !include a file. +export NIX_USER_CONF_FILES=$here/config/nix-with-bang-include.conf +var=$(nix config show | grep '^experimental-features =' | cut -d '=' -f 2 | xargs) +[[ $var == nix-command ]] + # Test that it's possible to load config from the environment prev=$(nix config show | grep '^cores' | cut -d '=' -f 2 | xargs) export NIX_CONFIG="cores = 4242"$'\n'"experimental-features = nix-command flakes" @@ -56,4 +66,4 @@ exp_features=$(nix config show | grep '^experimental-features' | cut -d '=' -f 2 # Test that it's possible to retrieve a single setting's value val=$(nix config show | grep '^warn-dirty' | cut -d '=' -f 2 | xargs) val2=$(nix config show warn-dirty) -[[ $val == $val2 ]] +[[ $val == $val2 ]] \ No newline at end of file diff --git a/tests/functional/config/extra-config.conf b/tests/functional/config/extra-config.conf new file mode 100644 index 000000000..d110f06e4 --- /dev/null +++ b/tests/functional/config/extra-config.conf @@ -0,0 +1 @@ +allowed-uris = https://github.com/NixOS/nix \ No newline at end of file diff --git a/tests/functional/config/nix-with-bang-include.conf b/tests/functional/config/nix-with-bang-include.conf new file mode 100644 index 000000000..fa600e6ff --- /dev/null +++ b/tests/functional/config/nix-with-bang-include.conf @@ -0,0 +1,2 @@ +experimental-features = nix-command +!include ./missing-extra-config.conf \ No newline at end of file diff --git a/tests/functional/config/nix-with-include.conf b/tests/functional/config/nix-with-include.conf new file mode 100644 index 000000000..17b8958ba --- /dev/null +++ b/tests/functional/config/nix-with-include.conf @@ -0,0 +1,2 @@ +experimental-features = nix-command +include ./extra-config.conf \ No newline at end of file From 8d84de455e53e1cd503f8618ed90ef4b01dfc9ef Mon Sep 17 00:00:00 2001 From: Tharun T Date: Wed, 3 Apr 2024 08:26:42 +0530 Subject: [PATCH 157/168] outputSpecified doesnt exit in top attr-set --- tests/functional/flakes/build-paths.sh | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/functional/flakes/build-paths.sh b/tests/functional/flakes/build-paths.sh index 98827947d..4e5c68095 100644 --- a/tests/functional/flakes/build-paths.sh +++ b/tests/functional/flakes/build-paths.sh @@ -57,16 +57,21 @@ cat > $flake1Dir/flake.nix < \$foo/file - echo "out" > \$out/file - ''; - outputSpecified = true; + a14 = with import ./config.nix; let + top = mkDerivation { + name = "dot-installable"; + outputs = [ "foo" "out" ]; + meta.outputsToInstall = [ "out" ]; + buildCommand = '' + mkdir \$foo \$out + echo "foo" > \$foo/file + echo "out" > \$out/file + ''; + }; + in top // { + foo = top.foo // { + outputSpecified = true; + }; }; }; } From 59597628cb5841cbefc38accd89e7eda663551a1 Mon Sep 17 00:00:00 2001 From: Valentin Gagarin Date: Wed, 3 Apr 2024 16:22:47 +0200 Subject: [PATCH 158/168] show Nix logo in the manual (#9870) * show Nix logo in the manual the location of files is hard-coded by mdBook. there is also seems to be no way to define custom templates, therefore all styling has to be done in the CSS override. Co-authored-by: Robert Hensing --- doc/manual/custom.css | 22 ++++++++++++++++++++++ doc/manual/local.mk | 2 +- doc/manual/src/favicon.png | Bin 0 -> 1205 bytes doc/manual/src/favicon.svg | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 doc/manual/src/favicon.png create mode 100644 doc/manual/src/favicon.svg diff --git a/doc/manual/custom.css b/doc/manual/custom.css index b90f5423f..9e8e3886f 100644 --- a/doc/manual/custom.css +++ b/doc/manual/custom.css @@ -1,3 +1,25 @@ +:root { + --sidebar-width: 23em; +} + +h1.menu-title::before { + content: ""; + background-image: url("./favicon.svg"); + padding: 1.25em; + background-position: center center; + background-size: 2em; + background-repeat: no-repeat; +} + + +h1.menu-title { + padding: 0.5em; +} + +.sidebar .sidebar-scrollbox { + padding: 1em; +} + h1:not(:first-of-type) { margin-top: 1.3em; } diff --git a/doc/manual/local.mk b/doc/manual/local.mk index 698a9289b..71ad5c8e6 100644 --- a/doc/manual/local.mk +++ b/doc/manual/local.mk @@ -217,7 +217,7 @@ doc/manual/generated/man1/nix3-manpages: $(d)/src/command-ref/new-cli # `@docroot@` is to be preserved for documenting the mechanism # FIXME: maybe contributing guides should live right next to the code # instead of in the manual -$(docdir)/manual/index.html: $(MANUAL_SRCS) $(d)/book.toml $(d)/anchors.jq $(d)/custom.css $(d)/src/SUMMARY.md $(d)/src/store/types $(d)/src/command-ref/new-cli $(d)/src/contributing/experimental-feature-descriptions.md $(d)/src/command-ref/conf-file.md $(d)/src/language/builtins.md $(d)/src/language/builtin-constants.md $(d)/src/release-notes/rl-next.md +$(docdir)/manual/index.html: $(MANUAL_SRCS) $(d)/book.toml $(d)/anchors.jq $(d)/custom.css $(d)/src/SUMMARY.md $(d)/src/store/types $(d)/src/command-ref/new-cli $(d)/src/contributing/experimental-feature-descriptions.md $(d)/src/command-ref/conf-file.md $(d)/src/language/builtins.md $(d)/src/language/builtin-constants.md $(d)/src/release-notes/rl-next.md $(d)/src/figures $(d)/src/favicon.png $(d)/src/favicon.svg $(trace-gen) \ tmp="$$(mktemp -d)"; \ cp -r doc/manual "$$tmp"; \ diff --git a/doc/manual/src/favicon.png b/doc/manual/src/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..1ed2b5fe0fdf7a6144adc5cdfa31b5f553df4610 GIT binary patch literal 1205 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbL!Lb6AYF9SoB8UsT^3j@P1pisjL z28L1t28LG&3=CE?7#PG0=IjczVPIf#2=EDU1u6(CKb+Wiwr=O+;uW{c*4&A1Jrz{8 z)4ynQ?dJPcYwy%;xnI8WmUr$t-~6qfnQMIVwv;WuQL*Yq-L5CatL}tUABt^18CZ5O zZ{c;IIY350$$p>|3flP?CbiEsc~jS!_w~D@-8-UT(J^iW{g;gG5$d2=oUQB>1m z@0`tUsjG?>Uj}-le#`xk$|De!wc8%H9(?})|Nq7v4}s!!o9``u`G4xw?{%B*m9M-R zQVF#5P*BAYs3W&O0$NnJ^+EmC2f#3iZ95)VdI0Dcpiyv-WY0P0p1#T_Z>w*?cE6%s z0mXX*O7{cRz=NWC>%+j(ZC*JW9phIsFhqD|Z2-E>D|<5oL#Aiu_JGoT5S2AM9(m_% z^2y&CR-T^|Z`YHa^B;WkcK|~ywEAG>n%i}o z?*m;33^cfn5C+gCK-VVsovq)Bq973HJg^IaA?BI2x8vB0x*d-|{@wK?2;{Xx@L&gd z6Br#}DWLg41A!_7O7;R1&jO{3!1TqiyCldDl>V>-|M%V{f8t%=1*kvR${~LK!l5U+ zE;&bk_oXlWQ^n1wc;Wk}FW#{_g-;-%DvtP?D4mF z3v@eUlDE4HLkFv@2av;A;1O92%udl*pgo2QFoh{WaOgaa%+lY%b!7;P&0 z#MO01K~H({q)lC`X638PfeR-pwsWptDH(9!&Y?@EZXLUJ?%u(R zCvUb+Y&?7Q?A^ncPv1U%{k*+`LB~Wz3l$R`8>LBem9?zY%+A@&Q&cq6v{aosxqG&{ zZmqvtnwWE$-@AKzYU1qb8k-I+dNe7hZPTZ&Q>$J{IWD}l>sQ;cWoc{=r(N6jt?%5r zck}M;`^PElD$%0uJI|=}))z_R^t1Df!;|M3G=KQRx`_L`Zq%NXmz(B1)oy8II+Uax zzII#0=CZrLc%@q#nRs{}%{I%wxhr?~k-oXRTQ>fXWq5hY \ No newline at end of file From 09551fabd092fe26d6b6a13417502a514fbd78b3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 3 Apr 2024 15:28:13 +0200 Subject: [PATCH 159/168] Handle the case where a parent of ~/.nix-defexpr is a symlink Fixes https://github.com/DeterminateSystems/nix-installer/issues/912 and probably #10247. --- src/nix-env/nix-env.cc | 2 +- tests/functional/user-envs.sh | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 177344044..1cc33558f 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -109,7 +109,7 @@ static void getAllExprs(EvalState & state, const SourcePath & path, StringSet & seen, BindingsBuilder & attrs) { StringSet namesSorted; - for (auto & [name, _] : path.readDirectory()) namesSorted.insert(name); + for (auto & [name, _] : path.resolveSymlinks().readDirectory()) namesSorted.insert(name); for (auto & i : namesSorted) { /* Ignore the manifest.nix used by profiles. This is diff --git a/tests/functional/user-envs.sh b/tests/functional/user-envs.sh index dcd6b1b97..7c643f355 100644 --- a/tests/functional/user-envs.sh +++ b/tests/functional/user-envs.sh @@ -189,3 +189,9 @@ nix-env --set $outPath10 [ "$(nix-store -q --resolve $profiles/test)" = $outPath10 ] nix-env --set $drvPath10 [ "$(nix-store -q --resolve $profiles/test)" = $outPath10 ] + +# Test the case where $HOME contains a symlink. +mkdir -p $TEST_ROOT/real-home/alice/.nix-defexpr/channels +ln -sfn $TEST_ROOT/real-home $TEST_ROOT/home +ln -sfn $(pwd)/user-envs.nix $TEST_ROOT/home/alice/.nix-defexpr/channels/foo +HOME=$TEST_ROOT/home/alice nix-env -i foo-0.1 From 50cb14fcf9be3568b36d3bc16f3c12153022da99 Mon Sep 17 00:00:00 2001 From: HaeNoe <57222371+haenoe@users.noreply.github.com> Date: Wed, 3 Apr 2024 20:04:00 +0200 Subject: [PATCH 160/168] Improve checked json casting (#10087) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This introduces new utility functions to get elements from JSON — in an ergonomic way and with nice error messages if the expected type does not match. Co-authored-by: John Ericson --- .gitignore | 3 + Makefile | 1 + src/libfetchers/unix/git.cc | 9 ++- src/libstore/derivations.cc | 45 +++++------- src/libstore/nar-info.cc | 9 +-- src/libstore/path-info.cc | 27 +++---- src/libutil/json-utils.cc | 104 ++++++++++++++++++++++++-- src/libutil/json-utils.hh | 31 +++++--- tests/unit/libfetchers/local.mk | 32 ++++++++ tests/unit/libfetchers/public-key.cc | 18 +++++ tests/unit/libutil/json-utils.cc | 105 +++++++++++++++++++++++++++ 11 files changed, 315 insertions(+), 69 deletions(-) create mode 100644 tests/unit/libfetchers/local.mk create mode 100644 tests/unit/libfetchers/public-key.cc diff --git a/.gitignore b/.gitignore index 01fafa5a9..5a33c00ea 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,9 @@ perl/Makefile.config /src/libexpr/tests /tests/unit/libexpr/libnixexpr-tests +# /src/libfetchers +/tests/unit/libfetchers/libnixfetchers-tests + # /src/libstore/ *.gen.* /src/libstore/tests diff --git a/Makefile b/Makefile index c3dc83c77..a33b8c458 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ makefiles += \ tests/unit/libutil-support/local.mk \ tests/unit/libstore/local.mk \ tests/unit/libstore-support/local.mk \ + tests/unit/libfetchers/local.mk \ tests/unit/libexpr/local.mk \ tests/unit/libexpr-support/local.mk endif diff --git a/src/libfetchers/unix/git.cc b/src/libfetchers/unix/git.cc index 34cfd3f5b..0966c4710 100644 --- a/src/libfetchers/unix/git.cc +++ b/src/libfetchers/unix/git.cc @@ -147,9 +147,12 @@ std::vector getPublicKeys(const Attrs & attrs) { std::vector publicKeys; if (attrs.contains("publicKeys")) { - nlohmann::json publicKeysJson = nlohmann::json::parse(getStrAttr(attrs, "publicKeys")); - ensureType(publicKeysJson, nlohmann::json::value_t::array); - publicKeys = publicKeysJson.get>(); + auto pubKeysJson = nlohmann::json::parse(getStrAttr(attrs, "publicKeys")); + auto & pubKeys = getArray(pubKeysJson); + + for (auto & key : pubKeys) { + publicKeys.push_back(key); + } } if (attrs.contains("publicKey")) publicKeys.push_back(PublicKey{maybeGetStrAttr(attrs, "keytype").value_or("ssh-ed25519"),getStrAttr(attrs, "publicKey")}); diff --git a/src/libstore/derivations.cc b/src/libstore/derivations.cc index df14e979f..fcf813a37 100644 --- a/src/libstore/derivations.cc +++ b/src/libstore/derivations.cc @@ -1239,16 +1239,14 @@ DerivationOutput DerivationOutput::fromJSON( const ExperimentalFeatureSettings & xpSettings) { std::set keys; - ensureType(_json, nlohmann::detail::value_t::object); - auto json = (std::map) _json; + auto & json = getObject(_json); for (const auto & [key, _] : json) keys.insert(key); auto methodAlgo = [&]() -> std::pair { - std::string hashAlgoStr = json["hashAlgo"]; - // remaining to parse, will be mutated by parsers - std::string_view s = hashAlgoStr; + auto & str = getString(valueAt(json, "hashAlgo")); + std::string_view s = str; ContentAddressMethod method = ContentAddressMethod::parsePrefix(s); if (method == TextIngestionMethod {}) xpSettings.require(Xp::DynamicDerivations); @@ -1258,7 +1256,7 @@ DerivationOutput DerivationOutput::fromJSON( if (keys == (std::set { "path" })) { return DerivationOutput::InputAddressed { - .path = store.parseStorePath((std::string) json["path"]), + .path = store.parseStorePath(getString(valueAt(json, "path"))), }; } @@ -1267,10 +1265,10 @@ DerivationOutput DerivationOutput::fromJSON( auto dof = DerivationOutput::CAFixed { .ca = ContentAddress { .method = std::move(method), - .hash = Hash::parseNonSRIUnprefixed((std::string) json["hash"], hashAlgo), + .hash = Hash::parseNonSRIUnprefixed(getString(valueAt(json, "hash")), hashAlgo), }, }; - if (dof.path(store, drvName, outputName) != store.parseStorePath((std::string) json["path"])) + if (dof.path(store, drvName, outputName) != store.parseStorePath(getString(valueAt(json, "path")))) throw Error("Path doesn't match derivation output"); return dof; } @@ -1357,20 +1355,19 @@ nlohmann::json Derivation::toJSON(const StoreDirConfig & store) const Derivation Derivation::fromJSON( const StoreDirConfig & store, - const nlohmann::json & json, + const nlohmann::json & _json, const ExperimentalFeatureSettings & xpSettings) { using nlohmann::detail::value_t; Derivation res; - ensureType(json, value_t::object); + auto & json = getObject(_json); - res.name = ensureType(valueAt(json, "name"), value_t::string); + res.name = getString(valueAt(json, "name")); try { - auto & outputsObj = ensureType(valueAt(json, "outputs"), value_t::object); - for (auto & [outputName, output] : outputsObj.items()) { + for (auto & [outputName, output] : getObject(valueAt(json, "outputs"))) { res.outputs.insert_or_assign( outputName, DerivationOutput::fromJSON(store, res.name, outputName, output)); @@ -1381,8 +1378,7 @@ Derivation Derivation::fromJSON( } try { - auto & inputsList = ensureType(valueAt(json, "inputSrcs"), value_t::array); - for (auto & input : inputsList) + for (auto & input : getArray(valueAt(json, "inputSrcs"))) res.inputSrcs.insert(store.parseStorePath(static_cast(input))); } catch (Error & e) { e.addTrace({}, "while reading key 'inputSrcs'"); @@ -1391,18 +1387,17 @@ Derivation Derivation::fromJSON( try { std::function::ChildNode(const nlohmann::json &)> doInput; - doInput = [&](const auto & json) { + doInput = [&](const auto & _json) { + auto & json = getObject(_json); DerivedPathMap::ChildNode node; - node.value = static_cast( - ensureType(valueAt(json, "outputs"), value_t::array)); - for (auto & [outputId, childNode] : ensureType(valueAt(json, "dynamicOutputs"), value_t::object).items()) { + node.value = getStringSet(valueAt(json, "outputs")); + for (auto & [outputId, childNode] : getObject(valueAt(json, "dynamicOutputs"))) { xpSettings.require(Xp::DynamicDerivations); node.childMap[outputId] = doInput(childNode); } return node; }; - auto & inputDrvsObj = ensureType(valueAt(json, "inputDrvs"), value_t::object); - for (auto & [inputDrvPath, inputOutputs] : inputDrvsObj.items()) + for (auto & [inputDrvPath, inputOutputs] : getObject(valueAt(json, "inputDrvs"))) res.inputDrvs.map[store.parseStorePath(inputDrvPath)] = doInput(inputOutputs); } catch (Error & e) { @@ -1410,10 +1405,10 @@ Derivation Derivation::fromJSON( throw; } - res.platform = ensureType(valueAt(json, "system"), value_t::string); - res.builder = ensureType(valueAt(json, "builder"), value_t::string); - res.args = ensureType(valueAt(json, "args"), value_t::array); - res.env = ensureType(valueAt(json, "env"), value_t::object); + res.platform = getString(valueAt(json, "system")); + res.builder = getString(valueAt(json, "builder")); + res.args = getStringList(valueAt(json, "args")); + res.env = getStringMap(valueAt(json, "env")); return res; } diff --git a/src/libstore/nar-info.cc b/src/libstore/nar-info.cc index d9618d04c..0d219a489 100644 --- a/src/libstore/nar-info.cc +++ b/src/libstore/nar-info.cc @@ -172,19 +172,18 @@ NarInfo NarInfo::fromJSON( }; if (json.contains("url")) - res.url = ensureType(valueAt(json, "url"), value_t::string); + res.url = getString(valueAt(json, "url")); if (json.contains("compression")) - res.compression = ensureType(valueAt(json, "compression"), value_t::string); + res.compression = getString(valueAt(json, "compression")); if (json.contains("downloadHash")) res.fileHash = Hash::parseAny( - static_cast( - ensureType(valueAt(json, "downloadHash"), value_t::string)), + getString(valueAt(json, "downloadHash")), std::nullopt); if (json.contains("downloadSize")) - res.fileSize = ensureType(valueAt(json, "downloadSize"), value_t::number_integer); + res.fileSize = getInteger(valueAt(json, "downloadSize")); return res; } diff --git a/src/libstore/path-info.cc b/src/libstore/path-info.cc index d82ccd0c9..6523cb425 100644 --- a/src/libstore/path-info.cc +++ b/src/libstore/path-info.cc @@ -190,23 +190,18 @@ nlohmann::json UnkeyedValidPathInfo::toJSON( UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON( const Store & store, - const nlohmann::json & json) + const nlohmann::json & _json) { - using nlohmann::detail::value_t; - UnkeyedValidPathInfo res { Hash(Hash::dummy), }; - ensureType(json, value_t::object); - res.narHash = Hash::parseAny( - static_cast( - ensureType(valueAt(json, "narHash"), value_t::string)), - std::nullopt); - res.narSize = ensureType(valueAt(json, "narSize"), value_t::number_integer); + auto & json = getObject(_json); + res.narHash = Hash::parseAny(getString(valueAt(json, "narHash")), std::nullopt); + res.narSize = getInteger(valueAt(json, "narSize")); try { - auto & references = ensureType(valueAt(json, "references"), value_t::array); + auto references = getStringList(valueAt(json, "references")); for (auto & input : references) res.references.insert(store.parseStorePath(static_cast (input))); @@ -216,20 +211,16 @@ UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON( } if (json.contains("ca")) - res.ca = ContentAddress::parse( - static_cast( - ensureType(valueAt(json, "ca"), value_t::string))); + res.ca = ContentAddress::parse(getString(valueAt(json, "ca"))); if (json.contains("deriver")) - res.deriver = store.parseStorePath( - static_cast( - ensureType(valueAt(json, "deriver"), value_t::string))); + res.deriver = store.parseStorePath(getString(valueAt(json, "deriver"))); if (json.contains("registrationTime")) - res.registrationTime = ensureType(valueAt(json, "registrationTime"), value_t::number_integer); + res.registrationTime = getInteger(valueAt(json, "registrationTime")); if (json.contains("ultimate")) - res.ultimate = ensureType(valueAt(json, "ultimate"), value_t::boolean); + res.ultimate = getBoolean(valueAt(json, "ultimate")); if (json.contains("signatures")) res.sigs = valueAt(json, "signatures"); diff --git a/src/libutil/json-utils.cc b/src/libutil/json-utils.cc index 61cef743d..7a7264a9a 100644 --- a/src/libutil/json-utils.cc +++ b/src/libutil/json-utils.cc @@ -1,5 +1,8 @@ #include "json-utils.hh" #include "error.hh" +#include "types.hh" +#include +#include namespace nix { @@ -18,26 +21,115 @@ nlohmann::json * get(nlohmann::json & map, const std::string & key) } const nlohmann::json & valueAt( - const nlohmann::json & map, + const nlohmann::json::object_t & map, const std::string & key) { if (!map.contains(key)) - throw Error("Expected JSON object to contain key '%s' but it doesn't", key); + throw Error("Expected JSON object to contain key '%s' but it doesn't: %s", key, nlohmann::json(map).dump()); - return map[key]; + return map.at(key); } -const nlohmann::json & ensureType( +std::optional optionalValueAt(const nlohmann::json & value, const std::string & key) +{ + try { + auto & v = valueAt(value, key); + return v.get(); + } catch (...) { + return std::nullopt; + } +} + + +std::optional getNullable(const nlohmann::json & value) +{ + if (value.is_null()) + return std::nullopt; + + return value.get(); +} + +/** + * Ensure the type of a JSON object is what you expect, failing with a + * ensure type if it isn't. + * + * Use before type conversions and element access to avoid ugly + * exceptions, but only part of this module to define the other `get*` + * functions. It is too cumbersome and easy to forget to expect regular + * JSON code to use it directly. + */ +static const nlohmann::json & ensureType( const nlohmann::json & value, nlohmann::json::value_type expectedType ) { if (value.type() != expectedType) throw Error( - "Expected JSON value to be of type '%s' but it is of type '%s'", + "Expected JSON value to be of type '%s' but it is of type '%s': %s", nlohmann::json(expectedType).type_name(), - value.type_name()); + value.type_name(), value.dump()); return value; } + +const nlohmann::json::object_t & getObject(const nlohmann::json & value) +{ + return ensureType(value, nlohmann::json::value_t::object).get_ref(); +} + +const nlohmann::json::array_t & getArray(const nlohmann::json & value) +{ + return ensureType(value, nlohmann::json::value_t::array).get_ref(); +} + +const nlohmann::json::string_t & getString(const nlohmann::json & value) +{ + return ensureType(value, nlohmann::json::value_t::string).get_ref(); +} + +const nlohmann::json::number_integer_t & getInteger(const nlohmann::json & value) +{ + return ensureType(value, nlohmann::json::value_t::number_integer).get_ref(); +} + +const nlohmann::json::boolean_t & getBoolean(const nlohmann::json & value) +{ + return ensureType(value, nlohmann::json::value_t::boolean).get_ref(); +} + +Strings getStringList(const nlohmann::json & value) +{ + auto & jsonArray = getArray(value); + + Strings stringList; + + for (const auto & elem : jsonArray) + stringList.push_back(getString(elem)); + + return stringList; +} + +StringMap getStringMap(const nlohmann::json & value) +{ + auto & jsonObject = getObject(value); + + StringMap stringMap; + + for (const auto & [key, value] : jsonObject) + stringMap[getString(key)] = getString(value); + + return stringMap; +} + +StringSet getStringSet(const nlohmann::json & value) +{ + auto & jsonArray = getArray(value); + + StringSet stringSet; + + for (const auto & elem : jsonArray) + stringSet.insert(getString(elem)); + + return stringSet; +} } diff --git a/src/libutil/json-utils.hh b/src/libutil/json-utils.hh index 06dd80cf7..2024624f4 100644 --- a/src/libutil/json-utils.hh +++ b/src/libutil/json-utils.hh @@ -3,6 +3,9 @@ #include #include +#include + +#include "types.hh" namespace nix { @@ -11,26 +14,30 @@ const nlohmann::json * get(const nlohmann::json & map, const std::string & key); nlohmann::json * get(nlohmann::json & map, const std::string & key); /** - * Get the value of a json object at a key safely, failing - * with a Nix Error if the key does not exist. + * Get the value of a json object at a key safely, failing with a nice + * error if the key does not exist. * * Use instead of nlohmann::json::at() to avoid ugly exceptions. - * - * _Does not check whether `map` is an object_, use `ensureType` for that. */ const nlohmann::json & valueAt( - const nlohmann::json & map, + const nlohmann::json::object_t & map, const std::string & key); +std::optional optionalValueAt(const nlohmann::json & value, const std::string & key); + /** - * Ensure the type of a json object is what you expect, failing - * with a Nix Error if it isn't. - * - * Use before type conversions and element access to avoid ugly exceptions. + * Downcast the json object, failing with a nice error if the conversion fails. + * See https://json.nlohmann.me/features/types/ */ -const nlohmann::json & ensureType( - const nlohmann::json & value, - nlohmann::json::value_type expectedType); +std::optional getNullable(const nlohmann::json & value); +const nlohmann::json::object_t & getObject(const nlohmann::json & value); +const nlohmann::json::array_t & getArray(const nlohmann::json & value); +const nlohmann::json::string_t & getString(const nlohmann::json & value); +const nlohmann::json::number_integer_t & getInteger(const nlohmann::json & value); +const nlohmann::json::boolean_t & getBoolean(const nlohmann::json & value); +Strings getStringList(const nlohmann::json & value); +StringMap getStringMap(const nlohmann::json & value); +StringSet getStringSet(const nlohmann::json & value); /** * For `adl_serializer>` below, we need to track what diff --git a/tests/unit/libfetchers/local.mk b/tests/unit/libfetchers/local.mk new file mode 100644 index 000000000..e9f659fd7 --- /dev/null +++ b/tests/unit/libfetchers/local.mk @@ -0,0 +1,32 @@ +check: libfetchers-tests_RUN + +programs += libfetchers-tests + +libfetchers-tests_NAME = libnixfetchers-tests + +libfetchers-tests_ENV := _NIX_TEST_UNIT_DATA=$(d)/data + +libfetchers-tests_DIR := $(d) + +ifeq ($(INSTALL_UNIT_TESTS), yes) + libfetchers-tests_INSTALL_DIR := $(checkbindir) +else + libfetchers-tests_INSTALL_DIR := +endif + +libfetchers-tests_SOURCES := $(wildcard $(d)/*.cc) + +libfetchers-tests_EXTRA_INCLUDES = \ + -I tests/unit/libstore-support \ + -I tests/unit/libutil-support \ + $(INCLUDE_libfetchers) \ + $(INCLUDE_libstore) \ + $(INCLUDE_libutil) + +libfetchers-tests_CXXFLAGS += $(libfetchers-tests_EXTRA_INCLUDES) + +libfetchers-tests_LIBS = \ + libstore-test-support libutil-test-support \ + libfetchers libstore libutil + +libfetchers-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) diff --git a/tests/unit/libfetchers/public-key.cc b/tests/unit/libfetchers/public-key.cc new file mode 100644 index 000000000..fcd5c3af0 --- /dev/null +++ b/tests/unit/libfetchers/public-key.cc @@ -0,0 +1,18 @@ +#include +#include "fetchers.hh" +#include "json-utils.hh" + +namespace nix { + TEST(PublicKey, jsonSerialization) { + auto json = nlohmann::json(fetchers::PublicKey { .key = "ABCDE" }); + + ASSERT_EQ(json, R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json); + } + TEST(PublicKey, jsonDeserialization) { + auto pubKeyJson = R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json; + fetchers::PublicKey pubKey = pubKeyJson; + + ASSERT_EQ(pubKey.key, "ABCDE"); + ASSERT_EQ(pubKey.type, "ssh-ed25519"); + } +} diff --git a/tests/unit/libutil/json-utils.cc b/tests/unit/libutil/json-utils.cc index f0ce15c93..ffa667806 100644 --- a/tests/unit/libutil/json-utils.cc +++ b/tests/unit/libutil/json-utils.cc @@ -3,6 +3,7 @@ #include +#include "error.hh" #include "json-utils.hh" namespace nix { @@ -55,4 +56,108 @@ TEST(from_json, vectorOfOptionalInts) { ASSERT_FALSE(vals.at(1).has_value()); } +TEST(valueAt, simpleObject) { + auto simple = R"({ "hello": "world" })"_json; + + ASSERT_EQ(valueAt(getObject(simple), "hello"), "world"); + + auto nested = R"({ "hello": { "world": "" } })"_json; + + auto & nestedObject = valueAt(getObject(nested), "hello"); + + ASSERT_EQ(valueAt(nestedObject, "world"), ""); +} + +TEST(valueAt, missingKey) { + auto json = R"({ "hello": { "nested": "world" } })"_json; + + auto & obj = getObject(json); + + ASSERT_THROW(valueAt(obj, "foo"), Error); +} + +TEST(getObject, rightAssertions) { + auto simple = R"({ "object": {} })"_json; + + ASSERT_EQ(getObject(valueAt(getObject(simple), "object")), (nlohmann::json::object_t {})); + + auto nested = R"({ "object": { "object": {} } })"_json; + + auto & nestedObject = getObject(valueAt(getObject(nested), "object")); + + ASSERT_EQ(nestedObject, getObject(nlohmann::json::parse(R"({ "object": {} })"))); + ASSERT_EQ(getObject(valueAt(getObject(nestedObject), "object")), (nlohmann::json::object_t {})); +} + +TEST(getObject, wrongAssertions) { + auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json; + + auto & obj = getObject(json); + + ASSERT_THROW(getObject(valueAt(obj, "array")), Error); + ASSERT_THROW(getObject(valueAt(obj, "string")), Error); + ASSERT_THROW(getObject(valueAt(obj, "int")), Error); + ASSERT_THROW(getObject(valueAt(obj, "boolean")), Error); +} + +TEST(getArray, rightAssertions) { + auto simple = R"({ "array": [] })"_json; + + ASSERT_EQ(getArray(valueAt(getObject(simple), "array")), (nlohmann::json::array_t {})); +} + +TEST(getArray, wrongAssertions) { + auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json; + + ASSERT_THROW(getArray(valueAt(json, "object")), Error); + ASSERT_THROW(getArray(valueAt(json, "string")), Error); + ASSERT_THROW(getArray(valueAt(json, "int")), Error); + ASSERT_THROW(getArray(valueAt(json, "boolean")), Error); +} + +TEST(getString, rightAssertions) { + auto simple = R"({ "string": "" })"_json; + + ASSERT_EQ(getString(valueAt(getObject(simple), "string")), ""); +} + +TEST(getString, wrongAssertions) { + auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json; + + ASSERT_THROW(getString(valueAt(json, "object")), Error); + ASSERT_THROW(getString(valueAt(json, "array")), Error); + ASSERT_THROW(getString(valueAt(json, "int")), Error); + ASSERT_THROW(getString(valueAt(json, "boolean")), Error); +} + +TEST(getInteger, rightAssertions) { + auto simple = R"({ "int": 0 })"_json; + + ASSERT_EQ(getInteger(valueAt(getObject(simple), "int")), 0); +} + +TEST(getInteger, wrongAssertions) { + auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json; + + ASSERT_THROW(getInteger(valueAt(json, "object")), Error); + ASSERT_THROW(getInteger(valueAt(json, "array")), Error); + ASSERT_THROW(getInteger(valueAt(json, "string")), Error); + ASSERT_THROW(getInteger(valueAt(json, "boolean")), Error); +} + +TEST(getBoolean, rightAssertions) { + auto simple = R"({ "boolean": false })"_json; + + ASSERT_EQ(getBoolean(valueAt(getObject(simple), "boolean")), false); +} + +TEST(getBoolean, wrongAssertions) { + auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json; + + ASSERT_THROW(getBoolean(valueAt(json, "object")), Error); + ASSERT_THROW(getBoolean(valueAt(json, "array")), Error); + ASSERT_THROW(getBoolean(valueAt(json, "string")), Error); + ASSERT_THROW(getBoolean(valueAt(json, "int")), Error); +} + } /* namespace nix */ From 1577b5fa6732ef55d51ace6fc930f27c78e095b6 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 4 Apr 2024 12:42:41 -0400 Subject: [PATCH 161/168] Make SQLite busy back-off logic portable Use C++ standard library not Unix functions for sleeping and randomness. Suggested by @edolstra in https://github.com/NixOS/nix/pull/8901#discussion_r1550416615 --- src/libstore/sqlite.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libstore/sqlite.cc b/src/libstore/sqlite.cc index 06abfb90b..3175c1978 100644 --- a/src/libstore/sqlite.cc +++ b/src/libstore/sqlite.cc @@ -7,6 +7,7 @@ #include #include +#include namespace nix { @@ -256,10 +257,8 @@ void handleSQLiteBusy(const SQLiteBusy & e, time_t & nextWarning) /* Sleep for a while since retrying the transaction right away is likely to fail again. */ checkInterrupt(); - struct timespec t; - t.tv_sec = 0; - t.tv_nsec = (random() % 100) * 1000 * 1000; /* <= 0.1s */ - nanosleep(&t, 0); + /* <= 0.1s */ + std::this_thread::sleep_for(std::chrono::milliseconds { rand() % 100 }); } } From ef2d10f7e71fbbeb2fe78c327ffe5469e8ae4d04 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 4 Apr 2024 12:48:54 -0400 Subject: [PATCH 162/168] Clean up env var logic in preparation for Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's a little weird we don't check the return status for these, but changing that would introduce risk so I did not. Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com> --- src/libmain/shared.cc | 4 ++-- src/libutil/environment-variables.cc | 13 +++++++++++++ src/libutil/environment-variables.hh | 8 ++++++++ src/libutil/unix/environment-variables.cc | 17 ++++------------- src/nix-build/nix-build.cc | 2 +- src/nix/develop.cc | 4 ++-- src/nix/unix/run.cc | 2 +- tests/unit/libexpr/primops.cc | 2 +- 8 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 7bced0aa4..f3dd73101 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -308,7 +308,7 @@ void printVersion(const std::string & programName) void showManPage(const std::string & name) { restoreProcessContext(); - setenv("MANPATH", settings.nixManDir.c_str(), 1); + setEnv("MANPATH", settings.nixManDir.c_str()); execlp("man", "man", name.c_str(), nullptr); throw SysError("command 'man %1%' failed", name.c_str()); } @@ -369,7 +369,7 @@ RunPager::RunPager() if (dup2(toPager.readSide.get(), STDIN_FILENO) == -1) throw SysError("dupping stdin"); if (!getenv("LESS")) - setenv("LESS", "FRSXMK", 1); + setEnv("LESS", "FRSXMK"); restoreProcessContext(); if (pager) execl("/bin/sh", "sh", "-c", pager, nullptr); diff --git a/src/libutil/environment-variables.cc b/src/libutil/environment-variables.cc index 7f4bb2d00..d43197aa0 100644 --- a/src/libutil/environment-variables.cc +++ b/src/libutil/environment-variables.cc @@ -32,4 +32,17 @@ std::map getEnv() return env; } +void clearEnv() +{ + for (auto & name : getEnv()) + unsetenv(name.first.c_str()); +} + +void replaceEnv(const std::map & newEnv) +{ + clearEnv(); + for (auto & newEnvVar : newEnv) + setEnv(newEnvVar.first.c_str(), newEnvVar.second.c_str()); +} + } diff --git a/src/libutil/environment-variables.hh b/src/libutil/environment-variables.hh index 21eb4619b..21c2356a4 100644 --- a/src/libutil/environment-variables.hh +++ b/src/libutil/environment-variables.hh @@ -28,6 +28,14 @@ std::optional getEnvNonEmpty(const std::string & key); */ std::map getEnv(); +/** + * Like POSIX `setenv`, but always overrides. + * + * We don't need the non-overriding version, and this is easier to + * reimplement on Windows. + */ +int setEnv(const char * name, const char * value); + /** * Clear the environment. */ diff --git a/src/libutil/unix/environment-variables.cc b/src/libutil/unix/environment-variables.cc index c72880896..9c6fd3b18 100644 --- a/src/libutil/unix/environment-variables.cc +++ b/src/libutil/unix/environment-variables.cc @@ -1,21 +1,12 @@ -#include "util.hh" -#include "environment-variables.hh" +#include -extern char * * environ __attribute__((weak)); +#include "environment-variables.hh" namespace nix { -void clearEnv() +int setEnv(const char * name, const char * value) { - for (auto & name : getEnv()) - unsetenv(name.first.c_str()); -} - -void replaceEnv(const std::map & newEnv) -{ - clearEnv(); - for (auto & newEnvVar : newEnv) - setenv(newEnvVar.first.c_str(), newEnvVar.second.c_str(), 1); + return ::setenv(name, value, 1); } } diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 8276be8e8..60dea3a80 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -288,7 +288,7 @@ static void main_nix_build(int argc, char * * argv) } if (runEnv) - setenv("IN_NIX_SHELL", pure ? "pure" : "impure", 1); + setEnv("IN_NIX_SHELL", pure ? "pure" : "impure"); PackageInfos drvs; diff --git a/src/nix/develop.cc b/src/nix/develop.cc index c1842f2d5..bb96f7786 100644 --- a/src/nix/develop.cc +++ b/src/nix/develop.cc @@ -603,7 +603,7 @@ struct CmdDevelop : Common, MixEnvironment setEnviron(); // prevent garbage collection until shell exits - setenv("NIX_GCROOT", gcroot.c_str(), 1); + setEnv("NIX_GCROOT", gcroot.c_str()); Path shell = "bash"; @@ -648,7 +648,7 @@ struct CmdDevelop : Common, MixEnvironment // Override SHELL with the one chosen for this environment. // This is to make sure the system shell doesn't leak into the build environment. - setenv("SHELL", shell.c_str(), 1); + setEnv("SHELL", shell.c_str()); // If running a phase or single command, don't want an interactive shell running after // Ctrl-C, so don't pass --rcfile diff --git a/src/nix/unix/run.cc b/src/nix/unix/run.cc index e86837679..02e809e5c 100644 --- a/src/nix/unix/run.cc +++ b/src/nix/unix/run.cc @@ -134,7 +134,7 @@ struct CmdShell : InstallablesCommand, MixEnvironment auto unixPath = tokenizeString(getEnv("PATH").value_or(""), ":"); unixPath.insert(unixPath.begin(), pathAdditions.begin(), pathAdditions.end()); auto unixPathString = concatStringsSep(":", unixPath); - setenv("PATH", unixPathString.c_str(), 1); + setEnv("PATH", unixPathString.c_str()); Strings args; for (auto & arg : command) args.push_back(arg); diff --git a/tests/unit/libexpr/primops.cc b/tests/unit/libexpr/primops.cc index b1426edae..92319e0c3 100644 --- a/tests/unit/libexpr/primops.cc +++ b/tests/unit/libexpr/primops.cc @@ -91,7 +91,7 @@ namespace nix { } TEST_F(PrimOpTest, getEnv) { - setenv("_NIX_UNIT_TEST_ENV_VALUE", "test value", 1); + setEnv("_NIX_UNIT_TEST_ENV_VALUE", "test value"); auto v = eval("builtins.getEnv \"_NIX_UNIT_TEST_ENV_VALUE\""); ASSERT_THAT(v, IsStringEq("test value")); } From c1e07693558eb94855b97ae44e01f51909d9fdac Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 4 Apr 2024 15:24:42 -0400 Subject: [PATCH 163/168] Fix some portability issues with the new C bindings Build without GC is unbroken Fix #10403 Also building tests with Windows (assuming rest of Windows fixes) is unbroken. --- src/libexpr-c/local.mk | 2 +- src/libexpr-c/nix_api_expr.cc | 4 ++-- src/libexpr-c/nix_api_external.cc | 6 +++--- src/libexpr-c/nix_api_value.cc | 8 ++++---- src/libstore-c/local.mk | 2 +- src/libutil-c/local.mk | 2 +- tests/unit/libstore-support/tests/nix_api_store.hh | 14 +++++++++++++- 7 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/libexpr-c/local.mk b/src/libexpr-c/local.mk index ce5d321d6..51b02562e 100644 --- a/src/libexpr-c/local.mk +++ b/src/libexpr-c/local.mk @@ -17,7 +17,7 @@ libexprc_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libutilc) \ libexprc_LIBS = libutil libutilc libstore libstorec libexpr -libexprc_LDFLAGS += -pthread +libexprc_LDFLAGS += $(THREAD_LDFLAGS) $(eval $(call install-file-in, $(d)/nix-expr-c.pc, $(libdir)/pkgconfig, 0644)) diff --git a/src/libexpr-c/nix_api_expr.cc b/src/libexpr-c/nix_api_expr.cc index f18ef399b..a5c03d5aa 100644 --- a/src/libexpr-c/nix_api_expr.cc +++ b/src/libexpr-c/nix_api_expr.cc @@ -155,13 +155,13 @@ void nix_gc_now() } #else -void nix_gc_incref(nix_c_context * context, const void *) +nix_err nix_gc_incref(nix_c_context * context, const void *) { if (context) context->last_err_code = NIX_OK; return NIX_OK; } -void nix_gc_decref(nix_c_context * context, const void *) +nix_err nix_gc_decref(nix_c_context * context, const void *) { if (context) context->last_err_code = NIX_OK; diff --git a/src/libexpr-c/nix_api_external.cc b/src/libexpr-c/nix_api_external.cc index c237cfb70..3c3dd6ca9 100644 --- a/src/libexpr-c/nix_api_external.cc +++ b/src/libexpr-c/nix_api_external.cc @@ -1,7 +1,6 @@ #include "attr-set.hh" #include "config.hh" #include "eval.hh" -#include "gc/gc.h" #include "globals.hh" #include "value.hh" @@ -16,8 +15,9 @@ #include #ifdef HAVE_BOEHMGC -#define GC_INCLUDE_NEW 1 -#include "gc_cpp.h" +# include "gc/gc.h" +# define GC_INCLUDE_NEW 1 +# include "gc_cpp.h" #endif void nix_set_string_return(nix_string_return * str, const char * c) diff --git a/src/libexpr-c/nix_api_value.cc b/src/libexpr-c/nix_api_value.cc index 80e853b87..02bd154b3 100644 --- a/src/libexpr-c/nix_api_value.cc +++ b/src/libexpr-c/nix_api_value.cc @@ -1,7 +1,6 @@ #include "attr-set.hh" #include "config.hh" #include "eval.hh" -#include "gc/gc.h" #include "globals.hh" #include "primops.hh" #include "value.hh" @@ -13,8 +12,9 @@ #include "nix_api_value.h" #ifdef HAVE_BOEHMGC -#define GC_INCLUDE_NEW 1 -#include "gc_cpp.h" +# include "gc/gc.h" +# define GC_INCLUDE_NEW 1 +# include "gc_cpp.h" #endif // Helper function to throw an exception if value is null @@ -444,7 +444,7 @@ void nix_list_builder_free(ListBuilder * list_builder) #if HAVE_BOEHMGC GC_FREE(list_builder); #else - delete bb; + delete list_builder; #endif } diff --git a/src/libstore-c/local.mk b/src/libstore-c/local.mk index 36a8e77a4..5e3eff06a 100644 --- a/src/libstore-c/local.mk +++ b/src/libstore-c/local.mk @@ -8,7 +8,7 @@ libstorec_SOURCES := $(wildcard $(d)/*.cc) libstorec_LIBS = libutil libstore libutilc -libstorec_LDFLAGS += -pthread +libstorec_LDFLAGS += $(THREAD_LDFLAGS) # Not just for this library itself, but also for downstream libraries using this library diff --git a/src/libutil-c/local.mk b/src/libutil-c/local.mk index 342dc2d8b..f2df1ef43 100644 --- a/src/libutil-c/local.mk +++ b/src/libutil-c/local.mk @@ -13,6 +13,6 @@ libutilc_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libutilc) libutilc_LIBS = libutil -libutilc_LDFLAGS += -pthread +libutilc_LDFLAGS += $(THREAD_LDFLAGS) libutilc_FORCE_INSTALL := 1 diff --git a/tests/unit/libstore-support/tests/nix_api_store.hh b/tests/unit/libstore-support/tests/nix_api_store.hh index a8b60fbc3..a2d35d083 100644 --- a/tests/unit/libstore-support/tests/nix_api_store.hh +++ b/tests/unit/libstore-support/tests/nix_api_store.hh @@ -2,6 +2,8 @@ ///@file #include "tests/nix_api_util.hh" +#include "file-system.hh" + #include "nix_api_store.h" #include "nix_api_store_internal.h" @@ -37,8 +39,18 @@ public: protected: void init_local_store() { - auto tmpl = nix::getEnv("TMPDIR").value_or("/tmp") + "/tests_nix-store.XXXXXX"; +#ifdef _WIN32 + // no `mkdtemp` with MinGW + auto tmpl = nix::defaultTempDir() + "/tests_nix-store."; + for (size_t i = 0; true; ++i) { + nixDir = tmpl + std::string { i }; + if (fs::create_directory(nixDir)) break; + } +#else + auto tmpl = nix::defaultTempDir() + "/tests_nix-store.XXXXXX"; nixDir = mkdtemp((char *) tmpl.c_str()); +#endif + nixStoreDir = nixDir + "/my_nix_store"; // Options documented in `nix help-stores` From 50f621b24191fca6f4841c92d134e5945a77e512 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 4 Apr 2024 12:25:01 -0400 Subject: [PATCH 164/168] Better signals interface This avoids some CPP and accidentally using Unix stuff in client code. --- src/libcmd/repl.cc | 2 +- src/libfetchers/git-utils.cc | 4 +- src/libmain/shared.cc | 2 +- src/libstore/daemon.cc | 3 +- src/libstore/filetransfer.cc | 8 +-- src/libutil/current-process.cc | 2 +- src/libutil/signals.hh | 70 +++++++++++++++++++ src/libutil/thread-pool.cc | 2 +- src/libutil/unix/monitor-fd.hh | 2 +- .../unix/{signals.hh => signals-impl.hh} | 67 ++++++++++-------- src/libutil/unix/signals.cc | 23 +++--- 11 files changed, 134 insertions(+), 51 deletions(-) create mode 100644 src/libutil/signals.hh rename src/libutil/unix/{signals.hh => signals-impl.hh} (80%) diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 7cecc60b7..4f1bf4516 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -357,7 +357,7 @@ ProcessLineResult NixRepl::processLine(std::string line) if (line.empty()) return ProcessLineResult::PromptAgain; - _isInterrupted = false; + setInterrupted(false); std::string command, arg; diff --git a/src/libfetchers/git-utils.cc b/src/libfetchers/git-utils.cc index b723554cc..5e560f5f3 100644 --- a/src/libfetchers/git-utils.cc +++ b/src/libfetchers/git-utils.cc @@ -348,7 +348,7 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this { auto act = (Activity *) payload; act->result(resFetchStatus, trim(std::string_view(str, len))); - return _isInterrupted ? -1 : 0; + return getInterrupted() ? -1 : 0; } static int transferProgressCallback(const git_indexer_progress * stats, void * payload) @@ -361,7 +361,7 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this stats->indexed_deltas, stats->total_deltas, stats->received_bytes / (1024.0 * 1024.0))); - return _isInterrupted ? -1 : 0; + return getInterrupted() ? -1 : 0; } void fetch( diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index f3dd73101..4c9051d3b 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -121,7 +121,7 @@ void initNix() initLibStore(); - startSignalHandlerThread(); + unix::startSignalHandlerThread(); /* Reset SIGCHLD to its default. */ struct sigaction act; diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index 2c808015d..def2c80b2 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -1,5 +1,6 @@ #include "daemon.hh" #include "monitor-fd.hh" +#include "signals.hh" #include "worker-protocol.hh" #include "worker-protocol-impl.hh" #include "build-result.hh" @@ -1038,7 +1039,7 @@ void processConnection( unsigned int opCount = 0; Finally finally([&]() { - _isInterrupted = false; + setInterrupted(false); printMsgUsing(prevLogger, lvlDebug, "%d operations", opCount); }); diff --git a/src/libstore/filetransfer.cc b/src/libstore/filetransfer.cc index 0d5379d25..df89b5bd1 100644 --- a/src/libstore/filetransfer.cc +++ b/src/libstore/filetransfer.cc @@ -258,11 +258,11 @@ struct curlFileTransfer : public FileTransfer int progressCallback(double dltotal, double dlnow) { try { - act.progress(dlnow, dltotal); + act.progress(dlnow, dltotal); } catch (nix::Interrupted &) { - assert(_isInterrupted); + assert(getInterrupted()); } - return _isInterrupted; + return getInterrupted(); } static int progressCallbackWrapper(void * userp, double dltotal, double dlnow, double ultotal, double ulnow) @@ -466,7 +466,7 @@ struct curlFileTransfer : public FileTransfer if (errorSink) response = std::move(errorSink->s); auto exc = - code == CURLE_ABORTED_BY_CALLBACK && _isInterrupted + code == CURLE_ABORTED_BY_CALLBACK && getInterrupted() ? FileTransferError(Interrupted, std::move(response), "%s of '%s' was interrupted", request.verb(), request.uri) : httpStatus != 0 ? FileTransferError(err, diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index c13e6d567..d33f7163a 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -82,7 +82,7 @@ void setStackSize(rlim_t stackSize) void restoreProcessContext(bool restoreMounts) { - restoreSignals(); + unix::restoreSignals(); if (restoreMounts) { #if __linux__ restoreMountNamespace(); diff --git a/src/libutil/signals.hh b/src/libutil/signals.hh new file mode 100644 index 000000000..0473128db --- /dev/null +++ b/src/libutil/signals.hh @@ -0,0 +1,70 @@ +#pragma once +///@file + +#include "types.hh" +#include "error.hh" +#include "logging.hh" + +#include + +namespace nix { + +/* User interruption. */ + +/** + * @note Does nothing on Windows + */ +static inline void setInterrupted(bool isInterrupted); + +/** + * @note Does nothing on Windows + */ +static inline bool getInterrupted(); + +/** + * @note Does nothing on Windows + */ +static inline void setInterruptCheck(std::function interruptCheck); + +/** + * @note Does nothing on Windows + */ +void setInterruptThrown(); + +/** + * @note Does nothing on Windows + */ +inline void checkInterrupt(); + +/** + * @note Never will happen on Windows + */ +MakeError(Interrupted, BaseError); + + +struct InterruptCallback +{ + virtual ~InterruptCallback() { }; +}; + +/** + * Register a function that gets called on SIGINT (in a non-signal + * context). + * + * @note Does nothing on Windows + */ +std::unique_ptr createInterruptCallback( + std::function callback); + +/** + * A RAII class that causes the current thread to receive SIGUSR1 when + * the signal handler thread receives SIGINT. That is, this allows + * SIGINT to be multiplexed to multiple threads. + * + * @note Does nothing on Windows + */ +struct ReceiveInterrupts; + +} + +#include "signals-impl.hh" diff --git a/src/libutil/thread-pool.cc b/src/libutil/thread-pool.cc index 9a7dfee56..805f31d80 100644 --- a/src/libutil/thread-pool.cc +++ b/src/libutil/thread-pool.cc @@ -82,7 +82,7 @@ void ThreadPool::doWork(bool mainThread) ReceiveInterrupts receiveInterrupts; if (!mainThread) - interruptCheck = [&]() { return (bool) quit; }; + unix::interruptCheck = [&]() { return (bool) quit; }; bool didWork = false; std::exception_ptr exc; diff --git a/src/libutil/unix/monitor-fd.hh b/src/libutil/unix/monitor-fd.hh index 228fb13f8..103894de9 100644 --- a/src/libutil/unix/monitor-fd.hh +++ b/src/libutil/unix/monitor-fd.hh @@ -50,7 +50,7 @@ public: */ if (count == 0) continue; if (fds[0].revents & POLLHUP) { - triggerInterrupt(); + unix::triggerInterrupt(); break; } /* This will only happen on macOS. We sleep a bit to diff --git a/src/libutil/unix/signals.hh b/src/libutil/unix/signals-impl.hh similarity index 80% rename from src/libutil/unix/signals.hh rename to src/libutil/unix/signals-impl.hh index 7e8beff33..7ac8c914d 100644 --- a/src/libutil/unix/signals.hh +++ b/src/libutil/unix/signals-impl.hh @@ -1,5 +1,14 @@ #pragma once -///@file +/** + * @file + * + * Implementation of some inline definitions for Unix signals, and also + * some extra Unix-only interfaces. + * + * (The only reason everything about signals isn't Unix-only is some + * no-op definitions are provided on Windows to avoid excess CPP in + * downstream code.) + */ #include "types.hh" #include "error.hh" @@ -24,22 +33,20 @@ namespace nix { /* User interruption. */ +namespace unix { + extern std::atomic _isInterrupted; extern thread_local std::function interruptCheck; -void setInterruptThrown(); - void _interrupted(); -void inline checkInterrupt() -{ - if (_isInterrupted || (interruptCheck && interruptCheck())) - _interrupted(); -} - -MakeError(Interrupted, BaseError); - +/** + * Sets the signal mask. Like saveSignalMask() but for a signal set that doesn't + * necessarily match the current thread's mask. + * See saveSignalMask() to set the saved mask to the current mask. + */ +void setChildSignalMask(sigset_t *sigs); /** * Start a thread that handles various signals. Also block those signals @@ -63,27 +70,27 @@ void saveSignalMask(); */ void restoreSignals(); -/** - * Sets the signal mask. Like saveSignalMask() but for a signal set that doesn't - * necessarily match the current thread's mask. - * See saveSignalMask() to set the saved mask to the current mask. - */ -void setChildSignalMask(sigset_t *sigs); - -struct InterruptCallback -{ - virtual ~InterruptCallback() { }; -}; - -/** - * Register a function that gets called on SIGINT (in a non-signal - * context). - */ -std::unique_ptr createInterruptCallback( - std::function callback); - void triggerInterrupt(); +} + +static inline void setInterrupted(bool isInterrupted) +{ + unix::_isInterrupted = isInterrupted; +} + +static inline bool getInterrupted() +{ + return unix::_isInterrupted; +} + +void inline checkInterrupt() +{ + using namespace unix; + if (_isInterrupted || (interruptCheck && interruptCheck())) + _interrupted(); +} + /** * A RAII class that causes the current thread to receive SIGUSR1 when * the signal handler thread receives SIGINT. That is, this allows diff --git a/src/libutil/unix/signals.cc b/src/libutil/unix/signals.cc index eaa4ea30e..7e30687d8 100644 --- a/src/libutil/unix/signals.cc +++ b/src/libutil/unix/signals.cc @@ -8,17 +8,22 @@ namespace nix { -std::atomic _isInterrupted = false; +using namespace unix; +std::atomic unix::_isInterrupted = false; + +namespace unix { static thread_local bool interruptThrown = false; -thread_local std::function interruptCheck; +} + +thread_local std::function unix::interruptCheck; void setInterruptThrown() { - interruptThrown = true; + unix::interruptThrown = true; } -void _interrupted() +void unix::_interrupted() { /* Block user interrupts while an exception is being handled. Throwing an exception while another exception is being handled @@ -65,7 +70,7 @@ static void signalHandlerThread(sigset_t set) } } -void triggerInterrupt() +void unix::triggerInterrupt() { _isInterrupted = true; @@ -96,7 +101,7 @@ void triggerInterrupt() static sigset_t savedSignalMask; static bool savedSignalMaskIsSet = false; -void setChildSignalMask(sigset_t * sigs) +void unix::setChildSignalMask(sigset_t * sigs) { assert(sigs); // C style function, but think of sigs as a reference @@ -115,14 +120,14 @@ void setChildSignalMask(sigset_t * sigs) savedSignalMaskIsSet = true; } -void saveSignalMask() { +void unix::saveSignalMask() { if (sigprocmask(SIG_BLOCK, nullptr, &savedSignalMask)) throw SysError("querying signal mask"); savedSignalMaskIsSet = true; } -void startSignalHandlerThread() +void unix::startSignalHandlerThread() { updateWindowSize(); @@ -141,7 +146,7 @@ void startSignalHandlerThread() std::thread(signalHandlerThread, set).detach(); } -void restoreSignals() +void unix::restoreSignals() { // If startSignalHandlerThread wasn't called, that means we're not running // in a proper libmain process, but a process that presumably manages its From 25584e215e8861ffe5cd6afc01995987f0629190 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 5 Apr 2024 12:03:53 +0200 Subject: [PATCH 165/168] fix: Remove duplicate imports from Makefile --- Makefile | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 9dc005f79..62194278d 100644 --- a/Makefile +++ b/Makefile @@ -28,10 +28,7 @@ makefiles = \ misc/zsh/local.mk \ misc/systemd/local.mk \ misc/launchd/local.mk \ - misc/upstart/local.mk \ - doc/manual/local.mk \ - doc/internal-api/local.mk \ - doc/external-api/local.mk + misc/upstart/local.mk endif ifeq ($(ENABLE_UNIT_TESTS), yes) From 75be3f23c69870946daf7d8bbe87b641f39a9e28 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 5 Apr 2024 16:03:25 +0200 Subject: [PATCH 166/168] setInterruptCheck(): Remove declared but undefined function --- src/libutil/signals.hh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libutil/signals.hh b/src/libutil/signals.hh index 0473128db..8bff345c3 100644 --- a/src/libutil/signals.hh +++ b/src/libutil/signals.hh @@ -21,11 +21,6 @@ static inline void setInterrupted(bool isInterrupted); */ static inline bool getInterrupted(); -/** - * @note Does nothing on Windows - */ -static inline void setInterruptCheck(std::function interruptCheck); - /** * @note Does nothing on Windows */ From a3d5a71c5fad25c6f16b97986e5b7a66accff205 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 5 Apr 2024 14:10:28 -0400 Subject: [PATCH 167/168] Slight cleanup of `builtins.derivation` `outputHashAlgo` logic (#10417) This was part of approved PR #10021. Unfortunately that one is stalled on a peculiar Linux test timeout, so trying to get bits of it merged first to bisect failure. --- src/libexpr/primops.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 752176178..8e79f4953 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1127,7 +1127,7 @@ drvName, Bindings * attrs, Value & v) bool contentAddressed = false; bool isImpure = false; std::optional outputHash; - std::string outputHashAlgo; + std::optional outputHashAlgo; std::optional ingestionMethod; StringSet outputs; @@ -1226,7 +1226,7 @@ drvName, Bindings * attrs, Value & v) else if (i->name == state.sOutputHash) outputHash = state.forceStringNoCtx(*i->value, pos, context_below); else if (i->name == state.sOutputHashAlgo) - outputHashAlgo = state.forceStringNoCtx(*i->value, pos, context_below); + outputHashAlgo = parseHashAlgoOpt(state.forceStringNoCtx(*i->value, pos, context_below)); else if (i->name == state.sOutputHashMode) handleHashMode(state.forceStringNoCtx(*i->value, pos, context_below)); else if (i->name == state.sOutputs) { @@ -1244,7 +1244,7 @@ drvName, Bindings * attrs, Value & v) if (i->name == state.sBuilder) drv.builder = std::move(s); else if (i->name == state.sSystem) drv.platform = std::move(s); else if (i->name == state.sOutputHash) outputHash = std::move(s); - else if (i->name == state.sOutputHashAlgo) outputHashAlgo = std::move(s); + else if (i->name == state.sOutputHashAlgo) outputHashAlgo = parseHashAlgoOpt(s); else if (i->name == state.sOutputHashMode) handleHashMode(s); else if (i->name == state.sOutputs) handleOutputs(tokenizeString(s)); @@ -1327,7 +1327,7 @@ drvName, Bindings * attrs, Value & v) "multiple outputs are not supported in fixed-output derivations" ).atPos(v).debugThrow(); - auto h = newHashAllowEmpty(*outputHash, parseHashAlgoOpt(outputHashAlgo)); + auto h = newHashAllowEmpty(*outputHash, outputHashAlgo); auto method = ingestionMethod.value_or(FileIngestionMethod::Flat); @@ -1347,7 +1347,7 @@ drvName, Bindings * attrs, Value & v) state.error("derivation cannot be both content-addressed and impure") .atPos(v).debugThrow(); - auto ha = parseHashAlgoOpt(outputHashAlgo).value_or(HashAlgorithm::SHA256); + auto ha = outputHashAlgo.value_or(HashAlgorithm::SHA256); auto method = ingestionMethod.value_or(FileIngestionMethod::Recursive); for (auto & i : outputs) { From 5a365b0c891b88fc9ade9faa7084ff6ed20c5b59 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 5 Apr 2024 14:31:43 -0400 Subject: [PATCH 168/168] Delete dead `openFile` in `binary-cache-store.cc` (#10418) d64cb33e90a5d178222c4e8e3f49d44c33fd93ae / #5111 previously deleted the dead code where this was used, but missed this. --- src/libstore/binary-cache-store.cc | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc index bea2bb370..97b6ec052 100644 --- a/src/libstore/binary-cache-store.cc +++ b/src/libstore/binary-cache-store.cc @@ -124,14 +124,6 @@ void BinaryCacheStore::writeNarInfo(ref narInfo) diskCache->upsertNarInfo(getUri(), std::string(narInfo->path.hashPart()), std::shared_ptr(narInfo)); } -AutoCloseFD openFile(const Path & path) -{ - auto fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); - if (!fd) - throw SysError("opening file '%1%'", path); - return fd; -} - ref BinaryCacheStore::addToStoreCommon( Source & narSource, RepairFlag repair, CheckSigsFlag checkSigs, std::function mkInfo)