From fcc689e1887bcbeb9d1c4441266a63ea2114ae9f Mon Sep 17 00:00:00 2001
From: Connor Baker <ConnorBaker01@gmail.com>
Date: Thu, 18 Sep 2025 13:15:43 -0700
Subject: [PATCH] cmake/OpenCVUtils.cmake: invalidate Nix store paths before
 including in version_string.inc

Signed-off-by: Connor Baker <ConnorBaker01@gmail.com>
---
 cmake/OpenCVUtils.cmake | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/cmake/OpenCVUtils.cmake b/cmake/OpenCVUtils.cmake
index 5886f4f3cb..9f3a3a74e9 100644
--- a/cmake/OpenCVUtils.cmake
+++ b/cmake/OpenCVUtils.cmake
@@ -956,6 +956,15 @@ function(ocv_output_status msg)
     message(WARNING "String to be inserted to version_string.inc has an unexpected line break: '${msg}'")
     string(REPLACE "\n" "\\n" msg "${msg}")
   endif()
+  # The hash portion of Nix store paths are 32 characters long. Replace them with an invalid hash to avoid bloating
+  # the runtime closure by includnig references to the compiler and other build tools.
+  # https://fzakaria.com/2025/03/28/what-s-in-a-nix-store-path
+  # NOTE: This assumes the name of the Nix store is always /nix/store.
+  # NOTE: The regex [a-z0-9] is larger than Nix's base32, but it simplifies things.
+  # NOTE: This assumes all Nix store paths have additional characters after the hash.
+  # NOTE: CMake doesn't support fixed-length matching. See:
+  # https://cmake.org/cmake/help/latest/command/string.html#regex-specification
+  string(REGEX REPLACE "/nix/store/[a-z0-9]+-" "/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-" msg "${msg}")
   set(OPENCV_BUILD_INFO_STR "${OPENCV_BUILD_INFO_STR}\"${msg}\\n\"\n" CACHE INTERNAL "")
 endfunction()
 
-- 
2.48.1

