Been doing this for years, working on Rust graphics libraries. What helps me sleep is just being totally exhausted. But focus at night is amazing, and motivation is very easy to sustain if the timing is regular. During the day, I keep thinking in the background what I can do in those 2 night hours, so when the time comes there is no procrastination. Feeling very productive this way.