From d12d69ea1a871d631d77c8ef5e8468b4a2bff80f Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <edolstra@gmail.com>
Date: Fri, 22 Nov 2019 23:07:35 +0100
Subject: [PATCH] Turn NIX_PATH into a config setting

This allows it to be set in nix.conf.
---
 src/libexpr/eval.cc | 31 +++++++++++++++++--------------
 src/libexpr/eval.hh |  7 +++++++
 2 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index 65466a0b4..62812f90a 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -275,17 +275,6 @@ static Strings parseNixPath(const string & s)
 }
 
 
-static Strings getDefaultNixPath()
-{
-    Strings res;
-    auto add = [&](const Path & p) { if (pathExists(p)) { res.push_back(p); } };
-    add(getHome() + "/.nix-defexpr/channels");
-    add("nixpkgs=" + settings.nixStateDir + "/nix/profiles/per-user/root/channels/nixpkgs");
-    add(settings.nixStateDir + "/nix/profiles/per-user/root/channels");
-    return res;
-}
-
-
 EvalState::EvalState(const Strings & _searchPath, ref<Store> store)
     : sWith(symbols.create("<with>"))
     , sOutPath(symbols.create("outPath"))
@@ -325,10 +314,8 @@ EvalState::EvalState(const Strings & _searchPath, ref<Store> store)
 
     /* Initialise the Nix expression search path. */
     if (!evalSettings.pureEval) {
-        auto nixPath = getEnv("NIX_PATH");
-        auto paths = nixPath ? parseNixPath(*nixPath) : getDefaultNixPath();
         for (auto & i : _searchPath) addToSearchPath(i);
-        for (auto & i : paths) addToSearchPath(i);
+        for (auto & i : evalSettings.nixPath.get()) addToSearchPath(i);
     }
     addToSearchPath("nix=" + canonPath(settings.nixDataDir + "/nix/corepkgs", true));
 
@@ -1986,6 +1973,22 @@ std::ostream & operator << (std::ostream & str, const ExternalValueBase & v) {
 }
 
 
+EvalSettings::EvalSettings()
+{
+    auto var = getEnv("NIX_PATH");
+    if (var) nixPath = parseNixPath(*var);
+}
+
+Strings EvalSettings::getDefaultNixPath()
+{
+    Strings res;
+    auto add = [&](const Path & p) { if (pathExists(p)) { res.push_back(p); } };
+    add(getHome() + "/.nix-defexpr/channels");
+    add("nixpkgs=" + settings.nixStateDir + "/nix/profiles/per-user/root/channels/nixpkgs");
+    add(settings.nixStateDir + "/nix/profiles/per-user/root/channels");
+    return res;
+}
+
 EvalSettings evalSettings;
 
 static GlobalConfig::Register r1(&evalSettings);
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index 61ee4a73b..9d075a48a 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -339,9 +339,16 @@ struct InvalidPathError : EvalError
 
 struct EvalSettings : Config
 {
+    EvalSettings();
+
+    static Strings getDefaultNixPath();
+
     Setting<bool> enableNativeCode{this, false, "allow-unsafe-native-code-during-evaluation",
         "Whether builtin functions that allow executing native code should be enabled."};
 
+    Setting<Strings> nixPath{this, getDefaultNixPath(), "nix-path",
+        "List of directories to be searched for <...> file references."};
+
     Setting<bool> restrictEval{this, false, "restrict-eval",
         "Whether to restrict file system access to paths in $NIX_PATH, "
         "and network access to the URI prefixes listed in 'allowed-uris'."};
-- 
GitLab