This was my first thought, use a declarative system like Nix.