diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh
index 8a2d3ff755721cc36e7a8e16b4a7446b7bacc888..02721285ae30b03dcf7dcb5ef131d834bb508ef8 100644
--- a/src/libstore/globals.hh
+++ b/src/libstore/globals.hh
@@ -2,6 +2,7 @@
 
 #include "types.hh"
 #include "config.hh"
+#include "abstractsettingtojson.hh"
 #include "util.hh"
 
 #include <map>
diff --git a/src/libutil/abstractsettingtojson.hh b/src/libutil/abstractsettingtojson.hh
new file mode 100644
index 0000000000000000000000000000000000000000..b3fbc84f76ac054ebcf0825b6c45e62e11d9ada7
--- /dev/null
+++ b/src/libutil/abstractsettingtojson.hh
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <nlohmann/json.hpp>
+#include "config.hh"
+
+namespace nix {
+template<typename T>
+std::map<std::string, nlohmann::json> BaseSetting<T>::toJSONObject()
+{
+    auto obj = AbstractSetting::toJSONObject();
+    obj.emplace("value", value);
+    obj.emplace("defaultValue", defaultValue);
+    return obj;
+}
+}
diff --git a/src/libutil/config.cc b/src/libutil/config.cc
index faa5cdbeb5ea3da23b71e06d94ef682f05584afb..309d23b40a5dedb9bda66c74e0f124ce3d057cd1 100644
--- a/src/libutil/config.cc
+++ b/src/libutil/config.cc
@@ -1,5 +1,6 @@
 #include "config.hh"
 #include "args.hh"
+#include "abstractsettingtojson.hh"
 
 #include <nlohmann/json.hpp>
 
diff --git a/src/libutil/config.hh b/src/libutil/config.hh
index 18fa9dea80995ff45370740fa5b5397aecaea83d..1f5f4e7b9dfb77877e3f9bdebc9bc0bd9bbb8a98 100644
--- a/src/libutil/config.hh
+++ b/src/libutil/config.hh
@@ -4,7 +4,7 @@
 
 #include "types.hh"
 
-#include <nlohmann/json.hpp>
+#include <nlohmann/json_fwd.hpp>
 
 #pragma once
 
@@ -255,13 +255,7 @@ public:
 
     void convertToArg(Args & args, const std::string & category) override;
 
-    std::map<std::string, nlohmann::json> toJSONObject() override
-    {
-        auto obj = AbstractSetting::toJSONObject();
-        obj.emplace("value", value);
-        obj.emplace("defaultValue", defaultValue);
-        return obj;
-    }
+    std::map<std::string, nlohmann::json> toJSONObject() override;
 };
 
 template<typename T>