Moral considerations aside, Richard N. Haas gives a clear explanation of why nuclear weapons are attractive to countries.
Decades ago, Israel made such a calculation in the face of Arab threats to eliminate the Jewish state. More recently, Ukraine, Libya, and Iraq all gave up their nuclear weapons programs either voluntarily or under pressure. Subsequently, Ukraine was invaded by Russia, Iraq by the US, and Libya by the US and several of its European partners. Saddam Hussein in Iraq and Muammar el-Qaddafi in Libya were ousted.
North Korea has avoided such a fate, and the third generation of the Kim family rules with an iron fist. It is doubtful that the lesson is lost on Kim Jong-un.
You could try to estimate what it would cost for a particular country to have a conventional military equal in deterrent power to a few nuclear weapons. And the answer is almost certainly that they couldn’t afford it, even if they had the people and technology and productive capacity necessary. So a nuclear deterrent seems very logical. Of course the problem is that when everybody has them, or even a substantial fraction of countries has them, the risk to everybody becomes much higher than if nobody had them.
Another thing I learned from this article is that the UN General Assembly in July created (I don’t know if that is the right legal word) a treaty designed to facilitate long-term disarmament and eventual elimination of nuclear weapons. It may not be legally binding or backed by serious political power at the moment, but it shows leadership and could give serious, ethical leaders something to point to in future negotiations. Imagine for example if China, Russia, and the United States decided to throw their weight behind this.