If we have a linear regression equation $y=X\beta + u$, then we can find the OLS estimate of $\beta$ by minimizing wrt $\hat \beta$: $E(\hat u)=E(y-X\hat\beta )$
However, my textbook suddenly says, out of nowhere, that the OLS estimate of the variance of $u$ (each $u_i$ is iid). $\sigma ^2$ is $\hat \sigma ^2 = \frac {\hat u^T \hat u}{n-K}$, where $n $ is the sample size and $K$ is the amount of independent variables.
I understand that this estimator is unbiased, but I have absolutely no idea how it is derived from the assumption of OLS, or why it is called the OLS estimate of $\sigma$.
How do we derive this estimator?