The theory of an X-ray free electron laser inside a crystal is developed. In the Bragg diffraction geometry, a crystal provides a distributed feedback. In the low-gain regime expressions for the generation threshold are found which take absorption into account. Cold- and warm-beam regimes both for magnetic and for laser wigglers are considered. A detailed explanation of the physics of the generation condition is given. The case of an electron beam propagating in a narrow slit, without multiple scattering is considered. The advantage of a two-dimensional diffraction geometry is shown to be that it provides lower generation threshold values than those in one-dimensional geometry. The warm-beam case is found to be more preferable due to less rigid requirements for the beam quality. Realization of the given scheme requires an approximately 30-fold increase of the brightness of electron beams in storage rings.