fix(darwin): handle code signatures for CA derivations#14999
Draft
andrewgazelka wants to merge 3 commits intoNixOS:masterfrom
Draft
fix(darwin): handle code signatures for CA derivations#14999andrewgazelka wants to merge 3 commits intoNixOS:masterfrom
andrewgazelka wants to merge 3 commits intoNixOS:masterfrom
Conversation
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
This was referenced Apr 8, 2026
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
|
Hi @andrewgazelka — I built an alternative at #15638 that covers the InputAddressed call site (which is what bites fish on 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
LC_CODE_SIGNATUREsections in Mach-O binaries (similar to how self-references are already handled withHashModuloSink)codesign -f -s -Files changed
darwin-codesign.cc/hh: New utilities for detecting Mach-O binaries, zeroing code signatures, and re-signingderivation-builder.cc: Integrate signature handling intoregisterOutputs()for CA derivationsThis 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
LC_CODE_SIGNATUREin Mach-O binaries, not extended attributes or_CodeSignaturebundles/usr/bin/codesigndirectly (should always be available on macOS)Test plan
Fixes #6065