diff --git a/CHANGELOG.md b/CHANGELOG.md
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,16 @@
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [0.8.6] - 2022-03-07
+
+### Fixed
+
+ - Build with newer Nix versions 2.5, 2.6
+
+### Added
+
+ - Improved conditional code support with `cabal-pkg-config-version-hook`
+
 ## [0.8.5]
 
 ### Added
@@ -478,6 +488,8 @@
 
 - Initial release
 
+[0.8.6]: https://github.com/hercules-ci/hercules-ci-agent/compare/hercules-ci-agent-0.8.5...hercules-ci-agent-0.8.6
+[0.8.5]: https://github.com/hercules-ci/hercules-ci-agent/compare/hercules-ci-agent-0.8.4...hercules-ci-agent-0.8.5
 [0.8.4]: https://github.com/hercules-ci/hercules-ci-agent/compare/hercules-ci-agent-0.8.3...hercules-ci-agent-0.8.4
 [0.8.3]: https://github.com/hercules-ci/hercules-ci-agent/compare/hercules-ci-agent-0.8.2...hercules-ci-agent-0.8.3
 [0.8.2]: https://github.com/hercules-ci/hercules-ci-agent/compare/hercules-ci-agent-0.8.1...hercules-ci-agent-0.8.2
diff --git a/Setup.hs b/Setup.hs
--- a/Setup.hs
+++ b/Setup.hs
@@ -1,3 +1,13 @@
+import Data.Function ((&))
+import Distribution.PkgConfigVersionHook as PV
 import Distribution.Simple
 
-main = defaultMain
+main :: IO ()
+main =
+  defaultMainWithHooks $
+    simpleUserHooks
+      & PV.addHook
+        (PV.mkSettings "nix-store")
+          { PV.macroName = "NIX",
+            PV.flagPrefixName = "nix"
+          }
diff --git a/cbits/nix-2.3/hercules-store.cxx b/cbits/nix-2.3/hercules-store.cxx
--- a/cbits/nix-2.3/hercules-store.cxx
+++ b/cbits/nix-2.3/hercules-store.cxx
@@ -7,7 +7,6 @@
 #include <nix/common-eval-args.hh>
 #include <nix/get-drvs.hh>
 #include <nix/derivations.hh>
-#include <nix/affinity.hh>
 #include <nix/globals.hh>
 
 #include "hercules-store.hh"
diff --git a/cbits/nix-2.3/hercules-store.hh b/cbits/nix-2.3/hercules-store.hh
--- a/cbits/nix-2.3/hercules-store.hh
+++ b/cbits/nix-2.3/hercules-store.hh
@@ -7,7 +7,6 @@
 #include <nix/common-eval-args.hh>
 #include <nix/get-drvs.hh>
 #include <nix/derivations.hh>
-#include <nix/affinity.hh>
 #include <nix/globals.hh>
 #include <nix-compat.hh>
 #include "HsFFI.h"
diff --git a/cbits/nix-2.4/hercules-store.cxx b/cbits/nix-2.4/hercules-store.cxx
--- a/cbits/nix-2.4/hercules-store.cxx
+++ b/cbits/nix-2.4/hercules-store.cxx
@@ -8,7 +8,6 @@
 #include <nix/common-eval-args.hh>
 #include <nix/get-drvs.hh>
 #include <nix/derivations.hh>
-#include <nix/affinity.hh>
 #include <nix/globals.hh>
 #include <nix/callback.hh>
 
@@ -82,13 +81,32 @@
 
 StorePath WrappingStore::addToStore(const string & name, const Path & srcPath,
       FileIngestionMethod method, HashType hashAlgo,
-      PathFilter & filter, RepairFlag repair) {
-  return wrappedStore->addToStore(name, srcPath, method, hashAlgo, filter, repair);
+      PathFilter & filter, RepairFlag repair
+#if NIX_IS_AT_LEAST(2,5,0)
+      , const StorePathSet & references
+#endif
+      ) {
+
+  return wrappedStore->addToStore(name, srcPath, method, hashAlgo, filter, repair
+#if NIX_IS_AT_LEAST(2,5,0)
+      , references
+#endif
+  );
+
 }
 
 StorePath WrappingStore::addToStoreFromDump(Source & dump, const string & name,
-      FileIngestionMethod method, HashType hashAlgo, RepairFlag repair) {
-  return wrappedStore->addToStoreFromDump(dump, name, method, hashAlgo, repair);
+      FileIngestionMethod method, HashType hashAlgo, RepairFlag repair
+#if NIX_IS_AT_LEAST(2,5,0)
+      , const StorePathSet & references
+#endif
+      ) {
+
+  return wrappedStore->addToStoreFromDump(dump, name, method, hashAlgo, repair
+#if NIX_IS_AT_LEAST(2,5,0)
+      , references
+#endif
+  );
 }
 
 StorePath WrappingStore::addTextToStore(const string & name, const string & s,
@@ -124,9 +142,11 @@
   wrappedStore->addIndirectRoot(path);
 }
 
+#if !NIX_IS_AT_LEAST(2,5,0)
 void WrappingStore::syncWithGC() {
   wrappedStore->syncWithGC();
 }
+#endif
 
 void WrappingStore::collectGarbage(const GCOptions& options,
                                    GCResults& results) {
@@ -166,7 +186,12 @@
                              downloadSize, narSize);
 }
 
-std::shared_ptr<std::string> WrappingStore::getBuildLog(const StorePath& path) {
+#if NIX_IS_AT_LEAST(2,6,0)
+std::optional<std::string>
+#else
+std::shared_ptr<std::string>
+#endif
+WrappingStore::getBuildLog(const StorePath& path) {
   return wrappedStore->getBuildLog(path);
 }
 
@@ -200,10 +225,16 @@
   return "wrapped " + wrappedStore->name();
 }
 
+#if NIX_IS_AT_LEAST(2,5,0)
+void HerculesStore::queryRealisationUncached(const DrvOutput &drvOutput,
+  Callback<std::shared_ptr<const Realisation>> callback) noexcept {
+  wrappedStore->queryRealisation(drvOutput, std::move(callback));
+}
+#else
 std::optional<const Realisation> HerculesStore::queryRealisation(const DrvOutput &drvOut) {
-  // TODO (ca-derivations) use?
   return wrappedStore->queryRealisation(drvOut);
 }
+#endif
 
 void HerculesStore::ensurePath(const StorePath& path) {
   /* We avoid asking substituters for paths, since
diff --git a/cbits/nix-2.4/hercules-store.hh b/cbits/nix-2.4/hercules-store.hh
--- a/cbits/nix-2.4/hercules-store.hh
+++ b/cbits/nix-2.4/hercules-store.hh
@@ -7,7 +7,6 @@
 #include <nix/common-eval-args.hh>
 #include <nix/get-drvs.hh>
 #include <nix/derivations.hh>
-#include <nix/affinity.hh>
 #include <nix/globals.hh>
 #include <nix-compat.hh>
 #include "HsFFI.h"
@@ -60,10 +59,18 @@
 
   virtual StorePath addToStore(const string & name, const Path & srcPath,
       FileIngestionMethod method = FileIngestionMethod::Recursive, HashType hashAlgo = htSHA256,
-      PathFilter & filter = defaultPathFilter, RepairFlag repair = NoRepair) override;
+      PathFilter & filter = defaultPathFilter, RepairFlag repair = NoRepair
+#if NIX_IS_AT_LEAST(2,5,0)
+      , const StorePathSet & references = StorePathSet()
+#endif
+      ) override;
 
   virtual StorePath addToStoreFromDump(Source & dump, const string & name,
-      FileIngestionMethod method = FileIngestionMethod::Recursive, HashType hashAlgo = htSHA256, RepairFlag repair = NoRepair) override;
+      FileIngestionMethod method = FileIngestionMethod::Recursive, HashType hashAlgo = htSHA256, RepairFlag repair = NoRepair
+#if NIX_IS_AT_LEAST(2,5,0)
+      , const StorePathSet & references = StorePathSet()
+#endif
+      ) override;
 
   virtual StorePath addTextToStore(const string & name, const string & s,
       const StorePathSet & references, RepairFlag repair = NoRepair) override;
@@ -84,7 +91,9 @@
 
   virtual void addIndirectRoot(const Path & path) override;
 
+#if !NIX_IS_AT_LEAST(2,5,0)
   virtual void syncWithGC() override;
+#endif
 
   virtual Roots findRoots(bool censor) override;
 
@@ -106,7 +115,12 @@
       StorePathSet & willBuild, StorePathSet & willSubstitute, StorePathSet & unknown,
       uint64_t & downloadSize, uint64_t & narSize) override;
 
-  virtual std::shared_ptr<std::string> getBuildLog(const StorePath & path) override;
+#if NIX_IS_AT_LEAST(2,6,0)
+  virtual std::optional<std::string>
+#else
+  virtual std::shared_ptr<std::string>
+#endif
+    getBuildLog(const StorePath & path) override;
 
   virtual unsigned int getProtocol() override;
 
@@ -129,7 +143,12 @@
 
   // Overrides
 
+#if NIX_IS_AT_LEAST(2,5,0)
+  virtual void queryRealisationUncached(const DrvOutput &,
+        Callback<std::shared_ptr<const Realisation>> callback) noexcept override;
+#else
   virtual std::optional<const Realisation> queryRealisation(const DrvOutput &) override;
+#endif
 
   virtual void ensurePath(const StorePath & path) override;
 
diff --git a/hercules-ci-agent-worker/Hercules/Agent/Worker/Build/Prefetched.hs b/hercules-ci-agent-worker/Hercules/Agent/Worker/Build/Prefetched.hs
--- a/hercules-ci-agent-worker/Hercules/Agent/Worker/Build/Prefetched.hs
+++ b/hercules-ci-agent-worker/Hercules/Agent/Worker/Build/Prefetched.hs
@@ -32,8 +32,6 @@
 
 C.include "<nix/derivations.hh>"
 
-C.include "<nix/affinity.hh>"
-
 C.include "<nix/globals.hh>"
 
 C.include "<nix/fs-accessor.hh>"
diff --git a/hercules-ci-agent-worker/Hercules/Agent/Worker/HerculesStore.hs b/hercules-ci-agent-worker/Hercules/Agent/Worker/HerculesStore.hs
--- a/hercules-ci-agent-worker/Hercules/Agent/Worker/HerculesStore.hs
+++ b/hercules-ci-agent-worker/Hercules/Agent/Worker/HerculesStore.hs
@@ -43,8 +43,6 @@
 
 C.include "<nix/derivations.hh>"
 
-C.include "<nix/affinity.hh>"
-
 C.include "<nix/globals.hh>"
 
 C.include "<nix-compat.hh>"
diff --git a/hercules-ci-agent.cabal b/hercules-ci-agent.cabal
--- a/hercules-ci-agent.cabal
+++ b/hercules-ci-agent.cabal
@@ -1,7 +1,7 @@
 cabal-version: 2.4
 
 name:           hercules-ci-agent
-version:        0.8.5
+version:        0.8.6
 synopsis:       Runs Continuous Integration tasks on your machines
 category:       Nix, CI, Testing, DevOps
 homepage:       https://docs.hercules-ci.com
@@ -10,7 +10,7 @@
 maintainer:     info@hercules-ci.com
 copyright:      2018-2020 Hercules CI
 license:        Apache-2.0
-build-type:     Simple
+build-type:     Custom
 extra-source-files:
     CHANGELOG.md
     cbits/hercules-aliases.h
@@ -58,6 +58,13 @@
         -DNIX_2_4
 
 
+custom-setup
+  setup-depends:
+    base
+    , Cabal >= 2.2.0.0
+    , cabal-pkg-config-version-hook
+
+
 library
   exposed-modules:
       Data.Fixed.Extras
@@ -198,7 +205,7 @@
     , filepath
     , hercules-ci-agent
     , hercules-ci-api-core == 0.1.3.0
-    , hercules-ci-api-agent == 0.4.1.0
+    , hercules-ci-api-agent == 0.4.1.1
     , hostname
     , http-client
     , http-client-tls
diff --git a/hercules-ci-agent/Hercules/Agent/Evaluate.hs b/hercules-ci-agent/Hercules/Agent/Evaluate.hs
--- a/hercules-ci-agent/Hercules/Agent/Evaluate.hs
+++ b/hercules-ci-agent/Hercules/Agent/Evaluate.hs
@@ -79,7 +79,6 @@
 import qualified System.Directory as Dir
 import System.FilePath
 import System.Process
-import System.Timeout.Lifted (timeout)
 
 eventLimit :: Int
 eventLimit = 50000
