It's generally legal (at least in the US) to contract for almost anything. Consideration does not have to be money, it can be foregoing something.
So for example, a contract for sale that said "i give you a copy of windows, and you agree not to marry anyone for 3 years and give me $50" would generally be valid, at least in the US.
Everything else is an argument about whether the contract is "against public policy", or whether the use of that contract constitutes "anticompetitive behavior".
These are two very difficult questions, and in general very fact/doctrinal specific. The Sherman Antitrust act in the US is a very vague piece of law that, if read strictly, outlaws all contracts (Since it outlaws contracts in restraint of trade, but basically all contracts are restraints on trade in one way or another)
Fun fact: Technically you can get 10 years in jail for violating antitrust law.