Skip to content

Instantly share code, notes, and snippets.

@iffy
iffy / Dockerfile
Created January 20, 2023 13:15
Fly.io example demonstrating libsodium use failure. Launch with `fly launch` and see the logs for the error.
# -- Stage 1 -- #
FROM nimlang/nim:1.6.10-alpine@sha256:408ebac99ad2d170a59e7a09c10e82e7336cf71fa38c7a1322aaa598a54d32c2 as builder
WORKDIR /app
RUN apk update && apk add libsodium-static libsodium musl-dev
RUN nimble install -y libsodium
COPY . .
RUN nim c -o:testapp main.nim
RUN ls -al
# -- Stage 2 -- #
@iffy
iffy / nbody.nim
Created October 26, 2022 17:49
I wanted to see how Nim compared (readability and speedwise) with what's reported here: https://jott.live/markdown/py3.11_vs_3.8
## Compile for speed with `nim c -d:danger nbody.nim`
## Run `./nbody 10000000`
import std/os
import std/sequtils
import std/strformat
import std/strutils
import std/tables
from std/math import pow
@iffy
iffy / proposal.md
Created March 30, 2021 12:51
Idea for making Nim's `staticExec`/`staticRead` auditable

I agree that it's impossible for Nim to protect you if you're willing to compile other people's code.

However, it could be good if Nim provided a means for auditing calls to staticExec/staticRead. Or at least provided means for someone else to write an auditing library. I imagine something like the following could work and not be onerous either to the user or the compiler:

  1. nim c --strictStatic myfile.nim

This would fail on all instances of staticExec with a message like:

ERROR: Prevented execution of myfile.nim:staticExec("/bin/echo foo") Pass --allowStatic:'myfile.nim:staticExec("/bin/echo foo")' to allow it

@iffy
iffy / equalproc.nim
Last active May 7, 2019 18:20
How to make proc `==`(a,b: Thing):bool for a variant object.
import sequtils
import strutils
import macros
import sugar
export macros
proc replaceNodes*(ast: NimNode): NimNode =
## Replace NimIdent and NimSym by a fresh ident node
##
## Use with the results of ``quote do: ...`` to get
@iffy
iffy / dumpMem.nim
Last active February 13, 2019 19:07
Print out memory in Nim
import strutils
import terminal
template lowerHex(x:untyped):untyped =
toHex(x).toLower()
proc charRepr(x:byte):char =
case x
of 32..126: result = chr(x)
else: result = '.'
@iffy
iffy / results.txt
Created November 5, 2018 16:22
Small, probably non-representative benchmark of JSON Changelog with SQLite https://blog.budgetwithbuckets.com/2018/08/27/sqlite-changelog.html
python testsqlite.py 10000 :memory: 0
6.65568494797 seconds to do 10000 records db=:memory:, triggers=False
6.66251087189 seconds to do 10000 records db=:memory:, triggers=False
6.69321203232 seconds to do 10000 records db=:memory:, triggers=False
python testsqlite.py 10000 :memory: 1
6.76699709892 seconds to do 10000 records db=:memory:, triggers=True
6.82708096504 seconds to do 10000 records db=:memory:, triggers=True
6.82913088799 seconds to do 10000 records db=:memory:, triggers=True

v1

$ nim objc -r v1_manual.nim 
Hint: used config file '/Users/matt/lib/Nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: v1_manual [Processing]
CC: v1_manual
CC: stdlib_system
Error: execution of an external compiler program 'clang -c -w -I/Users/matt/lib/Nim/lib -o /Users/matt/.cache/nim/v1_manual_d/v1_manual.m.o /Users/matt/.cache/nim/v1_manual_d/v1_manual.m' failed with exit code: 1
--- /tmp/orig.nim 2018-10-24 09:01:22.000000000 -0600
+++ /tmp/frommanual.nim 2018-10-24 09:00:51.000000000 -0600
@@ -1,7 +1,6 @@
-{.passL: "-lobjc".}
{.emit: """
-#include <objc/Object.h>
-@interface Greeter:Object
+#include <Cocoa/Cocoa.h>
+@interface Greeter:NSObject
{
@iffy
iffy / .gitignore
Last active September 21, 2018 19:33
Comparing line-reading in Python/Nim/Node
hugefile.txt
myreader
builtinreader
hugebin.txt
hugestr.txt
@iffy
iffy / compile.sh
Created June 15, 2017 16:19
Is "Account" a reserved name?
set -x
tsc okay.ts
tsc fails.ts