Mealie or Tandoor maybe? You can add your recipes, select which ones you want to make, and it'll create a grocery list based on the recipe ingredients. I'm not sure if either of them handles ad-hoc grocery lists though.
Another option is a note app with flexible content types, like Anytype. You can self-host the Anytype sync server https://github.com/grishy/any-sync-bundle