The USSR had nukes which were deployed in Ukraine, Belarus, and Kazakhstan. All command and control and other authority was completely centralized within Russia. Then the USSR collapsed in shocking rapidity, leaving these nukes in these locations. So the nukes were never theirs to begin with. It'd be like if the USA collapsed tomorrow and Germany tried to claim the US nukes located in Germany.
The reason these states returned them in exchange for a few bucks and empty assurances (which is an tautology - the difference between 'assurance' and 'guarantee' is precisely that there's no legal obligations or bindings with assurances) is because the alternative was being deemed a potentially rogue nuclear state which would have seen them rapidly invaded, very likely be a coalition of nations - viably including the US as well, and disarmed before they could manage to reverse engineer the protocols.
And yeah, Ukraine would not have been invaded if it had its own nukes, neither would Iran. It's also highly unlikely that the revolution, coup, or whatever you want to call it in 2014 would have happened if Ukraine had nukes. That involved things like John McCain giving speeches encouraging Ukrainians to overthrow their Russian-leaning president right in the center of Kyiv. [1] That was a massive power play that probably would never have happened with a nuclear state. Similar for the assassination of Soleimani in Iran, and so on. In any case, if a country wants to guarantee its sovereignty, it needs nukes.
And the reason this guarantees sovereignty is because every single country is aware of Proud Prophet [2] even if most people are not. It was a large scale nuclear wargame simulating all sorts of scenarios for 'peace through force' with nuclear weapons. In every single scenario it resulted in the defacto extinction of the Northern hemisphere. It led the US to turn away from escalation and hostility and towards reconciliation and negotiation with the USSR. Within a decade the first McDonalds would open up in the USSR, and shortly thereafter the entire USSR would collapse with Donald Duck being held up as a sign of freedom.