How to make custom made script (a custom UI) for a rig work properly when using Library Override?

With the introduction of Library Override, I’ve been doing some experimentation on how to properly set up my rigged models. However, I can’t seem to make my script work properly when linked and after using Library Override. How do I make it work properly? I experimented with Rigify and it works well as it is included when Linked and works properly after running the script.

How does Rigify do this? Are there various ways to make custom scripts in rigs work properly?

Edit:
I actually made some of the parts work with proper Collection organization. Most of the custom UI is actually driven by Custom Properties and enabling Is Library Overrideable I’m able to get them working again. The problem now is how I’m going to automatically make them overridable from the start as I have to enable them one by one. Rigify makes them overrideable from the start which again makes me curious how Rigify works.

Another thing is that while I’m able to successfully make some of my custom UI buttons work, I can’t make my buttons that affect my material nodes (also driven by Custom Properties) work. Is it because of Library Override’s current limitations?

I know this answer is very late but I just had the same problem and found a solution. The solution that Rigify uses is adding the Text data block directly to the rig as a property. For small custom scripts, I put them in the register function as follows (Here i am looking for an object named ‘chair_rig’ and adding a script called ‘chair_rig.py’). You should probably do the same with your material, adding the script to the datablock directly.

def register():
    # Add this script to data block for library overrides
    obj =  bpy.data.objects['chair_rig']
    obj['rig_ui'] = bpy.data.texts["chair_rig.py"]
    # your usual registration
    bpy.utils.register_class(VIEW_3D_PT_chair_ui)
    
def unregister():
    del bpy.data.objects['chair_rig']['rig_ui']
    bpy.utils.unregister_class(CHAIR_OT_reset_rigid_bodies)
    bpy.utils.unregister_class(VIEW_3D_PT_chair_ui)

You can also just type it into the console.

bpy.data.objects['chair_rig']['rig_ui'] = bpy.data.texts["chair_rig.py"]