So just how did the noun "nut" become a term for "crazy person?" An interesting article in Slate.com explains the history of the slang usage of the word.
More interesting is the part at the end of the article which points out the sexual side of many slang terms for mental illness:
"Psychologist Timothy Anderson points out that many recent euphemisms for insanity have sexual connotations. The "nut" once described the head of a man's penis—only later did it come to mean the head of his body, and then his testicles. "Screw" already meant copulation by the time "screwy" came into usage. And various fruit-themed words (like, well, "fruit" itself) connoted homosexuality before they became associated with craziness (as in "she went bananas" or "he's nutty as a fruitcake")."