hcju/stacksqa huggingface dataset:
If $a^k x = 0$ for $x \in R$, then $x$ is contained in all the minimal primes of $R$ and hence nilpotent, see Lemma \ref{lemma-Zariski-topology}. Thus the kernel of $R \to R[\frac{I}{a}]$ consists of nilpotent elements. Hence the result follows from Lemma \ref{lemma-image-dense-generic-points}.
Any blowup algebra $R[\frac{I}{a}]$ is a domain contained in $K$ see Lemma \ref{lemma-blowup-domain}. The lemma simply says that $A$ is the directed union of the ones where $a \in I$ have properties (1), (2), (3). If $R[\frac{I}{a}] \subset A$ and $R[\frac{J}{b}] \subset A$, then we have $$ R[\textstyle{\frac{I}{a}}] \cup R[\textstyle{\frac{J}{b}}]  \subset R[\textstyle{\frac{IJ}{ab}}] \subset A $$ The first inclusion because $x/a^n = b^nx/(ab)^n$ and the second one because if $z \in (IJ)^n$, then $z = \sum x_iy_i$ with $x_i \in I^n$ and $y_i \in J^n$ and hence $z/(ab)^n = \sum (x_i/a^n)(y_i/b^n)$ is contained in $A$. \medskip\noindent Consider a finite subset $E \subset A$. Say $E = \{e_1, \ldots, e_n\}$. Choose a nonzero $a \in R$ such that we can write $e_i = f_i/a$ for all $i = 1, \ldots, n$. Set $I = (f_1, \ldots, f_n, a)$. We claim that $R[\frac{I}{a}] \subset A$. This is clear as an element of $R[\frac{I}{a}]$ can be represented as a polynomial in the elements $e_i$. The lemma follows immediately from this observation.
Another map $\beta : F_{\bullet} \to G_{\bullet}$ inducing $\varphi$ is homotopic to $\alpha$ by Lemma \ref{lemma-compare-resolutions}. Hence the maps $\Hom_R(F_\bullet, N) \to \Hom_R(G_\bullet, N)$ are homotopic. Hence the independence result follows from Lemma \ref{lemma-homotopic-equal-homology}. \medskip\noindent Suppose that $\varphi$ is an isomorphism. Let $\psi : M_2 \to M_1$ be an inverse. Choose $\beta : G_{\bullet} \to F_{\bullet}$ be a map inducing $\psi : M_2 = \Coker(d_{G, 1}) \to M_1 = \Coker(d_{F, 1})$, see Lemma \ref{lemma-compare-resolutions}. OK, and now consider the map $H^i(\alpha) \circ H^i(\beta) = H^i(\alpha \circ \beta)$. By the above the map $H^i(\alpha \circ \beta)$ is the {\it same} as the map $H^i(\text{id}_{G_{\bullet}}) = \text{id}$. Similarly for the composition $H^i(\beta) \circ H^i(\alpha)$. Hence $H^i(\alpha)$ and $H^i(\beta)$ are inverses of each other.
Pick a free resolution $F_{\bullet}$ of $M$. Since $\Ext^i_R(M, N)$ is defined as the cohomology of the complex $\Hom_R(F_{\bullet}, N)$ the lemma is clear when $xN = 0$. If $xM = 0$, then we see that multiplication by $x$ on $F_{\bullet}$ lifts the zero map on $M$. Hence by Lemma \ref{lemma-ext-welldefined} we see that it induces the same map on Ext groups as the zero map.
Suppose that $IM = M$. Then Lemma \ref{lemma-NAK} shows there exists an $f \in I$ such that $f : M \to M$ is $\text{id}_M$. Hence $f, 0, 0, 0, \ldots$ is an infinite sequence of successive nonzerodivisors and we see agreement holds in this case. If $IM \not =  M$, then we see that a sequence as in the lemma is an $M$-regular sequence and we conclude that agreement holds as well.
The proof is by induction on $\dim(\text{Supp}(M))$. If $\dim(\text{Supp}(M)) = 0$, then $\text{Supp}(M) = \{\mathfrak m\}$, whence $\text{Ass}(M) = \{\mathfrak m\}$ (by Lemmas \ref{lemma-ass-support} and \ref{lemma-ass-zero}), and hence the depth of $M$ is zero for example by Lemma \ref{lemma-ideal-nonzerodivisor}. For the induction step we assume $\dim(\text{Supp}(M)) > 0$. Let $f_1, \ldots, f_d$ be a sequence of elements of $\mathfrak m$ such that $f_i$ is a nonzerodivisor on $M/(f_1, \ldots, f_{i - 1})M$. According to Lemma \ref{lemma-depth-weak-sequence} it suffices to prove $\dim(\text{Supp}(M)) \geq d$. We may assume $d > 0$ otherwise the lemma holds. By Lemma \ref{lemma-one-equation-module} we have $\dim(\text{Supp}(M/f_1M)) = \dim(\text{Supp}(M)) - 1$. By induction we conclude $\dim(\text{Supp}(M/f_1M)) \geq d - 1$ as desired.
Since $M/IM$ is nonzero we can choose $\mathfrak p \in \text{Supp}(M/IM)$ by Lemma \ref{lemma-support-zero}. Then $(M/IM)_\mathfrak p \not = 0$ which implies $I \subset \mathfrak p$ and moreover implies $M_\mathfrak p \not = IM_\mathfrak p$ as localization is exact. Let $f_1, \ldots, f_r \in I$ be an $M$-regular sequence. Then $M_\mathfrak p/(f_1, \ldots, f_r)M_\mathfrak p$ is nonzero as $(f_1, \ldots, f_r) \subset I$. As localization is flat we see that the images of $f_1, \ldots, f_r$ form a $M_\mathfrak p$-regular sequence in $I_\mathfrak p$. Since this works for every $M$-regular sequence in $I$ we conclude that $\text{depth}_I(M) \leq \text{depth}_{I_\mathfrak p}(M_\mathfrak p)$. The latter is $\leq \text{depth}(M_\mathfrak p)$ which is $< \infty$ by Lemma \ref{lemma-bound-depth}.
Let $\delta(M)$ denote the depth of $M$ and let $i(M)$ denote the smallest integer $i$ such that $\Ext^i_R(R/\mathfrak m, M)$ is nonzero. We will see in a moment that $i(M) < \infty$. By Lemma \ref{lemma-ideal-nonzerodivisor} we have $\delta(M) = 0$ if and only if $i(M) = 0$, because $\mathfrak m \in \text{Ass}(M)$ exactly means that $i(M) = 0$. Hence if $\delta(M)$ or $i(M)$ is $> 0$, then we may choose $x \in \mathfrak m$ such that (a) $x$ is a nonzerodivisor on $M$, and (b) $\text{depth}(M/xM) = \delta(M) - 1$. Consider the long exact sequence of Ext-groups associated to the short exact sequence $0 \to M \to M \to M/xM \to 0$ by Lemma \ref{lemma-long-exact-seq-ext}: $$ \begin{matrix} 0 \to \Hom_R(\kappa, M) \to \Hom_R(\kappa, M) \to \Hom_R(\kappa, M/xM) \\ \phantom{0\ } \to \Ext^1_R(\kappa, M) \to \Ext^1_R(\kappa, M) \to \Ext^1_R(\kappa, M/xM) \to \ldots \end{matrix} $$ Since $x \in \mathfrak m$ all the maps $\Ext^i_R(\kappa, M) \to \Ext^i_R(\kappa, M)$ are zero, see Lemma \ref{lemma-annihilate-ext}. Thus it is clear that $i(M/xM) = i(M) - 1$. Induction on $\delta(M)$ finishes the proof.
Use the characterization of depth using the Ext groups $\Ext^i(\kappa, N)$, see Lemma \ref{lemma-depth-ext}, and use the long exact cohomology sequence $$ \begin{matrix} 0 \to \Hom_R(\kappa, N') \to \Hom_R(\kappa, N) \to \Hom_R(\kappa, N'') \\ \phantom{0\ } \to \Ext^1_R(\kappa, N') \to \Ext^1_R(\kappa, N) \to \Ext^1_R(\kappa, N'') \to \ldots \end{matrix} $$ from Lemma \ref{lemma-long-exact-seq-ext}.
Part (2) is a formal consequence of part (1). Let $x \in R$ be as in (1). By the short exact sequence $0 \to M \to M \to M/xM \to 0$ and Lemma \ref{lemma-depth-in-ses} we see that the depth drops by at most 1. On the other hand, if $x_1, \ldots, x_r \in \mathfrak m$ is a regular sequence for $M/xM$, then $x, x_1, \ldots, x_r$ is a regular sequence for $M$. Hence we see that the depth drops by at least 1.
Pick a submodule $N \subset M$ with $N \cong R/\mathfrak p$. By the Artin-Rees lemma (Lemma \ref{lemma-Artin-Rees}) we can pick $n > 0$ such that $N \cap x^nM \subset xN$. Let $\overline{N} \subset M/x^nM$ be the image of $N \to M \to M/x^nM$. By Lemma \ref{lemma-ass} it suffices to show $\mathfrak q \in \text{Ass}(\overline{N})$. By our choice of $n$ there is a surjection $\overline{N} \to N/xN = R/\mathfrak p + (x)$ and hence $\mathfrak q$ is in the support of $\overline{N}$. Since $\overline{N}$ is annihilated by $x^n$ and $\mathfrak p$ we see that $\mathfrak q$ is minimal among the primes in the support of $\overline{N}$. Thus $\mathfrak q$ is an associated prime of $\overline{N}$ by Lemma \ref{lemma-ass-minimal-prime-support}.
If $\mathfrak m \in \text{Ass}(M)$ then there is a nonzero element $x \in M$ which is annihilated by all elements of $\mathfrak m$. Thus $\text{depth}(M) = 0$. In particular the lemma holds in this case. \medskip\noindent If $\text{depth}(M) = 1$, then by the first paragraph we find that $\mathfrak m \not \in \text{Ass}(M)$. Hence $\dim(R/\mathfrak p) \geq 1$ for all $\mathfrak p \in \text{Ass}(M)$ and the lemma is true in this case as well. \medskip\noindent We will prove the lemma in general by induction on $\text{depth}(M)$ which we may and do assume to be $> 1$. Pick $x \in \mathfrak m$ which is a nonzerodivisor on $M$. Note $x \not \in \mathfrak p$ (Lemma \ref{lemma-ass-zero-divisors}). By Lemma \ref{lemma-one-equation} we have $\dim(R/\mathfrak p + (x)) = \dim(R/\mathfrak p) - 1$. Thus there exists a prime $\mathfrak q$ minimal over $\mathfrak p + (x)$ with $\dim(R/\mathfrak q) = \dim(R/\mathfrak p) - 1$ (small argument omitted; hint: the dimension of a Noetherian local ring $A$ is the maximum of the dimensions of $A/\mathfrak r$ taken over the minimal primes $\mathfrak r$ of $A$). Pick $n$ as in Lemma \ref{lemma-inherit-minimal-primes} so that $\mathfrak q$ is an associated prime of $M/x^nM$. We may apply induction hypothesis to $M/x^nM$ and $\mathfrak q$ because $\text{depth}(M/x^nM) = \text{depth}(M) - 1$ by Lemma \ref{lemma-depth-drops-by-one}. We find $\dim(R/\mathfrak q) \geq \text{depth}(M/x^nM)$ and we win.
If $M_\mathfrak p = 0$, then $\text{depth}(M_\mathfrak p) = \infty$ and the lemma holds. If $\text{depth}(M) \leq \dim(R/\mathfrak p)$, then the lemma is true. If $\text{depth}(M) > \dim(R/\mathfrak p)$, then $\mathfrak p$ is not contained in any associated prime $\mathfrak q$ of $M$ by Lemma \ref{lemma-depth-dim-associated-primes}. Hence we can find an $x \in \mathfrak p$ not contained in any associated prime of $M$ by Lemma \ref{lemma-silly} and Lemma \ref{lemma-finite-ass}. Then $x$ is a nonzerodivisor on $M$, see Lemma \ref{lemma-ass-zero-divisors}. Hence $\text{depth}(M/xM) = \text{depth}(M) - 1$ and $\text{depth}(M_\mathfrak p / x M_\mathfrak p) = \text{depth}(M_\mathfrak p) - 1$ provided $M_\mathfrak p$ is nonzero, see Lemma \ref{lemma-depth-drops-by-one}. Thus we conclude by induction on $\text{depth}(M)$.
By Lemmas \ref{lemma-integral-no-inclusion}, \ref{lemma-integral-going-up}, and Lemma \ref{lemma-finite-finite-fibres} the maximal ideals of $S$ are exactly the primes of $S$ lying over $\mathfrak m$ and there are finitely many of them. Hence the statement of the lemma makes sense. We will prove the lemma by induction on $k = \min\nolimits_{i = 1, \ldots, n} \text{depth}(N_{\mathfrak m_i})$. If $k = 0$, then $\text{depth}(N_{\mathfrak m_i}) = 0$ for some $i$. By Lemma \ref{lemma-depth-ext} this means $\mathfrak m_i S_{\mathfrak m_i}$ is an associated prime of $N_{\mathfrak m_i}$ and hence $\mathfrak m_i$ is an associated prime of $N$ (Lemma \ref{lemma-localize-ass}). By Lemma \ref{lemma-ass-functorial-Noetherian} we see that $\mathfrak m$ is an associated prime of $N$ as an $R$-module. Whence $\text{depth}_\mathfrak m(N) = 0$. This proves the base case. If $k > 0$, then we see that $\mathfrak m_i \not \in \text{Ass}_S(N)$. Hence $\mathfrak m \not \in \text{Ass}_R(N)$, again by Lemma \ref{lemma-ass-functorial-Noetherian}. Thus we can find $f \in \mathfrak m$ which is not a zerodivisor on $N$, see Lemma \ref{lemma-ideal-nonzerodivisor}. By Lemma \ref{lemma-depth-drops-by-one} all the depths drop exactly by $1$ when passing from $N$ to $N/fN$ and the induction hypothesis does the rest.
Choose a free resolution $F_\bullet$ of $M$. Since $R \to R'$ is flat we see that $F_\bullet \otimes_R R'$ is a free resolution of $M \otimes_R R'$ over $R'$. The statement is that the map $$ \Hom_{R'}(F_\bullet \otimes_R R', N') \to \Hom_R(F_\bullet, N') $$ induces an isomorphism on homology groups, which is true because it is an isomorphism of complexes by Lemma \ref{lemma-adjoint-tensor-restrict}.
Assume $\varphi : N \to M$ satisfies the assumptions of the lemma. Note that this implies that $\Ker(\varphi) \subset I^nN$ for arbitrarily large $n$. Hence by Lemma \ref{lemma-intersection-powers-ideal-module} we see that $\varphi$ is injection. Let $Q = M/N$ so that we have a short exact sequence $$ 0 \to N \to M \to Q \to 0. $$ Let $$ F_2 \xrightarrow{d_2} F_1 \xrightarrow{d_1} F_0 \to Q \to 0 $$ be a finite free resolution of $Q$. We can choose a map $\alpha : F_0 \to M$ lifting the map $F_0 \to Q$. This induces a map $\beta : F_1 \to N$ such that $\beta \circ d_2 = 0$. The extension above is split if and only if there exists a map $\gamma : F_0 \to N$ such that $\beta = \gamma \circ d_1$. In other words, the class of $\beta$ in $\Ext^1_R(Q, N)$ is the obstruction to splitting the short exact sequence above. \medskip\noindent Suppose $n$ is a large integer such that $N/I^nN \to M/I^nM$ is a split injection. This implies $$ 0 \to N/I^nN \to M/I^nM \to Q/I^nQ \to 0. $$ is still short exact. Also, the sequence $$ F_1/I^nF_1 \xrightarrow{d_1} F_0/I^nF_0 \to Q/I^nQ \to 0 $$ is still exact. Arguing as above we see that the map $\overline{\beta} : F_1/I^nF_1 \to N/I^nN$ induced by $\beta$ is equal to $\gamma_n \circ d_1$ for some map $\overline{\gamma_n} : F_0/I^nF_0 \to N/I^n$. Since $F_0$ is free we can lift $\overline{\gamma_n}$ to a map $\gamma_n : F_0 \to N$ and then we see that $\beta - \gamma_n \circ d_1$ is a map from $F_1$ into $I^nN$. In other words we conclude that $$ \beta \in \Im\Big(\Hom_R(F_0, N) \to \Hom_R(F_1, N)\Big) + I^n\Hom_R(F_1, N). $$ for this $n$. \medskip\noindent Since we have this property for arbitrarily large $n$ by assumption we conclude that the image of $\beta$ in the cokernel of $\Hom_R(F_0, N) \to \Hom_R(F_1, N)$ is zero by  Lemma \ref{lemma-intersection-powers-ideal-module}. Hence $\beta$ is in the image of the map $\Hom_R(F_0, N) \to \Hom_R(F_1, N)$ as desired.
Suppose $M$ is flat. Let $N$ be an $R$-module. Let $F_\bullet$ be a free resolution of $N$. Then $F_\bullet \otimes_R M$ is a resolution of $N \otimes_R M$, by flatness of $M$. Hence all higher Tor groups vanish. \medskip\noindent It now suffices to show that the last condition implies that $M$ is flat. Let $I \subset R$ be an ideal. Consider the short exact sequence $0 \to I \to R \to R/I \to 0$. Apply Lemma \ref{lemma-long-exact-sequence-tor}. We get an exact sequence $$ \text{Tor}_1^R(M, R/I) \to M \otimes_R I \to M \otimes_R R \to M \otimes_R R/I \to 0 $$ Since obviously $M \otimes_R R = M$ we conclude that the last hypothesis implies that $M \otimes_R I \to M$ is injective for every finitely generated ideal $I$. Thus $M$ is flat by Lemma \ref{lemma-flat}.
By Lemma \ref{lemma-characterize-projective} we can choose a set $A$ and a direct sum decomposition $\bigoplus_{\alpha \in A} R/I = \overline{P} \oplus \overline{K}$ for some $R/I$-module $\overline{K}$. Write $F = \bigoplus_{\alpha \in A} R$ for the free $R$-module on $A$. Choose a lift $p : F \to F$ of the projector $\overline{p}$ associated to the direct summand $\overline{P}$ of $\bigoplus_{\alpha \in A} R/I$. Note that $p^2 - p \in \text{End}_R(F)$ is a nilpotent endomorphism of $F$ (as $I$ is nilpotent and the matrix entries of $p^2 - p$ are in $I$; more precisely, if $I^n = 0$, then $(p^2 - p)^n = 0$). Hence by Lemma \ref{lemma-lift-idempotents-noncommutative} we can modify our choice of $p$ and assume that $p$ is a projector. Set $P = \Im(p)$.
Recall that $\overline{P}$ is a direct summand of a free $R/I$-module $\bigoplus_{\alpha \in A} R/I$ by Lemma \ref{lemma-characterize-projective}. As $\overline{P}$ is finite, it follows that $\overline{P}$ is contained in $\bigoplus_{\alpha \in A'} R/I$ for some $A' \subset A$ finite. Hence we may assume we have a direct sum decomposition $(R/I)^{\oplus n} = \overline{P} \oplus \overline{K}$ for some $n$ and some $R/I$-module $\overline{K}$. Choose a lift $p \in \text{Mat}(n \times n, R)$ of the projector $\overline{p}$ associated to the direct summand $\overline{P}$ of $(R/I)^{\oplus n}$. Note that $p^2 - p \in \text{Mat}(n \times n, R)$ is nilpotent: as $I$ is locally nilpotent and the matrix entries $c_{ij}$ of $p^2 - p$ are in $I$ we have $c_{ij}^t = 0$ for some $t > 0$ and then $(p^2 - p)^{tn^2} = 0$ (by looking at the matrix coefficients). Hence by Lemma \ref{lemma-lift-idempotents-noncommutative} we can modify our choice of $p$ and assume that $p$ is a projector. Set $P = \Im(p)$.
By Lemma \ref{lemma-lift-projective-module} we can find a projective $R$-module $P$ and an isomorphism $P/IP \to M/IM$. We are going to show that $M$ is isomorphic to $P$ which will finish the proof. Because $P$ is projective we can lift the map $P \to P/IP \to M/IM$ to an $R$-module map $P \to M$ which is an isomorphism modulo $I$. Since $I^n = 0$ for some $n$, we can use the filtrations \begin{align*} 0 = I^nM \subset I^{n - 1}M \subset \ldots \subset IM \subset M \\ 0 = I^nP \subset I^{n - 1}P \subset \ldots \subset IP \subset P \end{align*} to see that it suffices to show that the induced maps $I^aP/I^{a + 1}P \to I^aM/I^{a + 1}M$ are bijective. Since both $P$ and $M$ are flat $R$-modules we can identify this with the map $$ I^a/I^{a + 1} \otimes_{R/I} P/IP \longrightarrow I^a/I^{a + 1} \otimes_{R/I} M/IM $$ induced by $P \to M$. Since we chose $P \to M$ such that the induced map $P/IP \to M/IM$ is an isomorphism, we win.
Pick a maximal ideal $\mathfrak m \subset R$. Pick $x_1, \ldots, x_r \in M$ which map to a $\kappa(\mathfrak m)$-basis of $M \otimes_R \kappa(\mathfrak m) = M/\mathfrak mM$. In particular $\rho_M(\mathfrak m) = r$. By Nakayama's Lemma \ref{lemma-NAK} there exists an $f \in R$, $f \not \in \mathfrak m$ such that $x_1, \ldots, x_r$ generate $M_f$ over $R_f$. By the assumption that $\rho_M$ is locally constant there exists a $g \in R$, $g \not \in \mathfrak m$ such that $\rho_M$ is constant equal to $r$ on $D(g)$. We claim that $$ \Psi : R_{fg}^{\oplus r} \longrightarrow M_{fg}, \quad (a_1, \ldots, a_r) \longmapsto \sum a_i x_i $$ is an isomorphism. This claim will show that $M$ is finite locally free, i.e., that (7) holds. Since $\Psi$ is surjective, it suffices to show that $\Psi$ is injective. Since $R_{fg}$ is reduced, it suffices to show that $\Psi$ is injective after localization at all minimal primes $\mathfrak p$ of $R_{fg}$, see Lemma \ref{lemma-reduced-ring-sub-product-fields}. However, we know that $R_\mathfrak p = \kappa(\mathfrak p)$ by Lemma \ref{lemma-minimal-prime-reduced-ring} and $\rho_M(\mathfrak p) = r$ hence $\Psi_\mathfrak p : R_\mathfrak p^{\oplus r} \to M \otimes_R \kappa(\mathfrak p)$ is an isomorphism as a surjective map of finite dimensional vector spaces of the same dimension.
Follows from the equational criterion of flatness, see Lemma \ref{lemma-flat-eq}. Namely, suppose that $x_1, \ldots, x_r \in M$ map to a basis of $M/\mathfrak mM$. By Nakayama's Lemma \ref{lemma-NAK} these elements generate $M$. We want to show there is no relation among the $x_i$. Instead, we will show by induction on $n$ that if $x_1, \ldots, x_n \in M$ are linearly independent in the vector space $M/\mathfrak mM$ then they are independent over $R$. \medskip\noindent The base case of the induction is where we have $x \in M$, $x \not\in \mathfrak mM$ and a relation $fx = 0$. By the equational criterion there exist $y_j \in M$ and $a_j \in R$ such that $x = \sum a_j y_j$ and $fa_j = 0$ for all $j$. Since $x \not\in \mathfrak mM$ we see that at least one $a_j$ is a unit and hence $f = 0 $. \medskip\noindent Suppose that $\sum f_i x_i$ is a relation among $x_1, \ldots, x_n$. By our choice of $x_i$ we have $f_i \in \mathfrak m$. According to the equational criterion of flatness there exist $a_{ij} \in R$ and $y_j \in M$ such that $x_i = \sum a_{ij} y_j$ and $\sum f_i a_{ij} = 0$. Since $x_n \not \in \mathfrak mM$ we see that $a_{nj}\not\in \mathfrak m$ for at least one $j$. Since $\sum f_i a_{ij} = 0$ we get $f_n = \sum_{i = 1}^{n-1} (-a_{ij}/a_{nj}) f_i$. The relation $\sum f_i x_i = 0$ now can be rewritten as $\sum_{i = 1}^{n-1} f_i( x_i + (-a_{ij}/a_{nj}) x_n) = 0$. Note that the elements $x_i + (-a_{ij}/a_{nj}) x_n$ map to $n-1$ linearly independent elements of $M/\mathfrak mM$. By induction assumption we get that all the $f_i$, $i \leq n-1$ have to be zero, and also $f_n = \sum_{i = 1}^{n-1} (-a_{ij}/a_{nj}) f_i$. This proves the induction step.
By Lemma \ref{lemma-finite-projective} being finite projective over a local ring is the same thing as being finite free. Suppose that $M \otimes_R S$ is a finite free $S$-module. Pick $x_1, \ldots, x_r \in M$ whose images in $M/\mathfrak m_RM$ form a basis over $\kappa(\mathfrak m)$. Then we see that $x_1 \otimes 1, \ldots, x_r \otimes 1$ are a basis for $M \otimes_R S$. This implies that the map $R^{\oplus r} \to M, (a_i) \mapsto \sum a_i x_i$ becomes an isomorphism after tensoring with $S$. By faithful flatness of $R \to S$, see Lemma \ref{lemma-local-flat-ff} we see that it is an isomorphism.
Assume $M$ is free of rank $n$. Let $I \subset S$ be the Jacobson radical. By Nakayama's Lemma \ref{lemma-NAK} a sequence of elements $m_1, \ldots, m_n$ is a basis for $M$ if and only if $\overline{m}_i \in M/IM$ generate $M/IM$. Hence we may replace $M$ by $M/IM$, $N$ by $N/(N \cap IM)$, $R$ by $R/\mathfrak m$, and $S$ by $S/IS$. In this case we see that $S$ is a finite product of fields $S = k_1 \times \ldots \times k_r$ and $M = k_1^{\oplus n} \times \ldots \times k_r^{\oplus n}$. The fact that $N \subset M$ generates $M$ as an $S$-module means that there exist $x_j \in N$ such that a linear combination $\sum a_j x_j$ with $a_j \in S$ has a nonzero component in each factor $k_i^{\oplus n}$. Because $R = k$ is an infinite field, this means that also some linear combination $y = \sum c_j x_j$ with $c_j \in k$ has a nonzero component in each factor. Hence $y \in N$ generates a free direct summand $Sy \subset M$. By induction on $n$ the result holds for $M/Sy$ and the submodule $\overline{N} = N/(N \cap Sy)$. In other words there exist $\overline{y}_2, \ldots, \overline{y}_n$ in $\overline{N}$ which (freely) generate $M/Sy$. Then $y, y_2, \ldots, y_n$ (freely) generate $M$ and we win.
By Lemma \ref{lemma-finite-projective} we see that $M$ is finitely presented as well as finite locally free. By Lemmas \ref{lemma-hom-from-finitely-presented} and \ref{lemma-tensor-product-localization} formation of the left and right hand side of the arrow commutes with localization. We may check that our map is an isomorphism after localization, see Lemma \ref{lemma-cover}. Thus we may assume $M$ is finite free. In this case the lemma is immediate.
The equality in the displayed formula follows from Nakayama's lemma. Nakayama's lemma also implies that $U$ is open. See Lemma \ref{lemma-NAK} especially part (3). If $D(f) \subset U$, then $M_f \to N_f$ is surjective on all localizations at primes of $R_f$, and hence it is surjective by Lemma \ref{lemma-characterize-zero-local}.
Let $\mathfrak p \in U$. Pick a presentation $N = R^{\oplus n}/\sum_{j = 1, \ldots, m} R k_j$. Denote $e_i$ the image in $N$ of the $i$th basis vector of $R^{\oplus n}$. For each $i \in \{1, \ldots, n\}$ choose an element $m_i \in M_{\mathfrak p}$ such that $\varphi(m_i) = f_i e_i$ for some $f_i \in R$, $f_i \not \in \mathfrak p$. This is possible as $\varphi_{\mathfrak p}$ is an isomorphism. Set $f = f_1 \ldots f_n$ and let $\psi : R_f^{\oplus n} \to M_f$ be the map which maps the $i$th basis vector to $m_i/f_i$. Note that $\varphi_f \circ \psi$ is the localization at $f$ of the given map $R^{\oplus n} \to N$. As $\varphi_{\mathfrak p}$ is an isomorphism we see that $\psi(k_j)$ is an element of $M$ which maps to zero in $M_{\mathfrak p}$. Hence we see that there exist $g_j \in R$, $g_j \not \in \mathfrak p$ such that $g_j \psi(k_j) = 0$. Setting $g = g_1 \ldots g_m$, we see that $\psi_g$ factors through $N_{fg}$ to give a map $\chi : N_{fg} \to M_{fg}$. By construction $\chi$ is a right inverse to $\varphi_{fg}$. It follows that $\chi_\mathfrak p$ is an isomorphism. By Lemma \ref{lemma-map-between-finite} there is an $h \in R$, $h \not \in \mathfrak p$ such that $\chi_h : N_{fgh} \to M_{fgh}$ is surjective. Hence $\varphi_{fgh}$ and $\chi_h$ are mutually inverse maps, which implies that $D(fgh) \subset U$ as desired.