The work done by a gas on a piston, by definition:
$$W_{gas} = \int \vec{F} \cdot \vec{dr}$$
Where $\vec{F}$ is the net force on the piston, $\vec{dr}$ is the infinitesimal displacement of this piston.
Since the force on the piston, is against the direction of displacement (for my coordinate system), F and dr are in opposite directions hence the work done on the gas
$$W_{gas} = \int -|F||dr|$$
Assuming that I move my piston in the purely x direction
$$W_{gas} = \int -|F|dx$$
I then move my piston from its initial position relative to my coordinates, to a distance x, thus the work done by the gas on the piston is
$$W_{gas} = \int_{0}^{x} -|F|dx$$
Therefore, the negative of this quantity is the work done by the piston on the gas (work done on the gas):
$$W = \int_{0}^{x} |F|dx$$
Let's forget about converting this into a pressure volume integral for simplicity
From here I am having trouble reconciling the standard formula for the work done on a gas.
$$W= -\int_{v_{i}}^{v_{f}} \rho dv$$
This converted into linear form is
$$W=-\int_{l_{1 I}}^{l_{f}} |F| dx$$
Let's take a look at the different formulas.
$$W = \int_{0}^{x} |F|dx$$
$$W=-\int_{l_{i}}^{l_{f}} |F| dx$$
One is integrating from the pistons initial position, to the pistons final position.
One is integrating from the remaining length when the piston is at its starting position, to the remaining length when the piston has extended.
(Much like the volume form is the remaining volume, and the remaining volume after the piston extends)
From first principles, the one that I derived, only matches the given formula given that $\rho$( and therefore F) is constant.
$$\int_{0}^{x} |F|dx = -\int_{l_{1 I}}^{l_{f}} |F| dx$$
$$|F|x - |F|0 = |F|(l_{i} -l_{f})$$
From the diagram $l_{i} -l_{f} = x$
$$|F|x = |F|x$$
The formulas differ when F is not constant.
Which formula is correct? Have i made any mistakes?