Android M has been filling various threads across the Internet since its announcement and developer preview release at the end of the first day of Google I/O last week. The latest version of Android has been sitting on many Nexus devices since then, and one thing has evaded most of them: root access. For those who don't know what root access is, it's similar to having administrator permissions on a desktop or laptop computer. It allows you to have direct access to the /data partition, as well as the ability to change or overwrite things in the /system partition, in your phone or tablet. Essentially, it is the last bit of restricted access that Android has, purely out of security of your device and data. If you happen to own a Nexus 5, have Android M's developer preview installed, but have been dreaming of root for the device, we've got you covered. Note, we aren't to blame if something goes wrong during the process, you're following these steps on your own wishes, knowing it can put your device at risk if something goes wrong.
With that, let's get going. First, you'll need to know what download and have installed. For the Nexus 5, you'll need to download Team Win's custom recovery, TWRP, to allow some not-so-official files install to your device. You'll also need to have the basic Android developer's toolkit, to be able to use ADB and Fastboot to interact with the device and install the custom recovery. You'll also need the beta version of Chainfire's SuperSU app, version 2.49 currently, and a custom kernel, either Despair or Hell's Core, to allow SuperSU to run at boot, which Google's stock kernel doesn't do. So, the link to the XDA thread by user CronosTech will contain all the files you need, so go download them and come back.
First, on your Nexus 5, go into Settings, under About Device, and tap the Build number, MPZ44Q in this case, until you get a message that says that you're now a developer. Hit the back button, and go to the newly-available Developer Options, and scroll down to Debugging, and switch USB Debugging to be on, to the right. Next, you'll want to move the downloaded custom kernel, in zip format, and the Super SU 2.49 zip to the phone's internal storage, for easy access in just a bit.
Now, go into the device's bootloader. Power the phone off, and press power and volume-down until you get the phone to a screen with an Android with his chest open. From there, go to your computer and, once you open a command window in the location where the ADB and Fastboot drivers are (that's a shift + right-click, then 'open command window here'), move your TWRP custom recovery (just Google 'Nexus 5 twrp' and download the latest version, which is 18.104.22.168 at time of writing, and download the .img file) file into the same folder as mentioned before.
In the command window, type:
to test if your Nexus 5 is being detected, and if a number comes up, then you should be fine. From there, type:
"fastboot flash recovery twrp-22.214.171.124-hammerhead.img"
and let the process complete. TWRP is now the recovery installed on your device. From there, you should only need your device.
On the phone, use the volume up and down keys to navigate to recovery, and click the power button to select, and you'll boot into TWRP version 126.96.36.199, which you just installed. From there, go to Install, and find the two files you downloaded, the custom kernel and the SuperSU 2.49 zip files. First, choose the custom kernel. SuperSU is useless without a custom kernel to allow it to work, and will cause an infinite bootloop if you don't flash the kernel first. Once the kernel installs, you'll select and install the SuperSU 2.49 zip file, and let that complete. From there, you'll select Reboot, then System, to boot into the device normally. And voila! Your Nexus 5 is now rooted on Google's sweet and mysterious Android M. The device now has root, meaning you can now hack to your heart's content, or just restore your app data using the ever-popular Titanium Backup.