DEV Community


Posted on

Learning Nim: Adding A Package to Nim Ecosystem

I recently published my first package in the Nim ecosystem. In this post I am going to show how I did it.

The package I published is called luhny it is the Luhn's Algorithm's implementation in Nim. Using this, you can validate any credit card number as Luhn's Algorithm is the standard for credit card validation.

But why would I make a package out of it?

I created it for two reasons:

  • I wanted to know how to publish a package
  • I want to add a generate valid numbers functionality that people may be able to use in the future.

Step 1: Make a Repository

No matter how small or how easy of a thing you're building. Good habits help you go a long way.

git init <name of your package>

Step 2: Repository Structure

You're free to choose whatever structure you want. I saw a couple of packages on github and decided to go with the following structure:

|_____________ .git
|_____________ .gitignore
|_____________ luhny
               |________ luhny.nim
|_____________ tests
               |________ test.nim
               |________ config.nims #notice the extra 's'
|_____________ luhny.nimble

You can know about the updated package structure here

Step 4: Tests

Again, habits matter. No matter how small or easy your code is, you got to test it. I tried to use nim's unittest module.

# Blueprint for a basic test suite

import unittest
import luhny # code that I want to test

#A test may or may not contain more than one test
suite "<name of your test suite>":
    echo "Initializing"

    # use it if you want to run any code before every test
        echo "setup code executed"

    # use it if you want to run any code after every test
        echo "teardown code executed"

    test "<name of your test that your want to display>":
        check(<function you want to test>() == <its expected outcome>)

    #for example
    test "Wrong input test":
        check(check_number("12345") == false)

    test "Correct input test":
        check(check_number("4532196437678779") == true)

    test "Single 0":
        check(check_number("0") == true)

    test "Empty String":
        check(check_number("") == true)

    # Message indicating end of tests
    echo "Tests Completed"

But how does test.nim find luhny.nim? They are in different folders.

Yes, for that we use nimscript. Remember the config.nims file? We'll use that to switch the path.

switch("path", "$projectDir/../luhny") 
# changed the path of the current directory from tests to luhny.

... and that's how tests found luhny. Although, there's another (official) way to do it.

After running your tests and getting a satisfactory output, we can work towards making it an actual package by adding a .nimble file.

Step 5: Add a .nimble File

Go to your project directory, that is, the top directory that has readme file. There you execute the following command:

nimble init

It will be followed by a series of questions like

  • What's the package version
  • what's the author name
  • what's license and dependencies etc

After filling the details, you can even try to install the package by using nimble install inside the project directory.

Step 6: Uploading to github

Remember that till now, everything is going on in your local repository. Now it's time to push the changes to github so that we can share our package with the world.

git remote add origin<your-user-name>/<you package name>.git
git push -u origin master

Step 7: Telling Nimble About Your Package

There's a separate package repository of Nim packages hosted on github that covers this step and is fairly comprehensive about it.

Create a PR and your package would be added to the Nim's Ecosystem when the PR is accepted.

If you learnt anything from this post, please hit ❤️ and share it with other people!

Top comments (1)

sigmapie8 profile image

If anyone is wondering where did the step 3 go. That is where you write your package code :)