I've been using a carabiner through the strap to clip my helmet to my backpack. But it seems clumsy, and the helmet dangles around a lot. I've seen other people with helmets closely mounted on a bag. How do they do it? Is there some kind of clip for this purpose?
You can make your own retention system for any bag which will hold the helmet secure and close to the bag. I've done this on a number of my bags as I carry both bicycle and climbing helmets at times and there is never enough space for them inside a bag as they're such awkward shapes.
Buy some elasticated cord (often called shock cord) and some cord locks of an appropriate size for the diameter of the shock cord you have. Threading this through a strap or some webbing on the bag and leaving the cord locks outermost will give you a size adjustable retaining loop that will hug the helmet to the bag.
Alternatively, find something (I've used shaped pieces of wood or plastic) that will fit lengthways through a vent on your helmet, but is too long to fit through the vent when rotated sideways. This becomes your toggle, tie this to the shock cord and you can use a single length (easier and neater to tuck away when not in use) with the toggle at the end. To fasten the helmet, push the toggle through a vent and twist it sideways so it can't fall back through the vent.
EDIT: a video demonstration and a couple of photos for you. Other manufacturers do this, Osprey and their 'lid lock' feature are just the one that came to mind for me to search out images.
I just use the strap with its own clip. It does swing around though.
Some bags have an elastic net that works very well (I have a rucksack that does this but I hardly ever ride with it).
Can you get the carabiner through both sides of the strap? That might help. It might be too tight though. Replacing the carabiner with a strap you can pull tighter might be a good compromise.
Stick on or sew on Velcro fastener on the strap close to the helmet and on a suitable part of the back pack or an industrial size press stud fastener.
Hopefully you do not plan to carry the helmet on your back pack when you are riding
I would suggest following - try to clip helmet chin strap around both backpack shoulder straps or, if backpack is not too big, try to clip it around whole backpack. (If your backpack is large enough you can put helmet inside it :). There is also possibility to use rubber strap with hooks to fasten your helmet.
Tbh, I have backpack which has dedicated helmet holder but I rarely use it. I find it much faster to hang it around stem and handlebars. That way it is much faster to put your helmet on compared to taking off backpack, untying helmet and putting it back on.
This answer concerns mountain biking.
My backpack has a dedicated helmet holding piece, but I don't use it, as inserting or removing the helmet requires adjusting 6 adjustment straps.
Instead, for longer climbs, when wearing a helmet would only be filling my eyes with sweat, I clip the chin strap under both of the top cover straps. This keeps the helmet mostly in place.
On the other hand, on a short climb, or when in the city I want to enter a shop, I clip the shoulder strap to the plastic ring on the shoulder strap. That way I can put the helmet on and off without stopping riding. On a side note, I use those rings also for protective glasses and cable lock.
For me, both of those methods result in gradual stretch at the buckle of the chin strap, so the helmet needs to be re-adjusted now and then. Experience on a couple of 2 mid-range helmets, mostly dirt road and trail riding.