This should do it: https://nim-lang.org/docs/system.html#setControlCHook
And here's an example: https://rosettacode.org/wiki/Handle_a_signal#Nim