i want to setup a git pre commit hook entirely in nimscript
this means I cant execute the file with nim e somefile
e.g. this file
## .git/hooks/nimprecommit
#!/usr/bin/env nim
echo "123"
if executed
$ chmod +x .git/hooks/nimprecommit
$ ./.git/hooks/nimprecommit
Error: invalid command: .git/hooks/nimprecommit
I'm thinking this is a OS configuration issue, as node's shebang works just fine
On Unix, you can also use the shebang #!/usr/bin/env nim, as long as your filename ends with .nims:
is there anyway to get around this? git hooks cannot have file extensions
#!/usr/bin/env nim r
echo "hi"
chmod +x hello.nim
There's the problem though, nim refuses to run hello without the nim extension.
This is a bit different though, since you'll be running Nim code, and not NimScript code.
For Nim specifically there's https://github.com/PMunch/nimcr
In your case this can be solved that by creating a "wrapper" Git hook that actually executes Nim to run your NimScript file with an extension.
yeah i went with
#!/bin/sh
echo "executing nim tests"
echo
nim e .github/scripts/test.nims |
while IFS= read -r line; do
echo "$line"
done
This is a bit different though, since you'll be running Nim code, and not NimScript code.
I consider this way as superior to NimScripts, as NimScript has some limitation, I don't remember exactly the problem, but my program didn't compiled when I run it as NimScript.
And, I have feeling that nim compiler actually cache the compilation, so it's fast.
The only annoyance is the .nim extension you had to keep.
yea my heuristic is nim is to nimscript as nimscript is to bash
moving up the ladder makes sense depending on the context, but ideally im hoping to sunset bash unless necessary (e.g. the git hook thing)
Yeah, you're missing the e option for the nim compiler.
Also the --hints:off is purely optional. It's just that I prefer it to be more silent.
This will turn off hints from the compiler that would be shown on each execution otherwise thanks to the interpreted nature of nimscript.
Filenames without extensions are a big misfeature of Unix anyway. It's like a "dynamically typed" variable, pure obfuscation.
Isn't nim without extension?
On Unix, you can also use the shebang #!/usr/bin/env nim, as long as your filename ends with .nims
Missing an e is correct. However it only works on files prefixed with .nims, otherwise it gives "Error: invalid command: ...".
Also I've heard that two or more arguments in shebang might not be accepted by the system or shell, so I usually use #!/usr/bin/nim --hints:off with extension name .nims.
The nim executable has no extension.
Depends on the OS, for me it has an .exe extension. And now tell your .gitignore to ignore produced binary files because they are artifacts of compilation and fundamentally different from hand written scripts. Oops.
if you end up using a shell to execute your nim tests (e.g. reusing a git workflow script) make sure to use bash so you can fail on pipe erorrs
#!/usr/bin/env bash
set -euo pipefail