The derivative work has to be literally
derived from the original "such as a translation, musical arrangement, dramatization, fictionalization, motion picture version, sound recording, art reproduction, abridgment, condensation, or any other form in which a work may be recast, transformed, or adapted." (per 17 USC 101).
https://www.law.cornell.edu/uscode/text/17/101The merger and scenes a faire doctrine permit lots of overlap in terms of themes, tropes and set dressing. You could certainly write a coming-of-age story set in a magical boarding school; indeed, Harry Potter is neither the first nor the last such novel. One of the classic cases is Walker v. Time Life. The two works, which were found to be non-infringing, both start with a double murder of two cops (one Black, one white) in the South Bronx, both feature demoralized Irish-American cops, and both have similar tropes (rats, cop-talk, etc). A reviewer might reasonably describe it as derivative (and a few did, I think), but not legally so.
What you probably couldn't do is publish the (non-parodic) story of Perry Hotter and his substantially-similar adventures at Pigworts, though that's also absolutely spoiling for a trademark fight.
Music gets weird because it a) seems like there are a lot of possible note sequences but b) there aren't really.