The Roommate Agreement was written by Sheldon and was signed by Leonard when they first became roommates. These events were shown during a flashback in The Staircase Implementation (S03E22). Sheldon starts mentioning paragraphs from the Roommate Agreement in the episode The Cooper-Nowitzki Theorem (S02E06) and continues to do so throughout the series, usually when one of the clauses is being violated.
This Roommate Agreement is one you could also use in real life. It is complete and coherent with real legal clauses used in real agreements. The first 42 sections form a roommate agreement you might have even signed. The last sections contain Sheldon's special clauses to suit his quirks. They come with foot notes so you can find the references.