How to create MacOS file associations with nix-darwin?

Hey,

I’m trying to rewrite my dotfiles to solely use nix-darwin on MacOS.

I would want to associate certain files to open in VS Code. I have so far used GitHub - moretension/duti: A command-line tool to select default applications for document types and URL schemes on Mac OS X to achieve this. Here’s example for .svelte files:

$ duti -s com.microsoft.VSCode .svelte all

It doesn’t really matter if user runs this multiple times and it can run everytime when darwin-rebuild is used.

Is there a de-facto way to do this in nix-darwin?

One can also read these file associations without duti like this:

defaults read com.apple.LaunchServices/com.apple.launchservices.secure

There seems to be 3 different attempts that people have used for this based on Github search for LSHandlerContentType for *.nix files: Code search results · GitHub.

The problem on just overwriting this seems to be that then it wipes away all file associations which the different programs are creating when installed and I would not want to manage such a huge list of these :thinking:.

So something like this definitely can be done to associate the files but it also removes all other file associations everytime darwin-rebuild is ran:

I ended up using duti with nix-darwin and home-manager and an home-manager activation script.

See more in this commit: Automate file associations with duti and nix · onnimonni/dotfiles@49b2878 · GitHub