From aeb12ca9fa21b5e775ca831469dbb9172bb47bdf Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Wed, 4 Nov 2026 07:59:57 +0000 Subject: [PATCH] nix: make the iOS core package reproducible --- flake.nix | 7 +++-- scripts/nix/zero-ar-headers.py | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 scripts/nix/zero-ar-headers.py diff --git a/flake.nix b/flake.nix index 2125c470c5..e4c3a8139b 100644 --- a/flake.nix +++ b/flake.nix @@ -108,6 +108,7 @@ chmod +w $pkg ${mac2ios.packages.${system}.mac2ios}/bin/mac2ios $pkg [[ "$pkg" == *simplex-chat* ]] && ${pkgs.stdenv.cc.targetPrefix}strip -x $pkg + ${pkgs.python3}/bin/python3 ${./scripts/nix/zero-ar-headers.py} $pkg chmod -w $pkg done @@ -117,7 +118,8 @@ sha256sum $out/_pkg/*.a - (cd $out/_pkg; ${pkgs.zip}/bin/zip -r -9 $out/${bundleName}.zip *) + find "$out/_pkg" -exec touch -h -d '@1764547200' {} + + (cd $out/_pkg; ${pkgs.zip}/bin/zip -r -9 -X $out/${bundleName}.zip *) rm -fR $out/_pkg mkdir -p $out/nix-support echo "file binary-dist \"$(echo $out/*.zip)\"" \ @@ -129,7 +131,8 @@ # we need threaded here, otherwise all the queing logic doesn't work properly. # for iOS we also use -staticlib, to get one rolled up library. # still needs mac2ios patching of the archives. - ghcOptions = [ "-staticlib" "-threaded" "-DIOS" ]; + # this list replaces the -j1 that all packages get: GHC's parallel compilation gives non-deterministic objects. + ghcOptions = [ "-staticlib" "-threaded" "-DIOS" "-j1" ]; postInstall = iosPostInstall bundleName; }; in rec { diff --git a/scripts/nix/zero-ar-headers.py b/scripts/nix/zero-ar-headers.py new file mode 100644 index 0000000000..7a3e2a54fd --- /dev/null +++ b/scripts/nix/zero-ar-headers.py @@ -0,0 +1,51 @@ +"""Zeroes the dates and owners in the member headers of ar archives, which GHC and strip take from the build's clock and user.""" + +import struct +import sys +from pathlib import Path + +AR_MAGIC = b"!\n" + + +# name, date, uid, gid, mode, size and end of a member header +AR_HEADER = struct.Struct("16s12s6s6s8s10s2s") + + +AR_HEADER_END = b"`\n" + + +# the date, uid, gid and mode that llvm-ar's deterministic mode writes +DETERMINISTIC_FIELDS = (b"0".ljust(12), b"0".ljust(6), b"0".ljust(6), b"644".ljust(8)) + + +class ArchiveError(Exception): + pass + + +def zero_headers(data: bytes) -> bytes: + if not data.startswith(AR_MAGIC): + raise ArchiveError("not an ar archive") + zeroed = bytearray(data) + offset = len(AR_MAGIC) + while offset < len(zeroed): + if offset + AR_HEADER.size > len(zeroed): + raise ArchiveError(f"ends in a member header at {offset}") + name, *_, size, end = AR_HEADER.unpack_from(zeroed, offset) + if end != AR_HEADER_END: + raise ArchiveError(f"no member header at {offset}") + AR_HEADER.pack_into(zeroed, offset, name, *DETERMINISTIC_FIELDS, size, end) + # a member of odd size is padded with a newline + offset += AR_HEADER.size + int(size) + int(size) % 2 + return bytes(zeroed) + + +def main(paths: list[str]) -> None: + for path in map(Path, paths): + try: + path.write_bytes(zero_headers(path.read_bytes())) + except ArchiveError as e: + sys.exit(f"{path}: {e}") + + +if __name__ == "__main__": + main(sys.argv[1:])