Solution (source code)

= Solution

Maximize the <Gibbs entropy> subject to normalization and the two mean constraints. Introduce multipliers $\eta,A,B$ and differentiate
$$
-k\sum_i\rho_i\log\rho_i-\eta(\sum_i\rho_i-1)
-A(\sum_i\rho_iE_i-E)-B(\sum_i\rho_iN_i-N).
$$
Stationarity gives $\rho_i\propto\exp[-(AE_i+BN_i)/k]$. Strict concavity of entropy gives the constrained maximum, on its support. Differentiating the maximum value with respect to the constrained means, or differentiating the entropy along the maximizing distribution and using $\sum_i d\rho_i=0$, gives $dS=A\,dE+B\,dN$. The thermodynamic identification is $A=1/T$, $B=-\mu/T$. Therefore
$$
\boxed{\frac{\partial S}{\partial E}=\frac1T,\qquad
\frac{\partial S}{\partial N}=-\frac\mu T,\qquad
\overline\rho_i=\mathcal Z^{-1}e^{-(E_i-\mu N_i)/(kT)}.}
$$