aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libexpr/eval.cc36
-rw-r--r--src/libexpr/eval.hh13
-rw-r--r--src/libexpr/flake/call-flake.nix20
-rw-r--r--src/libexpr/primops/fetchTree.cc12
-rw-r--r--src/libstore/globals.hh11
-rw-r--r--src/libutil/tests/url.cc33
-rw-r--r--src/libutil/url.cc17
-rw-r--r--src/libutil/url.hh1
8 files changed, 102 insertions, 41 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index 3e37c7f60..21fc4d0fe 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -519,7 +519,6 @@ EvalState::EvalState(
static_assert(sizeof(Env) <= 16, "environment must be <= 16 bytes");
/* Initialise the Nix expression search path. */
- evalSettings.nixPath.setDefault(evalSettings.getDefaultNixPath());
if (!evalSettings.pureEval) {
for (auto & i : _searchPath) addToSearchPath(i);
for (auto & i : evalSettings.nixPath.get()) addToSearchPath(i);
@@ -2473,35 +2472,30 @@ std::ostream & operator << (std::ostream & str, const ExternalValueBase & v) {
EvalSettings::EvalSettings()
{
+ auto var = getEnv("NIX_PATH");
+ if (var) nixPath = parseNixPath(*var);
}
-/* impure => NIX_PATH or a default path
- * restrict-eval => NIX_PATH
- * pure-eval => empty
- */
Strings EvalSettings::getDefaultNixPath()
{
- if (pureEval)
- return {};
-
- auto var = getEnv("NIX_PATH");
- if (var) {
- return parseNixPath(*var);
- } else if (restrictEval) {
- return {};
- } else {
- Strings res;
- auto add = [&](const Path & p, const std::optional<std::string> & s = std::nullopt) {
- if (pathExists(p))
- res.push_back(s ? *s + "=" + p : p);
- };
+ Strings res;
+ auto add = [&](const Path & p, const std::string & s = std::string()) {
+ if (pathExists(p)) {
+ if (s.empty()) {
+ res.push_back(p);
+ } else {
+ res.push_back(s + "=" + p);
+ }
+ }
+ };
+ if (!evalSettings.restrictEval && !evalSettings.pureEval) {
add(settings.useXDGBaseDirectories ? getStateDir() + "/nix/defexpr/channels" : getHome() + "/.nix-defexpr/channels");
add(settings.nixStateDir + "/profiles/per-user/root/channels/nixpkgs", "nixpkgs");
add(settings.nixStateDir + "/profiles/per-user/root/channels");
-
- return res;
}
+
+ return res;
}
bool EvalSettings::isPseudoUrl(std::string_view s)
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index 2340ef67b..e4d5906bd 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -570,7 +570,7 @@ struct EvalSettings : Config
{
EvalSettings();
- Strings getDefaultNixPath();
+ static Strings getDefaultNixPath();
static bool isPseudoUrl(std::string_view s);
@@ -580,15 +580,8 @@ struct EvalSettings : Config
"Whether builtin functions that allow executing native code should be enabled."};
Setting<Strings> nixPath{
- this, {}, "nix-path",
- R"(
- List of directories to be searched for `<...>` file references.
-
- If [pure evaluation](#conf-pure-eval) is disabled,
- this is initialised using the [`NIX_PATH`](@docroot@/command-ref/env-common.md#env-NIX_PATH)
- environment variable, or, if it is unset and [restricted evaluation](#conf-restrict-eval)
- is disabled, a default search path including the user's and `root`'s channels.
- )"};
+ this, getDefaultNixPath(), "nix-path",
+ "List of directories to be searched for `<...>` file references."};
Setting<bool> restrictEval{
this, false, "restrict-eval",
diff --git a/src/libexpr/flake/call-flake.nix b/src/libexpr/flake/call-flake.nix
index 8061db3df..4beb0b0fe 100644
--- a/src/libexpr/flake/call-flake.nix
+++ b/src/libexpr/flake/call-flake.nix
@@ -16,7 +16,9 @@ let
subdir = if key == lockFile.root then rootSubdir else node.locked.dir or "";
- flake = import (sourceInfo + (if subdir != "" then "/" else "") + subdir + "/flake.nix");
+ outPath = sourceInfo + ((if subdir == "" then "" else "/") + subdir);
+
+ flake = import (outPath + "/flake.nix");
inputs = builtins.mapAttrs
(inputName: inputSpec: allNodes.${resolveInput inputSpec})
@@ -43,7 +45,21 @@ let
outputs = flake.outputs (inputs // { self = result; });
- result = outputs // sourceInfo // { inherit inputs; inherit outputs; inherit sourceInfo; _type = "flake"; };
+ result =
+ outputs
+ # We add the sourceInfo attribute for its metadata, as they are
+ # relevant metadata for the flake. However, the outPath of the
+ # sourceInfo does not necessarily match the outPath of the flake,
+ # as the flake may be in a subdirectory of a source.
+ # This is shadowed in the next //
+ // sourceInfo
+ // {
+ # This shadows the sourceInfo.outPath
+ inherit outPath;
+
+ inherit inputs; inherit outputs; inherit sourceInfo; _type = "flake";
+ };
+
in
if node.flake or true then
assert builtins.isFunction flake.outputs;
diff --git a/src/libexpr/primops/fetchTree.cc b/src/libexpr/primops/fetchTree.cc
index 2e924c302..c9faf3ffb 100644
--- a/src/libexpr/primops/fetchTree.cc
+++ b/src/libexpr/primops/fetchTree.cc
@@ -4,6 +4,7 @@
#include "fetchers.hh"
#include "filetransfer.hh"
#include "registry.hh"
+#include "url.hh"
#include <ctime>
#include <iomanip>
@@ -68,7 +69,16 @@ void emitTreeAttrs(
std::string fixURI(std::string uri, EvalState & state, const std::string & defaultScheme = "file")
{
state.checkURI(uri);
- return uri.find("://") != std::string::npos ? uri : defaultScheme + "://" + uri;
+ if (uri.find("://") == std::string::npos) {
+ const auto p = ParsedURL {
+ .scheme = defaultScheme,
+ .authority = "",
+ .path = uri
+ };
+ return p.to_string();
+ } else {
+ return uri;
+ }
}
std::string fixURIForGit(std::string uri, EvalState & state)
diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh
index 0a4912f67..93086eaf8 100644
--- a/src/libstore/globals.hh
+++ b/src/libstore/globals.hh
@@ -201,7 +201,16 @@ public:
{"build-timeout"}};
PathSetting buildHook{this, true, "", "build-hook",
- "The path of the helper program that executes builds to remote machines."};
+ R"(
+ The path to the helper program that executes remote builds.
+
+ Nix communicates with the build hook over `stdio` using a custom protocol to request builds that cannot be performed directly by the Nix daemon.
+ The default value is the internal Nix binary that implements remote building.
+
+ > **Important**
+ >
+ > Change this setting only if you really know what you’re doing.
+ )"};
Setting<std::string> builders{
this, "@" + nixConfDir + "/machines", "builders",
diff --git a/src/libutil/tests/url.cc b/src/libutil/tests/url.cc
index e0c438b4d..a908631e6 100644
--- a/src/libutil/tests/url.cc
+++ b/src/libutil/tests/url.cc
@@ -302,4 +302,37 @@ namespace nix {
ASSERT_EQ(d, s);
}
+
+ /* ----------------------------------------------------------------------------
+ * percentEncode
+ * --------------------------------------------------------------------------*/
+
+ TEST(percentEncode, encodesUrlEncodedString) {
+ std::string s = percentEncode("==@==");
+ std::string d = "%3D%3D%40%3D%3D";
+ ASSERT_EQ(d, s);
+ }
+
+ TEST(percentEncode, keepArgument) {
+ std::string a = percentEncode("abd / def");
+ std::string b = percentEncode("abd / def", "/");
+ ASSERT_EQ(a, "abd%20%2F%20def");
+ ASSERT_EQ(b, "abd%20/%20def");
+ }
+
+ TEST(percentEncode, inverseOfDecode) {
+ std::string original = "%3D%3D%40%3D%3D";
+ std::string once = percentEncode(original);
+ std::string back = percentDecode(once);
+
+ ASSERT_EQ(back, original);
+ }
+
+ TEST(percentEncode, trailingPercent) {
+ std::string s = percentEncode("==@==%");
+ std::string d = "%3D%3D%40%3D%3D%25";
+
+ ASSERT_EQ(d, s);
+ }
+
}
diff --git a/src/libutil/url.cc b/src/libutil/url.cc
index 4e43455e1..9e44241ac 100644
--- a/src/libutil/url.cc
+++ b/src/libutil/url.cc
@@ -88,17 +88,22 @@ std::map<std::string, std::string> decodeQuery(const std::string & query)
return result;
}
-std::string percentEncode(std::string_view s)
+const static std::string allowedInQuery = ":@/?";
+const static std::string allowedInPath = ":@/";
+
+std::string percentEncode(std::string_view s, std::string_view keep)
{
std::string res;
for (auto & c : s)
+ // unreserved + keep
if ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
- || strchr("-._~!$&'()*+,;=:@", c))
+ || strchr("-._~", c)
+ || keep.find(c) != std::string::npos)
res += c;
else
- res += fmt("%%%02x", (unsigned int) c);
+ res += fmt("%%%02X", (unsigned int) c);
return res;
}
@@ -109,9 +114,9 @@ std::string encodeQuery(const std::map<std::string, std::string> & ss)
for (auto & [name, value] : ss) {
if (!first) res += '&';
first = false;
- res += percentEncode(name);
+ res += percentEncode(name, allowedInQuery);
res += '=';
- res += percentEncode(value);
+ res += percentEncode(value, allowedInQuery);
}
return res;
}
@@ -122,7 +127,7 @@ std::string ParsedURL::to_string() const
scheme
+ ":"
+ (authority ? "//" + *authority : "")
- + path
+ + percentEncode(path, allowedInPath)
+ (query.empty() ? "" : "?" + encodeQuery(query))
+ (fragment.empty() ? "" : "#" + percentEncode(fragment));
}
diff --git a/src/libutil/url.hh b/src/libutil/url.hh
index 2a9fb34c1..ddd673d65 100644
--- a/src/libutil/url.hh
+++ b/src/libutil/url.hh
@@ -22,6 +22,7 @@ struct ParsedURL
MakeError(BadURL, Error);
std::string percentDecode(std::string_view in);
+std::string percentEncode(std::string_view s, std::string_view keep="");
std::map<std::string, std::string> decodeQuery(const std::string & query);