What should a contract contain to protect an employees rights to work on unrelated side projects in their free time?
Assume UK employment law.
It should contain one of the following two clauses:
(1) The author of a work is the first owner of any copyright in it, subject to the following provisions.
(2) Where a literary, dramatic, musical or artistic work, or a film, is made by an employee in the course of his employment, his employer is the first owner of any copyright in the work subject to any agreement to the contrary.
(3) This section does not apply to Crown copyright or Parliamentary copyright (see sections 163 and 165) or to copyright which subsists by virtue of section 168 (copyright of certain international organisations).
This would mean that your existing side projects are protected, but any future side projects would transfer to your employer under S11(2). This might be fine if you don't intend on doing any more side projects during your employment and simply want to protect your existing work.
Generally, the second clause seems to be what most employees would want in a contract to protect their side projects. Whether it can successfully be inserted depends on the negotiation between both sides. Most reasonable employers would not refuse it.
The leading case on the matter of S11(2) is Penhallurick v MD5 Ltd  EWCA Civ 1770 (02 December 2021) which future courts of first instance are presently bound to follow, unless the Supreme Court issues a different decision.
Per Penhallurick mentioned above, "in the course of employment" is generally held to mean doing things that you are paid for as part of your employment. For example, if you are a baker and you write a novel in your free time, it is clear that has nothing to do with your employment: you are not being paid by the bakery to write novels. However, if you were to write a baking cookbook that incorporated recipes that you developed at the bakery, that could be held to be done "in the course of employment" because the bakery could argue that you are paid to develop new recipes for them.
The development of side projects in relation to software development is fraught with peril. A lot of employers will try and advance the argument that, although you did not develop the work using company resources and time, your work is still "connected" to the area they operate in.
Well known examples of this include Google asserting ownership of software development side projects that, on the surface of it, aren't done on company time and resources and don't appear to have been done "in the course of employment" as you are not being paid specifically to work on, e.g. game development when you are employed as a website backend engineer.
If the employer can present enough evidence that what you are/were working on is "connected" to their business in some fashion, that is (at present, in England and Wales) sufficient for ownership to transfer to them under S11(2) even if the work wasn't done in company time and/or using company resources.
Both Parties agree that Section 11(2) of the Copyright, Designs and Patent Act 1988 ("the Act") does not apply to any and all Side Projects started prior to the commencement of employment. Both Parties also agree that S11(2) of the Act does not apply to any and all Side Projects started after the commencement of employment, specifically where said Side Projects are unrelated to the Employer's business of
area of business.
For the avoidance of doubt, "unrelated" has the ordinary meaning that a reasonable person would assign to it. For example, since the Employer operates in
area of business, a Side Project regarding the development of an online video game is clearly unrelated to the Employer's business.
This ex post presumption is rebuttable on the provision of clear and compelling evidence presented in a competent and validly constituted court of law in England and Wales that the Side Project was developed on company time and/or using company resources (e.g. a laptop) or that it validly infringes upon a business domain the Employer operates in. Both Parties agree that it will not be sufficient to adduce that the Employer "could" seek to operate in the Side Project's domain. For ownership to transfer under S11(2) of the Act, the Employer must demonstrate that they operated in that domain prior to the creation of the Side Project or that the Side Project has taken advantage of company resources that were not legitimately available outside of the company. For example, confidential company know-how that is specific to the Employer and not an industry standard or widely known thing. For the avoidance of doubt, use of open-source software (used in compliance with the licence and regardless of whether that software was created, released, or maintained by the Employer or someone else) does not constitute taking advantage of company resources.
The Employer agrees to pay the Employee a sum of special damages assessed and awarded appropriately by a court of law in England and Wales for any attempt to subvert this clause or sue the Employee for a Side Project contrary to this clause. In the alternative, at the Employee's absolute discretion, a permanent injunction enforcing this clause may be awarded against the Employer.