Theorem 8.17 of Chapter II in Hartshorne's Algebraic Geometry states the following:
Theorem. Let $X$ be a nonsingular variety over $k$. Let $Y \subseteq X$ be an irreducible closed subscheme defined by a sheaf of ideals $\mathscr{I}$. Then $Y$ is nonsingular if and only if
- $\Omega_{Y/k}$ is locally free, and
- the sequence of (8.12) is exact on the left also, that is, $$0 \longrightarrow \mathscr{I}/\mathscr{I}^2 \overset{\delta}{\longrightarrow} \Omega_{X/k} \otimes \mathscr{O}_Y \longrightarrow \Omega_{Y/k} \longrightarrow 0$$ is a short exact sequence.
Furthermore, in this case, $\mathscr{I}$ is locally generated by $r = \operatorname{codim}(Y,X)$ elements, and $\mathscr{I}/\mathscr{I}^2$ is a locally free sheaf of rank $r$ on $Y$.
In the direction $\Leftarrow$, Hartshorne proceeds as follows. Using (1), $\Omega_{Y/k}$ is locally free with $\operatorname{rank} \Omega_{Y/k} = q$, and so it suffices by Thm. 8.15 to show that $q = \dim Y$. We already know by Thm. 8.15 that $\Omega_{X/k}$ is locally free with $\operatorname{rank} \Omega_{X/k} = n = \dim X$, and so (2) implies that $\mathscr{I}/\mathscr{I}^2$ is also locally free of rank $n-q$.
My question is as follows: Why does Nakayama's lemma imply that $\mathscr{I}$ can be locally generated by $n-q$ elements?