In order to get the mass of an object from density, we might use \begin{equation} m = \int\rho(x)dx \tag{1} \end{equation}
I understand why this works on a conceptual level, but I would like to be able to derive this equation from
\begin{align} m = \rho x \tag{2} \end{align}
If I differentiate both sides, I get
$$ dm = \rho(x)dx + d\rho(x)x = \rho(x)dx + \rho'(x)xdx$$
Evidently, I want just the first term on the RHS, which means starting from (2) and differentiating doesn't work for deriving (1). Is this because $\rho$ functions as an average value in (2) and therefore we cannot differentiate like we would normally? i.e.
$$\rho = \frac{\int\rho(x)dx}{\int dx}$$
such that
$$m = \rho x = \int\rho(x)dx$$
I would also like to compare this to a similar treatment of momentum $p = mv$, where $dp = vdm + mdv$. To my understanding this is correct. Does this work because we are not referring to an average variable but a scalar quantity tied to a moving object? Following, does the above treatment of differentiating an equation to get a relative rate equation work, given that none of the variables are averages?