diff --git a/CHANGELOG.md b/CHANGELOG.md
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,12 @@
 
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 
+## 0.3.5.0 - 2022-12-29
+
+### Added
+
+ - `initThread` for stack overflow handling
+
 ## 0.3.4.0 - 2022-11-15
 
 ### Added
diff --git a/hercules-ci-cnix-expr.cabal b/hercules-ci-cnix-expr.cabal
--- a/hercules-ci-cnix-expr.cabal
+++ b/hercules-ci-cnix-expr.cabal
@@ -1,7 +1,7 @@
 cabal-version: 2.4
 
 name:           hercules-ci-cnix-expr
-version:        0.3.4.0
+version:        0.3.5.0
 synopsis:       Bindings for the Nix evaluator
 category:       Nix, CI, Testing, DevOps
 homepage:       https://docs.hercules-ci.com
diff --git a/src/Hercules/CNix/Expr.hs b/src/Hercules/CNix/Expr.hs
--- a/src/Hercules/CNix/Expr.hs
+++ b/src/Hercules/CNix/Expr.hs
@@ -18,6 +18,8 @@
     setDebug,
     setGlobalOption,
     setOption,
+    setExtraStackOverflowHandlerToSleep,
+    initThread,
     logInfo,
     withEvalState,
     withEvalStateConduit,
@@ -144,6 +146,39 @@
 #endif
       nix::settings.experimentalFeatures.assign(features);
     } |]
+
+initThread :: IO ()
+initThread =
+  void
+    [C.throwBlock| void {
+      nix::detectStackOverflow();
+    }|]
+
+{- | Configure the stack overflow handler to sleep before returning, allowing
+  other threads to continue for a bit.
+
+  No-op before Nix 2.12
+-}
+setExtraStackOverflowHandlerToSleep :: IO ()
+setExtraStackOverflowHandlerToSleep =
+#if NIX_IS_AT_LEAST(2,12,0)
+  void
+    [C.throwBlock| void {
+      nix::stackOverflowHandler = [](siginfo_t *info, void *ctx) {
+        Error error("stack overflow");
+        logError(error.info());
+
+        // This is risky for a signal handler.
+        // Note that the original thread is now in a permanently blocked state
+        // so we can easily create a deadlock.
+        // Allow the rest of the process to continue for a bit.
+        sleep(1);
+        _exit(1);
+        };
+    }|]
+#else
+  pass
+#endif
 
 setTalkative :: IO ()
 setTalkative =
diff --git a/src/Hercules/CNix/Expr/Context.hs b/src/Hercules/CNix/Expr/Context.hs
--- a/src/Hercules/CNix/Expr/Context.hs
+++ b/src/Hercules/CNix/Expr/Context.hs
@@ -31,7 +31,8 @@
 
 context :: C.Context
 context =
-  C.cppCtx <> C.fptrCtx
+  C.cppCtx
+    <> C.fptrCtx
     <> C.bsCtx
     <> evalContext
 
