Valentin is right, keeping your nails a bit shorter is probably one of the most useful things to do. But, let me add a few things.
I am primarily a guitarist, and I play the piano, but wouldn't consider myself a pianist; so, much of my advice is from a guitarist's perspective.
Even though most of the classical guitarists I know (including me!) use nails, you can learn to play without nails. In fact, most of the guitar teachers I know (including me), initially teach beginner classical guitarists to play without nails. However, for a wide range of tone, and plenty of volume and articulation, using nails generally seems best.
If you do use nails, there are still a variety of approaches to right-hand technique. Some players play with nails alone, which requires slightly longer nails; some players initially touch the string with the flesh of the finger or thumb and the nail at the same time. Personally I favour the latter approach for several reasons: feeling the string on the flesh of finger or thumb helps me feel more in control of each stroke; the tone seems less brittle; and of course, I can keep my nails shorter!
It is also worth noting that a warmer, richer tone can be achieved by leading with the left-hand side of your finger nails (this will always happen with the thumb anyway). In order to do this, the nails are shaped to be curved, but shorter on the left-hand side (or right if you are left-handed!) This allows the nail to create a 'ramp' which releases the string more gradually. This kind of nail shaping will particularly help your piano playing when using your thumb, as it is the left-hand side of the thumb which touches the keys.
From a piano point of view, just a very slight 'flattening' of the fingers will allow you to avoid touching the keys with your nails. But, as any pianist/piano teacher will point out, your fingers should still be fairly curved, rather than straight, with your wrist neither to low or too arched.
In the end, I compromise a little: I keep right-hand nails for guitar, but keep them quite short (although this is no big deal as I prefer this anyway, particularly as it helps to keep them in good condition); and I very slightly flatten my hand/fingers for piano playing, but certainly not to the extent that my fingers are straight. (If you don't have piano lessons, it might be a good idea to discuss this with a piano teacher.)
There is a fantastic book about why and how to shape nails for classical guitar playing called Tone Production on the Classical Guitar by J Taylor. It is now out of print, but can be found at scribd.com.
The techniques associated with flamenco playing can place more demands on your nails, and a brighter tone, which longer nails can give, may seem more attractive, but again having shorter nails should help you to keep them in good condition.
I'm sure I don't need to add that you should always keep the nails very short on your fretting hand…!