From 3f9e7107d17a4b5fb81c07e7f06383a66da63437 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Sun, 19 May 2024 12:57:14 -0600 Subject: [PATCH 1/7] repl: log errors writing to history file These errors are now logged and explicitly ignored, rather than implicitly ignored. Change-Id: Ia26015466a17f2b11952df5317a4d150d79dc184 --- src/libcmd/repl-interacter.cc | 18 ++++++++++++++++-- src/libcmd/repl-interacter.hh | 5 +++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/libcmd/repl-interacter.cc b/src/libcmd/repl-interacter.cc index d3567e021..4696bd51a 100644 --- a/src/libcmd/repl-interacter.cc +++ b/src/libcmd/repl-interacter.cc @@ -1,6 +1,8 @@ #include #include #include +#include +#include #ifdef READLINE #include @@ -176,15 +178,27 @@ bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptT if (!s) return false; - write_history(historyFile.c_str()); + this->writeHistory(); input += s; input += '\n'; return true; } +void ReadlineLikeInteracter::writeHistory() +{ + if (write_history(historyFile.c_str()) != 0) { + // write_history returns the result of fclose() (which also flushes). + // We should explicitly ignore these errors, but log them so the user + // isn't confused why their history is getting eaten. + int const fcloseErr = errno; + std::string_view const errMsg(std::strerror(fcloseErr)); + warn("ignoring error writing repl history to %s: %s", this->historyFile, errMsg); + } +} + ReadlineLikeInteracter::~ReadlineLikeInteracter() { - write_history(historyFile.c_str()); + this->writeHistory(); } AutomationInteracter::Guard AutomationInteracter::init(detail::ReplCompleterMixin *) diff --git a/src/libcmd/repl-interacter.hh b/src/libcmd/repl-interacter.hh index c31b1a1e6..8f815fceb 100644 --- a/src/libcmd/repl-interacter.hh +++ b/src/libcmd/repl-interacter.hh @@ -42,6 +42,11 @@ public: } virtual Guard init(detail::ReplCompleterMixin * repl) override; virtual bool getLine(std::string & input, ReplPromptType promptType) override; + /** Writes the current history to the history file. + * + * This function logs but ignores errors from readline's write_history(). + */ + virtual void writeHistory(); virtual ~ReadlineLikeInteracter() override; }; From 40fb07c31207de3e2befc5aac2c233073e37a179 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Tue, 21 May 2024 12:10:24 -0600 Subject: [PATCH 2/7] add libcmd test for lookupFileArg Change-Id: I9e2ef170ffe916f902daec8b5630d29434c5d5f2 --- tests/unit/libcmd/args.cc | 57 +++++++++++++++++++++++++++++++++++++++ tests/unit/meson.build | 27 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/unit/libcmd/args.cc diff --git a/tests/unit/libcmd/args.cc b/tests/unit/libcmd/args.cc new file mode 100644 index 000000000..da7ad598e --- /dev/null +++ b/tests/unit/libcmd/args.cc @@ -0,0 +1,57 @@ +#include +#include +#include + +#include +#include + +#include "common-eval-args.hh" +#include "eval.hh" +#include "filetransfer.hh" +#include "shared.hh" +#include "store-api.hh" +#include "util.hh" + +constexpr std::string_view INVALID_CHANNEL = "channel:example"; +constexpr std::string_view CHANNEL_URL = "https://nixos.org/channels/example/nixexprs.tar.xz"; + +namespace nix +{ + +TEST(Arguments, lookupFileArg) { + initNix(); + initGC(); + + std::string const unitDataPath = getEnv("_NIX_TEST_UNIT_DATA").value(); + // Meson should be allowed to pass us a relative path here tbh. + auto const canonDataPath = CanonPath::fromCwd(unitDataPath); + + std::string const searchPathElem = fmt("example=%s", unitDataPath); + + SearchPath searchPath; + searchPath.elements.push_back(SearchPath::Elem::parse(searchPathElem)); + + auto store = openStore("dummy://"); + auto statePtr = std::make_shared(searchPath, store, store); + auto & state = *statePtr; + + SourcePath const foundUnitData = lookupFileArg(state, ""); + EXPECT_EQ(foundUnitData.path, canonDataPath); + + // lookupFileArg should not resolve if anything else is before or after it. + SourcePath const yepEvenSpaces = lookupFileArg(state, " "); + EXPECT_EQ(yepEvenSpaces.path, CanonPath::fromCwd(" ")); + EXPECT_EQ(lookupFileArg(state, "/nixos").path, CanonPath::fromCwd("/nixos")); + + try { + lookupFileArg(state, INVALID_CHANNEL); + } catch (FileTransferError const & ex) { + std::string_view msg(ex.what()); + EXPECT_NE(msg.find(CHANNEL_URL), msg.npos); + } + + SourcePath const normalFile = lookupFileArg(state, unitDataPath); + EXPECT_EQ(normalFile.path, canonDataPath); +} + +} diff --git a/tests/unit/meson.build b/tests/unit/meson.build index f5355cce8..c53677d48 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -212,3 +212,30 @@ test( protocol : 'gtest', verbose : true, ) + +libcmd_tester = executable( + 'liblixcmd-tests', + files('libcmd/args.cc'), + dependencies : [ + liblixcmd, + liblixutil, + liblixmain, + liblixexpr, + liblixstore, + gtest, + boost, + ], +) + +test( + 'libcmd-eval-args', + libcmd_tester, + args : [tests_args, 'TestEvalArgs'], + env : { + # No special meaning here, it's just a file laying around that is unlikely to go anywhere + # any time soon. + '_NIX_TEST_UNIT_DATA': meson.project_source_root() / 'src/nix-env/buildenv.nix', + }, + suite : 'check', + protocol : 'gtest', +) From b67a38a55efb634e0343f4c29ffd2551ef18062a Mon Sep 17 00:00:00 2001 From: Qyriad Date: Mon, 20 May 2024 13:16:09 -0600 Subject: [PATCH 3/7] add docstring to lookupFileArg Change-Id: Ifc149764f5a15725d3d630677c6da29def4b0f3e --- src/libcmd/common-eval-args.hh | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/libcmd/common-eval-args.hh b/src/libcmd/common-eval-args.hh index 6359b2579..08a4b65e4 100644 --- a/src/libcmd/common-eval-args.hh +++ b/src/libcmd/common-eval-args.hh @@ -28,6 +28,26 @@ private: std::map autoArgs; }; -SourcePath lookupFileArg(EvalState & state, std::string_view s); +/** @brief Resolve an argument that is generally a file, but could be something that + * is easy to resolve to a file, like a or a tarball URL. + * + * In particular, this will resolve and fetch pseudo-URLs starting with + * @c channel:, flakerefs starting with @c flake:, and anything that + * @ref nix::fetchers::downloadTarball() can take. + * + * Non-absolute files are looked up relative to the current directory(?) + * FIXME: the process's current directory or EvalState's current directory? + * + * @param state The nix::EvalState to base settings, store, and nixPath from. + * + * @param fileArg The the path-ish to resolve. + * + * @return A nix::SourcePath to the resolved and fetched file. + * + * @exception nix::FileTransferError from nix::fetchers::downloadTarball(). Probably others. + * + * @exception nix::ThrownError for failed search path lookup. Probably others. + */ +SourcePath lookupFileArg(EvalState & state, std::string_view fileArg); } From 65317900fc4312ca056ec5005a9988d853d25538 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Mon, 20 May 2024 13:51:42 -0600 Subject: [PATCH 4/7] cleanup lookupFileArg Change-Id: I2acd56e7a542b12138f43c95af78fdd50e944619 --- src/libcmd/common-eval-args.cc | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/libcmd/common-eval-args.cc b/src/libcmd/common-eval-args.cc index 94a4b7922..9beea5aa2 100644 --- a/src/libcmd/common-eval-args.cc +++ b/src/libcmd/common-eval-args.cc @@ -164,28 +164,30 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state) return res.finish(); } -SourcePath lookupFileArg(EvalState & state, std::string_view s) +SourcePath lookupFileArg(EvalState & state, std::string_view fileArg) { - if (EvalSettings::isPseudoUrl(s)) { - auto storePath = fetchers::downloadTarball( - state.store, EvalSettings::resolvePseudoUrl(s), "source", false).tree.storePath; + if (EvalSettings::isPseudoUrl(fileArg)) { + auto const url = EvalSettings::resolvePseudoUrl(fileArg); + auto const downloaded = fetchers::downloadTarball( + state.store, + url, + /* name */ "source", + /* locked */ false + ); + StorePath const storePath = downloaded.tree.storePath; return state.rootPath(CanonPath(state.store->toRealPath(storePath))); - } - - else if (s.starts_with("flake:")) { + } else if (fileArg.starts_with("flake:")) { experimentalFeatureSettings.require(Xp::Flakes); - auto flakeRef = parseFlakeRef(std::string(s.substr(6)), {}, true, false); + static constexpr size_t FLAKE_LEN = std::string_view("flake:").size(); + auto flakeRef = parseFlakeRef(std::string(fileArg.substr(FLAKE_LEN)), {}, true, false); auto storePath = flakeRef.resolve(state.store).fetchTree(state.store).first.storePath; return state.rootPath(CanonPath(state.store->toRealPath(storePath))); - } - - else if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') { - Path p(s.substr(1, s.size() - 2)); + } else if (fileArg.size() > 2 && fileArg.at(0) == '<' && fileArg.at(fileArg.size() - 1) == '>') { + Path p(fileArg.substr(1, fileArg.size() - 2)); return state.findFile(p); + } else { + return state.rootPath(CanonPath::fromCwd(fileArg)); } - - else - return state.rootPath(CanonPath::fromCwd(s)); } } From e491f8029a3b183cfe2292c8f882c3d7b05bcaff Mon Sep 17 00:00:00 2001 From: Qyriad Date: Mon, 20 May 2024 20:54:00 -0600 Subject: [PATCH 5/7] build: make internal-api-docs PHONEY Change-Id: I3b0bcea30ee9a4830023ccc5bededf995e96cccc --- doc/internal-api/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/internal-api/meson.build b/doc/internal-api/meson.build index 35d8a0e5b..faa30f194 100644 --- a/doc/internal-api/meson.build +++ b/doc/internal-api/meson.build @@ -28,6 +28,7 @@ internal_api_docs = custom_target( output : 'html', install : true, install_dir : datadir / 'doc/nix/internal-api', + build_always_stale : true, ) alias_target('internal-api-html', internal_api_docs) From e7b08676053d246fdfd07ec4317ccbb7ba7add20 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Mon, 20 May 2024 21:58:27 -0600 Subject: [PATCH 6/7] docs: linkify nix3-build mention in nix-build.md Change-Id: I462a8cf0da42b5045ce84b48dc1841ecdccbb89e --- doc/manual/src/command-ref/nix-build.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/manual/src/command-ref/nix-build.md b/doc/manual/src/command-ref/nix-build.md index b548edf82..92e642ddd 100644 --- a/doc/manual/src/command-ref/nix-build.md +++ b/doc/manual/src/command-ref/nix-build.md @@ -14,9 +14,8 @@ # Disambiguation -This man page describes the command `nix-build`, which is distinct from `nix -build`. For documentation on the latter, run `nix build --help` or see `man -nix3-build`. +This man page describes the command [`nix-build`](./new-cli/nix3-build.md), which is distinct from `nix build`. +For documentation on the latter, run `nix build --help` or see `man nix3-build`. # Description From 13bf168d0e8a20a3546a79f748404a4dec10f975 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Mon, 20 May 2024 22:21:14 -0600 Subject: [PATCH 7/7] docs: document lookupFileArg syntax in nix-build Change-Id: Ib6d68594a16132805ba5d97526e16f7b3633117e --- doc/manual/src/command-ref/nix-build.md | 38 ++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/doc/manual/src/command-ref/nix-build.md b/doc/manual/src/command-ref/nix-build.md index 92e642ddd..5baa86e47 100644 --- a/doc/manual/src/command-ref/nix-build.md +++ b/doc/manual/src/command-ref/nix-build.md @@ -29,10 +29,40 @@ created (`result`, `result-2`, and so on). If no *paths* are specified, then `nix-build` will use `default.nix` in the current directory, if it exists. -If an element of *paths* starts with `http://` or `https://`, it is -interpreted as the URL of a tarball that will be downloaded and unpacked -to a temporary location. The tarball must include a single top-level -directory containing at least a file named `default.nix`. +## Path Syntax + +A given element of *paths* may take one of a few different forms, the first being a simple filesystem path, e.g. `nix-build /tmp/some-file.nix`. +It may also be a [search path](./env-common.html#env-NIX_PATH) like ``, which is convenient to use with `--attr`/`-A`: + +```console +$ nix-build '' -A firefox +``` + +(Note the quotation marks around ``, which will be necessary in most Unix shells.) + +If an element of *paths* starts with `http://` or `https://`, it is interpreted as the URL of a tarball which will be fetched and unpacked. +Lix will then `import` the unpacked directory, so these tarballs must include at least single top-level directory with a file called `default.nix` +For example, you could build from a specific version of Nixpkgs with something like: + +```console +$ nix-build "https://github.com/NixOS/nixpkgs/archive/refs/heads/release-23.11.tar.gz" -A firefox +``` + +If a path starts with `flake:`, the rest of the argument is interpreted as a [flakeref](./new-cli/nix3-flake.html#flake-references) (see `nix flake --help` or `man nix3-flake`), which requires the "flakes" experimental feature to be enabled. +Lix will fetch the flake, and then `import` its unpacked directory, so the flake must include a file called `default.nix`. +For example, the flake analogue to the above `nix-build` commands are: + +```console +$ nix-build flake:nixpkgs -A firefox +$ nix-build flake:github:NixOS/nixpkgs/release-23.11 -A firefox +``` + +Finally, for legacy reasons, if a path starts with `channel:`, the rest of the argument is interpreted as the name of a channel to fetch from `https://nixos.org/channels/$CHANNEL_NAME/nixexprs.tar.xz`. + +> **NOTE:**: any of the special syntaxes may always be disambiguated by prefixing the path. +> For example: a file in the current directory called `'` can be addressed as `./`, to escape the special interpretation. + +## Notes `nix-build` is essentially a wrapper around [`nix-instantiate`](nix-instantiate.md) (to translate a high-level Nix