Learn Nim: Create a README Template Downloader
Create a command-line tool which downloads a README template for your coding projects
Why Nim? Nim is a statically typed systems programming language.
Nim generates small, native dependency-free executables. The language combines a Python-like syntax with powerful features like meta-programming.
Nim supports macOS, Linux, BSD, and Windows. The language is open-source and has no corporate affiliation.
Nim compiles to multiple backends, for example, C, C++, or JavaScript.
The ecosystem and community are small, but the language has reached its first stable release.
Run Nim Docker Container as Non Root
Today I improved my Nim web application by running the docker container as non=root-user:
## base imageFROMnimlang/nim:1.0.4-regular## install dependenciesRUN apt-get update && \ apt-get install -y --no-install-recommends libpq-dev netcat-openbsd## set working directoryWORKDIR/usr/src/app## add userRUN addgroup --system nim && adduser --system --group nimRUN chown -R nim:nim /usr/src/app && chmod -R 755 /usr/src/app## Nim environmentENV NIM_ENV=production ENV NIMBLE_DIR=/home/nim/.nimbleENV PATH=$PATH:/home/nim/.nimble/bin## copy entrypoint, make executableCOPY ./entrypoint.sh .RUN chmod +x entrypoint.sh## install dependencies, bundle assets, compileRUN nimble refresh && nimble install nimassets jesterCOPY .
How to Serve Static Files With Nim and Jester on Heroku
The Problem Let’s say that you create a simple web application with the Nim Heroku Buildpack.
Project structure:
├── nim_heroku.nimble ├── Procfile ├── src │ ├── nim_heroku │ ├── nim_heroku.nim │ └── views │ └── general.nim ├── static_dir │ ├── favicon.ico │ └── style.css └── tags 3 directories, 8 files Bin file (src/nim_heroku.nim):
import jester, asyncdispatch, os, strutils import views/general var settings = newSettings() if existsEnv("PORT"): settings.port = Port(parseInt(getEnv("PORT"))) settings.staticDir = ".
TIL: PostgreSQL Insert ID Only Works With id as Primary Key
I’ve created a database table for my PostgreSQL database with Nim:
import db_postgres, os ## some code proc setup*(database: Database) = database.db.exec(sql""" CREATE TABLE IF NOT EXISTS Url( shortcode SERIAL PRIMARY KEY, orig_url VARCHAR(255) NOT NULL ); """) ## more code My primary key is shortcode as a SERIAL data type. That means the shortcode column automatically increments.
I want to insert data into the database and return the generated ID for the row.
Refactoring My Nim sqlite DB to Postgres
Today I’ve spent all my coding time on re-factoring my sqlite3 database to postgres. Nim and Jester (a web framework) ship with inbuilt sqlite3 features.
I couldn’t get them working within Docker.
I know how to use Docker and Postgres. Plus, the wrapper library for Nim around SQL databases offers a consistent API.
My re-factoring only needed minimal changes.
The project is still in flux.
And I have some annoying errors with Nim and SQL queries.
Nim First Impressions
I’ve started dabbling in Nim some days ago.
My experience level: I’m a self-taught hobby developer. No professional experience, but a lot of enthusiasm.
I’ve created some toy applications, but nothing production-grade.
I’m most familiar with JavaScript and React.js, but also know a bit of Clojure, Elixir, Python, and Reason.
What is Nim? From the Nim website:
Efficient, expressive, elegant
Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula.
Learning Progress Nim
I’ve finished chapter 3 of Nim in Action. I completed the terminal-based chat application.
Learning Nim is fun, but there are still many concepts I’m unsure about. For example, I don’t know how the memory model works.
I started chapter 6 of the book, and I don’t understand how to parse a file. Chapter 6 introduces parallelism, and I’m confused about locks.
Still, I like that the book uses practical examples to teach concepts.
TIL: Objects in Nim
Objects in Nim use the type constructor:
## Type Definition type MusicTrack = object index: int title: string ## (Mutable) Value var summer = MusicTrack(index: 1, title: "Summer in the City") That reminds me of Records in Reason:
/* Type definition */ type musicTrack = {index: int, title: string} /* Value */ let summerInTheCity = {index: 1, title: "Summer in the City"} In Nim, “An object is a value type, which means that when an object is assigned to a new variable all its components are copied as well.
Reading “Nim in Action”
I’m currently working through the excellent book Nim in Action by Dominik Picheta.
Nim looks like a promising and fresh language:
Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula.
What I like about Nim is that it looks like Python, but it compiles to a dependency-free executable (C, C++, or JavaScript).
The language is fairly small, but it allows for writing macros (meta-programming) like a Lisp.
Setup Nim With Neovim
Use Neovim as your Nim IDE
Why Nim? Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. 1
Nim resembles Python, but the language is significantly faster and statically compiled. Nim comes with meta-programming abilities (like a LISP). You can compile a Nim program into a stand-alone C binary that runs on every system.