X-Ray Spectroscopy With End-to-End Optimized Nanophotonic Scintillators