Trimming XDG_DATA_DIRS to not contain duplicates

Hello everyone :wave:

I have the following question: how can I trim the generated XDG_DATA_DIRS environment my DE uses to the non-duplicated paths only?

I noticed KDE apps are insanely slow to start compared to other distributions. Read about XDG_DATA_DIRS duplicates causing the startup here:

And in fact, there are tons of duplicates, mostly KDE-related.

 nico@dragons in ~ 
❯ echo $XDG_DATA_DIRS | tr ':' '\n' | wc -l
203

 nico@dragons in ~ took 6ms
❯ echo $XDG_DATA_DIRS | tr ':' '\n' | sort | uniq | wc -l
87

The full list of entries can be found here.
I have a script to export this variable without the unnecessary duplicates, which I would like to run during login. I tried programs.bash.loginShellInit, as well as home-managers option for appending content to .bashrc, but both of these fail to reduce the amount of paths set in XDG_DATA_DIRS.

So I’m assuming something else exports the variables, overwriting my changes. Any idea how to proceed here?

My flake can be found here, using latest nixpkgs-unstable with KDE 6.