I always thought the cost of capital (borrowing money) in real terms already included inflation. And then the nominal interest rate is equal to inflation plus profit margin for lenders.
The cost of borrowing money in NOMINAL terms you mean (the actual rate you have to pay) includes, as you say, inflation and indeed, the expected profit margin for the lenders, which is exactly the real interest rate.
The profit margin of the lenders is indeed what is market-determined, between demand for borrowing value, and offer for borrowing value. It is "the price for putting value at disposal during a year". If we want to compare things, we have to keep this condition constant.
The REAL margin for creditors (the real price for debtors) is the real interest. It is, as I said, determined by the offer by creditors and the demand by debtors.
If a potential creditor wants to put a certain value on the market, which is worth $1000 right now, and there is 10% inflation, he will only consider that he makes a benefit for everything ABOVE 10%. Indeed, if he lends his $1000 right now, and he gets back $1100 a year from now, he cannot buy anything more with it than right now. So there's no incentive for him to not buy the stuff right away and lend his money. If the nominal interest he asks is 15%, he will consider that he got 5% VALUE back. The real interest rate. The nominal rate on the market will then be 15%, and the real interest rate 5%.
If the inflation is only 2%, he will be satisfied, in the same circumstances, with a nominal rate of 7%. In the same market conditions, then, the nominal rate will be 7%, and the real interest rate still 5%.
Because the lender looks at his real rate to offer or not to offer. So the offer on the money market will be determined by the REAL interest rate. It is the same, for a lender, to get 15% if inflation is 10%, or to get 7% if inflation is 2%. Offer will be the same in both conditions.
On the side of the borrower, you get a similar reasoning. His demand will also depend on the real rate, not on the nominal rate.
This is why the market conditions actually determine the real rate.