build: cross-compile with Nix gcc toolchains instead of zig; drop macOS; v0.1.1
ci / release (push) Canceled after 47m21s

This commit is contained in:
2026-11-22 17:56:42 +01:00
parent 82ffcaf783
commit 32ec81ff6c
8 changed files with 118 additions and 112 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
# Gitea Actions, triggered only by pushed v* tags (the runner is slow): tests, then release. # Gitea Actions, triggered only by pushed v* tags (the runner is slow): tests, then release.
# Runs on the NixOS host runner (label `nixos`, host mode) with plain shell steps only. # Runs on the NixOS host runner (label `nixos`, host mode) with plain shell steps only.
# Tooling comes from the host's devbox, which honours devbox.lock like local runs. # Tooling comes from the host's devbox (honours devbox.lock); cross gcc comes from nix shell.
# Release upload goes through the Gitea REST API with the built-in job token. # Release upload goes through the Gitea REST API with the built-in job token.
name: ci name: ci
on: on:
+12 -8
View File
@@ -3,7 +3,7 @@
Nim CLI (stdlib only, no nimble deps) for a file-based knowledge-graph memory. Nim CLI (stdlib only, no nimble deps) for a file-based knowledge-graph memory.
## Tooling: devbox ## Tooling: devbox
All tooling comes from devbox (nim, zig, git). Use `devbox shell` or All tooling comes from devbox (nim, git, file). Cross toolchains come from Nix (below). Use `devbox shell` or
`devbox run <script>`. Fixing devbox.json / devbox.lock or running `devbox run <script>`. Fixing devbox.json / devbox.lock or running
`devbox update` needs no permission. `devbox update` needs no permission.
@@ -11,19 +11,23 @@ All tooling comes from devbox (nim, zig, git). Use `devbox shell` or
- `src/sirmem/core.nim`: all logic; every command is a `cmdXxx` proc returning its output, `dispatch` routes args. - `src/sirmem/core.nim`: all logic; every command is a `cmdXxx` proc returning its output, `dispatch` routes args.
- `src/sirmem.nim`: thin main (`--dir`/`SIRMEM_DIR`, exit codes, stderr). - `src/sirmem.nim`: thin main (`--dir`/`SIRMEM_DIR`, exit codes, stderr).
- `tests/test_all.nim`: std/unittest; the CLI suite runs `bin/sirmem`, so build first. - `tests/test_all.nim`: std/unittest; the CLI suite runs `bin/sirmem`, so build first.
- `scripts/build.sh`, `scripts/zigcc`: cross-compile with `zig cc`. - `scripts/build.sh`: cross-compile with Nix-provided gcc toolchains.
- `.gitea/workflows/release.yml`: CI (test on push, release on `v*` tag). - `.gitea/workflows/release.yml`: CI (test on push, release on `v*` tag).
## Build / test ## Build / test
- `devbox run build` -> `bin/sirmem` - `devbox run build` -> `bin/sirmem`
- `devbox run test` (builds the binary, then runs the tests) - `devbox run test` (builds the binary, then runs the tests)
## Cross-compile (zig cc) ## Cross-compile (Nix cross gcc)
`scripts/build.sh <nim-os> <nim-cpu> <zig-target> <out>`, or `devbox run cross-all` -> `dist/`. `scripts/build.sh <linux-x86_64|linux-aarch64|windows-x86_64> [out]`, or `devbox run cross-all` -> `dist/`.
Targets: linux amd64 `x86_64-linux-musl`, linux arm64 `aarch64-linux-musl`, Follows https://nim-lang.org/docs/nimc.html#crossminuscompilation: the script runs `nix shell nixpkgs#<pkg>.stdenv.cc`
macosx arm64 `aarch64-macos`, windows amd64 `x86_64-windows-gnu`. and sets `--os/--cpu` plus `--<cpu>.<os>.gcc.exe/.linkerexe/.path` (and plain `--gcc.*`, since the
Known zig 0.16 quirks (documented in build.sh): do not use `--opt:size`; linux-musl needs per-platform ones are ignored when target == host).
`-d:useMalloc`; linux arm64 needs `--tlsEmulation:on` and links flakily, so the script retries. - linux-x86_64: `pkgsCross.musl64`, static. linux-aarch64: `pkgsCross.aarch64-multiplatform-musl`, static.
- windows-x86_64: `pkgsCross.mingwW64`, `-d:mingw`, static; needs `windows.mcfgthreads` on `-L` (gcc 15 threading lib).
Supported targets: linux x86_64, linux aarch64, windows x86_64. **No macOS build** (dropped; no darwin toolchain).
Quirk: nim.cfg pins the mingw `gcc.path` to /usr/bin, so the script overrides `.gcc.path`.
The Nim version must be the devbox one (>= 2.2.12); older system nims fail to compile.
## Conventions ## Conventions
- Errors: raise `SirmemError` (via `fail`); main prints `sirmem: error: ...`, exit 1. Warnings go to `Store.warnings` (stderr). - Errors: raise `SirmemError` (via `fail`); main prints `sirmem: error: ...`, exit 1. Warnings go to `Store.warnings` (stderr).
+5
View File
@@ -22,3 +22,8 @@ Build: `devbox run build`, test: `devbox run test`. See AGENTS.md.
Extra over the spec: `init` command; env `SIRMEM_MACHINE` overrides the detected hostname. Extra over the spec: `init` command; env `SIRMEM_MACHINE` overrides the detected hostname.
MIT licensed. MIT licensed.
## Releases
Prebuilt, statically linked binaries for linux x86_64, linux aarch64 and windows x86_64 are attached to each
release (`sirmem-linux-x86_64`, `sirmem-linux-aarch64`, `sirmem-windows-x86_64.exe`, plus `SHA256SUMS`).
There is no macOS build for now. They are cross-built with Nix-provided gcc toolchains (`devbox run cross-all`).
+5 -6
View File
@@ -2,8 +2,8 @@
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.17.2/.schema/devbox.schema.json", "$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.17.2/.schema/devbox.schema.json",
"packages": [ "packages": [
"nim@latest", "nim@latest",
"zig@latest", "git@latest",
"git@latest" "file@latest"
], ],
"shell": { "shell": {
"scripts": { "scripts": {
@@ -14,10 +14,9 @@
"mkdir -p bin && nim c -d:release --hints:off -o:bin/sirmem src/sirmem.nim && nim c -r --hints:off -o:bin/test_all tests/test_all.nim" "mkdir -p bin && nim c -d:release --hints:off -o:bin/sirmem src/sirmem.nim && nim c -r --hints:off -o:bin/test_all tests/test_all.nim"
], ],
"cross-all": [ "cross-all": [
"scripts/build.sh linux amd64 x86_64-linux-musl dist/sirmem-linux-x86_64", "scripts/build.sh linux-x86_64",
"scripts/build.sh linux arm64 aarch64-linux-musl dist/sirmem-linux-aarch64", "scripts/build.sh linux-aarch64",
"scripts/build.sh macosx arm64 aarch64-macos dist/sirmem-macos-aarch64", "scripts/build.sh windows-x86_64"
"scripts/build.sh windows amd64 x86_64-windows-gnu dist/sirmem-windows-x86_64.exe"
] ]
} }
} }
+65 -64
View File
@@ -1,6 +1,71 @@
{ {
"lockfile_version": "1", "lockfile_version": "1",
"packages": { "packages": {
"file@latest": {
"last_modified": "2026-10-02T22:55:44Z",
"resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#file",
"source": "devbox-search",
"version": "5.48",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/y44vpziwbzfm8020dzlqqbybyyy7lb6g-file-5.48",
"default": true
},
{
"name": "man",
"path": "/nix/store/7yg6zjnp45r9wmqcl25j53la8kq4vgqx-file-5.48-man",
"default": true
},
{
"name": "dev",
"path": "/nix/store/dwcwy285s7cxb2j17czakarhdhhy7ckm-file-5.48-dev"
}
],
"store_path": "/nix/store/y44vpziwbzfm8020dzlqqbybyyy7lb6g-file-5.48"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/66sd1x1hls0yijibn00iz08jh4fnas9z-file-5.48",
"default": true
},
{
"name": "man",
"path": "/nix/store/zxp92l25sfj2hlk18khjhzq1gq4s6596-file-5.48-man",
"default": true
},
{
"name": "dev",
"path": "/nix/store/0164l0qhhgf7q5lbpyqi0iplk3l9p0q9-file-5.48-dev"
}
],
"store_path": "/nix/store/66sd1x1hls0yijibn00iz08jh4fnas9z-file-5.48"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/g0fbnccaan8q0rxlw43xnbyb90i94vlc-file-5.48",
"default": true
},
{
"name": "man",
"path": "/nix/store/8za7zyz740a9zhdzpfxsm4bw3g9pw6g0-file-5.48-man",
"default": true
},
{
"name": "dev",
"path": "/nix/store/gqv62wzqrdmdspyrzwx4b028rvf1yshv-file-5.48-dev"
}
],
"store_path": "/nix/store/g0fbnccaan8q0rxlw43xnbyb90i94vlc-file-5.48"
}
}
},
"git@latest": { "git@latest": {
"last_modified": "2026-10-08T19:21:32Z", "last_modified": "2026-10-08T19:21:32Z",
"resolved": "github:NixOS/nixpkgs/8edc0c72e3a38faf5434e40d1f19431125fe4b30#git", "resolved": "github:NixOS/nixpkgs/8edc0c72e3a38faf5434e40d1f19431125fe4b30#git",
@@ -100,70 +165,6 @@
"store_path": "/nix/store/0cbdz34lsakfirycixckf6ypsd8hazhg-x86_64-unknown-linux-gnu-nim-wrapper-2.2.12" "store_path": "/nix/store/0cbdz34lsakfirycixckf6ypsd8hazhg-x86_64-unknown-linux-gnu-nim-wrapper-2.2.12"
} }
} }
},
"zig@latest": {
"last_modified": "2026-10-02T22:55:44Z",
"resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#zig",
"source": "devbox-search",
"version": "0.16.0",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/1yqcgzbny4k5p4b9irn5vw4vy8b5d5dh-zig-0.16.0",
"default": true
},
{
"name": "doc",
"path": "/nix/store/5d4ycm0d1mc7j7a58b7bj1b22mw4cprr-zig-0.16.0-doc"
}
],
"store_path": "/nix/store/1yqcgzbny4k5p4b9irn5vw4vy8b5d5dh-zig-0.16.0"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/amh3bnymjncd56jwmd2hqdkciz9d7pys-zig-0.16.0",
"default": true
},
{
"name": "doc",
"path": "/nix/store/nggishdy0y2cqx3p1js220m1jl76gkmh-zig-0.16.0-doc"
}
],
"store_path": "/nix/store/amh3bnymjncd56jwmd2hqdkciz9d7pys-zig-0.16.0"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/7wsm80i713618s84j1hw2nk2v58zf081-zig-0.16.0",
"default": true
},
{
"name": "doc",
"path": "/nix/store/6h4nc1xc8kr3wfn7f4f6ma4my48mn9qk-zig-0.16.0-doc"
}
],
"store_path": "/nix/store/7wsm80i713618s84j1hw2nk2v58zf081-zig-0.16.0"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/qsk2cha4nk70bpz53qix9mypj4mjcbsr-zig-0.16.0",
"default": true
},
{
"name": "doc",
"path": "/nix/store/7g6azggrab0gzrcw2vrbzl0i33pb762d-zig-0.16.0-doc"
}
],
"store_path": "/nix/store/qsk2cha4nk70bpz53qix9mypj4mjcbsr-zig-0.16.0"
}
}
} }
} }
} }
+29 -20
View File
@@ -1,24 +1,33 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# usage: scripts/build.sh <nim-os> <nim-cpu> <zig-target> <output> # usage: scripts/build.sh <linux-x86_64|linux-aarch64|windows-x86_64> [output]
# e.g. scripts/build.sh windows amd64 x86_64-windows-gnu dist/sirmem-windows-x86_64.exe # Cross-compiles following https://nim-lang.org/docs/nimc.html#crossminuscompilation
# with the cross gcc from nixpkgs, fetched on demand through `nix shell`.
set -euo pipefail set -euo pipefail
cd "$(dirname "$0")/.." cd "$(dirname "$0")/.."
os=$1 cpu=$2 target=$3 out=$4 target=${1:?target: linux-x86_64 | linux-aarch64 | windows-x86_64}
case $target in
linux-x86_64)
out=${2:-dist/sirmem-linux-x86_64}; pkg=pkgsCross.musl64; triple=x86_64-unknown-linux-musl
nimcpu=amd64 nimos=linux; flags=(--passL:-static) ;;
linux-aarch64)
out=${2:-dist/sirmem-linux-aarch64}; pkg=pkgsCross.aarch64-multiplatform-musl; triple=aarch64-unknown-linux-musl
nimcpu=arm64 nimos=linux; flags=(--passL:-static) ;;
windows-x86_64)
out=${2:-dist/sirmem-windows-x86_64.exe}; pkg=pkgsCross.mingwW64; triple=x86_64-w64-mingw32
nimcpu=amd64 nimos=windows
# gcc 15 mingw threads via mcfgthread: its import lib is not on gcc's default search path
mcf=$(nix build --no-link --print-out-paths "nixpkgs#$pkg.windows.mcfgthreads")
flags=(-d:mingw --passL:-static "--passL:-L$mcf/lib") ;;
*) echo "unknown target $target" >&2; exit 2 ;;
esac
mkdir -p "$(dirname "$out")" mkdir -p "$(dirname "$out")"
export ZIG_TARGET=$target # `nix shell` puts <triple>-gcc on PATH. Nim's <cpu>.<os>.gcc.* settings (as in the Nim
# Notes from trial and error with zig 0.16: --opt:size breaks linking (empty undefined # cross-compilation docs) are what a cross build needs; they are ignored when target == host,
# symbols); -d:useMalloc is needed on linux-musl; aarch64-linux needs --tlsEmulation:on # so plain gcc.* is set too. nim.cfg pins the mingw gcc.path to /usr/bin, so path is set too.
# (TLS relocs fail to link otherwise) but x86_64-linux fails *with* it. export NIMCPU=$nimcpu NIMOS=$nimos TRIPLE=$triple OUT=$out TARGET=$target
extra=() exec nix shell "nixpkgs#$pkg.stdenv.cc" --command bash -c '
if [ "$os" = linux ] && [ "$cpu" = arm64 ]; then extra+=(--tlsEmulation:on); fi gcc=$TRIPLE-gcc; dir=$(dirname "$(command -v "$gcc")")
# zig cc 0.16 sporadically fails to link aarch64-linux ("undefined symbol:" with an empty exec nim c -d:release -d:strip --hints:off --os:$NIMOS --cpu:$NIMCPU "$@" \
# name, also seen as CacheCheckFailed); a retry succeeds, so retry up to 5 times. --gcc.exe:"$gcc" --gcc.linkerexe:"$gcc" \
for attempt in 1 2 3 4 5; do --$NIMCPU.$NIMOS.gcc.path:"$dir" --$NIMCPU.$NIMOS.gcc.exe:"$gcc" --$NIMCPU.$NIMOS.gcc.linkerexe:"$gcc" \
rm -rf "nimcache/$target" --nimcache:"nimcache/$TARGET" -o:"$OUT" src/sirmem.nim' _ "${flags[@]}"
if nim c -d:release -d:strip -d:useMalloc --parallelBuild:1 --hints:off \
"${extra[@]}" --os:"$os" --cpu:"$cpu" \
--cc:clang --clang.exe:"$PWD/scripts/zigcc" --clang.linkerexe:"$PWD/scripts/zigcc" \
--nimcache:"nimcache/$target" -o:"$out" src/sirmem.nim ; then exit 0; fi
echo "build attempt $attempt failed, retrying" >&2
done
exit 1
-12
View File
@@ -1,12 +0,0 @@
#!/usr/bin/env bash
# C compiler/linker wrapper so Nim can use `zig cc` for cross-compiling.
# Needs ZIG_TARGET (e.g. aarch64-linux-musl). Filters flags zig cc rejects.
args=()
for a in "$@"; do
case "$a" in
-Wl,--no-gc-sections|-Wl,-rpath*|-fpie|-pie) ;;
-ldl) case "$ZIG_TARGET" in *windows*) ;; *) args+=("$a") ;; esac ;;
*) args+=("$a") ;;
esac
done
exec zig cc -target "${ZIG_TARGET:?ZIG_TARGET not set}" "${args[@]}"
+1 -1
View File
@@ -6,7 +6,7 @@ import std/[os, strutils, sequtils, algorithm, tables, times, osproc,
nativesockets, deques] nativesockets, deques]
const const
Version* = "0.1.0" Version* = "0.1.1"
CycleVerbs* = ["part-of", "supersedes"] CycleVerbs* = ["part-of", "supersedes"]
DefaultVerb* = "relates-to" DefaultVerb* = "relates-to"