home-manager icon indicating copy to clipboard operation
home-manager copied to clipboard

Expose handlers.json from firefox profile

Open llakala opened this issue 1 year ago • 3 comments

Firefox provides a way to control which file types are opened in firefox, and which file types open separate apps. This is controlled via the file path .mozilla/firefox/profile_name/handlers.json.

Home-manager provides many ways to control the firefox configuration, yet it doesn't extend to these app types. It should be fairly simple to control declaratively, since it's controlled via a simple key-value pair structure.

Here's how my personal file is structured:

{ "defaultHandlersVersion": { }, "mimeTypes": { "application/pdf": { "action":3, "extensions":["pdf"] }, "image/webp": { "action":3, "extensions":["webp"] }, "image/avif": { "action":3, "extensions":["avif"] } }, "schemes": {

    "mailto":
    {
        "stubEntry":true,
        "handlers":
        [
            null,
            {
                "name":"Gmail",
                "uriTemplate":"https://mail.google.com/mail/?extsrc=mailto&url=%s"
            }
        ]
    },
    
    "vscode": 
    {
        "action":4
    }
},
"isDownloadsImprovementsAlreadyMigrated":false

}

Most of the functions seem fairly obvious. What I call "imports" are controlled in mimeTypes, and controls what files open firefox. There are several options for what to do with this filetype, numbered from 0. I'll list what the numbers correspond to below.

  • 0 corresponds to "Save File".
  • 1 corresponds to “Always Ask”.
  • 2 corresponds to “Use ___”. This is somewhat vague, since it applies to both using a specific binary, or using something like Gmail. It seems to just mean that it redirects into a custom handler: whether that handler is a URL or a filepath. You can control whether it links to a filepath or URL by either passing it a path, or a "uriTemplate".
  • 3 corresponds to "Open in Firefox".
  • 4 seems to correspond to using a default handler. I'm not totally sure on this, since I've only seen one example with VSCode. More research is required.

Then, there's what I call "exports", controlled in schemes. This manages urls of a certain scheme redirecting into an app, like Discord or VSCode. These seem to take the same actions as above. However, I haven't seem 0,or 3 used here. I'd assume that these would never occur, since it's a url, not a file. With this in mind, the only valid codes for exports would be 1, 2, or 4.

That's all the research I've done so far. Hope this helps!

llakala avatar Jul 07 '24 21:07 llakala

Sorry, the file contents weren't properly formatted. This should help:

{
	"defaultHandlersVersion":
	{
	},
	"mimeTypes":
	{
    	"application/pdf":
    	{
        	"action":3,
        	"extensions":["pdf"]
    	},
    	"image/webp":
    	{
        	"action":3,
        	"extensions":["webp"]
    	},
    	"image/avif":
    	{
         	"action":3,
        	"extensions":["avif"]
    	}
	},
	"schemes":
	{
    
    	    "mailto":
    	    {
        	"stubEntry":true,
        	"handlers":
        	[
            	    null,
            	    {
                	"name":"Gmail",
                	"uriTemplate":"https://mail.google.com/mail/?extsrc=mailto&url=%s"
            	    }
        	]
    	    },
   	 
    	    "vscode":
    	    {
        	"action":4
    	    }
	},
	"isDownloadsImprovementsAlreadyMigrated":false
}

llakala avatar Jul 07 '24 21:07 llakala

Found some documentation: https://mozilla.github.io/policy-templates/#handlers

llakala avatar Jul 07 '24 22:07 llakala

home-manager supports setting policies from the link you posted already:

programs.firefox = {
  enable = true;
  policies = {
    Handlers = {
      "schemes" = {
        "mpv" = {
          "action" = "useHelperApp";
          "handlers" = [
            {
              name = "mpv";
              path = "/run/current-system/sw/bin/mpv-handler.sh";
            }
          ];
        };
      };
    };
  };
};

KucharczykL avatar Aug 16 '24 20:08 KucharczykL

Thank you for your contribution! I marked this issue as stale due to inactivity. Please be considerate of people watching this issue and receiving notifications before commenting 'I have this issue too'. We welcome additional information that will help resolve this issue. Please read the relevant sections below before commenting.

If you are the original author of the issue

  • If this is resolved, please consider closing it so that the maintainers know not to focus on this.
  • If this might still be an issue, but you are not interested in promoting its resolution, please consider closing it while encouraging others to take over and reopen an issue if they care enough.
  • If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.
If you are not the original author of the issue

  • If you are also experiencing this issue, please add details of your situation to help with the debugging process.
  • If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.
Memorandum on closing issues

Don't be afraid to manually close an issue, even if it holds valuable information. Closed issues stay in the system for people to search, read, cross-reference, or even reopen – nothing is lost! Closing obsolete issues is an important way to help maintainers focus their time and effort.

stale[bot] avatar Jan 03 '25 07:01 stale[bot]

I did a preliminary implementation, which for now only works with the default profile. It is available on my nur-packages repo.

https://github.com/kugland/nur-packages/blob/master/modules/firefox-handlers.nix

kugland avatar Nov 21 '25 05:11 kugland