If you do something in the nick of time, you do it just in time.
'We arrived at the railway station just in the nick of time.'
It's the last possible moment to do something before it's too late.
There's a classic Hitchcock film of the same name with a racy 1995 remake featuring Johhny Depp. It's quite a nail-biter; here's the beginning.