-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Run nim source files like scripts (with tcc and binfmt_misc)
niv edited this page Feb 26, 2017
·
1 revision
This is a rough guide on how to make nim source files behave like scripts. This document targets debian/ubuntu-likes; you may have to adjust for other systems.
- Install tcc (optional, but recommended):
sudo apt install tcc
- Make sure binfmt_misc support is available:
ls /proc/sys/fs/binfmt_misc/register
Create /usr/local/bin/nimtcc
, like so:
#!/bin/bash
if [[ "$#" -eq 0 ]]; then
echo "at least 1 argument required" >&2
exit 1
fi
if [[ ! -e "$1" ]]; then
echo "not found: $1" >&2
exit 1
fi
umask u+rwx
out=$(mktemp ${TMPDIR:-/tmp/}$(basename $1).XXXXXXXXXXXX)
function finish() {
rm -- "$out"
}
trap finish EXIT
nim compile \
--verbosity:0 \
'--hint[Processing]:off' \
--cc:tcc \
"-o:$out" \
-r $@
The script creates a temp file (in /tmp) to write the compiled program to, and immediately removes it thereafter again.
- Make sure nim is available on PATH, or change the path to the binary in the script.
- Mark it as executable:
sudo chmod +x /usr/local/bin/nimtcc
- .. and test it:
$ nimtcc my_script.nim
. If it doesn't work, fix it.
N.B.:
- This still creates a
nimcache
in the current directory. I've left it in as a compromise to speed up iterative script development. If this is not desired, you will have to adjust the script to create an additional directory (with mktemp -d), pass that to the compiler; and then remove it afterwards. - Similarily, you can get rid of the whole mktemp to write the script binary to the current directory.
Now, optionally, make it so you can mark scripts as executable and run them directly. This requires binfmt_misc support in your kernel. Most modern kernels have this enabled by default.
$ echo ':nimtcc:E::nim::/usr/local/bin/nimtcc:' | sudo tee -a /proc/sys/fs/binfmt_misc/register
$ chmod +x my_script.nim
$ ./my_script.nim
Intro
Getting Started
- Install
- Docs
- Curated Packages
- Editor Support
- Unofficial FAQ
- Nim for C programmers
- Nim for Python programmers
- Nim for TypeScript programmers
- Nim for D programmers
- Nim for Java programmers
- Nim for Haskell programmers
Developing
- Build
- Contribute
- Creating a release
- Compiler module reference
- Consts defined by the compiler
- Debugging the compiler
- GitHub Actions/Travis CI/Circle CI/Appveyor
- GitLab CI setup
- Standard library and the JavaScript backend
Misc