mieki256's diary



2026/02/24(火) [n年前の日記]

#1 [nim] Nimをインストールして試用してみた

Pythonと似た感じでソースコードを書けて、一発で exe を作ってくれる、Nim というプログラミング言語があるらしい。

_Nim Programming Language

ちょっと気になる。少し試用してみようかなと…。環境は Windows11 x64 25H2。

choosenimを使ってインストール :

Nimのバージョンを管理できる choosenim というツールがあるらしい。最初はそちらを使ってインストールを試みた。

注意点。インストールする前に、%USERPROFILE%\.nimble\bin\ (通常は C:\Users\ユーザ名\.nimble\bin\) というフォルダを作っておいたほうが良さそう。インストール時、該当フォルダに.dllをコピーするのだけど、最初はそのフォルダが無いから「コピーできない」とエラーになる…。事前に作っておけばそのエラーは出なくなりそう。

_nim-lang/choosenim: Official tool for easily installing and managing multiple versions of the Nim programming language.
_Releases - nim-lang/choosenim
_Nim の環境構築・アップデート・削除 〜Windows 編〜 - akagma

choosenim-installer-0.8.4_windows_amd64-download-this-not-the-exe.zip を入手して解凍。今回は、D:\Dev\nim\choosenim\ というフォルダを作成して、その中にファイル群を入れた。

runme.bat というファイルがあるのでコマンドプロンプト上で実行。管理者権限は要らない…と思う。

%USERPROFILE%\.nimble\bin\ に色々なファイルがコピーされた。

環境変数PATHに、%USERPROFILE%\.nimble\bin を追加しておく。

> which choosenim
"C:\Users\USERNAME\.nimble\bin\choosenim.exe"

> which nim
"C:\Users\USERNAME\.nimble\bin\nim.exe"

> which nimble
"C:\Users\USERNAME\.nimble\bin\nimble.exe"

> choosenim show
  Selected: 2.2.8
   Channel: stable
      Path: C:\Users\USERNAME\.choosenim\toolchains\nim-2.2.8

> nim --version
Nim Compiler Version 2.2.8 [Windows: amd64]
Compiled at 2026-02-23
Copyright (c) 2006-2026 by Andreas Rumpf

active boot switches: -d:release

インストールできたっぽい。choosenim 自体も、%USERPROFILE%\.nimble\bin\ の中にコピーされていた。

公式バイナリをインストールし直し :

公式サイトを眺めていたら、MinGW(MinGW64)が必要と書いてあった。先ほど試した choosenim によるインストールでは、MinGW64 はインストールされていない気がする…。公式版でインストールし直してみるか…。

_Windows installation - Nim Programming Language

nim-2.2.6_x64.zip を入手して解凍。今回は、D:\Dev\nim\nim-2.2.6_x64\ に中身を置いておいた。

中に finish.exe が入っているのでコマンドプロンプト上で実行。

  • D:\Dev\nim\nim-2.2.6_x64\bin と %USERPROFILE%\.nimble\bin をPATHに追加するかと尋ねてくるので「y」を入力。
  • MinGW をインストールするかと尋ねてきたので「y」。D:\Dev\nim\nim-2.2.6_x64\dist\mingw64\ 以下に MinGW64 がインストールされたように見えた。
  • スタートメニューに項目を追加するかと尋ねてきたので「y」。

これでインストールされたらしい。

MinGW64がインストールされている場所、D:\Dev\nim\nim-2.2.8_x64\dist\mingw64\bin にPATHを通しておかないとダメっぽい…。普段 gcc にはパスを通したくないんだけどなあ…。

batファイルを作って対応することにした。

nim_enable.bat
@echo off
@rem Nim enable. Add PATH.

set NIMDIR=D:\Dev\nim\nim-2.2.8_x64
@rem set NIMDIR=D:\Dev\nim\nim-2.2.6_x64

set NIMLOCALBIN=%USERPROFILE%\.nimble\bin
set NIMBINPATH=%NIMDIR%\bin
set MINGW64PATH=%NIMDIR%\dist\mingw64\bin
set NIMALLPATH=%NIMLOCALBIN%;%NIMBINPATH%;%MINGW64PATH%

set PATH=%NIMALLPATH%;%PATH%

@rem PerlのPATHだけ除外。自分の環境はPerl内にもgccが入ってしまっているので…
call %HOMEBIN%\rmperl.bat

echo Nim enable. Add Path [%NIMALLPATH%]

ここまで作業して気づいたけれど、ダウンロードページの下のほうに Nim 2.2.8 があるではないか…。

_Download - Nim Programming Language

Nim 2.2.6 をインストールしてしまった…。Nim 2.2.8 をインストールし直した。nim-2.2.8-windows_x64.zip をダウンロードして解凍してここまでの作業を繰り返し。

Hello World :

hello.nim というテキストファイルを作成して以下の内容で保存。

echo "Hello, World !"


コンパイルは以下。

nim compile hello.nim
or
nim c hello

  • 「compile」は「c」だけで済ませられる。
  • 拡張子が無かったら「.nim」を追加して処理してくれる。

hello.exe が作成された。実行すると「Hello, World !」が表示された。

コンパイルした直後に実行することもできる模様。

nim c --run hello
or
nim c -r hello

wNimをインストール :

Nim言語でWindows用のGUIアプリを作成できる、wNim というライブラリがあるらしい。インストールしてみる。

_khchen/wNim: Nim's Windows GUI Framework
_NimのGUIでいい感じの「wNim」 | Nな人のWeb示録

環境は Windows11 x64 25H2 + Nim 2.2.8。

> nimble install wnim

Downloading Official package list
    Success Package list downloaded.
Downloading https://github.com/khchen/wNim using git
Downloading https://github.com/khchen/winim using git
Downloading https://github.com/zevv/npeg using git
Downloading https://github.com/khchen/memlib using git
   Warning: The package has no tagged releases, downloading HEAD instead.
Downloading https://github.com/khchen/minhook using git
Downloading https://github.com/zevv/npeg using git
Downloading https://github.com/khchen/winim using git
Downloading https://github.com/khchen/memlib using git

しかし、ただウインドウを表示するだけのサンプルですらコンパイルエラーが出る。

hellonim.nim
import wNim/[wApp, wFrame]

let app = App()
let frame = Frame(title="Hello World", size=(400, 300))

frame.center()
frame.show()
app.mainLoop()

nim c hellonim.nim

fatal.nim(62)            sysFatal
Error: unhandled exception: field 'floatVal' is not accessible for type 'TFullReg' using 'kind = rkInt' [FieldDefect]

wnim の githubページのサンプルファイル群すらコンパイルエラーになる…。Nim 2.2.6、Nim 2.2.8 のどちらを使ってもダメ。

「Nim 1.6.20 なら動くんじゃね? ユー、choosenim を使っちゃいなよ」、とAI(Google Gemini)が言っている。

choosenim は Nim のバージョンを切り替えることができるツール、らしい。

choosenim 1.6.20

> nim -v
Nim Compiler Version 1.6.20 [Windows: amd64]
Compiled at 2024-04-07
Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release

これで Nim 1.6.20 が使えるようになった。

nimble uninstall wnim
nimble install wnim

nim c hellonim.nim

コンパイルが通ってexeが生成できた。実行もできたし、ウインドウも表示された。

choosenim 2.0.0 と打って Nim 2.0.0 もインストールしてみた。Nim 2.0.0 でもコンパイルは通った。

Nim 2.0.6 で wnim が動いてるという記事も見かけた。choosenim 2.0.6 で Nim 2.0.6 もインストールしてみた。Nim 2.0.6 でもコンパイルは通った。


他の方法として、「--mm:refc」をつけて試せ、とAI(Google Gemini)が言っている。Nim 2.2.8 で試してみよう…。

_Nim's Memory Management

> nim -v
Nim Compiler Version 2.2.8 [Windows: amd64]
Compiled at 2026-02-23
Copyright (c) 2006-2026 by Andreas Rumpf

active boot switches: -d:release

> nim compile --mm:refc hellonim.nim
Hint: used config file 'C:\Users\USERNAME\.choosenim\toolchains\nim-2.2.8\config\nim.cfg' [Conf]
Hint: used config file 'C:\Users\USERNAME\.choosenim\toolchains\nim-2.2.8\config\config.nims' [Conf]
..............................................................................................................................fatal.nim(62)            sysFatal
Error: unhandled exception: field 'floatVal' is not accessible for type 'TFullReg' using 'kind = rkInt' [FieldDefect]

これはダメっぽい。


現状で wnim を使うには、Nim のバージョンを 1.6.20、2.0.0、2.0.6 に落とすぐらいしか手は無さそう。

なんだか Ruby と同じ臭いがしてきた…。本体のバージョンアップに外部ライブラリが追従できてない状況がありそうな気がする…。

VSCodeをNimに対応させたい :

VSCode(Visual Studio Code)でNimに対応させたい。環境は Windows11 x64 25H2。

_nim-lang.org - Visual Studio Marketplace
_Nim - Visual Studio Marketplace

後者のほうがDL数は多いけど、最終更新日が古い。前者のほうがまだ最終更新日が新しい。ただ、nimlangserver なるものが必要らしくて…。

nimlangserver とは以下のことだろうか。

_nim-lang/langserver: The Nim language server implementation (based on nimsuggest)

本来なら以下でインストールできるらしいのだけど…。
nimble install nimlangserver

2026/02/24の時点ではエラーが出る。Nimのバージョンを 2.2.8、2.0.6、1.6.20 にしてもダメ。
> nimble install nimlangserver
Downloading https://github.com/nim-lang/Nim.git using git
       Tip: 5 messages have been suppressed, use --verbose to show them.
nimble.nim(797)          downloadDependency

    Error:  Downloaded package checksum does not correspond to that in the lock file:
        ...   Package:           nim@v.2.0.8@r.5935c3bfa9fec6505394867b23510eb5cbab3dbf
        ...   Checksum:          efb469b3ca8c4ec85c5829fa18997508672f9740
        ...   Expected checksum: 46333e8f4bda41dd6d3852a3f5fa4975b96b66a2

どうしたものかと nimlangserver のページを眺めていたら Releasesページにバイナリがあるように見えた。

_Releases - nim-lang/langserver

nimlangserver-windows-amd64.zip をDLして解凍すると、中に nimlangserver.exe がある。これを使ってみようか…。C:\Users\USERNAME\.nimble\bin\ 以下にコピーしておいた。

VSCode を起動して拡張機能をインストール。

_nim-lang.org - Visual Studio Marketplace

少なくとも .nim を開いた時に色付け表示はされた。

ただ、VSCode 使用時に、環境変数PATHに Nim関係のパスを含めておかないといけない…。常に Nim を使いたいわけではないのだよな…。どうしたものか…。色分け表示してくれるぐらいでも十分なんだけどなあ…。

Notepad++のNim言語設定ファイル :

テキストエディタ NotePad++用のNim言語定義ファイルは無いのかなとググってみた。

_nim notepad++ - Pastebin.com
_Nim Notepad++ language definition - Pastebin.com
_Nim support for Notepad ++ - Nim forum

どれかしらをダウンロードして、Notepad++の、言語 → ユーザー定義 → 言語の定義。インポートをクリックして取り込めば設定が保存される…のだろうか? 設定ダイアログを一旦閉じてまた開けば、Nimの設定が選べるようになった。

しかし、設定した後で以下の投稿を目にした。

_Updated Nim support in Notepad++ - Nim forum

Notepad++ 8.9.1 から、Nim に公式対応したらしい。せっかくだから NotePad++ をアップデートしておくか…。

32bit ポータブル版の npp.8.9.2.portable.zip をダウンロードして解凍して、今まで利用していた版に上書きインストールしておいた。ユーザー定義した Nim の設定は削除。


余談。Notepad++ は中国から敵視されている模様…。あの国は異常…。

_Notepad++ v8.9.2 release - Double?Lock Update Security | Notepad++
_Important Clarification: Notepad++ Security Incident | Notepad++
_Notepad++ Hijacked by State-Sponsored Hackers | Notepad++
_人気テキストエディター「Notepad++」が検索エンジンBingをブロック、その理由が波紋呼ぶ【やじうまWatch】 - INTERNET Watch
_Notepad++が国家支援ハッカーにハイジャックされマルウェア入りインストーラーを配布していたことが判明 - GIGAZINE

Mery用のNim言語構文ファイル :

テキストエディタ Mery用のNim言語構文ファイルがあるらしい。

_Mery用構文ファイル(Nim)
_構文ファイル - MeryWiki

Meryを起動して、表示 → 編集モード → 編集モードの設定。新規作成 → Nimと入力 → プロパティ → インポート → .msy を選択。

.nim を開いたら色がついた表示になった。

以上、1 日分です。

過去ログ表示

Prev - 2026/02 -
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28

カテゴリで表示

検索機能は Namazu for hns で提供されています。(詳細指定/ヘルプ


注意: 現在使用の日記自動生成システムは Version 2.19.6 です。
公開されている日記自動生成システムは Version 2.19.5 です。

Powered by hns-2.19.6, HyperNikkiSystem Project