Nim C Wrappers logo

Nim C Wrappers

Community
planetis-m
nim-c-wrappers

Turn low-level C FFI bindings into an idiomatic Nim API, with safer types, cleaner module boundaries, resource management, callbacks, enums, bitflags, and exception-based error handling. Use when building a high-level Nim wrapper around a C library or improving the usability of existing raw bindings.

Overview

Publisherplanetis-m
Repositoryskills
Skill namenim-c-wrappers
Stars
66
Forks
7
Bundled files
5
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 5 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by planetis-m on GitHub. Read the source before you install it.

Installation

Install the Nim C Wrappers AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/planetis-m/skills/tree/master/nim-c-wrappers
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/planetis-m/skills.git /tmp/skills
mkdir -p .claude/skills
cp -r /tmp/skills/nim-c-wrappers .claude/skills/nim-c-wrappers
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Nim C Wrappers in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Nim C Wrappers on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Nim C Wrappers is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Nim C Wrappers

Rules

Architecture

  • Use a two-layer design: raw FFI layer (ABI-faithful) + ergonomic Nim layer (safe, idiomatic). Keep the raw layer thin and stable.
  • Split modules by library domain. Mirror the C header structure for large libraries.
  • Centralize shared types in a lib_raw_types module to avoid cyclic imports.
  • Export raw symbols only for advanced users; re-export selected symbols from ergonomic modules.

Raw FFI Layer

  • Use importc with cdecl (or stdcall if the library requires it). Prefer {.push callconv: cdecl, importc.} blocks for shared conventions.
  • header declares linker-resolved imports; dynlib loads symbols at runtime. Do not combine them on one proc. Guard library names with when defined(windows): etc.
  • Never reorder struct fields. Use object in C field order. Add packed only if C headers specify packing.
  • Use incompleteStruct and list only needed fields to reduce ABI risk.
  • For C enums, use distinct integer types + const values. Do not use Nim enum in raw bindings. Find actual enum values in C headers—do not guess.
  • For bitflags, use distinct integer types with bitwise helpers. Do not use set[Enum].
  • Map C macros: numeric → const; function-like → template; use inline proc when arguments should evaluate once.
  • Keep pointer types as C intends — do not convert them in the raw layer.

Type Mapping

C TypeNim TypeNotes
charccharPlatform-dependent signedness
signed charcscharAlways int8
unsigned charuint8Safer default spelling; avoid relying on cuchar status across Nim versions
shortcshortAlways int16
unsigned shortcushortAlways uint16
intcintAlways int32
unsigned intcuintAlways uint32
longclongABI-sized (4 on Win64, 8 on LP64)
unsigned longculongABI-sized
long longclonglongAlways int64
unsigned long longculonglongAlways uint64
size_tcsize_tAlias for uint
intptr_tintPointer-sized signed
uintptr_tuintPointer-sized unsigned
floatcfloatAlways float32
doublecdoubleAlways float64
long doubleclongdoubleLimited codegen support
void*pointerUntyped
T*ptr TNullable
T**ptr ptr T
char* / const char*cstringNUL-terminated
char**cstringArrayptr UncheckedArray[cstring]

Struct types: object in C order. Fixed arrays: array[N, T]. Pointer+length: ptr T + csize_t. Raw buffers: ptr UncheckedArray[T]. Opaque handles: pointer or ptr OpaqueObj (empty object).

Ergonomic Layer

  • In =destroy, explicitly call =destroy on owned nested GC-managed fields (string, seq) after releasing C resources.
  • Define ownership hooks in the same module as the type. Cross-module hook definitions are a compile error.
  • Raise exceptions (IOError, ValueError, etc.) for C errors — do not return result wrappers that only carry ok/kind/message.
  • Do not create custom exception types unless callers handle them differently.
  • Catch errors only at translation boundaries (C return code → Nim exception, or exception → domain result). Let exceptions propagate otherwise.

Naming

  • In raw bindings, match C identifier names directly.
  • importc without an explicit string resolves the C symbol from the Nim name.
  • Strip redundant C prefixes (LIB_, foo_) in the ergonomic layer.
  • Keep raw constant names in C style (e.g., CURLE_OK).
  • When a C identifier collides with a Nim keyword, rename the Nim identifier and use importc: to map to the original C name.

Callbacks

  • Declare callbacks as plain C-callable procs such as proc onEvent(code: cint; userData: pointer) {.cdecl.}.
  • Pass closure state through userdata via rawProc/rawEnv. See references/callback_pattern.md.

Verification

  • Add static: doAssert sizeof(T) == N and offsetOf checks in tests to verify struct layouts.
  • Test ABI with compile + link + smoke test + runtime checks.

Workflow

  1. Read the C API. Identify structs, enums, functions, callbacks, and ownership semantics.
  2. Map types. Use the type table. Verify sizes with static: doAssert sizeof.
  3. Write raw bindings. One module per C header or subsystem. Centralize shared raw types to avoid cycles. Use importc, correct calling convention, packed only when C specifies it. Use typed integer aliases for enums.
  4. Write ergonomic wrappers. Add destructors for owned resources (move-only or RC). Raise exceptions for C errors. Re-export only the intended public surface. Strip prefixes.
  5. Test. Compile check → link check → smoke test → ABI sizeof/offset asserts → runtime ownership tests.
  6. Iterate. Add missing APIs as needed. Keep raw layer stable.

Common Mistakes

MistakeWhy it's wrong
Passing Nim string directly to char* parameterType mismatch; use .cstring
Using seq where C expects stable pointeradd() can reallocate, invalidating the pointer
Reordering struct fieldsBreaks ABI layout, causes garbage values
Using Nim enum for C enum valuesABI incompatibility; use typed integer aliases
Adding enum or constant values without inspecting headersFind actual enum values in C headers, do not guess
Passing closures as C callbacksC expects plain function pointers, not GC closures
Storing cstring beyond the callcstring from string.cstring dangles after the source is freed
=dup body using result = src in RC patternMay trigger implicit =copy instead of =dup; use field-by-field assignment
Wrong calling conventionCauses crashes at call sites

References

  • references/move_only_resource.md — A move-only wrapper for one owner of a C handle.
  • references/rc_resource.md — A reference-counted wrapper for shared C resource ownership.
  • references/callback_pattern.md — C callbacks with userdata, captured Nim state, and lifetime management.
  • references/enum_and_bitflags.md — Distinct-integer wrappers for non-contiguous enums and bitwise flags.
  • references/module_layout.md — Separating raw FFI from an ergonomic API in two modules.

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Nim C Wrappers AI skill do?

Turn low-level C FFI bindings into an idiomatic Nim API, with safer types, cleaner module boundaries, resource management, callbacks, enums, bitflags, and exception-based error handling. Use when building a high-level Nim wrapper around a C library or improving the usability of existing raw bindings.

Why use Nim C Wrappers on TypingMind?

Because you install it once and use it with any model. Nim C Wrappers is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Nim C Wrappers in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/planetis-m/skills/tree/master/nim-c-wrappers. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Nim C Wrappers?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Nim C Wrappers?

As many as you like. As long as a model supports skills, you can use Nim C Wrappers with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Nim C Wrappers AI skill free?

It is published on GitHub by planetis-m. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇