From 1b28a1e224d6e5eb20ac09bc4604ca4d4a4a6b8e Mon Sep 17 00:00:00 2001 From: Emily Date: Fri, 20 Jun 2025 11:05:21 +0100 Subject: [PATCH] sanitiseHeaderPathsHook: init C++ headers often use `__FILE__` in error messages, causing the development outputs of libraries to leak into the runtime closure of packages using them. This hook abstracts away a pattern used in a few places throughout the tree to have headers identify themselves by a sanitized path that does not cause runtime dependencies. This does unfortunately mean that compiler error messages will reference the sanitized path. The only alternatives I can imagine are to patch compilers to handle `__FILE__` specially, or to have libraries propagate a hook that removes references. The latter would potentially need to be propagated recursively due to `#include` semantics and would be less precise than this. --- .../sa/sanitiseHeaderPathsHook/package.nix | 18 ++++++++++++++++++ .../sanitise-header-paths-hook.bash | 10 ++++++++++ 2 files changed, 28 insertions(+) create mode 100644 pkgs/by-name/sa/sanitiseHeaderPathsHook/package.nix create mode 100644 pkgs/by-name/sa/sanitiseHeaderPathsHook/sanitise-header-paths-hook.bash diff --git a/pkgs/by-name/sa/sanitiseHeaderPathsHook/package.nix b/pkgs/by-name/sa/sanitiseHeaderPathsHook/package.nix new file mode 100644 index 000000000000..6e6bd00ebf2f --- /dev/null +++ b/pkgs/by-name/sa/sanitiseHeaderPathsHook/package.nix @@ -0,0 +1,18 @@ +{ + lib, + makeSetupHook, + removeReferencesTo, +}: + +makeSetupHook { + name = "sanitise-header-paths-hook"; + + substitutions = { + removeReferencesTo = lib.getExe removeReferencesTo; + }; + + meta = { + description = "Setup hook to sanitise header file paths to avoid leaked references through `__FILE__`"; + maintainers = [ lib.maintainers.emily ]; + }; +} ./sanitise-header-paths-hook.bash diff --git a/pkgs/by-name/sa/sanitiseHeaderPathsHook/sanitise-header-paths-hook.bash b/pkgs/by-name/sa/sanitiseHeaderPathsHook/sanitise-header-paths-hook.bash new file mode 100644 index 000000000000..60e311e12a84 --- /dev/null +++ b/pkgs/by-name/sa/sanitiseHeaderPathsHook/sanitise-header-paths-hook.bash @@ -0,0 +1,10 @@ +sanitiseHeaderPaths() { + local header + while IFS= read -r -d '' header; do + nixLog "sanitising header path in $header" + sed -i "1i#line 1 \"$header\"" "$header" + @removeReferencesTo@ -t "${!outputInclude}" "$header" + done < <(find "${!outputInclude}/include" -type f -print0) +} + +preFixupHooks+=(sanitiseHeaderPaths)