|
| 1 | +## Nix refactor plan: shared core + thin adapters |
| 2 | + |
| 3 | +### Goal |
| 4 | +- **Same functionality** in both entrypoints: |
| 5 | + - `nix develop --impure --no-eval-cache` |
| 6 | + - `devenv shell` |
| 7 | +- Make it **obvious** what is: |
| 8 | + - **core logic** (reusable) |
| 9 | + - **module adapter** (options + wiring) |
| 10 | +- Remove duplication between `nix/modules/*` and `nix/lib/*`. |
| 11 | + |
| 12 | +### Target layout (north star) |
| 13 | +- **`nix/lib/core/`**: shared behavior (pure-ish functions returning `{ packages, env, shellHook/enterShell, files?, warnings? }`) |
| 14 | +- **`nix/modules/*`**: thin adapters that: |
| 15 | + - define options |
| 16 | + - map `config.stackpanel.*` → `nix/lib/core/*` |
| 17 | + - merge returned attrs into devenv/Nix module outputs |
| 18 | +- **Entrypoints** |
| 19 | + - `nix/stackpanel.nix`: devenv module aggregator |
| 20 | + - `nix/modules/devenv/devenv.nix`: devenv directory import entrypoint (obvious import path) |
| 21 | + - `nix/modules/devenv.nix`: compatibility shim (only if needed) |
| 22 | + - `flake.nix`: exports `flake.lib.*`, `flake.devenvModules.*` (and any module paths) |
| 23 | + |
| 24 | +### Principles |
| 25 | +- **Single source of truth**: implement behavior once (core), call it from both adapters. |
| 26 | +- **Adapters stay tiny**: avoid duplicating computation in modules. |
| 27 | +- **Purity by default**: impure reads must be explicit and optional. |
| 28 | +- **Compatibility**: prefer shims/deprecations over breaking moves. |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Todo list |
| 33 | + |
| 34 | +### Completed (already done) |
| 35 | +- [x] Create shared core for global services: `nix/lib/core/global-services.nix` |
| 36 | +- [x] Refactor `nix/lib/devshell.nix` to use shared core |
| 37 | +- [x] Refactor `nix/modules/global-services.nix` to use shared core |
| 38 | +- [x] Add obvious devenv import path: `nix/modules/devenv/devenv.nix` |
| 39 | +- [x] Add compatibility shim: `nix/modules/devenv.nix` |
| 40 | +- [x] Eval sanity checks: |
| 41 | + - [x] `nix eval '.#devenvModules.default'` |
| 42 | + - [x] `nix eval '.#lib'` |
| 43 | + - [x] `nix eval '.#nixosModules.default'` |
| 44 | + |
| 45 | +### Next (core parity module-by-module) - COMPLETED |
| 46 | +- [x] **Ports**: moved deterministic port computation into `nix/lib/core/ports.nix`; `nix/modules/ports.nix` now uses shared core. |
| 47 | +- [x] **Caddy**: module already uses `nix/lib/caddy.nix` as the single source of truth; no duplicated behavior. |
| 48 | +- [x] **Network / Step CA**: module already uses `nix/lib/network.nix` as the single source of truth. |
| 49 | +- [x] **AWS**: module already uses `nix/lib/aws.nix` as the single source of truth. |
| 50 | +- [x] **Theme / starship**: module already uses `nix/lib/theme.nix` as the single source of truth. |
| 51 | +- [x] **IDE integration**: |
| 52 | + - [x] Already separates "pure generation" vs "impure merge existing settings" as explicit options (`existing-settings-path`) |
| 53 | + - [x] Module only wires + writes files; `nix/lib/integrations/ide.nix` generates content |
| 54 | + |
| 55 | +### Optional (high-leverage) |
| 56 | +- [ ] Centralize shared option types/defaults in `nix/lib/modules/options.nix` (or `nix/modules/options/`) so both adapters share one schema. |
| 57 | +- [ ] Update docs to recommend `stackpanel/nix/modules/devenv` import path everywhere. |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## Summary of Changes (2024-12-23) |
| 62 | + |
| 63 | +### Files Created |
| 64 | +- `nix/lib/core/ports.nix`: Pure port computation library with functions: |
| 65 | + - `computeBasePort`: Deterministic port from project name |
| 66 | + - `computeServicePort`: Port for a service by index |
| 67 | + - `computeServicesWithPorts`: Compute ports for a list of services |
| 68 | + - `mkServicesByKey`: Create lookup attrset by service key |
| 69 | + - `mkServiceEnvVars`: Generate environment variables for services |
| 70 | + - `mkPortsConfig`: Convenience function for full port configuration |
| 71 | + |
| 72 | +### Files Modified |
| 73 | +- `nix/modules/ports.nix`: Now imports and uses `nix/lib/core/ports.nix` for all computation |
| 74 | +- `nix/lib/default.nix`: Added `ports` export for port computation utilities |
| 75 | +- `nix/lib/devshell.nix`: Added `ports` export for mkShell users |
| 76 | + |
| 77 | +### Architecture |
| 78 | +The codebase now follows a consistent pattern where: |
| 79 | +1. **Core libraries** (`nix/lib/core/*.nix`, `nix/lib/*.nix`): Pure functions that implement behavior |
| 80 | +2. **Modules** (`nix/modules/*.nix`): Thin adapters that define options and call core libraries |
0 commit comments