After creating the custom post type in my plugin, I tried visiting domain.com/, but got a 404 error page instead.
I came across a blog post by Kovshenin which explains how to get permalinks working with custom post types. The key thing is to specify the slug with the ‘rewrite’ parameter when registering the CPT. After that, remember to reset the permalinks by visiting the permalinks settings page in Settings -> Permalinks. Quite a lot of 404 grief can be solved by doing so.
It makes sense to automate this in the plugin. To do so, I added
flush_rewrite_rules() to the activation and deactivation hooks. This would ensure that the permalinks are reset only when the CPT becomes active or inactive.
Do not let the function run on every page load. It is very resource intensive and will slow your site down.