My Nim Environment

linkSetting up my nim environment

Inspired by a post I found on setting up a python environment I've decided I'll write one for my nim environment. This is not a guide on setting up Nim itself for that use choosenim. I am also presuming that you're using nimble along with the standard nimble project layout

linkCode Editor and plugins

I currently use Visual Studio Code (VSC) as my editor however I've been meaning to try Intellij's Nim support.

I use the following plugins in VSC:

linkSetting up the launch template

I've setup one single launch template for VSC that allows for quick debugging with GDB

launch.json
1{

2 // Use IntelliSense to learn about possible attributes.

3 // Hover to view descriptions of existing attributes.

4 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387

5 "version": "0.2.0",

6 "configurations": [

7 {

8 "name": "Debug current Test",

9 "type": "gdb",

10 "request": "launch",

11 "target": "./bin/tests/${fileBasenameNoExtension}.exe",

12 "cwd": "${workspaceRoot}",

13 "valuesFormatting": "parseText",

14 "preLaunchTask": "debug",

15 }

16 ]

17}

we also need to setup a tasks.json

tasks.json
1{

2 // See https://go.microsoft.com/fwlink/?LinkId=733558

3 // for the documentation about the tasks.json format

4 "version": "2.0.0",

5 "tasks": [

6 {

7 "label": "debug",

8 "type": "shell",

9 "command": "nim c --debugger:native tests/${fileBasename}",

10 "group": "test"

11 }

12 ]

13 }

Now when I want to debug a test I simply need to press f5 in that test file to open the debugger!

linkNim Configuration

Nim allows you to pass command line flags to the compiler via nim.cfg files. I have 2 nim.cfg's setup one in the root of the project and one in the tests folder

nim.cfg
1--gc:arc

2--experimental:strictFuncs

3--outdir:bin

For my projects I like to default to using the arc garbage collector. If your project needs a more specialized GC check out the garbage colletion documentation

Likewise I am a big fan of immutability so I've enabled the strictFuncs parameter for stricter checks on mutations.

warning Enabling --experimental:strictFuncsvia a compiler flag will also activate this for the standard library. Parts of the standard library that you would not expect to be flagged as mutating will be flagged such as the json module. If you know your code won't mutate you tell the compiler to ignore a block of code by using the {.cast(noSideEffect).}:pragma

Finally I've set the outdir flag so that our executable is created in the bin directory. Please note that nimble build does not currently respect the --outdir flag!

tests/nim.cfg
1--path: "../src/"

2--outdir: "bin/tests"

We overwrite the outdir for tests so that the binaries generated for tests are put in a test folder to not clutter our workspace.

Likewise we update our path so that we can import project modules without them needing to be in your ~/.nimble directory. For a more detailed explanation on the path param check out the nimble documentation

warning If you use testament as your test runner then don't set the --outdirflag as testament will not be able to find the megatest executable. For more information see here

linkGit hooks

I use the pre-commit tool to run scripts before I commit changes. Currently I've set it up to auto format my nim files and run tests on commit. My config can be found below

.pre-commit-config.yaml
1repos:

2 - repo: local

3 hooks:

4 - id: format

5 name: format

6 files: "^.*nim$"

7 stages:

8 - commit

9 language: system

10 entry: nimpretty -maxLineLen:120

11 types:

12 - file

13 - id: test

14 name: test

15 stages:

16 - commit

17 language: system

18 entry: nimble test

19 types:

20 - file

You may want to adjust the maxLineLen parameter if you have a small amount of screen real estate.

Setting up my nim environmentCode Editor and pluginsSetting up the launch templateNim ConfigurationGit hooks

Home My Nim Environment First Post.... Again