The Iraqi use of mustard gas during the Iran-Iraq war is often cited as the trigger for Khamenei's quest for nukes - either the ultimate deterrant or the ultimate payback. It was around the time that Iraq was once again in position to possibly threaten Iran when their nuke program really took off, and by most accounts that nuke program ended about the same time Saddam was knocked off & the perceived Iraqi threat to Iran was eliminated. That they were researching & testing nuke weapon components during that time is almost certain.Such things are I will agree seldom rational, but why would Iran do this? They have certainly been holding their own since the end of the Iran-Iraq War without nuclear weapons, the West would have little reason to bother with them if they where not doing everything in their power to give the impression that they are working on a nuclear weapons program.
Iranian leaders aren't as stupid or suicidal as lots of folks assume them to be......they've watched Pakistan get billions of dollars in US aid, and North Korea also receive US aid, while Afghanistan and Iraq both get invaded. Its enough to make them go "hmmmmmm" and figure that the nuclear chip is a good one to have while bargaining. Maybe their nuke program really is on the backburner but they keep up the pretense to avoid getting hit with the US "stick". Maybe their program is dead in the water thanks to sanctions (maraging steel, good for missiles, is also good for centrifuges. Apparently Iran has had some problems with centrifuges because they're forced to use aluminum, which lacks the qualities of maraging steel), but they maintain the impression of a possible nuke weapons program as a show of (false) strength. Its a poker bluff, or brinksmanship.
But it still stands that there just isn't enough evidence of sufficient quality or quantity of a current & active weapons program, so theres no justification for escalating matters.