-Did it depend on the rank? How were the contracts set up? How were the years determined? Were they negotiable?
In general, the standard length of service for a Roman soldier was 25 years. In the beginning, since the Roman soldiers were militia, there were no set lengths of service since they would simply serve when called upon. When the Roman Army became a standing army through the reforms of the late republic, the length of service was originally 16 years. This was increased to 25 years in the mid of the 1st century. However, after 20 years, a soldier would receive his honesta missio, the honourable discharge, and he was now a veteran, expected to serve five more years with lighter duties upon which he would be totally dismissed from service. They also received their money and a bonus upon discharge.
You could, however, in later imperial times, end your service there after 20 years with your honesta missio. Your discharge bonus would be considerably lower, though - while, for example, in the 4th century, a veteran of 25 years would be excused 5 capita (heads) of taxation in kind, one having served only 20 years would receive only 2 capita exemption - presumably for himself and his wife [found on the Brigetio tablet].
There was also the extraordinary possibility to receive a special honourable discharge from the Emperor himself before the end of your duties, receiving full honours and benefits. For example, in Numidia, there is a honorary inscription by a man who claims to have been "discharged with honesta missio before his time by his [the emperors] indulgence" [CIL VIII, 4594].
Depending on the political climate, after your discharge you might be settled in a military colony, or you simply might get a patch of land in one of the provinces. Many settled in the area around their old barracks.
If you were injured or sick and could no longer serve, you were given the misso causaria and released from service, receiving money according to the length of your service; a missio ignominiosa, dishonourable discharge, could be given for certain crimes, which also disbarred you from such privileges as other veterans enjoyed.
You could absolutely serve longer and stay a soldier for as long as you liked. These were the evocati, literally 'those called upon', who of their own accord served a longer time on the request of their (former) commander - and they were handsomely rewarded, they received the same pay as a centurion, often had the same auctoritas, signified by them being allowed to carry a vitis, the vine-cane that the centurions used both as badge of rank and as tool to punish disobedient subordinates, and they were exempt from duties suh as building, road maintenance and such (which made up a large part of a legions day-to-day activities). There was a large part of the legions consisting of such career soldiers.
This Centurion claims to have served for 43 years (Stipendiorum XXXXIII); and there is even a Centurion from the Legio II Adiutrix, buried in Budapest [TitAq II, 499], that claims 61 years of service, which is the longest I could find. The individual facticity may be doubted, but if such a claim were obviously impossible it would not be made on such a representative piece as a tombstone. Uncommon, but not unbelievably so.
The Praetorians served a shorter term of 16 years, but they could also continue serving as evocati.
Then, there were the auxiliaries. After they had served their 25 year term, they would receive Roman citizenship (they were able to carry proof of that in a copy of the official edict, posted in Rome, in the form of a bronze tablet, a so called military diploma, stating name, unit, the emperors decree for their citizenship and some other interesting facts.)
But they also could serve longer if they wished. Take, for example, this man, Caius Iulius Andromachus, an archer in the Cohors II Cyrrestarum, who died at 56 and claims to have served 40 years (Stip(endiorum) XXXX).
How were the years determined?
In general, the legions had a large bureaucratic apparatus that recorded the date when a soldier joined the legion, and they kept track of the years of service and the money payed and owed, so it would not have been difficult. Or did you mean something else?
In essence, the length of service was quasi open ended, but a shorter length of service than 25 years was possible - though very seldom less than 20 years.