summaryrefslogtreecommitdiff
path: root/modules/shared/firefox-addons.nix
blob: 17b7293ce489c39e8f3159acc49f1cce35434683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.programs.firefox;

  policyFormat = pkgs.formats.json { };

  installURLFromId = id: "https://addons.mozilla.org/firefox/downloads/latest/${id}/latest.xpi";

  extensionSettingsSubmodule = lib.types.submodule (
    { config, ... }:
    {
      options = {
        id = lib.mkOption {
          type = lib.types.str;
          description = "Addon ID from addons.mozilla.org";
          example = "[email protected]";
        };

        settings = lib.mkOption {
          type = lib.types.submodule {
            freeformType = policyFormat.type;

            options = {
              installation_mode = lib.mkOption {
                type = lib.types.enum [
                  "allowed"
                  "blocked"
                  "force_installed"
                  "normal_installed"
                ];
                default = "normal_installed";
                description = ''
                  Installation mode for the addon.
                  See <link xlink:href="https://mozilla.github.io/policy-templates/#extensionsettings"/>.
                '';
              };

              install_url = lib.mkOption {
                type = lib.types.str;
                default = installURLFromId config.id;
                defaultText = lib.literalExpression ''
                  "https://addons.mozilla.org/firefox/downloads/latest/''${id}/latest.xpi"
                '';
                example = "https://addons.mozilla.org/firefox/downloads/file/4412673/ublock_origin-1.62.0.xpi";
              };
            };
          };
          default = { };
          description = ''
            Configuration for the `ExtensionSettings` policy
            described at
            <link xlink:href="https://mozilla.github.io/policy-templates/#extensionsettings"/>.
          '';
        };
      };
    }
  );

  # Ensure all addons given are submodules describing the `ExtensionSettings` object
  normalizeAddon =
    extensionIdOrSubmodule:
    if lib.isString extensionIdOrSubmodule then
      {
        id = extensionIdOrSubmodule;
        settings = {
          installation_mode = "normal_installed";
          install_url = installURLFromId extensionIdOrSubmodule;
        };
      }
    else
      extensionIdOrSubmodule;
in

{
  options.programs.firefox = {
    addons = lib.mkOption {
      type = lib.types.listOf (lib.types.either lib.types.str extensionSettingsSubmodule);
      default = { };
      description = ''
        List of addon IDs from addons.mozilla.org or configuration
        for the `ExtensionSettings` policy described at
        <link xlink:href="https://mozilla.github.io/policy-templates/#extensionsettings"/>.
      '';
      example = lib.literalExpression ''
        [
          # uBlock Origin
          {
            id = "[email protected]";
            settings = {
              installation_mode = "force_installed";
            };
          }

          # Bitwarden
          "{446900e4-71c2-419f-a6a7-df9c091e268b}"
        ]
      '';
    };
  };

  config = {
    programs.firefox.policies = {
      ExtensionSettings = lib.foldl' (lib.flip (
        addon:

        let
          normalizedAddon = normalizeAddon addon;
        in

        lib.recursiveUpdate { ${normalizedAddon.id} = normalizedAddon.settings; }
      )) { } cfg.addons;
    };
  };
}