← All languages

Servirtium for

Nim

No shipped package: vendor the binding's source at a release tag, with the native library from the release. Release v2.0.0-alpha.20.

FFI mechanism
{.importc.} + {.passL.} link at compile time. src/servirtium/native.nim declares the aether_vcr_embed_* ABI with importc/cdecl and links libservirtium_vcr when nim c runs, with its directory baked in as an rpath.
Toolchain
Nim 2.x and a C compiler (verified with Nim 2.4.2; the README states no minimum). --threads:on only if your own code uses threads.
Distribution
Vendor the source at the tag (--path) + link the release native library
Finding the library
Linked at COMPILE time: SERVIRTIUM_VCR_LIB (read by staticExec when nim c runs) is the path of the file libservirtium_vcr.so/.dylib; its directory becomes -L and an rpath. Changing it later needs nimcache deleted or nim c -f.
On GitHub
nim/ · README

Getting it

There is no published package for Nim. You vendor the binding's source into your project at the release tag v2.0.0-alpha.20 and take the native library from the same release.

  1. Fetch the native library for your OS/arch, verify its checksum, and give it the plain name the linker wants (-lservirtium_vcr). Windows: take the .dll asset by hand.
    TAG=v2.0.0-alpha.20
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/$TAG
    case "$(uname -s)" in
      Linux)   OS=linux;   EXT=so;    ldd --version 2>&1 | grep -qi musl && LIBC=-musl || LIBC= ;;
      Darwin)  OS=macos;   EXT=dylib; LIBC= ;;
      FreeBSD) OS=freebsd; EXT=so;    LIBC= ;;
    esac
    case "$(uname -m)" in x86_64|amd64) ARCH=x86_64 ;; arm64|aarch64) ARCH=arm64 ;; esac
    LIBDIR="$HOME/.servirtium-vcr/$TAG"; mkdir -p "$LIBDIR"
    LIB=libservirtium_vcr-$TAG-$OS-$ARCH$LIBC.$EXT
    curl -fsSL -o "$LIBDIR/$LIB" "$REL/$LIB"
    curl -fsSL -o "$LIBDIR/$LIB.sha256" "$REL/$LIB.sha256"
    ( cd "$LIBDIR" && { sha256sum -c "$LIB.sha256" || shasum -a 256 -c "$LIB.sha256"; } )
    cp "$LIBDIR/$LIB" "$LIBDIR/libservirtium_vcr.$EXT"
    
  2. macOS only: the release .dylib carries a CI build path as its install name, so a binary linked against it fails to start with 'Library not loaded: /home/runner/...'. Rewrite the install name and re-sign before compiling anything against it.
    install_name_tool -id @rpath/libservirtium_vcr.dylib "$LIBDIR/libservirtium_vcr.dylib"
    codesign -f -s - "$LIBDIR/libservirtium_vcr.dylib"
    
  3. Take the Nim sources of the same tag into your project and put them on the compiler's path.
    git clone --depth 1 --branch v2.0.0-alpha.20 https://github.com/servirtium/servirtium-vcr.git third_party/servirtium-vcr
    # nim c ... --path:third_party/servirtium-vcr/nim/src   (or switch("path", ...) in config.nims)
    
  4. Check it: compile and replay the canonical tape (expect 'ok-body Ok'). SERVIRTIUM_VCR_LIB must be set when nim c runs, not at run time.
    export SERVIRTIUM_VCR_LIB="$LIBDIR/libservirtium_vcr.$EXT"
    mkdir -p tapes
    curl -fsSL -o tapes/single_get.md https://raw.githubusercontent.com/servirtium/servirtium-vcr/v2.0.0-alpha.20/nim/example/tapes/single_get.md
    cat > servirtium_check.nim <<'NIM'
    import servirtium
    import std/httpclient
    let vcr = playback("tapes/single_get.md")
    doAssert vcr.start()
    echo newHttpClient().getContent(vcr.baseUrl() & "/ok"), " ", vcr.lastKind()
    vcr.close()
    NIM
    nim c -r --hints:off --path:third_party/servirtium-vcr/nim/src servirtium_check.nim
    

Record

Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.

import servirtium
import std/httpclient

let srv = record("tapes/climate_api.md", "https://climatedataapi.worldbank.org")
doAssert srv.start()
# ... drive your SUT against srv.baseUrl() ...
discard newHttpClient().getContent(srv.baseUrl() & "/api/v1/countries")
srv.commit()      # the only way a recording becomes a tape

Playback

The same tape, replayed with no network.

import servirtium
import std/httpclient

let srv = playback("tapes/climate_api.md")   # port 0 = OS-assigned
doAssert srv.start()
defer: srv.close()

let body = newHttpClient().getContent(srv.baseUrl() & "/api/v1/countries")
doAssert srv.lastKind() == Ok                 # optional: assert a clean match

Idiomatic extras

Discard the recording when the test fails

close on a recording that was neither committed nor discarded raises VcrError and writes nothing; discardRecording drops it deliberately.

let srv = record(tape, upstream)
doAssert srv.start()
try:
  # ... drive the SUT, assert on it ...
  srv.commit()
except CatchableError:
  srv.discardRecording()
  raise

Redact credentials

redactCredentials scrubs the standard credential headers from the tape; redactHeader, redactQueryParam and redactJsonField cover named ones.

let srv = record(tape, upstream)
srv.redactCredentials()
srv.redactJsonField("secretKey")
doAssert srv.start()

Volatile values and drift detection

Rules tokenise values that change on every run so a re-record is byte-identical; failIfChanged then makes commit raise only on a real upstream change.

let srv = record(tape, upstream)
srv.volatileRule("response headers", "Date: {{serverDate}}")
srv.volatileRule("body", "(?<id>[0-9a-f]{32})", "regex")
srv.failIfChanged()
doAssert srv.start()
# ... drive SUT ...
srv.commit()   # raises VcrError on drift (the fresh tape is still written)

Gotchas

  • The library is linked, not dlopen-ed: use the same tag for source and library or the link fails with undefined reference to aether_vcr_embed_...
  • The binary records the library's directory as an rpath: do not delete ~/.servirtium-vcr/<tag> while binaries built against it are in use.
  • The library file must be named exactly libservirtium_vcr.so/.dylib; SERVIRTIUM_VCR_LIB must point at the FILE, not the directory.
  • Use the -musl asset on Alpine.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your Nim project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.

Hey Gemini or Claude or Codex,

I want a test in this Nim project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.

1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.

2. Add the Servirtium binding for Nim. Follow https://servirtium.dev/languages/nim/ exactly. Servirtium ships no package for this language. Vendor the binding's source into the project at the release tag v2.0.0-alpha.20 and put the native library from the same release where the page says: Vendor the source at the tag (--path) + link the release native library.
   Toolchain and minimum version: Nim 2.x and a C compiler (verified with Nim 2.4.2; the README states no minimum). --threads:on only if your own code uses threads..
   How the binding finds the native library: Linked at COMPILE time: SERVIRTIUM_VCR_LIB (read by staticExec when nim c runs) is the path of the file libservirtium_vcr.so/.dylib; its directory becomes -L and an rpath. Changing it later needs nimcache deleted or nim c -f.

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Nim way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.

4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for Nim is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.

5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.

6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.

7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.

Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- The library is linked, not dlopen-ed: use the same tag for source and library or the link fails with undefined reference to aether_vcr_embed_...
- The binary records the library's directory as an rpath: do not delete ~/.servirtium-vcr/<tag> while binaries built against it are in use.
- The library file must be named exactly libservirtium_vcr.so/.dylib; SERVIRTIUM_VCR_LIB must point at the FILE, not the directory.
- Use the -musl asset on Alpine.

References:
- This language's page: https://servirtium.dev/languages/nim/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/nim
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)

How these snippets were checked

Ran on cachyos (Linux x86_64, glibc, Nim 2.4.2) with the release libservirtium_vcr-v2.0.0-alpha.20-linux-x86_64.so (sha256 checked) and a vendored clone of the tag via --path: playback of single_get.md (ok-body, Ok), record against a local python http.server then commit() then replay (hello-upstream, Ok), discardRecording leaves no tape, close() without commit raises VcrError and writes nothing, redactCredentials in the record. The volatile/failIfChanged extra is from docs/usage.md and not run. macOS not run for Nim (no toolchain on the Mac); the macOS install-name problem was reproduced with the Lua, Zig builds against the same dylib on macOS arm64.

They mirror these files in the repository:

See also the feature reference, llms.txt and the nim/ directory.