Skyler Grey | cd3d38b | 2024-02-25 15:18:32 +0000 | [diff] [blame] | 1 | { |
| 2 | config, |
| 3 | lib, |
| 4 | pkgs, |
| 5 | ... |
| 6 | }: let |
| 7 | possibleEngines = [ |
Samuel Shuert | 95554c5 | 2024-02-29 17:04:31 -0500 | [diff] [blame^] | 8 | "Amazon" |
Skyler Grey | cd3d38b | 2024-02-25 15:18:32 +0000 | [diff] [blame] | 9 | "Arch Wiki" |
| 10 | "Bing" |
| 11 | "DuckDuckGo" |
| 12 | "eBay" |
| 13 | "Gentoo Wiki" |
| 14 | "GitHub" |
| 15 | "Google" |
| 16 | "Home-Manager Options" |
| 17 | "Kagi" |
| 18 | "MDN" |
| 19 | "NixOS Options" |
| 20 | "NixOS Packages" |
| 21 | "Noogle" |
| 22 | "Wikipedia (en)" |
| 23 | ]; |
| 24 | in { |
| 25 | options.chimera.browser.firefox.search = { |
| 26 | enable = lib.mkEnableOption "Let Chimera control your firefox search engines"; |
| 27 | extensions.enable = lib.mkEnableOption "Install extensions relating to your chosen search engines"; |
| 28 | bookmarks.enable = lib.mkEnableOption "Add bookmarks to quickly jump to your chosen search engines. CAUTION: This option will overwrite (reset) any imperatively-added bookmarks"; |
| 29 | engines = lib.mkOption { |
| 30 | type = lib.types.listOf (lib.types.enum possibleEngines); |
| 31 | description = '' |
| 32 | A list of search engines you want to enable. Any enabled engines will have the following Chimera customizations added: |
| 33 | - They will be added to Firefox (if they are not already a default firefox search engine) |
| 34 | - They will have an alias given[1], you can type "{alias} query" to search query with the engine |
| 35 | - If you have enabled config.chimera.browser.firefox.search.installExtensions, they will have their respective extension |
| 36 | installed (if one exists), e.g. enabling Kagi will install the Kagi extension for private browsing support[2] |
| 37 | - If you have enabled config.chimera.browser.firefox.search.addBookmarks, they will have a shortcut added so you can type |
| 38 | only "{alias}" and go to their homepage. |
| 39 | |
| 40 | The search engines will be ordered according to the order you give them in this list. The first one will be set as your |
| 41 | default search engine. |
| 42 | |
| 43 | 1: |
Samuel Shuert | 95554c5 | 2024-02-29 17:04:31 -0500 | [diff] [blame^] | 44 | Amazon -> amazon |
Skyler Grey | cd3d38b | 2024-02-25 15:18:32 +0000 | [diff] [blame] | 45 | Arch Wiki -> arch |
| 46 | Bing -> bing |
| 47 | DuckDuckGo -> ddg |
| 48 | eBay -> ebay |
| 49 | Gentoo Wiki -> gentoo |
| 50 | GitHub -> gh |
| 51 | Google -> google |
| 52 | Home-Manager Options -> hm |
| 53 | Kagi -> kagi |
| 54 | MDN -> mdn |
| 55 | NixOS Options -> opts |
| 56 | NixOS Packages -> pkgs |
| 57 | Noogle -> lib |
| 58 | Wikipedia (en) -> wiki |
| 59 | 2: |
| 60 | DuckDuckGo installs https://addons.mozilla.org/en-US/firefox/addon/duckduckgo-for-firefox |
| 61 | Kagi installs https://addons.mozilla.org/en-US/firefox/addon/kagi-search-for-firefox/ |
| 62 | ''; |
| 63 | default = [ |
| 64 | "DuckDuckGo" |
| 65 | "MDN" |
| 66 | "NixOS Options" |
| 67 | "NixOS Packages" |
| 68 | "Home-Manager Options" |
| 69 | "Noogle" |
| 70 | "GitHub" |
| 71 | ]; |
| 72 | example = [ |
| 73 | "Kagi" |
| 74 | "MDN" |
| 75 | "NixOS Options" |
| 76 | "NixOS Packages" |
| 77 | "Home-Manager Options" |
| 78 | "Noogle" |
| 79 | "GitHub" |
| 80 | "Arch Wiki" |
| 81 | "Gentoo Wiki" |
| 82 | ]; |
| 83 | }; |
| 84 | }; |
| 85 | |
| 86 | config = |
| 87 | let |
| 88 | engineData = { |
Samuel Shuert | 95554c5 | 2024-02-29 17:04:31 -0500 | [diff] [blame^] | 89 | "Amazon" = { |
| 90 | homepage = "https://amazon.com"; |
| 91 | metaData.alias = "amazon"; |
| 92 | }; |
Skyler Grey | cd3d38b | 2024-02-25 15:18:32 +0000 | [diff] [blame] | 93 | "Arch Wiki" = { |
| 94 | urls = [ { template = "https://wiki.archlinux.org/index.php?search={searchTerms}"; } ]; |
| 95 | iconUpdateURL = "https://wiki.archlinux.org/favicon.ico"; |
| 96 | updateInterval = 24 * 60 * 60 * 1000; |
| 97 | definedAliases = [ "arch" ]; |
| 98 | homepage = "https://wiki.archlinux.org/"; |
| 99 | }; |
| 100 | "Bing" = { |
| 101 | homepage = "https://bing.com"; |
| 102 | metaData.alias = "bing"; |
| 103 | }; |
| 104 | "DuckDuckGo" = { |
| 105 | homepage = "https://duckduckgo.com"; |
| 106 | metaData.alias = "ddg"; |
| 107 | extraExtensions = [ config.nur.repos.rycee.firefox-addons.duckduckgo-privacy-essentials ]; |
| 108 | }; |
| 109 | "eBay" = { |
| 110 | homepage = "https://ebay.com"; |
| 111 | metaData.alias = "ebay"; |
| 112 | }; |
| 113 | "Gentoo Wiki" = { |
| 114 | urls = [ { template = "https://wiki.gentoo.org/index.php?search={searchTerms}"; } ]; |
| 115 | iconUpdateURL = "https://www.gentoo.org/favicon.ico"; |
| 116 | updateInterval = 24 * 60 * 60 * 1000; |
| 117 | definedAliases = [ "gentoo" ]; |
| 118 | homepage = "https://wiki.gentoo.org"; |
| 119 | }; |
| 120 | "GitHub" = { |
| 121 | urls = [ { template = "https://github.com/search?q={searchTerms}"; } ]; |
| 122 | iconUpdateURL = "https://github.com/favicon.ico"; |
| 123 | updateInterval = 24 * 60 * 60 * 1000; |
| 124 | definedAliases = [ "gh" ]; |
| 125 | homepage = "https://github.com"; |
| 126 | }; |
| 127 | "Google" = { |
| 128 | homepage = "https://google.com"; |
| 129 | metaData.alias = "google"; |
| 130 | }; |
| 131 | "Home-Manager Options" = { |
| 132 | urls = [ |
| 133 | { template = "https://mipmip.github.io/home-manager-option-search/?query={searchTerms}"; } |
| 134 | ]; |
| 135 | iconUpdateURL = "https://mipmip.github.io/home-manager-option-search/images/favicon.png"; |
| 136 | updateInterval = 24 * 60 * 60 * 1000; |
| 137 | definedAliases = [ "hm" ]; |
| 138 | homepage = "https://mipmip.github.io/home-manager-option-search/"; |
| 139 | }; |
| 140 | "Kagi" = { |
| 141 | urls = [ |
| 142 | { template = "https://kagi.com/search?q={searchTerms}"; } |
| 143 | { |
| 144 | template = "https://kagi.com/api/autosuggest?q={searchTerms}"; |
| 145 | type = "application/x-suggestions+json"; |
| 146 | } |
| 147 | ]; |
| 148 | iconUpdateURL = "https://assets.kagi.com/v2/favicon-32x32.png"; |
| 149 | updateInterval = 24 * 60 * 60 * 1000; |
| 150 | homepage = "https://kagi.com"; |
| 151 | definedAliases = [ "kagi" ]; |
| 152 | extraExtensions = [ config.nur.repos.rycee.firefox-addons.kagi-search ]; |
| 153 | }; |
| 154 | "MDN" = { |
| 155 | urls = [ { template = "https://developer.mozilla.org/en-US/search?q={searchTerms}"; } ]; |
| 156 | iconUpdateURL = "https://developer.mozilla.org/favicon.ico"; |
| 157 | updateInterval = 24 * 60 * 60 * 1000; |
| 158 | homepage = "https://developer.mozilla.org"; |
| 159 | definedAliases = [ "mdn" ]; |
| 160 | }; |
| 161 | "NixOS Options" = { |
| 162 | urls = [ { template = "https://search.nixos.org/options?channel=unstable&query={searchTerms}"; } ]; |
| 163 | iconUpdateURL = "https://nixos.org/logo/nix-wiki.png"; |
| 164 | updateInterval = 24 * 60 * 60 * 1000; |
| 165 | homepage = "https://search.nixos.org/options?channel=unstable"; |
| 166 | definedAliases = [ "opts" ]; |
| 167 | }; |
| 168 | "NixOS Packages" = { |
| 169 | urls = [ { template = "https://search.nixos.org/packages?channel=unstable&query={searchTerms}"; } ]; |
| 170 | iconUpdateURL = "https://nixos.org/logo/nix-wiki.png"; |
| 171 | updateInterval = 24 * 60 * 60 * 1000; |
| 172 | homepage = "https://search.nixos.org/packages?channel=unstable"; |
| 173 | definedAliases = [ "pkgs" ]; |
| 174 | }; |
| 175 | "Noogle" = { |
| 176 | urls = [ { template = "https://noogle.dev/q?term={searchTerms}"; } ]; |
| 177 | iconUpdateURL = "https://noogle.dev/favicon.png"; |
| 178 | updateInterval = 24 * 60 * 60 * 1000; |
| 179 | homepage = "https://noogle.dev"; |
| 180 | definedAliases = [ "lib" ]; |
| 181 | }; |
| 182 | "Wikipedia (en)" = { |
| 183 | homepage = "https://en.wikipedia.org"; |
| 184 | metaData.alias = "wiki"; |
| 185 | }; |
| 186 | }; |
Skyler Grey | cd3d38b | 2024-02-25 15:18:32 +0000 | [diff] [blame] | 187 | calculated = lib.pipe engineData [ |
Skyler Grey | 416d9d0 | 2024-02-29 17:08:30 -0500 | [diff] [blame] | 188 | (lib.filterAttrs (engine: _: builtins.elem engine config.chimera.browser.firefox.search.engines)) |
Skyler Grey | cd3d38b | 2024-02-25 15:18:32 +0000 | [diff] [blame] | 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 | } |