Firefox Extensions (Add-Ons)

How do I decoratively install Firefox extensions, or addons?

I’m sorry to make yet another post on this topic but I’ve been trying all weekend to make this work but coming up empty. Based on posts such as this and example configurations such as this one, I have the following flake.nix, home.nix and firefox.nix files in my /etc/nixos directory. But even after removing ~/.mozilla/firefox and ~/.cache/mozilla/ before running sudo nixos-rebuild switch --flake '/etc/nixos#nixos', I can’t get uBlock Origin to install, even though the other settings take effect.

Thanks in advance. :weary:

# /etc/nixos/flake.nix
{
  description = "NixOS configuration";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    home-manager.url = "github:nix-community/home-manager";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = inputs@{ nixpkgs, home-manager, ... }: {
    nixosConfigurations = {
      nixos = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ./configuration.nix
          home-manager.nixosModules.home-manager
          {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.users.virtsamir = import ./home.nix;
	    home-manager.extraSpecialArgs = { inherit inputs; };
	    home-manager.backupFileExtension = "backup";
          }
        ];
      };
    };
  };
}

# /etc/nixos/home.nix
{ config, pkgs, inputs, ... }:

{
  imports = [ ./firefox.nix  ];
  home.username = "samir";
  home.homeDirectory = "/home/samir";
  home.packages = with pkgs; [ ];
  home.stateVersion = "24.11";
  programs.home-manager.enable = true;
}

# /etc/nixos/firefox.nix
{ inputs, config, pkgs, ... }:
{
  programs = {
    firefox = {
      enable = true;
      package = pkgs.firefox;
      profiles.default = {
        id = 0;
        name = "default";
        isDefault = true;
        settings = {
           "browser.aboutConfig.showWarning" = true;
        };
      };
      
      policies = {
        DisableTelemetry = true;
        ExtensionSettings = {
          "uBlock0@raymondhill.net" = {
            installation_mode = "force_installed";
            install_url = "https://addons.mozilla.org/firefox/downloads/latest/ublock-origin/latest.xpi";
          };
        };
      };
    };
  };
}

I didn’t know you could use policies to install extensions. I have used this option to install extensions instead: Appendix A. Home Manager Configuration Options

Here’s my configuration as an example:

Note that I am on stable branch and the option has changed a bit in unstable. You need to put the list of extensions to subattribute .packages, I think.

Just to follow up on this, here is the configuration I eventually ended up with in case it is of use to anyone in the future. I don’t know if this is the canonical way of doing it, but at least for me, for now, it seems to be working. Note, I was also able to get my custom userChrome.css to work as well!

flake.nix
{
  description = "NixOS configuration";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    home-manager.url = "github:nix-community/home-manager";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
    firefox-addons = {
      url = "gitlab:rycee/nur-expressions?dir=pkgs/firefox-addons";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs@{ nixpkgs, home-manager, ... }: {
    nixosConfigurations = {
      nixos = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ./configuration.nix
          home-manager.nixosModules.home-manager
          {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.users.samir = import ./home.nix;
	        home-manager.extraSpecialArgs = { inherit inputs; };
          }
        ];
      };
    };
  };
}

home.nix
{ config, pkgs, inputs, ... }:

{
  imports = [
    ./firefox.nix
  ];
...
}

firefox.nix
{ inputs, pkgs, config, ... }:

{
  programs.firefox = {
      enable = true;
      profiles.default = {
          id = 0;
          name = "Default";
          settings = {
            # Browser settings go here
          };
          extensions = with inputs.firefox-addons.packages.${pkgs.system}; [
            ublock-origin
            tree-style-tab
          ];
	  userChrome = builtins.readFile ./userChrome.css;
      };
  };
}