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
Copy Markdown

@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.
ak2k added a commit to ak2k/nix that referenced this pull request Apr 8, 2026
This is based on the work of @andrewgazelka in NixOS#14999.

On darwin, `DerivationBuilderImpl::registerOutputs` calls `RewritingSink`
to substitute placeholder bytes in build outputs (e.g. when one output's
binary embeds a sibling output's store path via `${builtins.placeholder
"doc"}`). The substitution is byte-level and has no knowledge of Mach-O
code signatures, but Apple's `ld` ad-hoc-signs every binary at link time
with the `linker-signed` flag set in `LC_CODE_SIGNATURE`. The signature
covers the very bytes that were just rewritten, so one or more SHA-256
page hashes in the `CodeDirectory` are stale after the rewrite, and the
macOS kernel SIGKILLs the binary at first page-in with `cs_invalid_page`.
This surfaces in nixpkgs as NixOS/nixpkgs#507531
(fish on `nixpkgs-darwin` fails to start).

Add a darwin-only helper, `fixupMachoPageHashes`, called from the
`rewriteOutput` lambda after `movePath` and before
`canonicalisePathMetaData`. The helper recomputes only the affected
page-hash slots in place, leaving every other byte of the Mach-O
unchanged, including the `linker-signed` flag, the original 4 KiB
page size, the identifier, and every special slot. The fix-up is
length-preserving, so the result is bit-identical to a clean build of
the same input.

The same call site covers both `InputAddressed` and `CAFloating`/
`CAFixed`/`Impure` visitors, since they all go through the shared
`rewriteOutput` lambda.

Differences from NixOS#14999: (a) cover the InputAddressed call site, which
is what bites fish on `nixpkgs-darwin`; (b) preserve bit-reproducibility
so `nix build --check` does not fire on the rewrite alone; (c) preserve
`linker-signed` and the 4 KiB page size, both of which `codesign(1)`
would clear.

Closes: NixOS#6065
ak2k added a commit to ak2k/nix that referenced this pull request Apr 8, 2026
This is based on the work of @andrewgazelka in NixOS#14999.

On darwin, `DerivationBuilderImpl::registerOutputs` calls `RewritingSink`
to substitute placeholder bytes in build outputs (e.g. when one output's
binary embeds a sibling output's store path via `${builtins.placeholder
"doc"}`). The substitution is byte-level and has no knowledge of Mach-O
code signatures, but Apple's `ld` ad-hoc-signs every binary at link time
with the `linker-signed` flag set in `LC_CODE_SIGNATURE`. The signature
covers the very bytes that were just rewritten, so one or more SHA-256
page hashes in the `CodeDirectory` are stale after the rewrite, and the
macOS kernel SIGKILLs the binary at first page-in with `cs_invalid_page`.
This surfaces in nixpkgs as NixOS/nixpkgs#507531
(fish on `nixpkgs-darwin` fails to start).

Add a darwin-only helper, `fixupMachoPageHashes`, called from the
`rewriteOutput` lambda after `movePath` and before
`canonicalisePathMetaData`. The helper recomputes only the affected
page-hash slots in place, leaving every other byte of the Mach-O
unchanged, including the `linker-signed` flag, the original 4 KiB
page size, the identifier, and every special slot. The fix-up is
length-preserving, so the result is bit-identical to a clean build of
the same input.

The same call site covers both `InputAddressed` and `CAFloating`/
`CAFixed`/`Impure` visitors, since they all go through the shared
`rewriteOutput` lambda.

Differences from NixOS#14999: (a) cover the InputAddressed call site, which
is what bites fish on `nixpkgs-darwin`; (b) preserve bit-reproducibility
so `nix build --check` does not fire on the rewrite alone; (c) preserve
`linker-signed` and the 4 KiB page size, both of which `codesign(1)`
would clear.

Closes: NixOS#6065
@ak2k
Copy link
Copy Markdown

ak2k commented Apr 8, 2026

Hi @andrewgazelka — I built an alternative at #15638 that covers the InputAddressed call site (which is what bites fish on nixpkgs-darwin) and avoids codesign(1)'s 16 KiB page-size mismatch with ld -adhoc_codesign's 4 KiB.

Happy to close mine and fold into this branch instead if you'd rather; your call, credit for the original direction is yours either way.

ak2k added a commit to ak2k/nix that referenced this pull request Apr 8, 2026
This is based on the work of @andrewgazelka in NixOS#14999.

On darwin, `DerivationBuilderImpl::registerOutputs` calls `RewritingSink`
to substitute placeholder bytes in build outputs (e.g. when one output's
binary embeds a sibling output's store path via `${builtins.placeholder
"doc"}`). The substitution is byte-level and has no knowledge of Mach-O
code signatures, but Apple's `ld` ad-hoc-signs every binary at link time
with the `linker-signed` flag set in `LC_CODE_SIGNATURE`. The signature
covers the very bytes that were just rewritten, so one or more SHA-256
page hashes in the `CodeDirectory` are stale after the rewrite, and the
macOS kernel SIGKILLs the binary at first page-in with `cs_invalid_page`.
This surfaces in nixpkgs as NixOS/nixpkgs#507531
(fish on `nixpkgs-darwin` fails to start).

Add a darwin-only helper, `fixupMachoPageHashes`, called from the
`rewriteOutput` lambda after `movePath` and before
`canonicalisePathMetaData`. The helper recomputes only the affected
page-hash slots in place, leaving every other byte of the Mach-O
unchanged, including the `linker-signed` flag, the original 4 KiB
page size, the identifier, and every special slot. The fix-up is
length-preserving, so the result is bit-identical to a clean build of
the same input.

The same call site covers both `InputAddressed` and `CAFloating`/
`CAFixed`/`Impure` visitors, since they all go through the shared
`rewriteOutput` lambda.

Differences from NixOS#14999: (a) cover the InputAddressed call site, which
is what bites fish on `nixpkgs-darwin`; (b) preserve bit-reproducibility
so `nix build --check` does not fire on the rewrite alone; (c) preserve
`linker-signed` and the 4 KiB page size, both of which `codesign(1)`
would clear.

Closes: NixOS#6065
ak2k added a commit to ak2k/nix that referenced this pull request Apr 8, 2026
This is based on the work of @andrewgazelka in NixOS#14999.

On darwin, `DerivationBuilderImpl::registerOutputs` calls `RewritingSink`
to substitute scratch-path bytes in build outputs. When one output's
binary embeds a sibling output's store path via `${builtins.placeholder
"doc"}` and the sibling is already in the store at build start, the
embedded bytes are the sibling's scratch path (from `makeFallbackPath`),
which `registerOutputs` rewrites to the final hash after the builder
exits. The substitution is byte-level and has no knowledge of Mach-O
code signatures, but Apple's `ld` ad-hoc-signs every binary at link
time with the `linker-signed` flag set in `LC_CODE_SIGNATURE`. The
signature covers the very bytes that were just rewritten, so one or
more SHA-256 page hashes in the `CodeDirectory` are stale after the
rewrite, and the macOS kernel SIGKILLs the binary at first page-in
with `cs_invalid_page`. This surfaces in nixpkgs as
NixOS/nixpkgs#507531 (fish on
`nixpkgs-darwin` fails to start).

Add a darwin-only helper, `fixupMachoPageHashes`, called from the
`rewriteOutput` lambda after `movePath` and before
`canonicalisePathMetaData`. The helper recomputes only the affected
page-hash slots in place, leaving every other byte of the Mach-O
unchanged, including the `linker-signed` flag, the original 4 KiB
page size, the identifier, and every special slot. The fix-up is
length-preserving, so the result is bit-identical to a clean build of
the same input.

The same call site covers both `InputAddressed` and `CAFloating`/
`CAFixed`/`Impure` visitors, since they all go through the shared
`rewriteOutput` lambda.

Differences from NixOS#14999: (a) cover the InputAddressed call site, which
is what bites fish on `nixpkgs-darwin`; (b) preserve bit-reproducibility
so `nix build --check` does not fire on the rewrite alone; (c) preserve
`linker-signed` and the 4 KiB page size, both of which `codesign(1)`
would clear.

Closes: NixOS#6065
ak2k added a commit to ak2k/nix that referenced this pull request Apr 8, 2026
This is based on the work of @andrewgazelka in NixOS#14999.

On darwin, `DerivationBuilderImpl::registerOutputs` calls `RewritingSink`
to substitute scratch-path bytes in build outputs. When one output's
binary embeds a sibling output's store path via `${builtins.placeholder
"doc"}` and the sibling is already in the store at build start, the
embedded bytes are the sibling's scratch path (from `makeFallbackPath`),
which `registerOutputs` rewrites to the final hash after the builder
exits. The substitution is byte-level and has no knowledge of Mach-O
code signatures, but Apple's `ld` ad-hoc-signs every binary at link
time with the `linker-signed` flag set in `LC_CODE_SIGNATURE`. The
signature covers the very bytes that were just rewritten, so one or
more SHA-256 page hashes in the `CodeDirectory` are stale after the
rewrite, and the macOS kernel SIGKILLs the binary at first page-in
with `cs_invalid_page`. This surfaces in nixpkgs as
NixOS/nixpkgs#507531 (fish on
`nixpkgs-darwin` fails to start).

Add a darwin-only helper, `fixupMachoPageHashes`, called from the
`rewriteOutput` lambda after `movePath` and before
`canonicalisePathMetaData`. The helper recomputes only the affected
page-hash slots in place, leaving every other byte of the Mach-O
unchanged, including the `linker-signed` flag, the original 4 KiB
page size, the identifier, and every special slot. The fix-up is
length-preserving, so the result is bit-identical to a clean build of
the same input.

The same call site covers both `InputAddressed` and `CAFloating`/
`CAFixed`/`Impure` visitors, since they all go through the shared
`rewriteOutput` lambda.

Differences from NixOS#14999: (a) cover the InputAddressed call site, which
is what bites fish on `nixpkgs-darwin`; (b) preserve bit-reproducibility
so `nix build --check` does not fire on the rewrite alone; (c) preserve
`linker-signed` and the 4 KiB page size, both of which `codesign(1)`
would clear.

Closes: NixOS#6065
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

2 participants