How do I attach a mod to a weapon?

You have to select the weapon, and then hit the weapon mod key/button:

To attach a weapon mod you must have both the weapon it is intended for and the mod in your inventory. Highlight the weapon you want to mod within the items section of your Pip-Boy, then select the modding menu via the button as indicated in the top right of the info panel.

Then you'll switch to a list of the available compatible weapon mods for that weapon, and you can pick one to attach.

(As a side note, if you want a better Varmint Rifle, you may want to go looking for Ratslayer - it is a stronger variant that has all the mods already equipped, but with better stats. Getting to it can be tricky, though.)