A retired NYPD Internal Affairs detective described as a “ticking time bomb” had a cache of weapons seized after allegedly sending bizarre and threatening e-mails to former cop colleagues and department brass, sources said.

Police executed a search warrant at William Nolan’s Staten Island home on Dec. 20 and grabbed a loaded shotgun in his living room, another shotgun in the bedroom, two stun guns and four high-capacity magazines, court papers state. He was charged with weapons possession and resisting arrest.

The raid came after months of bizarre behavior in which Nolan, 55, who frequently wears a bodycam, would send creepy messages to anyone on his long “s–t list,” a source said.

“I’m glad they took the guns away from him,” said an ex-colleague who was on the receiving end of Nolan’s messages. “This guy is a ticking time bomb.”

Targets of his ire included dozens of cops, but also brass such as Internal Affairs Bureau Chief Joseph Reznick.

One chilling missive, sent under the name “toolbox63,” a handle sources said Nolan used in the past, was titled “Mass Fatality Human Remains Sealing Machine.” Attached was an 18-minute commercial video showing a simulated mass-casualty event in which responders use a new device to wrap corpses in body bags.

“You would think the next step is he was going to kill you,” said the source. “I would go home and make sure he wasn’t lurking somewhere.”

Nolan joined the NYPD in 1986, made detective in 1991 and retired in November 2005, police officials said. He worked undercover for Internal Affairs in the late 1980s and early 1990s and “everyone always thought he was off the wall, strange,” said the retired colleague.

Nolan began blasting out his e-mails in late February, said the source.

“Whether he was doing business with you or whether you were providing a service [to him], he thought he was being screwed,” the source said.

The source tipped off the NYPD’s License Division to Nolan’s volatile behavior and creepy e-mails in March.

On March 14, Nolan received a letter from the NYPD license division notifying him that his pistol license had been suspended because of his “failure to notify the division of numerous incidents.” The letter directed Nolan to “surrender any and all firearms to his local precinct.”

The next day, NYPD lawyer Larry Byrne sent Nolan a “cease and desist” letter ordering him to stop e-mailing Reznick and NYPD Chief of Staff Raymond Spinella.

“If you continue, you risk arrest,” it read.

But two days later, “toolbox63” began blasting out his body-bag video to many on his hit list, but not the two chiefs, a police source said.

Nolan then began firing off “wacky e-mails” to his union, the Detectives’ Endowment Association, the source said.

“It all revolves around his guns and gun permits. I think they took away his guns. Every once in a blue moon I get a slew of [e-mails].”

Nolan is out on $5,000 bail. He would not comment and directed inquiries to his lawyer, Matthew Santamauro, who also declined to comment.

Additional reporting by Nick Fugallo