We study optimal nonlinear income taxation when earnings can differ because of both ability and luck, so the income tax has both a redistributive role and an insurance role. A substantial literature on optimal redistribution in the absence of uncertainty has evolved since Mirrlees' original contribution. The literature on the income tax as a social insurance device is more limited. It has largely assumed that households are ex ante identical so unequal earnings are due to uncertainty alone. We provide a general treatment of the optimal income tax under uncertainty when households differ in ability. We characterize optimal marginal tax rates and interpret them in terms of redistribution, insurance and incentive effects. The case of ex ante identical households and the no-risk case with heterogeneous abilities come out as special cases.