Scotland’s top civil servant is leaving after three years in the role - but will stay until his successor is appointed