blob: d02aa70525720e734adc3dd6536b8e078ec81479 [file] [log] [blame]
Skyler Greycd3d38b2024-02-25 15:18:32 +00001{
2 config,
3 lib,
4 pkgs,
5 ...
6}: let
7 possibleEngines = [
8 "Arch Wiki"
9 "Bing"
10 "DuckDuckGo"
11 "eBay"
12 "Gentoo Wiki"
13 "GitHub"
14 "Google"
15 "Home-Manager Options"
16 "Kagi"
17 "MDN"
18 "NixOS Options"
19 "NixOS Packages"
20 "Noogle"
21 "Wikipedia (en)"
22 ];
23in {
24 options.chimera.browser.firefox.search = {
25 enable = lib.mkEnableOption "Let Chimera control your firefox search engines";
26 extensions.enable = lib.mkEnableOption "Install extensions relating to your chosen search engines";
27 bookmarks.enable = lib.mkEnableOption "Add bookmarks to quickly jump to your chosen search engines. CAUTION: This option will overwrite (reset) any imperatively-added bookmarks";
28 engines = lib.mkOption {
29 type = lib.types.listOf (lib.types.enum possibleEngines);
30 description = ''
31 A list of search engines you want to enable. Any enabled engines will have the following Chimera customizations added:
32 - They will be added to Firefox (if they are not already a default firefox search engine)
33 - They will have an alias given[1], you can type "{alias} query" to search query with the engine
34 - If you have enabled config.chimera.browser.firefox.search.installExtensions, they will have their respective extension
35 installed (if one exists), e.g. enabling Kagi will install the Kagi extension for private browsing support[2]
36 - If you have enabled config.chimera.browser.firefox.search.addBookmarks, they will have a shortcut added so you can type
37 only "{alias}" and go to their homepage.
38
39 The search engines will be ordered according to the order you give them in this list. The first one will be set as your
40 default search engine.
41
42 1:
43 Arch Wiki -> arch
44 Bing -> bing
45 DuckDuckGo -> ddg
46 eBay -> ebay
47 Gentoo Wiki -> gentoo
48 GitHub -> gh
49 Google -> google
50 Home-Manager Options -> hm
51 Kagi -> kagi
52 MDN -> mdn
53 NixOS Options -> opts
54 NixOS Packages -> pkgs
55 Noogle -> lib
56 Wikipedia (en) -> wiki
57 2:
58 DuckDuckGo installs https://addons.mozilla.org/en-US/firefox/addon/duckduckgo-for-firefox
59 Kagi installs https://addons.mozilla.org/en-US/firefox/addon/kagi-search-for-firefox/
60 '';
61 default = [
62 "DuckDuckGo"
63 "MDN"
64 "NixOS Options"
65 "NixOS Packages"
66 "Home-Manager Options"
67 "Noogle"
68 "GitHub"
69 ];
70 example = [
71 "Kagi"
72 "MDN"
73 "NixOS Options"
74 "NixOS Packages"
75 "Home-Manager Options"
76 "Noogle"
77 "GitHub"
78 "Arch Wiki"
79 "Gentoo Wiki"
80 ];
81 };
82 };
83
84 config =
85 let
86 engineData = {
87 "Arch Wiki" = {
88 urls = [ { template = "https://wiki.archlinux.org/index.php?search={searchTerms}"; } ];
89 iconUpdateURL = "https://wiki.archlinux.org/favicon.ico";
90 updateInterval = 24 * 60 * 60 * 1000;
91 definedAliases = [ "arch" ];
92 homepage = "https://wiki.archlinux.org/";
93 };
94 "Bing" = {
95 homepage = "https://bing.com";
96 metaData.alias = "bing";
97 };
98 "DuckDuckGo" = {
99 homepage = "https://duckduckgo.com";
100 metaData.alias = "ddg";
101 extraExtensions = [ config.nur.repos.rycee.firefox-addons.duckduckgo-privacy-essentials ];
102 };
103 "eBay" = {
104 homepage = "https://ebay.com";
105 metaData.alias = "ebay";
106 };
107 "Gentoo Wiki" = {
108 urls = [ { template = "https://wiki.gentoo.org/index.php?search={searchTerms}"; } ];
109 iconUpdateURL = "https://www.gentoo.org/favicon.ico";
110 updateInterval = 24 * 60 * 60 * 1000;
111 definedAliases = [ "gentoo" ];
112 homepage = "https://wiki.gentoo.org";
113 };
114 "GitHub" = {
115 urls = [ { template = "https://github.com/search?q={searchTerms}"; } ];
116 iconUpdateURL = "https://github.com/favicon.ico";
117 updateInterval = 24 * 60 * 60 * 1000;
118 definedAliases = [ "gh" ];
119 homepage = "https://github.com";
120 };
121 "Google" = {
122 homepage = "https://google.com";
123 metaData.alias = "google";
124 };
125 "Home-Manager Options" = {
126 urls = [
127 { template = "https://mipmip.github.io/home-manager-option-search/?query={searchTerms}"; }
128 ];
129 iconUpdateURL = "https://mipmip.github.io/home-manager-option-search/images/favicon.png";
130 updateInterval = 24 * 60 * 60 * 1000;
131 definedAliases = [ "hm" ];
132 homepage = "https://mipmip.github.io/home-manager-option-search/";
133 };
134 "Kagi" = {
135 urls = [
136 { template = "https://kagi.com/search?q={searchTerms}"; }
137 {
138 template = "https://kagi.com/api/autosuggest?q={searchTerms}";
139 type = "application/x-suggestions+json";
140 }
141 ];
142 iconUpdateURL = "https://assets.kagi.com/v2/favicon-32x32.png";
143 updateInterval = 24 * 60 * 60 * 1000;
144 homepage = "https://kagi.com";
145 definedAliases = [ "kagi" ];
146 extraExtensions = [ config.nur.repos.rycee.firefox-addons.kagi-search ];
147 };
148 "MDN" = {
149 urls = [ { template = "https://developer.mozilla.org/en-US/search?q={searchTerms}"; } ];
150 iconUpdateURL = "https://developer.mozilla.org/favicon.ico";
151 updateInterval = 24 * 60 * 60 * 1000;
152 homepage = "https://developer.mozilla.org";
153 definedAliases = [ "mdn" ];
154 };
155 "NixOS Options" = {
156 urls = [ { template = "https://search.nixos.org/options?channel=unstable&query={searchTerms}"; } ];
157 iconUpdateURL = "https://nixos.org/logo/nix-wiki.png";
158 updateInterval = 24 * 60 * 60 * 1000;
159 homepage = "https://search.nixos.org/options?channel=unstable";
160 definedAliases = [ "opts" ];
161 };
162 "NixOS Packages" = {
163 urls = [ { template = "https://search.nixos.org/packages?channel=unstable&query={searchTerms}"; } ];
164 iconUpdateURL = "https://nixos.org/logo/nix-wiki.png";
165 updateInterval = 24 * 60 * 60 * 1000;
166 homepage = "https://search.nixos.org/packages?channel=unstable";
167 definedAliases = [ "pkgs" ];
168 };
169 "Noogle" = {
170 urls = [ { template = "https://noogle.dev/q?term={searchTerms}"; } ];
171 iconUpdateURL = "https://noogle.dev/favicon.png";
172 updateInterval = 24 * 60 * 60 * 1000;
173 homepage = "https://noogle.dev";
174 definedAliases = [ "lib" ];
175 };
176 "Wikipedia (en)" = {
177 homepage = "https://en.wikipedia.org";
178 metaData.alias = "wiki";
179 };
180 };
181 firefoxDefaultEngines = [
182 "Bing"
183 "Google"
184 "eBay"
185 "DuckDuckGo"
186 "Wikipedia (en)"
187 ];
188 calculated = lib.pipe engineData [
189 (builtins.mapAttrs (
190 name: value: {
191 engines.${name} =
192 lib.filterAttrs
193 (
194 option: _:
195 !(builtins.elem option [
196 "homepage"
197 "extraExtensions"
198 ])
199 )
200 value;
201 extensions = if builtins.hasAttr "extraExtensions" value then value.extraExtensions else [ ];
202 bookmarks =
203 if builtins.hasAttr "homepage" value then
204 [
205 {
206 inherit name;
207 keyword = if builtins.hasAttr "definedAliases" value
208 then builtins.elemAt value.definedAliases 0
209 else value.metaData.alias;
210 url = value.homepage;
211 }
212 ]
213 else
214 [ ];
215 }
216 ))
217 builtins.attrValues
218 (lib.zipAttrsWithNames ["engines" "extensions" "bookmarks"] (name: values: if name == "engines"
219 then lib.attrsets.mergeAttrsList values
220 else builtins.concatLists values))
221 ];
222 removedEngines = lib.pipe possibleEngines [
223 (builtins.filter (engine: !(builtins.elem engine config.chimera.browser.firefox.search.engines)))
224 (map (engine: {
225 name = engine;
226 value.metaData.hidden = true;
227 }))
228 builtins.listToAttrs
229 ]; # We need to hide all the engines that we aren't selecting, because engines that we unset are not removed
230 in
231 lib.mkIf config.chimera.browser.firefox.search.enable {
232 programs.firefox.profiles.chimera = {
233 search = {
234 engines = calculated.engines // removedEngines;
235 order = config.chimera.browser.firefox.search.engines;
236 default = lib.mkIf (builtins.length config.chimera.browser.firefox.search.engines > 0) (
237 builtins.elemAt config.chimera.browser.firefox.search.engines 0
238 );
239 force = true;
240 };
241 extensions = lib.mkIf config.chimera.browser.firefox.search.extensions.enable calculated.extensions;
242 bookmarks = lib.mkIf config.chimera.browser.firefox.search.bookmarks.enable calculated.bookmarks;
243 };
244 };
245}