Help understanding this LED lighting transistor circuit
This circuit is designed to provide a constant current to the LED independent of the supply voltage.

The MOSFET is turned on by the voltage at the collector of Q1.  As soon as the current through R1 (which is the same as through the LED) results in a drop of about 0.6V, Q1 will start to turn on and divert current through R2.

This will then reduce the voltage at M1 gate to control the current through M1 and the LED.

The negative feedback will stabilize the current through D1, M1 and R1 at about 5mA as that will result in 0.6V at Q1 base.

The current will vary slightly as the supply voltage varies but much less than just using a resistor.

Also vary with temperature as the Vbe of the transistor will have ~2.2mV/deg temperature coefficient.

The same circuit can be used where M1 is a BJT (such as 2n2222) rather than a MOSFET. The value of R2 will be more critical as the transistor will require some base current from the R2.