The idea of reasoning is taken from the first paragraph of the book: "Topological Methods in Hydrodynamics by Vladimir I. Arnold, Boris A. Khesin".
As you probably know, the equations of hydrodynamics can be written in two ways
: in the Euler form and in the Lagrange form. In the first case, the velocity field is described,
and the equations take the form of the Euler or Navier-Stokes equations required in your
question. In the case of the Lagrange form, the trajectory of the "particle" $g(t, y)$ is described, where $y$ is a good set of parameters that uniquely describes each particle (further, we assume that $y = g(0, y)$ are the initial coordinates of the particle). These forms are equivalent, but it is more convenient to obtain the Lagrangian in the second case.
We obtain the formula for an ideal incompressible fluid (the density is then equal to unity):
In contrast to finite-dimensional mechanics, where the solution was a function only of time and the action was an integral only in time,in the case of continuum mechanics, a generalization occurs:
\begin{equation}
S(g) = \int dt \int_{G} dy \; L(g, \cfrac{\partial g}{\partial t}, \cfrac{\partial g}{\partial y})
\end{equation}
And the Euler-Lagrange equations will take the form (in tensor form, this formula looks not so scary):
\begin{equation}
\cfrac{\partial}{\partial t} \cfrac{\partial L}{\partial {(\partial g}/{\partial t})} + \cfrac{\partial}{\partial y} \cfrac{\partial L}{\partial {(\partial g}/{\partial y})} - \cfrac{\partial L}{\partial g} = 0
\end{equation}
As for the natural system, we will look for the Lagrangian in the form of the difference between kinetic and potential energy:
\begin{equation}
L(g, \cfrac{\partial g}{\partial t}, \cfrac{\partial g}{\partial y}) = T(g, \cfrac{\partial g}{\partial t}, \cfrac{\partial g}{\partial y}) - U(g, \cfrac{\partial g}{\partial t}, \cfrac{\partial g}{\partial y})
\end{equation}
Kinetic energy of a particle:
\begin{equation}
T = \cfrac{1}{2} \Big(\cfrac{\partial g(t, y)}{\partial t} \Big)^2
\end{equation}
The potential energy of a particle is simply the pressure:
\begin{equation}
U = P(g(t, y))
\end{equation}
Then the Euler-Lagrange equations will give the equation:
\begin{equation}
\cfrac{\partial^2 g(t, y)}{\partial t^2} + \cfrac{\partial P(t, g(t))}{\partial g} = 0
\end{equation}
The resulting equation is the Euler equation in the Lagrangian description.
The incompressibility condition of a fluid imposes a volume conservation constraint on the mapping $g(t, y): G\longrightarrow G$ that defines the motion of the fluid.
The Jacobian of this mapping must be equal to one at any given time:
\begin{equation}
det\Big(\cfrac{\partial g(t, y)}{\partial y} \Big) = 1, for \; all \; t
\end{equation}
Similar, but more cumbersome arguments can be used to obtain formulas for a compressible ideal fluid.