Skip to content

flake: rewrite Nix-store libiconv path in macOS binary - #305

Closed
pkit wants to merge 1 commit into
DeterminateSystems:mainfrom
pkit:fix/darwin-libiconv-install-name
Closed

pkit wants to merge 1 commit into
DeterminateSystems:mainfrom
pkit:fix/darwin-libiconv-install-name

Conversation

@pkit

@pkit pkit commented Apr 20, 2026 •

Copy link
Copy Markdown

Problem

Consumers of this Action on macos-latest runners fail with:

dyld[…]: Library not loaded: /nix/store/<hash>-libiconv-109.100.2/lib/libiconv.2.dylib
  Referenced from: <…> /Users/runner/work/_temp/flakehub-push-…/flakehub-push
  Reason: tried: '/nix/store/<hash>-libiconv-109.100.2/lib/libiconv.2.dylib' (no such file), …

The Darwin build in flake.nix links dynamically against pkgs.libiconv, which bakes the /nix/store/<hash>-libiconv-*/lib/libiconv.2.dylib path into the binary's LC_LOAD_DYLIB entry. That artifact is then uploaded via push-artifact-ids and fetched as a standalone binary by action consumers. On their runners the specific store path doesn't exist and dyld aborts on the first invocation.

Linux is unaffected because its build already uses +crt-static against a musl target.

Fix

Add a Darwin-only postInstall that rewrites any /nix/store/*libiconv*.dylib references in the binary's install names to /usr/lib/libiconv.2.dylib, which is always present on macOS. This mirrors the pattern used by e.g. gnosis/gnosis_vpn-client#502 for the same class of bug.

Verification

  • nix flake show --all-systems evaluates cleanly for x86_64-linux, aarch64-linux, and aarch64-darwin.
  • nixfmt --check flake.nix passes.
  • I don't have a Darwin machine handy to nix build and inspect otool -L on the result, but the postInstall is a no-op when no matching reference exists, and mirrors the exact pattern proven in the linked gnosis PR.

Summary by CodeRabbit

  • Bug Fixes
    • Improved macOS binary compatibility by optimizing library dependency resolution on Apple systems.

The Darwin build links dynamically against pkgs.libiconv, baking the
/nix/store/<hash>-libiconv-*/lib/libiconv.2.dylib path into the binary's
LC_LOAD_DYLIB entry. When the binary is distributed standalone via
push-artifact-ids (how consumers of this Action receive it), that store
path doesn't exist on their machine and dyld aborts with:

    Library not loaded: /nix/store/<hash>-libiconv-109.100.2/lib/libiconv.2.dylib
    Reason: tried: '...' (no such file)

Rewriting the install_name to /usr/lib/libiconv.2.dylib — which is
always present on macOS — makes the artifact relocatable.

Linux is unaffected because its build already uses +crt-static against
the musl target.
@coderabbitai

coderabbitai Bot commented Apr 20, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

A Darwin-only postInstall hook was added to flake.nix that detects and rewrites dynamic library references to libiconv from Nix store paths to the macOS system path during post-build processing.

Changes

Cohort / File(s) Summary
macOS Binary Linkage Post-Processing
flake.nix
Added Darwin-only postInstall hook to detect and rewrite libiconv dynamic library load commands from Nix store references to macOS system path /usr/lib/libiconv.2.dylib using otool and install_name_tool.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 The rabbit fixed the Darwin build,

With otool and install_name_tool filled,

Now libiconv links just right,

macOS binaries take their flight! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: adding a post-install hook to rewrite libiconv library paths in macOS binaries built from Nix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@flake.nix`:
- Around line 112-117: The current loop captures all matching libiconv install
names into a newline-separated variable "linked" and uses word-splitting-prone
command substitution for "bin", causing failures when there are multiple matches
or spaces in paths; change the logic to iterate safely over binaries with find
-print0 and a null-delimited read (reading into "bin"), then for each binary
parse otool -L and iterate over each matching install name with a null-delimited
loop (reading into "linked") and call install_name_tool -change "$linked"
"/usr/lib/libiconv.2.dylib" "$bin" for each match so each install name is
processed independently without word-splitting issues.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b57c4e4b-31cb-41ba-ae31-c0c09e54957e

📥 Commits

Reviewing files that changed from the base of the PR and between 9247b5d and 8843e50.

📒 Files selected for processing (1)
  • flake.nix

Comment thread flake.nix
Comment on lines +112 to +117
for bin in $(find "$out/bin" -type f); do
linked=$(otool -L "$bin" | awk '/\/nix\/store\/.*libiconv.*\.dylib/ {print $1}') || true
if [ -n "$linked" ]; then
echo "Rewriting $bin: $linked -> /usr/lib/libiconv.2.dylib"
install_name_tool -change "$linked" "/usr/lib/libiconv.2.dylib" "$bin"
fi

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

linked=$(
  printf '%s\n' \
    '/nix/store/aaa-libiconv/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)' \
    '/nix/store/bbb-libiconv/lib/libiconv-extra.dylib (compatibility version 1.0.0, current version 1.0.0)' |
    awk '/\/nix\/store\/.*libiconv.*\.dylib/ {print $1}'
)

python3 - "$linked" <<'PY'
import sys
arg = sys.argv[1]
assert "\n" in arg, repr(arg)
print("current snippet would pass one newline-containing argument:", repr(arg))
PY

Repository: DeterminateSystems/flakehub-push

Length of output: 232


🏁 Script executed:

head -n 120 flake.nix | tail -n 25 | cat -n

Repository: DeterminateSystems/flakehub-push

Length of output: 1562


Process each matched install name independently.

If otool emits more than one matching libiconv load command, the linked variable becomes a newline-separated string and install_name_tool -change "$linked" won't match any single install name. Additionally, the find command substitution can word-split binary paths. Loop over each match and binary using null delimiters instead.

Proposed shell hardening
-                for bin in $(find "$out/bin" -type f); do
-                  linked=$(otool -L "$bin" | awk '/\/nix\/store\/.*libiconv.*\.dylib/ {print $1}') || true
-                  if [ -n "$linked" ]; then
-                    echo "Rewriting $bin: $linked -> /usr/lib/libiconv.2.dylib"
-                    install_name_tool -change "$linked" "/usr/lib/libiconv.2.dylib" "$bin"
-                  fi
-                done
+                while IFS= read -r -d "" bin; do
+                  while IFS= read -r linked; do
+                    if [ -n "$linked" ]; then
+                      echo "Rewriting $bin: $linked -> /usr/lib/libiconv.2.dylib"
+                      install_name_tool -change "$linked" "/usr/lib/libiconv.2.dylib" "$bin"
+                    fi
+                  done < <(otool -L "$bin" 2>/dev/null | awk '/\/nix\/store\/.*libiconv.*\.dylib/ {print $1}' || true)
+                done < <(find "$out/bin" -type f -print0)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for bin in $(find "$out/bin" -type f); do
linked=$(otool -L "$bin" | awk '/\/nix\/store\/.*libiconv.*\.dylib/ {print $1}') || true
if [ -n "$linked" ]; then
echo "Rewriting $bin: $linked -> /usr/lib/libiconv.2.dylib"
install_name_tool -change "$linked" "/usr/lib/libiconv.2.dylib" "$bin"
fi
while IFS= read -r -d "" bin; do
while IFS= read -r linked; do
if [ -n "$linked" ]; then
echo "Rewriting $bin: $linked -> /usr/lib/libiconv.2.dylib"
install_name_tool -change "$linked" "/usr/lib/libiconv.2.dylib" "$bin"
fi
done < <(otool -L "$bin" 2>/dev/null | awk '/\/nix\/store\/.*libiconv.*\.dylib/ {print $1}' || true)
done < <(find "$out/bin" -type f -print0)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@flake.nix` around lines 112 - 117, The current loop captures all matching
libiconv install names into a newline-separated variable "linked" and uses
word-splitting-prone command substitution for "bin", causing failures when there
are multiple matches or spaces in paths; change the logic to iterate safely over
binaries with find -print0 and a null-delimited read (reading into "bin"), then
for each binary parse otool -L and iterate over each matching install name with
a null-delimited loop (reading into "linked") and call install_name_tool -change
"$linked" "/usr/lib/libiconv.2.dylib" "$bin" for each match so each install name
is processed independently without word-splitting issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant