EXCLUSIVE

Rapper Nelly was detained last night in Texas while police officers searched his tour bus -- and found HEROIN in addition to a loaded gun ... and MASSIVE amounts of weed ... law enforcement sources tell TMZ.

According to law enforcement sources, Nelly's bus was stopped at a border patrol checkpoint in Sierra Blanca -- and a police canine detected the presence of drugs.

We're told cops pulled the bus over -- and after performing a search, they found 36 small baggies of heroin (totaling .64 ounces) and over TEN POUNDS of marijuana inside a large green duffel bag (an amount roughly the size of a large bed pillow).

Cops also say they found a loaded .45 caliber pistol.

There were seven people riding on Nelly's bus at the time -- including the rapper -- and they were all detained during the search.

According to law enforcement sources, one of the passengers -- a guy named Brian Keith Jones -- admitted the drugs and the gun were his. He was subsequently placed under arrest.

Nelly and the remaining five were let go. Calls to Nelly's reps were not returned.