Flying of drones over railways in the UK is subject to the same restrictions as any other industrial infrastructure, that is no closer than 50m for larger drones but technically, the sub 250g drone exemptions apply.
However, there is an area of caution. Following a couple of near misses involving heritage steam locos (The Flying Scotsman) Network rail are paranoid over the issue.
The complication is that even though a drone flyer may well be compliant with CAA drone law, there is powerful Criminal legislation relating to the railways (for example it is one of the few places in the UK where trepass is a Criminal Offence and not a civil one) and there is a specific criminal offence of "Endangering persons on the Railway." It is an archaic law under section 34 Offences Against the Person Act 1861, but it is still current and never been repealed or updated, and the penalties carry a prison sentence of 2 years.
The British Transport Police can and still do charge people under this act so if a railway worker or the BTP ask you to desist from flying over the railway, even though you are satisfying CAA law, it is best to do so.