Files
FFF_vtt_frozen/docs/research/q20-nix-packaging.md
T
SirStoneandClaude Haiku 5.5 9b3b154fc8 docs: add Q13/Q20 research notes
Q13: mummy slow WebSocket clients (pin 01deb1f), option analysis.
Q20: Nix packaging of the Nim server (buildNimPackage, nim_lk lock, NixOS module).

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-08 11:21:19 +01:00

144 lines
14 KiB
Markdown

# Q20: Nix packaging of the Nim server + NixOS module
Date: 2026-11-08. Scope: research only. No repo code changed. Refs: `docs/DESIGN.md` Q20 (line 563) and §11.1 (line 445).
Source pins:
- NP = nixpkgs `c56a87d8e41ce69bd2af57eb35dcc117a9dfca33` (26.05.20261105). This is the owner's system nixpkgs (`nixos-version --json`). Links below use this commit. Local store copy: `/nix/store/qyccs9pc22b3fvl2lxia3jagcg97dn48-source` (nim 2.2.4).
- MUM = mummy commit `01deb1ff694988635c7bf0f24f6088a0bdf6db5d`.
- DEV = `devbox.lock` (local, `/home/davide/Projects/GDR/FFF_vtt/devbox.lock`): nixpkgs `55ba7f49ef2962b42cbd126522b7df5f95037679`, nim 2.2.12, nimble 0.24.1.
## 1. Summary
1. nixpkgs has a documented Nim builder: `buildNimPackage` plus a JSON `lockFile`, generated by `nim_lk`. The owner's system nixpkgs (NP) has it. [S1][S2]
2. Lock entries carry the source fetch: `method` `fetchzip` (tarball) or `git` (`url`, `rev`, `sha256`, `srcDir`, `fetchSubmodules`). The builder turns each into a fixed-output fetch plus `--path:` flag. Git-commit deps are fetched with `fetchgit` at the exact `rev`. [S1][S7][S8]
3. nim_builder ignores `requires` in the .nimble. Deps come only from the lock. The lock must list the full transitive closure. [S5][S1]
4. Not in nixpkgs: `mummy`, `db_connector`, `zippy`, `webby`, `crunchy`, `nimsimd` (no `by-name` entry, no `nimPackages`). They enter via the lock. [checked NP tree]
5. Nim 2.2.4 stdlib has no `db_*` module. `db_connector` must be a separate lock entry. [local stdlib]
6. Upstream now HAS tag `0.4.8` on mummy. DESIGN says "no tags" (stale). Tag `0.4.8` = `77a7563e`. MUM is 7 commits ahead; only `.github/workflows/*` differ. Library code is identical. [S13][S14]
7. nim_builder builds only nimble `bin` entries. Root `fff_vtt.nimble` has none and `server/` has no entrypoint yet (only `geometry.nim`). The Nix build would produce an empty package until a `bin` is declared. [S5][local]
8. Devbox pins nim 2.2.12. NP's nim-2_2 is 2.2.4. Version drift must be handled by pinning the nixpkgs input, not by hoping. [DEV][S10]
9. `.gitignore` already excludes `nix/`. DESIGN M6 plans `nix/`. Either remove that line or use another dir name before committing packaging. [local `.gitignore`]
10. Recommended: `buildNimPackage` + committed `nix/lock.json` (from `nim_lk`) + `nix/module.nix` modeled on nitter's systemd unit. Devbox stays the dev shell and is untouched.
## 2. Findings with sources
### 2.1 buildNimPackage (NP)
- Builder: `pkgs/build-support/build-nim-package.nix`. Phases delegate to `nim_builder --phase:{configure,build,check,install}`. `doCheck = true` by default. [S1]
- `lockFile` is read with `builtins.fromJSON`. Each `depends` entry becomes `--path:"<fod>/<srcDir>"` in `nimFlags`. Methods: `fetchzip` -> `fetchzip {url,sha256}`; `git` -> `fetchgit {url,rev,sha256,fetchSubmodules,leaveDotGit}`. `path` in the lock is ignored. [S1]
- `nimOverrides` maps a dep name to a function of (lockAttrs, prevAttrs) to add buildInputs etc. Mummy needs none (pure Nim). [S3][S2]
- `overrideNimAttrs` exists to change `src`/`lockFile` before lock flags are generated. [S2][S1]
- Nim: `nim2 = nim-2_2` (NP `pkgs/top-level/all-packages.nix` L4248). Nim 2.2.4 in NP. [S10][all-packages]
- Doc for the example and lock regeneration: `nix run -f . nim_lk ./result | jq --sort-keys > pkgs/by-name/.../lock.json`. The doc says run `nim_lk` on the source directory. [S2]
### 2.2 nim_builder behavior that matters
- `configurePhase`: writes `config.nims` (backend, nimcache, `-d:release`, `nimDefines`). It also adds `path` switches from `NIX_NIM_BUILD_INPUTS` *nimble files*, not from `requires`. [S5]
- `buildPhase`: `nim compile <nimFlags> --outdir:$out/bin <srcDir>/<bin>` for each `bin` in the root .nimble. No `bin` = nothing built. [S5]
- `checkPhase`: `nim r <nimFlags> tests/t*.nim` (top-level `tests/` only). Root has `tests/tgeometry.nim`, which matches. [S5][local]
- Root `nim.cfg` has `--mm:orc` and `--path:"server"`; no `--threads:on`. Nim 2.2 stdlib config defaults `threads:on` (local `.../nim/config/nim.cfg` L23). Mummy needs threads on. Keep it explicit in `nimFlags` anyway. [local stdlib][MUM README]
### 2.3 Lock file format (NP)
- Generator: `nim_lk` (sourcehut `~ehmry/nim_lk`; NP `pkgs/by-name/ni/nim_lk/package.nix`). It reads Nimble metadata and emits `{"depends":[...]}`. [S4][S2]
- Entry fields seen: `method`, `packages`, `path`, `ref`, `rev`, `sha256`, `srcDir`, `url`; git entries also `subDir`, `fetchSubmodules`, `leaveDotGit`. [S7][S8]
- Example git entry: `pkgs/by-name/ni/nimlangserver/lock.json`. Example fetchzip entry: `pkgs/by-name/ni/nimdow/lock.json`. [S7][S8]
- nim_lk README: sourcehut is bot-walled from my environment. I could NOT read the README. Whether `nim_lk` accepts `requires "pkg#<commit>"` and whether it reads `nimble.lock` is UNVERIFIED. Test it.
### 2.4 Dependency graph and revisions
Pinned in the owner's devbox nimble cache (`prototype-server-vision/.devbox/nimble/pkgs2/*/nimblemeta.json`, `vcsRevision`). Use as the first lock target, then compare with nim_lk output.
| dep | constraint in mummy/registry | devbox-resolved vcsRevision | upstream tag check |
|---|---|---|---|
| mummy | `requires "mummy#01deb1ff..."` (servervision.nimble) | `01deb1ff694988635c7bf0f24f6088a0bdf6db5d` | tag `0.4.8` = `77a7563e` [S13][S14] |
| zippy | mummy: `>= 0.10.9` | `cd562ba43ae8904a4d1ba55611b8a521ef4a179e` (0.10.20) | tag exists [API] |
| webby | mummy: `>= 0.2.1` | `325d6ade0970562bee7d7d53961a2c3287f0c4bc` (0.2.1); repo `treeform/webby` | no tags via API |
| crunchy | mummy: `>= 0.1.11` | `98eb6526982bb8aae8eec6e8781f4539fa19e049` (0.1.11) | tag exists [API] |
| nimsimd | crunchy dep | `3f6b2668ffb0867d0bf786a658b817763e611350` (1.3.2) | tag exists [API] |
| db_connector | fff: `requires "db_connector"` (unpinned) | `29450a2063970712422e1ab857695c12d80112a6` (0.1.0) | no tags via API [S15] |
- mummy nimble (at MUM): `requires nim >= 1.6.8`, `zippy >= 0.10.9`, `webby >= 0.2.1`, `crunchy >= 0.1.11`. [S12]
- db_connector nimble: `requires nim >= 1.7.3`, no other deps. [S15]
- Mummy commit diff vs tag 0.4.8: only `.github/workflows/build.yml` and `docs.yml`. [S13]
- Mummy README: needs `--threads:on` and `--mm:orc` or `--mm:arc`. [MUM README]
### 2.5 Are the deps in nixpkgs?
- Checked NP tree: `pkgs/by-name` has no `mummy`, `db_connector`, `zippy`, `webby`, `crunchy`, `nimsimd` directories. Grep of `pkgs/` for `mummy|db_connector` only hits unrelated files. No Nim package set (`nimPackages` is empty). [NP tree]
- Conclusion: all deps go through the lock. No nixpkgs package to reuse.
### 2.6 Alternatives
- **nimble.lock (Nimble native)**: `nimble.lock`, version 2, fields `vcsRevision`, `url`, `downloadMethod`, `checksums.sha1`. Written by `src/nimblepkg/lockfile.nim`. No nixpkgs builder consumes it. Converting it would be a custom script. [S18]
- **atlas**: documents `atlas.lock` via `pin` / `rep` commands. nixpkgs `nim-atlas` is pinned to `unstable-2023-09-22` (`ab22f997`). No nixpkgs builder reads `atlas.lock`. [S17][S11]
- **Vendored deps** (no lock tool): drop `lockFile`, commit deps (or `fetchFromGitHub` each) and pass `nimFlags = ["--path:..." ...]`. `buildNimPackage` makes `lockFile` optional (`optionalAttrs (hasAttr "lockFile")`). Simplest, zero tooling, but every bump is manual. [S1]
### 2.7 NixOS module precedent
- nitter: `nixos/modules/services/misc/nitter.nix` at NP, lines ~369-420. Unit: `DynamicUser`, `StateDirectory`, `LoadCredential`, `ProtectSystem`-style hardening block: `ProtectHome`, `PrivateDevices`, `ProtectKernel*`, `RestrictAddressFamilies = [AF_INET AF_INET6]`, `SystemCallFilter`, `UMask = 0077`. [S9]
- Nitter package: `pkgs/by-name/ni/nitter/package.nix`, `lockFile = ./lock.json`, `nixosTests`. [S-nitter-pkg]
- Note: nitter's `RestrictAddressFamilies` excludes `AF_UNIX`. DESIGN §11.1 includes `AF_UNIX`; keep it only if the FFF sidecar (Q18/Q19 option b) is chosen. [DESIGN L447]
### 2.8 Owner's host config
- `/home/davide/Projects/server_nixos_configuration/configuration.nix`: channel-style (`<agenix/modules/age.nix>`), not flake. Imports per-service files (`gitea.nix`, `foundryvtt.nix`). [local]
- Precedent for a service module file imported from `imports = [...]`. [local `configuration.nix` L13-23]
### 2.9 Devbox coexistence
- `devbox.json`: `nim: "2"`, `nimble: "latest"`, `NIMBLE_DIR=$DEVBOX_PROJECT_ROOT/.devbox/nimble`; scripts `test` = `nimble test`, `mutation`. [local devbox.json]
- `devbox.lock` resolves nim `2.2.12` (nixpkgs `55ba7f49`), nimble `0.24.1`. [DEV]
- Nix build uses NP nim `2.2.4` unless the flake pins a nixpkgs with 2.2.12. Mismatch risk.
- `nimble test` resolves deps from `requires` (registry + `#commit` pin). The Nix build resolves from the lock. Two resolution paths. Keep revs equal; check `vcsRevision` values above after lock generation.
- `.devbox/` is gitignored; Nix never sees it. No interference, provided the Nix package source does not depend on `.devbox/`.
## 3. Recommendation
Use `buildNimPackage` + committed `lock.json` generated by `nim_lk`, pinned to the same nixpkgs as devbox (or a nixpkgs with nim 2.2.12). Package and module live in-repo under `nix/`. Devbox stays the dev shell only.
Steps (sketch, no files written):
1. Un-ignore the packaging dir: remove `nix/` from `.gitignore` (or name it `packaging/nix/`). [local]
2. Declare a `bin` in `fff_vtt.nimble` once the entrypoint exists (for example `bin = @["main"]`, `srcDir` `server`). Until then the package is empty. Alternative: override `buildPhase`. [S5]
3. Decide mummy ref: keep `01deb1ff` (identical code to 0.4.8 except CI) or move both nimble and Nix to tag `0.4.8`. Fix the DESIGN "no tags" note either way. [S13]
4. Add `db_connector` pin to the lock (no tags upstream; pin the devbox `vcsRevision`). [S15]
5. Generate lock: `nix run <nixpkgs-with-nim_lk>#nim_lk -- <src> | jq --sort-keys > nix/lock.json`. Diff `rev`s against the devbox table in §2.4. [S2]
6. `nix/package.nix`: `buildNimPackage (finalAttrs: { pname = "fff-vtt-server"; version; src = lib.cleanSource ../.; lockFile = ./lock.json; nimFlags = ["--threads:on" "--mm:orc"]; ... })`. Let doCheck run `tests/t*.nim`. [S1][S5]
7. `nix/module.nix`: `options.services.fffVtt = { enable; package; port (127.0.0.1 only); publicOrigin; stateDir }`. `systemd.services.fff-vtt`: `wantedBy multi-user.target`, `DynamicUser`, `StateDirectory`, `ExecStart = ${cfg.package}/bin/<bin>`, hardening copied from nitter (§2.7), `AF_UNIX` only with sidecar. [S9][DESIGN L447]
8. Host: add `nix/module.nix` to `imports` in `server_nixos_configuration/configuration.nix` (path import or `builtins.fetchGit` pinned to a commit). Mirror `foundryvtt.nix` pattern. [local]
9. Smoke check: `nix build` the package; `nix-build` runs `nim r tests/t*.nim` via doCheck. Optional `nixosTests` VM test as nitter does. [S1][S9]
Fallback if `nim_lk` cannot resolve `mummy#<commit>` or db_connector: vendored deps with `nimFlags --path:` (§2.6). Nothing else changes.
## 4. Not checked / risks
- nim_lk CLI and its handling of `#commit` requires: unverified (sourcehut blocked).
- No `nix build` run; no lock generated; no fetch hashes computed. Hashes will come from `nim_lk`.
- Devbox nim 2.2.12 vs NP 2.2.4: pinned nixpkgs revision for the build not chosen yet.
- webby tags: none found via API. Pinned rev `325d6ade` is what the devbox resolved; upstream version list not checked.
- `nim_builder`'s `bin` handling assumes the root .nimble lists binaries; checked only the code, not a built example.
- atlas `atlas.lock` format not read in full; only the doc summary.
## 5. Sources
NixOS/nixpkgs @ c56a87d8e41ce69bd2af57eb35dcc117a9dfca33 (owner's system nixpkgs):
- [S1] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/build-support/build-nim-package.nix
- [S2] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/doc/languages-frameworks/nim.section.md
- [S3] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/top-level/nim-overrides.nix
- [S4] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/by-name/ni/nim_lk/package.nix
- [S5] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/by-name/ni/nim_builder/nim_builder.nim
- [S7] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/by-name/ni/nimdow/lock.json
- [S8] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/by-name/ni/nimlangserver/lock.json
- [S9] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/nixos/modules/services/misc/nitter.nix
- [S-nitter-pkg] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/by-name/ni/nitter/package.nix
- [S10] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/by-name/ni/nim-2_2/package.nix
- [S11] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/by-name/ni/nim-atlas/package.nix
- [all-packages] https://github.com/NixOS/nixpkgs/blob/c56a87d8e41ce69bd2af57eb35dcc117a9dfca33/pkgs/top-level/all-packages.nix (L4246-4255: nim, nim1, nim2, buildNimPackage, nimOverrides)
Upstream:
- [S12] mummy nimble @ pin: https://github.com/guzba/mummy/blob/01deb1ff694988635c7bf0f24f6088a0bdf6db5d/mummy.nimble
- [MUM README] https://github.com/guzba/mummy/blob/01deb1ff694988635c7bf0f24f6088a0bdf6db5d/README.md
- [S13] tag compare: https://api.github.com/repos/guzba/mummy/compare/77a7563e87a1045e4e407a3a68731e299fa3507e...01deb1ff694988635c7bf0f24f6088a0bdf6db5d
- [S14] mummy tags: https://api.github.com/repos/guzba/mummy/tags
- [S15] db_connector nimble: https://github.com/nim-lang/db_connector/blob/master/db_connector.nimble
- [S17] atlas doc: https://github.com/nim-lang/atlas/blob/master/doc/atlas.md
- [S18] nimble lockfile impl: https://github.com/nim-lang/nimble/blob/master/src/nimblepkg/lockfile.nim
- [API] zippy/crunchy/nimsimd tags: https://api.github.com/repos/guzba/zippy/tags , https://api.github.com/repos/guzba/crunchy/tags , https://api.github.com/repos/guzba/nimsimd/tags
Local (read-only):
- devbox.json / devbox.lock: /home/davide/Projects/GDR/FFF_vtt/devbox.json, /home/davide/Projects/GDR/FFF_vtt/devbox.lock
- devbox nimble cache (vcsRevision): /home/davide/Projects/GDR/FFF_vtt/prototype-server-vision/.devbox/nimble/pkgs2/*/nimblemeta.json
- nim 2.2.4 stdlib (threads default, no db_* modules): /nix/store/aa4fgarndnvq417l50imvqx5fdrrx9sn-nim-unwrapped-2.2.4/nim/config/nim.cfg, /nix/store/aa4fgarndnvq417l50imvqx5fdrrx9sn-nim-unwrapped-2.2.4/nim/lib
- owner config: /home/davide/Projects/server_nixos_configuration/configuration.nix, foundryvtt.nix
- repo .gitignore: /home/davide/Projects/GDR/FFF_vtt/.gitignore