Gobolinux does something similar, and has been around for years: https://www.gobolinux.org/
I'm not sure how Gobolinux organizes files exactly, but with NixOS, the hash in /nix/store/<hash>-packagename is produced by combining every input that might influence the package. By input I mean for example:
- The source: git revision / source url / etc.
- All dependencies, which means if a dependency of a dependency of this package changes, it gets a new hash
- Build instructions, including build flags
This wouldn't be possible with a more conventional file structure.