fork/home-manager

git clone https://git.julia.blue/fork/home-manager

networkExceptionchromium: add support for dictionariesf8c5fd75

master
7.2 KiB218 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  supportedBrowsers = [
8    "chromium"
9    "google-chrome"
10    "google-chrome-beta"
11    "google-chrome-dev"
12    "brave"
13    "vivaldi"
14  ];
15
16  browserModule = defaultPkg: name: visible:
17    let
18      browser = (builtins.parseDrvName defaultPkg.name).name;
19      isProprietaryChrome = hasPrefix "Google Chrome" name;
20    in {
21      enable = mkOption {
22        inherit visible;
23        type = types.bool;
24        default = false;
25        example = true;
26        description = "Whether to enable ${name}.";
27      };
28
29      package = mkOption {
30        inherit visible;
31        type = types.package;
32        default = defaultPkg;
33        defaultText = literalExpression "pkgs.${browser}";
34        description = "The ${name} package to use.";
35      };
36
37      commandLineArgs = mkOption {
38        inherit visible;
39        type = types.listOf types.str;
40        default = [ ];
41        example = [ "--enable-logging=stderr" "--ignore-gpu-blocklist" ];
42        description = ''
43          List of command-line arguments to be passed to ${name}.
44
45          For a list of common switches, see
46          [Chrome switches](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/chrome/common/chrome_switches.cc).
47
48          To search switches for other components, see
49          [Chromium codesearch](https://source.chromium.org/search?q=file:switches.cc&ss=chromium%2Fchromium%2Fsrc).
50        '';
51      };
52    } // optionalAttrs (!isProprietaryChrome) {
53      # Extensions do not work with Google Chrome
54      # see https://github.com/nix-community/home-manager/issues/1383
55      extensions = mkOption {
56        inherit visible;
57        type = with types;
58          let
59            extensionType = submodule {
60              options = {
61                id = mkOption {
62                  type = strMatching "[a-zA-Z]{32}";
63                  description = ''
64                    The extension's ID from the Chrome Web Store url or the unpacked crx.
65                  '';
66                  default = "";
67                };
68
69                updateUrl = mkOption {
70                  type = str;
71                  description = ''
72                    URL of the extension's update manifest XML file. Linux only.
73                  '';
74                  default = "https://clients2.google.com/service/update2/crx";
75                  visible = pkgs.stdenv.isLinux;
76                  readOnly = pkgs.stdenv.isDarwin;
77                };
78
79                crxPath = mkOption {
80                  type = nullOr path;
81                  description = ''
82                    Path to the extension's crx file. Linux only.
83                  '';
84                  default = null;
85                  visible = pkgs.stdenv.isLinux;
86                };
87
88                version = mkOption {
89                  type = nullOr str;
90                  description = ''
91                    The extension's version, required for local installation. Linux only.
92                  '';
93                  default = null;
94                  visible = pkgs.stdenv.isLinux;
95                };
96              };
97            };
98          in listOf (coercedTo str (v: { id = v; }) extensionType);
99        default = [ ];
100        example = literalExpression ''
101          [
102            { id = "cjpalhdlnbpafiamejdnhcphjbkeiagm"; } # ublock origin
103            {
104              id = "dcpihecpambacapedldabdbpakmachpb";
105              updateUrl = "https://raw.githubusercontent.com/iamadamdev/bypass-paywalls-chrome/master/updates.xml";
106            }
107            {
108              id = "aaaaaaaaaabbbbbbbbbbcccccccccc";
109              crxPath = "/home/share/extension.crx";
110              version = "1.0";
111            }
112          ]
113        '';
114        description = ''
115          List of ${name} extensions to install.
116          To find the extension ID, check its URL on the
117          [Chrome Web Store](https://chrome.google.com/webstore/category/extensions).
118
119          To install extensions outside of the Chrome Web Store set
120          `updateUrl` or `crxPath` and
121          `version` as explained in the
122          [Chrome
123          documentation](https://developer.chrome.com/docs/extensions/mv2/external_extensions).
124        '';
125      };
126
127      dictionaries = mkOption {
128        inherit visible;
129        type = types.listOf types.package;
130        default = [ ];
131        example = literalExpression ''
132          [
133            pkgs.hunspellDictsChromium.en_US
134          ]
135        '';
136        description = ''
137          List of ${name} dictionaries to install.
138        '';
139      };
140    };
141
142  browserConfig = cfg:
143    let
144
145      drvName = (builtins.parseDrvName cfg.package.name).name;
146      browser = if drvName == "ungoogled-chromium" then "chromium" else drvName;
147      isProprietaryChrome = hasPrefix "google-chrome" drvName;
148
149      darwinDirs = {
150        chromium = "Chromium";
151        google-chrome = "Google/Chrome";
152        google-chrome-beta = "Google/Chrome Beta";
153        google-chrome-dev = "Google/Chrome Dev";
154        brave = "BraveSoftware/Brave-Browser";
155      };
156
157      linuxDirs = { brave = "BraveSoftware/Brave-Browser"; };
158
159      configDir = if pkgs.stdenv.isDarwin then
160        "Library/Application Support/" + (darwinDirs."${browser}" or browser)
161      else
162        "${config.xdg.configHome}/" + (linuxDirs."${browser}" or browser);
163
164      extensionJson = ext:
165        assert ext.crxPath != null -> ext.version != null;
166        with builtins; {
167          name = "${configDir}/External Extensions/${ext.id}.json";
168          value.text = toJSON (if ext.crxPath != null then {
169            external_crx = ext.crxPath;
170            external_version = ext.version;
171          } else {
172            external_update_url = ext.updateUrl;
173          });
174        };
175
176      dictionary = pkg: {
177        name = "${configDir}/Dictionaries/${pkg.passthru.dictFileName}";
178        value.source = pkg;
179      };
180
181      package = if cfg.commandLineArgs != [ ] then
182        cfg.package.override {
183          commandLineArgs = concatStringsSep " " cfg.commandLineArgs;
184        }
185      else
186        cfg.package;
187
188    in mkIf cfg.enable {
189      home.packages = [ package ];
190      home.file = optionalAttrs (!isProprietaryChrome) (listToAttrs
191        ((map extensionJson cfg.extensions)
192          ++ (map dictionary cfg.dictionaries)));
193    };
194
195in {
196  # Extensions do not work with the proprietary Google Chrome version
197  # see https://github.com/nix-community/home-manager/issues/1383
198  imports = map (flip mkRemovedOptionModule
199    "The `extensions` option does not work on Google Chrome anymore.") [
200      [ "programs" "google-chrome" "extensions" ]
201      [ "programs" "google-chrome-beta" "extensions" ]
202      [ "programs" "google-chrome-dev" "extensions" ]
203    ];
204
205  options.programs = {
206    chromium = browserModule pkgs.chromium "Chromium" true;
207    google-chrome = browserModule pkgs.google-chrome "Google Chrome" false;
208    google-chrome-beta =
209      browserModule pkgs.google-chrome-beta "Google Chrome Beta" false;
210    google-chrome-dev =
211      browserModule pkgs.google-chrome-dev "Google Chrome Dev" false;
212    brave = browserModule pkgs.brave "Brave Browser" false;
213    vivaldi = browserModule pkgs.vivaldi "Vivaldi Browser" false;
214  };
215
216  config = mkMerge
217    (map (browser: browserConfig config.programs.${browser}) supportedBrowsers);
218}