From e79e45b0ced625d396cf29e49d3631ad66ecb02c Mon Sep 17 00:00:00 2001 From: "Adam C. Stephens" Date: Tue, 15 Sep 2026 00:06:06 -0400 Subject: [PATCH] beamPackages.mixRelease: move fixup to mixReleaseSetupHook Assisted-By: OpenAI Codex GPT-6 Astra --- .../hooks/mix-release-setup-hook.sh | 72 +++++++++++++++++++ pkgs/development/beam-modules/mix-release.nix | 64 ++--------------- 2 files changed, 79 insertions(+), 57 deletions(-) diff --git a/pkgs/development/beam-modules/hooks/mix-release-setup-hook.sh b/pkgs/development/beam-modules/hooks/mix-release-setup-hook.sh index b073f63cbcc1..798d46a36b75 100644 --- a/pkgs/development/beam-modules/hooks/mix-release-setup-hook.sh +++ b/pkgs/development/beam-modules/hooks/mix-release-setup-hook.sh @@ -12,6 +12,78 @@ mixReleaseInstallHook() { echo "Finished mixReleaseInstallHook" } +mixReleaseFixupHook() { + echo "Executing mixReleaseFixupHook" + + echo "Removing files for Microsoft Windows" + rm -f "$out"/bin/*.bat + + echo "Wrapping programs in $out/bin with their runtime deps" + find "$out/bin/" -type f -executable | while read f; do + wrapProgram "$f" --prefix PATH : "$mixReleaseRuntimePath" + done + + # shellcheck disable=SC2144 # this will only work for a single erts + if [ -e "$out"/erts-* ]; then + mixReleaseRemoveErlangReferences + fi + + echo "Finished mixReleaseFixupHook" +} + +mixReleaseRemoveErlangReferences() { + # ERTS is included in the release, then erlang is not required as a runtime dependency. + # But, erlang is still referenced in some places. To removed references to erlang, + # following steps are required. + + echo "Removing references to erlang" + + # 1. remove references to erlang from plain text files + for file in $(rg "${erlang}/lib/erlang" "$out" --files-with-matches); do + substituteInPlace "$file" --replace "${erlang}/lib/erlang" "$out" + done + + # 2. remove references to erlang from .beam files + # + # No need to do anything, because it has been handled by "deterministic" option specified + # by ERL_COMPILER_OPTIONS. + + # 3. remove references to erlang from normal binary files + for file in $(rg "${erlang}/lib/erlang" "$out" --files-with-matches --binary --iglob '!*.beam'); do + echo "removing references to erlang in $file" + # use bbe to substitute strings in binary files, because using substituteInPlace + # on binaries will raise errors + bbe -e "s|${erlang}/lib/erlang|$out|" -o "$file".tmp "$file" + rm -f "$file" + mv "$file".tmp "$file" + done + + # References to erlang should be removed from output after above processing. +} + +mixReleaseRemoveCookieHook() { + if [ -e "$out/releases/COOKIE" ]; then + echo "Removing $out/releases/COOKIE" + rm "$out/releases/COOKIE" + fi +} + +mixReleaseStripDebugHook() { + # Strip debug symbols to avoid hardreferences to "foreign" closures actually + # not needed at runtime, while at the same time reduce size of BEAM files. + erl -noinput -eval 'lists:foreach(fun(F) -> io:format("Stripping ~p.~n", [F]), beam_lib:strip(F) end, filelib:wildcard("'"$out"'/**/*.beam"))' -s init stop +} + +preFixupHooks+=(mixReleaseFixupHook) + +if [ -n "${removeCookie-1}" ]; then + preFixupHooks+=(mixReleaseRemoveCookieHook) +fi + +if [ -n "${stripDebug-}" ]; then + postFixupHooks+=(mixReleaseStripDebugHook) +fi + if [ -z "${dontMixReleaseInstall-}" ] && [ -z "${installPhase-}" ]; then installPhase=mixReleaseInstallHook fi diff --git a/pkgs/development/beam-modules/mix-release.nix b/pkgs/development/beam-modules/mix-release.nix index 2d3b21642878..dd04c592d30b 100644 --- a/pkgs/development/beam-modules/mix-release.nix +++ b/pkgs/development/beam-modules/mix-release.nix @@ -167,63 +167,13 @@ lib.extendMkDerivation { } // (attrs.env or { }); - postFixup = '' - echo "removing files for Microsoft Windows" - rm -f "$out"/bin/*.bat + inherit erlang; - echo "wrapping programs in $out/bin with their runtime deps" - for f in $(find $out/bin/ -type f -executable); do - wrapProgram "$f" \ - --prefix PATH : ${ - lib.makeBinPath [ - coreutils - gnused - gnugrep - gawk - ] - } - done - '' - + lib.optionalString removeCookie '' - if [ -e $out/releases/COOKIE ]; then - echo "removing $out/releases/COOKIE" - rm $out/releases/COOKIE - fi - '' - + '' - if [ -e $out/erts-* ]; then - # ERTS is included in the release, then erlang is not required as a runtime dependency. - # But, erlang is still referenced in some places. To removed references to erlang, - # following steps are required. - - # 1. remove references to erlang from plain text files - for file in $(rg "${erlang}/lib/erlang" "$out" --files-with-matches); do - echo "removing references to erlang in $file" - substituteInPlace "$file" --replace "${erlang}/lib/erlang" "$out" - done - - # 2. remove references to erlang from .beam files - # - # No need to do anything, because it has been handled by "deterministic" option specified - # by ERL_COMPILER_OPTIONS. - - # 3. remove references to erlang from normal binary files - for file in $(rg "${erlang}/lib/erlang" "$out" --files-with-matches --binary --iglob '!*.beam'); do - echo "removing references to erlang in $file" - # use bbe to substitute strings in binary files, because using substituteInPlace - # on binaries will raise errors - bbe -e "s|${erlang}/lib/erlang|$out|" -o "$file".tmp "$file" - rm -f "$file" - mv "$file".tmp "$file" - done - - # References to erlang should be removed from output after above processing. - fi - '' - + lib.optionalString stripDebug '' - # Strip debug symbols to avoid hardreferences to "foreign" closures actually - # not needed at runtime, while at the same time reduce size of BEAM files. - erl -noinput -eval 'lists:foreach(fun(F) -> io:format("Stripping ~p.~n", [F]), beam_lib:strip(F) end, filelib:wildcard("'"$out"'/**/*.beam"))' -s init stop - ''; + mixReleaseRuntimePath = lib.makeBinPath [ + coreutils + gnused + gnugrep + gawk + ]; }; }