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.
This commit is contained in:
Emily
2025-07-04 12:02:26 +01:00
parent 678cac2a01
commit 1b28a1e224
2 changed files with 28 additions and 0 deletions
@@ -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
@@ -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)