My personal view is that a rack is a framework and I would use that spelling in expressions having that connection such as ‘racked with pain’ because this use derives from the torture rack which is a framework. The expression 'nerve-racking' also conveys the idea of nerves stretched to breaking point. Wrack, however, I would relate to wrecking, breaking up and tearing apart. So I would use ‘wrack and ruin’ and ‘wreak havoc’.

In WW's Favorite definition of rack as a noun: a mass of usually broken-up wind driven clouds I would spell that as 'wrack'. If it described a sky with a series of bar shaped clouds with blue sky in between, such as you sometimes see, I would spell that 'rack' due to its resemblence to a rack or grid.

That is just my approach, I'm sure other logic could be applied. I don’t think it matters very much that the two spellings are used with no particular consistency. The context mostly sorts out the meaning.