From 432a3a18d2402cc13556b1045e5953e6ae237c59 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 12 Sep 2022 15:37:09 +0200 Subject: [PATCH] Move isUri() and resolveUri() out of filetransfer.cc These are purely related to NIX_PATH / -I command line parsing, so put them in libexpr. --- src/libcmd/common-eval-args.cc | 4 ++-- src/libexpr/eval.cc | 19 ++++++++++++++++++- src/libexpr/eval.hh | 4 ++++ src/libexpr/parser.y | 6 +++--- src/libexpr/primops/fetchTree.cc | 2 -- src/libstore/filetransfer.cc | 18 ------------------ src/libstore/filetransfer.hh | 5 ----- 7 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/libcmd/common-eval-args.cc b/src/libcmd/common-eval-args.cc index f2e7a8799..53d892530 100644 --- a/src/libcmd/common-eval-args.cc +++ b/src/libcmd/common-eval-args.cc @@ -93,9 +93,9 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state) SourcePath lookupFileArg(EvalState & state, std::string_view s) { - if (isUri(s)) { + if (EvalSettings::isPseudoUrl(s)) { auto storePath = fetchers::downloadTarball( - state.store, resolveUri(s), "source", false).first; + state.store, EvalSettings::resolvePseudoUrl(s), "source", false).first; auto accessor = makeStorePathAccessor(state.store, storePath); state.registerAccessor(accessor); return {accessor, CanonPath::root}; diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 8c2b680ec..e0bd4469c 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -405,7 +405,7 @@ static Strings parseNixPath(const std::string & s) } if (*p == ':') { - if (isUri(std::string(start2, s.end()))) { + if (EvalSettings::isPseudoUrl(std::string(start2, s.end()))) { ++p; while (p != s.end() && *p != ':') ++p; } @@ -2563,6 +2563,23 @@ Strings EvalSettings::getDefaultNixPath() return res; } +bool EvalSettings::isPseudoUrl(std::string_view s) +{ + if (s.compare(0, 8, "channel:") == 0) return true; + size_t pos = s.find("://"); + if (pos == std::string::npos) return false; + std::string scheme(s, 0, pos); + return scheme == "http" || scheme == "https" || scheme == "file" || scheme == "channel" || scheme == "git" || scheme == "s3" || scheme == "ssh"; +} + +std::string EvalSettings::resolvePseudoUrl(std::string_view url) +{ + if (hasPrefix(url, "channel:")) + return "https://nixos.org/channels/" + std::string(url.substr(8)) + "/nixexprs.tar.xz"; + else + return std::string(url); +} + EvalSettings evalSettings; static GlobalConfig::Register rEvalSettings(&evalSettings); diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index e7a219653..390b79617 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -602,6 +602,10 @@ struct EvalSettings : Config static Strings getDefaultNixPath(); + static bool isPseudoUrl(std::string_view s); + + static std::string resolvePseudoUrl(std::string_view url); + Setting enableNativeCode{this, false, "allow-unsafe-native-code-during-evaluation", "Whether builtin functions that allow executing native code should be enabled."}; diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index f119e8661..2d140d95d 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -776,17 +776,17 @@ SourcePath EvalState::findFile(SearchPath & searchPath, const std::string_view p } - std::optional EvalState::resolveSearchPathElem(const SearchPathElem & elem, bool initAccessControl) +std::optional EvalState::resolveSearchPathElem(const SearchPathElem & elem, bool initAccessControl) { auto i = searchPathResolved.find(elem.second); if (i != searchPathResolved.end()) return i->second; std::optional res; - if (isUri(elem.second)) { + if (EvalSettings::isPseudoUrl(elem.second)) { try { auto storePath = fetchers::downloadTarball( - store, resolveUri(elem.second), "source", false).first; + store, EvalSettings::resolvePseudoUrl(elem.second), "source", false).first; auto accessor = makeStorePathAccessor(store, storePath); registerAccessor(accessor); res.emplace(SourcePath {accessor, CanonPath::root}); diff --git a/src/libexpr/primops/fetchTree.cc b/src/libexpr/primops/fetchTree.cc index 458acddc7..3f4b07766 100644 --- a/src/libexpr/primops/fetchTree.cc +++ b/src/libexpr/primops/fetchTree.cc @@ -257,8 +257,6 @@ static void fetch(EvalState & state, const PosIdx pos, Value * * args, Value & v } else url = state.forceStringNoCtx(*args[0], pos); - url = resolveUri(*url); - state.checkURI(*url); if (name == "") diff --git a/src/libstore/filetransfer.cc b/src/libstore/filetransfer.cc index 506b676a4..5be37efdc 100644 --- a/src/libstore/filetransfer.cc +++ b/src/libstore/filetransfer.cc @@ -33,14 +33,6 @@ FileTransferSettings fileTransferSettings; static GlobalConfig::Register rFileTransferSettings(&fileTransferSettings); -std::string resolveUri(std::string_view uri) -{ - if (uri.compare(0, 8, "channel:") == 0) - return "https://nixos.org/channels/" + std::string(uri.substr(8)) + "/nixexprs.tar.xz"; - else - return std::string(uri); -} - struct curlFileTransfer : public FileTransfer { CURLM * curlm = 0; @@ -874,14 +866,4 @@ FileTransferError::FileTransferError(FileTransfer::Error error, std::optional response, const Args & ... args); }; -bool isUri(std::string_view s); - -/* Resolve deprecated 'channel:' URLs. */ -std::string resolveUri(std::string_view uri); - }