A chain rule for nonsmooth composite functions via minimisation