Nakayama's lemma in Theorem 8.17 of Chapter II in Hartshorne 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?
 A: There are a lot of equivalent ways to formulate the Nakayama's lemma, I don't know the one you are familiar with. One of the most quoted version is the following

Nakayama's lemma:Let $R$ be a commutative ring with unity and let $J(R)$ be its Jacobson radical (i.e. the intersection of all maximal ideals of R). If $M$ is a finitely generated $R$-module such that $J(R)M=M$, then $M=0$.

Usually, Nakayama's lemma is used in the context of local rings $R$ with maximal ideal $m$. In this case the Jacobson radical is just $m$, so that the Nakayama's lemma tells us that if $M$ is a finitely generated $R$-module s.t. $mM=M$, then $M=0$.
Now, suppose that $N\subset M$ is a submodule, and suppose moreover that $N+mM=M$. This implies that $m\cdot M/N=M/N$. We can apply Nakayama's lemma in this situation by taking as $R$-module $M/N$, obtaining that $M/N=0$ or, in other terms, $N=M$. So far we have deduced the following equivalent version of the Nakayama's lemma, that we are going to use to answer your question:

Nakayama's lemma (2): Let $R$ be a local ring with maximal ideal $m$ and let $M$ be a finitely generated $R$-module. If $N\subset M$ is an $R$-submodule s.t. $N+mM=M$, then $N=M$.

The statement we want to prove is clearly local, so we can suppose that $X=Spec(R)$ is affine and the sheaf of ideals $\mathcal{I}$ is the sheafification of a prime ideal $p\subset R$: let me remark that we know that the associated ideal is prime because by hypothesis $Y$ is irreducible, and we already deduced that is non-singular, thus reduced.
So, we know that $p/p^2$ is locally generated by $n-q$ elements as $R/p$-module and we want to prove that $p$ is locally generated by $n-q$ elements as $R$-module. It's equivalent to prove that, for $m\subset R$ a maximal ideal, if $p\subset R_m$ is a prime ideal such that $p/p^2$ is generated by $n-q$ elements, then $p$ is generated by $n-q$ elements.
Let $q\subset p$ be the subideal generated by the liftings of the generators of $p/p^2$. We want to prove that $p=q$. By hypothesis, $q+p^2=p$. Observe that this implies that $q+mp=p$, because
$$ p=q+p^2\subset q+mp \subset p $$
where we used the fact that in $R_m$ we have $p\subset m$. By Nakayama's lemma (2), we obtain that $p=q$, and we're done.
