I think you’re mixing two different questions here. One is about the difference between earned and unearned income, and the other is about setting prices. Coffee has a price, potatoes have a price, and labor has a price. Prices of things are set according to supply and demand (among other things), not by how valuable they are, or useful, or necessary. Water, for example, is necessary for life, but very cheap. Diamonds are useless baubles, but very expensive. Saving someone’s life, or dispensing justice might be very important, but aren’t likely to pay as well as (say) being really good at throwing a basketball through a hoop.
There’s nothing inherently just or fair about how prices are set, whether it’s for saving lives or for diamonds.
Nevertheless, money you get for saving someone’s life (if you’re a doctor, or a paramedic) or dispensing justice (if you’re a judge, or a juror) or throwing balls through hoops (if you’re a pro basketball player) is earned income. It’s money you get for doing work.
Money you get for owning a hospital, or a courthouse (if such a thing were possible) or a basketball team is unearned income.
If you’re a potato farmer, the money you get for planting and harvesting potatoes is earned income. If you own a potato farm, the money you get for owning the farm is unearned income. If you’re both the farmer and the farm-owner, it may be hard to tell what’s what, but the principle is the same. If the price of potatoes goes up, presumably the owners of potato farms will have greater than normal profits. Maybe they’ll pass on their profits in the form of higher wages to their workers. Or maybe not. Either way, if the money’s for making potatoes, it’s earned. If it’s for owning a farm, it’s not.
The moral justification for obtaining the value of a thing you make yourself seems pretty self-explanatory: you made it; it wouldn’t exist but for your efforts. If not yours, whose would it be?
The moral justification for obtaining the value of the work of others, however, is not obvious.