Skip to content

fix(darwin): handle code signatures for CA derivations#14999

Draft
andrewgazelka wants to merge 3 commits intoNixOS:masterfrom
andrewgazelka:master
Draft

fix(darwin): handle code signatures for CA derivations#14999
andrewgazelka wants to merge 3 commits intoNixOS:masterfrom
andrewgazelka:master

Conversation

@andrewgazelka
Copy link

@andrewgazelka andrewgazelka commented Jan 15, 2026

Summary

!!!!! claude code

will test later. do not trust code works just putting this out here as a potential fix in case anyone is interested. will test later to see if works.

On macOS, content-addressed (CA) derivations fail because Nix's hash rewriting invalidates Mach-O code signatures, causing the kernel to kill binaries with SIGKILL (Code Signature Invalid).

This PR implements the fix proposed in #6065:

  1. Before computing the CA hash: Zero out LC_CODE_SIGNATURE sections in Mach-O binaries (similar to how self-references are already handled with HashModuloSink)
  2. After moving outputs to their final CA location: Re-sign binaries with ad-hoc signatures using codesign -f -s -

Files changed

  • darwin-codesign.cc/hh: New utilities for detecting Mach-O binaries, zeroing code signatures, and re-signing
  • derivation-builder.cc: Integrate signature handling into registerOutputs() for CA derivations

⚠️ UNTESTED

This is a draft implementation that compiles but has not been tested with actual CA builds. I'm submitting this early to get feedback on the approach.

Known limitations

  • Only handles LC_CODE_SIGNATURE in Mach-O binaries, not extended attributes or _CodeSignature bundles
  • Uses /usr/bin/codesign directly (should always be available on macOS)

Test plan

  • Build a CA derivation on aarch64-darwin
  • Verify binaries are executable after CA hash computation
  • Test with fat (universal) binaries
  • Test edge cases (symlinks, non-executable files)

Fixes #6065

On macOS, content-addressed (CA) derivations fail because Nix's hash
rewriting invalidates Mach-O code signatures, causing the kernel to
kill binaries with SIGKILL (Code Signature Invalid).

This commit implements the fix proposed in issue NixOS#6065:
1. Before computing the CA hash, zero out LC_CODE_SIGNATURE sections
   in Mach-O binaries (similar to how self-references are handled)
2. After moving outputs to their final CA location, re-sign binaries
   with ad-hoc signatures using codesign

This allows CA derivations to work on Apple Silicon Macs without
requiring SIP to be disabled.

UNTESTED: This is a draft implementation that compiles but has not
been tested with actual CA builds. Feedback welcome.

Fixes: NixOS#6065
CA derivations have integrity guaranteed by content hash, so the ownership
check (which verifies current build user owns output) is unnecessary and
causes failures when outputs built by one nixbld user are reused by another.

Permission check (group/world writable) still applies to prevent tampering.
…hing

The previous approach zeroed signature bytes in-place, corrupting the
signature structure. This made re-signing impossible with error:
'invalid or unsupported format for signature'

Using codesign --remove-signature properly removes the LC_CODE_SIGNATURE
load command and truncates the signature blob, allowing clean re-signing
after hash computation.
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.

Content-addressed derivation fails to build on aarch64-darwin

1 participant