Nim Compiled, Python-like language

Version 2.2.12 · source

Start here

nim c -r <file.nim>
Compile and run
nim r <file.nim>
Run via nimcache
nim c -d:release <file.nim>
Optimized build
nim c -o:<out> <file.nim>
Name the binary
nim js <file.nim>
Compile to JavaScript
nimble init
Scaffold a package
nimble build
Build the package
nimble test
Run tests/t*.nim

Concepts

  1. Nim source app.nim
  2. Generated C in nimcache
  3. Binary app
  4. JavaScript app.js

Compile and run

nim c <file.nim>
Compile with the C backend
nim c -r <file.nim> <args>
Compile, then run with args
nim r <file.nim> <args>
Compile to nimcache and run
nim c -d:release <file.nim>
Optimized release build
nim c -o:<file> <file.nim>
Set the output file name
nim c --outdir:<dir> <file.nim>
Set the output directory
nim cpp <file.nim>
Compile via C++
nim js <file.nim>
Compile to JavaScript
nim c -d:danger <file.nim>
Optimize, all runtime checks off
nim c --opt:size <file.nim>
Optimize for size
nim c --app:lib <file.nim>
Build a shared library
nim c --app:staticlib <file.nim>
Build a static library
nim c -f <file.nim>
Force a rebuild of all modules
nim e <script.nims>
Run a NimScript file

Checks and flags

nim check <file.nim>
Check syntax and types only
nim c -d:<symbol> <file.nim>
Define a symbol
nim c -d:<symbol>:<val> <file.nim>
Define a symbol with a value
nim c --threads:on <file.nim>
Enable threads
nim c --mm:<orc|arc|refc> <file.nim>
Pick memory management
nim c --path:<dir> <file.nim>
Add a module search path
nim c --hints:off <file.nim>
Silence hints
nim c --warning:<name>:off <file.nim>
Switch off one warning
nim c --warningAsError:<w>:on <f.nim>
Make a warning an error
nim c -x:off <file.nim>
Disable runtime checks
nim c -a:off <file.nim>
Disable assertions
nim c --stackTrace:on --lineTrace:on <file.nim>
Trace lines in crashes
nim c --styleCheck:error <file.nim>
Enforce Nim naming style
nim c --parallelBuild:0 <file.nim>
Build with all cores
nim dump
Show defines and search paths

C toolchain and cross

nim c --cc:<gcc|clang> <file.nim>
Choose the C compiler
nim c --passC:<flag> <file.nim>
Pass a flag to the C compiler
nim c --passL:<flag> <file.nim>
Pass a flag to the linker
nim c --nimcache:<dir> <file.nim>
Set the generated-files directory
nim c --cpu:<cpu> --os:<os> <file.nim>
Cross compile
nim c -d:mingw <file.nim>
Cross compile to Windows
nim c --genScript <file.nim>
Emit a build script, no link
nim c --debugger:native <file.nim>
Build for gdb debugging
nim c --expandArc:<proc> <file.nim>
Show ARC/ORC lowered code

Configuration

nim.cfg
Per-project switches, one per line
config.nims
NimScript project config
--skipUserCfg
Ignore the user config file
--skipProjCfg
Ignore the project config file
--skipParentCfg
Ignore parent directory configs
config.nims
switch("d", "release")switch("mm", "orc")switch("path", "$projectDir/src")task build, "build app":  exec "nim c -o:bin/app src/app.nim"

Nimble packages

nimble init
Start a new package (wizard)
nimble build
Build the package binaries
nimble run <bin> <args>
Build and run a binary
nimble test
Run tests/t*.nim
nimble install
Install the package in this dir
nimble install <pkg>
Install a package
nimble install <pkg>@<ver>
Install a specific version
nimble install --depsOnly
Install only dependencies
nimble uninstall <pkg>
Remove a package
nimble search <term>
Find packages by name or tag
nimble list -i
List installed packages
nimble refresh
Update the package list
nimble check
Validate the package
nimble tasks
List custom tasks
nimble <task>
Run a task from the .nimble file
nimble dump
Show package metadata
nimble path <pkg>
Print a package's path
nimble deps
Show the dependency tree
nimble publish
Publish to the package list
nimble build --useSystemNim
Skip Nimble's Nim management

Local workflow

nimble setup
Create nimble.paths for the project
nimble lock
Pin versions in nimble.lock
nimble develop
Put all deps in develop mode
nimble develop <pkg>
Clone a package for hacking
nimble sync
Match working copies to the lock
nimble sync -l
List out-of-sync packages

Package file

mypkg.nimble
version     = "0.1.0"author      = "Jane Doe"description = "Small tool"license     = "MIT"srcDir      = "src"bin         = @["mytool"]requires "nim >= 2.0.0"requires "jester >= 0.5.0"
Custom task and hook
task hello, "Say hi":  echo "Hello"before hello:  echo "before"
requires "<pkg> ~= <ver>"
Semver-compatible dependency
pkg@#<branch>
Install from a git ref

Language basics

Variables
let x = 1        # immutablevar y = 2        # mutableconst z = 3      # compile time
Procs
proc add(a, b: int): int =  a + becho add(1, 2)
Loops and collections
for i in 0 ..< 3: echo ivar s = @[1, 2, 3]s.add 4for x in s: echo x
Types
type Point = object  x, y: intlet p = Point(x: 1, y: 2)
Strings
import strutilsecho "a,b".split(',')echo "x=$1" % "5"
Unit test
import unittesttest "adds":  check 1 + 1 == 2

Recipes

Start a new binary
mkdir hello && cd hellonimble initnimble buildnimble run
Fast iteration
nim r --hints:off app.nimnim check app.nim
Ship a release binary
nim c -d:release --opt:size \  -o:bin/app src/app.nim
Web build
nim js -d:release -o:app.js app.nim