Skip to content

Refactor nix/lib.nix: deduplicate builders#213

Closed
jappeace-sloth wants to merge 1 commit intojappeace:masterfrom
jappeace-sloth:refactor-lib-nix-dedup
Closed

Refactor nix/lib.nix: deduplicate builders#213
jappeace-sloth wants to merge 1 commit intojappeace:masterfrom
jappeace-sloth:refactor-lib-nix-dedup

Conversation

@jappeace-sloth
Copy link
Copy Markdown

Summary

  • Extract shared data lists (modules, C sources, headers, symbols) and helper functions into top-level let bindings — single source of truth
  • Merge mkIOSLib + mkWatchOSLib into internal mkAppleStaticLib parameterised by platform; public API preserved via thin wrappers
  • Merge mkSimulatorApp + mkWatchOSSimulatorApp into internal mkAppleSimulatorApp; public API preserved
  • Replace 14 repetitive NDK compile blocks with map over list
  • Hoist applePkgs, appleGhc, gmpStatic, libffiStatic to top-level (were duplicated in iOS and watchOS)
  • 995 → 684 lines (31% reduction). Adding a new bridge module now means editing 1 list, not 6+ places.

Test plan

  • nix-instantiate --parse nix/lib.nix passes
  • nix-instantiate --eval confirms all 6 public functions exported
  • Eval-checked mkAndroidLib, mkIOSLib, mkWatchOSLib, mkSimulatorApp, mkWatchOSSimulatorApp with real args
  • All caller files parse: android.nix, ios.nix, watchos.nix, simulator-app.nix, watchos-simulator-app.nix, apk.nix, emulator-all.nix, simulator-all.nix, watchos-simulator-all.nix
  • CI (full nix-build — Apple targets need macOS runners)

🤖 Generated with Claude Code

Factor out massive duplication across the 6 builder functions in lib.nix.
The same lists of modules, C sources, headers, and symbols were copy-pasted
2-4 times, and mkIOSLib/mkWatchOSLib were near-identical 170-line functions.

Changes:
- Extract shared lists: hatterModules, appleCbitsSources, bridgeHeaders,
  androidJniBridgeFiles, commonExportedSymbols, androidOnlySymbols,
  appleOnlySymbols
- Extract helpers: ndkCompileJni, undefinedSymbolFlags, copyBridgeHeaders,
  copyHatterModules, copyCbitsSources, cbitsSourceArgs
- Hoist Apple-common deps (applePkgs, appleGhc, gmpStatic, libffiStatic)
  to top-level let instead of duplicating in mkIOSLib and mkWatchOSLib
- Merge mkIOSLib + mkWatchOSLib into internal mkAppleStaticLib with
  platform parameter; public API preserved via thin wrappers
- Merge mkSimulatorApp + mkWatchOSSimulatorApp into internal
  mkAppleSimulatorApp; public API preserved via thin wrappers
- Replace 14 repetitive NDK compile blocks in mkAndroidLib with
  map over androidJniBridgeFiles list

995 → 684 lines (31% reduction). Public API unchanged — all 6 exported
functions accept the same arguments. Adding a new bridge module now
requires editing only the relevant list, not 6+ places.

Prompt: Implement the plan to refactor nix/lib.nix — factor out commonalities

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jappeace-sloth
Copy link
Copy Markdown
Author

Superseded by #215 (combined with #214)

jappeace pushed a commit that referenced this pull request Apr 26, 2026
Combines two improvements to the Nix build infrastructure:

1. Deduplicate nix/lib.nix (995 → ~700 lines, 30% reduction):
   - Extract shared data lists (modules, C sources, headers, symbols)
     and helper functions into top-level let bindings
   - Merge mkIOSLib + mkWatchOSLib into internal mkAppleStaticLib
     parameterised by platform; public API preserved via thin wrappers
   - Merge mkSimulatorApp + mkWatchOSSimulatorApp into internal
     mkAppleSimulatorApp; public API preserved
   - Replace 14 repetitive NDK compile blocks with map over list
   - Hoist applePkgs, appleGhc, gmpStatic, libffiStatic to top-level

2. Build hatter as a cached cabal package for iOS/watchOS:
   - ios-deps.nix: add hatterSrc param, build hatter via callCabal2nix
     so the .a and package DB entry are cached
   - ios.nix, watchos.nix: pass hatterSrc to ios-deps.nix
   - mkAppleStaticLib: when crossDeps is provided, only compile
     Main.hs + run_main.c instead of all 22 modules + 19 C sources
   - Standalone fallback (crossDeps == null) preserved

Adding a new bridge module now means editing 1 list, not 6+ places.
Demo apps and consumers only recompile their entry point.

Prompt: combine PR #213 and #214 into a single PR

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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