diff --git a/local.mk b/local.mk
index b57e4b0610e8ba251b9c3a5d5147db4c7ba330a6..b1ca832a6828501753d53ad27a84d568438f5c6b 100644
--- a/local.mk
+++ b/local.mk
@@ -6,9 +6,11 @@ dist-files += configure config.h.in perl/configure
 
 clean-files += Makefile.config
 
-GLOBAL_CXXFLAGS += -I . -I src -I src/libutil -I src/libstore -I src/libfetchers -I src/libmain -I src/libexpr -I src/nix -Wno-deprecated-declarations
+GLOBAL_CXXFLAGS += -Wno-deprecated-declarations
 
 $(foreach i, config.h $(wildcard src/lib*/*.hh), \
   $(eval $(call install-file-in, $(i), $(includedir)/nix, 0644)))
 
 $(GCH) $(PCH): src/libutil/util.hh config.h
+
+GCH_CXXFLAGS = -I src/libutil
diff --git a/mk/precompiled-headers.mk b/mk/precompiled-headers.mk
index 1a727ba1b70d17aad489842ac04e05fd358e9b44..1c0452dc2a50f677cb136dd754af6e2bf8cfd317 100644
--- a/mk/precompiled-headers.mk
+++ b/mk/precompiled-headers.mk
@@ -8,14 +8,14 @@ GCH = $(buildprefix)precompiled-headers.h.gch
 $(GCH): precompiled-headers.h
 	@rm -f $@
 	@mkdir -p "$(dir $@)"
-	$(trace-gen) $(CXX) -x c++-header -o $@ $< $(GLOBAL_CXXFLAGS)
+	$(trace-gen) $(CXX) -x c++-header -o $@ $< $(GLOBAL_CXXFLAGS) $(GCH_CXXFLAGS)
 
 PCH = $(buildprefix)precompiled-headers.h.pch
 
 $(PCH): precompiled-headers.h
 	@rm -f $@
 	@mkdir -p "$(dir $@)"
-	$(trace-gen) $(CXX) -x c++-header -o $@ $< $(GLOBAL_CXXFLAGS)
+	$(trace-gen) $(CXX) -x c++-header -o $@ $< $(GLOBAL_CXXFLAGS) $(GCH_CXXFLAGS)
 
 clean-files += $(GCH) $(PCH)
 
diff --git a/src/build-remote/build-remote.cc b/src/build-remote/build-remote.cc
index 69d1c6f7e46730e68884cbea4d6b282812a2a1ca..00340b7872aaf92c9d252e312d3356943e6c2917 100644
--- a/src/build-remote/build-remote.cc
+++ b/src/build-remote/build-remote.cc
@@ -17,7 +17,7 @@
 #include "store-api.hh"
 #include "derivations.hh"
 #include "local-store.hh"
-#include "legacy.hh"
+#include "../nix/legacy.hh"
 
 using namespace nix;
 using std::cin;
diff --git a/src/libexpr/local.mk b/src/libexpr/local.mk
index 85cb51f34ba95c83c203f90b0afa3c27a7feb855..f9460e821adbc97fc9ddeb7805630af31db69d15 100644
--- a/src/libexpr/local.mk
+++ b/src/libexpr/local.mk
@@ -11,6 +11,8 @@ libexpr_SOURCES := \
   $(d)/lexer-tab.cc \
   $(d)/parser-tab.cc
 
+libexpr_CXXFLAGS += -I src/libutil -I src/libstore -I src/libfetchers -I src/libmain -I src/libexpr
+
 libexpr_LIBS = libutil libstore libfetchers libnixrust
 
 libexpr_LDFLAGS =
diff --git a/src/libexpr/primops/fromTOML.cc b/src/libexpr/primops/fromTOML.cc
index a84e569e944dbabca8158c882593941541ae19b1..c43324dbb51a3a570de6e6de93dc8dcb7a715acf 100644
--- a/src/libexpr/primops/fromTOML.cc
+++ b/src/libexpr/primops/fromTOML.cc
@@ -1,7 +1,7 @@
 #include "primops.hh"
 #include "eval-inline.hh"
 
-#include "cpptoml/cpptoml.h"
+#include "../../cpptoml/cpptoml.h"
 
 namespace nix {
 
diff --git a/src/libfetchers/local.mk b/src/libfetchers/local.mk
index 4f3d4e85a7196a75ee3081653918db05af254c12..d7143d8a60c4b4cc6f3005eec28b18cd0a07c9ff 100644
--- a/src/libfetchers/local.mk
+++ b/src/libfetchers/local.mk
@@ -6,4 +6,6 @@ libfetchers_DIR := $(d)
 
 libfetchers_SOURCES := $(wildcard $(d)/*.cc)
 
+libfetchers_CXXFLAGS += -I src/libutil -I src/libstore
+
 libfetchers_LIBS = libutil libstore libnixrust
diff --git a/src/libmain/local.mk b/src/libmain/local.mk
index 0c80f5a0a037b8a07bf149b1551e70f3b0e9d7ae..a8eed6c65fe1e408fd843fd9193d532b19926ef2 100644
--- a/src/libmain/local.mk
+++ b/src/libmain/local.mk
@@ -6,6 +6,8 @@ libmain_DIR := $(d)
 
 libmain_SOURCES := $(wildcard $(d)/*.cc)
 
+libmain_CXXFLAGS += -I src/libutil -I src/libstore
+
 libmain_LDFLAGS = $(OPENSSL_LIBS)
 
 libmain_LIBS = libstore libutil
diff --git a/src/libstore/local.mk b/src/libstore/local.mk
index e8cbe422cb8c38c8d0d4a09c89ecf7c4570b733d..b6d04834ae9895981c957779991579955506be76 100644
--- a/src/libstore/local.mk
+++ b/src/libstore/local.mk
@@ -31,7 +31,8 @@ ifeq ($(HAVE_SECCOMP), 1)
 	libstore_LDFLAGS += -lseccomp
 endif
 
-libstore_CXXFLAGS = \
+libstore_CXXFLAGS += \
+ -I src/libutil -I src/libstore \
  -DNIX_PREFIX=\"$(prefix)\" \
  -DNIX_STORE_DIR=\"$(storedir)\" \
  -DNIX_DATA_DIR=\"$(datadir)\" \
diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc
index 1bda159d027c1d0c52dcc72c8ed04eb6f072a226..0a058a31b495bc418d1c5c79711d7ee2b6ff2866 100755
--- a/src/nix-build/nix-build.cc
+++ b/src/nix-build/nix-build.cc
@@ -16,7 +16,7 @@
 #include "get-drvs.hh"
 #include "common-eval-args.hh"
 #include "attr-path.hh"
-#include "legacy.hh"
+#include "../nix/legacy.hh"
 
 using namespace nix;
 using namespace std::string_literals;
diff --git a/src/nix-channel/nix-channel.cc b/src/nix-channel/nix-channel.cc
index 17f7c149f6e0973daf2f735505fd7a397f53a77f..2a9defb4e7b41ef30f0fffd7ff383e1b82920343 100755
--- a/src/nix-channel/nix-channel.cc
+++ b/src/nix-channel/nix-channel.cc
@@ -2,7 +2,7 @@
 #include "globals.hh"
 #include "download.hh"
 #include "store-api.hh"
-#include "legacy.hh"
+#include "../nix/legacy.hh"
 #include "fetchers.hh"
 
 #include <fcntl.h>
diff --git a/src/nix-collect-garbage/nix-collect-garbage.cc b/src/nix-collect-garbage/nix-collect-garbage.cc
index d4060ac937fc5d9d1cf8df585e347a07adaa3fca..aa5ada3a62bffa5c4229c2d32cbcdd803e42b1ff 100644
--- a/src/nix-collect-garbage/nix-collect-garbage.cc
+++ b/src/nix-collect-garbage/nix-collect-garbage.cc
@@ -2,7 +2,7 @@
 #include "profiles.hh"
 #include "shared.hh"
 #include "globals.hh"
-#include "legacy.hh"
+#include "../nix/legacy.hh"
 
 #include <iostream>
 #include <cerrno>
diff --git a/src/nix-copy-closure/nix-copy-closure.cc b/src/nix-copy-closure/nix-copy-closure.cc
index f8703576042730cb86f0228c6c04f569c9fef1e3..b10184718d1467f26f73415c6e5c4bff2afbb815 100755
--- a/src/nix-copy-closure/nix-copy-closure.cc
+++ b/src/nix-copy-closure/nix-copy-closure.cc
@@ -1,6 +1,6 @@
 #include "shared.hh"
 #include "store-api.hh"
-#include "legacy.hh"
+#include "../nix/legacy.hh"
 
 using namespace nix;
 
diff --git a/src/nix-daemon/nix-daemon.cc b/src/nix-daemon/nix-daemon.cc
index 1348985619eb17d3ff0748b2732aaf823d434a1a..e68d1b1bed6ebccf650f6fbac1bdb49709078196 100644
--- a/src/nix-daemon/nix-daemon.cc
+++ b/src/nix-daemon/nix-daemon.cc
@@ -6,7 +6,7 @@
 #include "globals.hh"
 #include "derivations.hh"
 #include "finally.hh"
-#include "legacy.hh"
+#include "../nix/legacy.hh"
 #include "daemon.hh"
 
 #include <algorithm>
diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc
index e47b00acf3b1a6fd5b3a3c13186753daa48b8f8a..1a2bb42a3c239e60f0ebca14c31e0b52d8bb23fe 100644
--- a/src/nix-env/nix-env.cc
+++ b/src/nix-env/nix-env.cc
@@ -13,7 +13,7 @@
 #include "json.hh"
 #include "value-to-json.hh"
 #include "xml-writer.hh"
-#include "legacy.hh"
+#include "../nix/legacy.hh"
 
 #include <cerrno>
 #include <ctime>
diff --git a/src/nix-instantiate/nix-instantiate.cc b/src/nix-instantiate/nix-instantiate.cc
index 617d927a4c1f8459e585faf845f5cdcf0c61b065..6c99d11818065b5b423db0572e1cba30f13bf7ca 100644
--- a/src/nix-instantiate/nix-instantiate.cc
+++ b/src/nix-instantiate/nix-instantiate.cc
@@ -9,7 +9,7 @@
 #include "util.hh"
 #include "store-api.hh"
 #include "common-eval-args.hh"
-#include "legacy.hh"
+#include "../nix/legacy.hh"
 
 #include <map>
 #include <iostream>
diff --git a/src/nix-prefetch-url/nix-prefetch-url.cc b/src/nix-prefetch-url/nix-prefetch-url.cc
index 18ced94b1a792864b69791ec755c28d274b81153..2b9254659a7eace76fb036a0e17da1bdcae75744 100644
--- a/src/nix-prefetch-url/nix-prefetch-url.cc
+++ b/src/nix-prefetch-url/nix-prefetch-url.cc
@@ -6,9 +6,9 @@
 #include "eval-inline.hh"
 #include "common-eval-args.hh"
 #include "attr-path.hh"
-#include "legacy.hh"
 #include "finally.hh"
-#include "progress-bar.hh"
+#include "../nix/legacy.hh"
+#include "../nix/progress-bar.hh"
 #include "tarfile.hh"
 
 #include <iostream>
diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc
index 806ab756381e5930844845d6c69d9dd06ace3bce..fcc00175a996b4928b5c0424cedc0d41160999eb 100644
--- a/src/nix-store/nix-store.cc
+++ b/src/nix-store/nix-store.cc
@@ -9,7 +9,7 @@
 #include "util.hh"
 #include "worker-protocol.hh"
 #include "graphml.hh"
-#include "legacy.hh"
+#include "../nix/legacy.hh"
 
 #include <iostream>
 #include <algorithm>
diff --git a/src/nix/local.mk b/src/nix/local.mk
index 622f490196c6129bc56d8be8835ea68119259496..3fcd15dc6b78361fde124b7471705b3142dc7f06 100644
--- a/src/nix/local.mk
+++ b/src/nix/local.mk
@@ -15,6 +15,8 @@ nix_SOURCES := \
   $(wildcard src/nix-prefetch-url/*.cc) \
   $(wildcard src/nix-store/*.cc) \
 
+nix_CXXFLAGS += -I src/libutil -I src/libstore -I src/libfetchers -I src/libexpr -I src/libmain
+
 nix_LIBS = libexpr libmain libfetchers libstore libutil libnixrust
 
 nix_LDFLAGS = -pthread $(SODIUM_LIBS) $(EDITLINE_LIBS) $(BOOST_LDFLAGS) -lboost_context -lboost_thread -lboost_system
diff --git a/src/nix/profile.cc b/src/nix/profile.cc
index 1759b83a3f0aa2fa4e89fdeafb6fbfc96b319525..c6a4ddd34fa27a8a727ab772a5b621b4fe78567a 100644
--- a/src/nix/profile.cc
+++ b/src/nix/profile.cc
@@ -6,7 +6,7 @@
 #include "archive.hh"
 #include "builtins/buildenv.hh"
 #include "flake/flakeref.hh"
-#include "nix-env/user-env.hh"
+#include "../nix-env/user-env.hh"
 
 #include <nlohmann/json.hpp>
 #include <regex>
diff --git a/tests/plugins/local.mk b/tests/plugins/local.mk
index 1d2bac052fd2ae27a210bce89edb4dfd021ba573..82ad9940286a0393663867f33c55642293caecd0 100644
--- a/tests/plugins/local.mk
+++ b/tests/plugins/local.mk
@@ -7,3 +7,5 @@ libplugintest_SOURCES := $(d)/plugintest.cc
 libplugintest_ALLOW_UNDEFINED := 1
 
 libplugintest_EXCLUDE_FROM_LIBRARY_LIST := 1
+
+libplugintest_CXXFLAGS := -I src/libutil -I src/libexpr