Nukes are arguably ONLY useful in defense (and deterring another from attacking). Using nuclear weapons against an opponent that also has nuclear weapons will very likely lead to a nuclear exchange and the decimation of both countries. It’s suicide, and so no one is going to do it unless they have nothing left to lose. A nation under attack, facing an existential threat, and about to be overrun, is in exactly that circumstance.
And yeah, maybe the defending countries leader isn’t willing to end it all, and maybe try collaboration or partisan resistance. But if you were the attacking country, would you be willing to make that bet? Or would you hesitate?