From 252cb97a0192b5fb5555ddd5a32777171e428698 Mon Sep 17 00:00:00 2001 From: Kira Bruneau Date: Fri, 21 Feb 2025 00:43:28 -0500 Subject: [PATCH] firefox: migrate search config to v7 --- modules/programs/firefox/profiles/search.nix | 219 ++++++++++++++++-- .../firefox/profiles/search/default.nix | 114 ++++++--- .../search/expected-migrate-search-v7.json | 79 +++++++ .../expected-search-without-default.json | 9 +- .../profiles/search/expected-search.json | 26 ++- .../programs/firefox/profiles/shared-path.nix | 8 +- 6 files changed, 387 insertions(+), 68 deletions(-) create mode 100644 tests/modules/programs/firefox/profiles/search/expected-migrate-search-v7.json diff --git a/modules/programs/firefox/profiles/search.nix b/modules/programs/firefox/profiles/search.nix index f0d5f5a30..074692b48 100644 --- a/modules/programs/firefox/profiles/search.nix +++ b/modules/programs/firefox/profiles/search.nix @@ -27,7 +27,9 @@ let }; processCustomEngineInput = input: - (removeAttrs input [ "icon" ]) // optionalAttrs (input ? icon) { + { + name = input.id; + } // (removeAttrs input [ "icon" ]) // optionalAttrs (input ? icon) { # Convenience to specify absolute path to icon iconURL = "file://${input.icon}"; } // (optionalAttrs (input ? iconUpdateURL) { @@ -40,14 +42,14 @@ let # like: [source]/path/to/engine.xml loadPath = "[home-manager]/${ concatStringsSep "." (map strings.escapeNixIdentifier - (modulePath ++ [ "engines" input.name ])) + (modulePath ++ [ "engines" input.id ])) }"; }); - processEngineInput = name: input: + processEngineInput = id: input: let requiredInput = { - inherit name; + inherit id; isAppProvided = input.isAppProvided or (removeAttrs input [ "metaData" ] == { }); metaData = input.metaData or { }; @@ -65,10 +67,10 @@ let sortEngineConfigs = configs: let - buildEngineConfigWithOrder = order: name: + buildEngineConfigWithOrder = order: id: let - config = configs.${name} or { - _name = name; + config = configs.${id} or { + inherit id; _isAppProvided = true; _metaData = { }; }; @@ -89,15 +91,15 @@ let }; settings = { - version = 6; + version = 7; engines = sortEngineConfigs (mapAttrs buildEngineConfig engineInput); metaData = optionalAttrs (config.default != null) { - current = config.default; - hash = "@hash@"; + defaultEngineId = config.default; + defaultEngineIdHash = "@hash@"; } // optionalAttrs (config.privateDefault != null) { - private = config.privateDefault; - privateHash = "@privateHash@"; + privateDefaultEngineId = config.privateDefault; + privateDefaultEngineIdHash = "@privateHash@"; } // { useSavedOrder = config.order != [ ]; }; @@ -155,6 +157,172 @@ let mozlz4a <(echo "$json") "$out" fi ''; + + engineNameToId = { + # Derived from https://searchfox.org/mozilla-central/rev/e3f42ec9320748b2aab3d474d1e47075def9000c/services/settings/dumps/main/search-config-v2.json + "1&1 Suche" = "1und1"; + "Allegro" = "allegro-pl"; + "Amazon.co.jp" = "amazon-jp"; + "Amazon.com" = "amazondotcom-us"; + "Azerdict" = "azerdict"; + "百度" = "baidu"; + "Bing" = "bing"; + "Ordbok" = "bok-NO"; + "Ceneje.si" = "ceneji"; + "Cốc Cốc" = "coccoc"; + "다음" = "daum-kr"; + "DuckDuckGo" = "ddg"; + "eBay" = "ebay"; + "Ecosia" = "ecosia"; + "EUdict Eng->Cro" = "eudict"; + "Am Faclair Beag" = "faclair-beag"; + "GMX Suche" = "gmx-de"; + "GMX Search" = "gmx-en-GB"; + "GMX - Búsqueda web" = "gmx-es"; + "GMX - Recherche web" = "gmx-fr"; + "GMX Shopping" = "gmx-shopping"; + "Google" = "google"; + "Gule sider" = "gulesider-NO"; + "LEO Eng-Deu" = "leo_ende_de"; + "พจนานุกรม ลองดู" = "longdo"; + "mail.com search" = "mailcom"; + "Mapy.cz" = "mapy-cz"; + "MercadoLibre Argentina" = "mercadolibre-ar"; + "MercadoLibre Chile" = "mercadolibre-cl"; + "MercadoLibre Mexico" = "mercadolibre-mx"; + "MercadoLivre" = "mercadolivre"; + "네이버" = "naver-kr"; + "Odpiralni Časi" = "odpiralni"; + "Pazaruvaj" = "pazaruvaj"; + "Priberam" = "priberam"; + "Prisjakt" = "prisjakt-sv-SE"; + "Qwant" = "qwant"; + "Qwant Junior" = "qwantjr"; + "楽天市場" = "rakuten"; + "Readmoo 讀墨電子書" = "readmoo"; + "Reddit" = "reddit"; + "Salidzini.lv" = "salidzinilv"; + "Seznam" = "seznam-cz"; + "Tyda.se" = "tyda-sv-SE"; + "Vatera.hu" = "vatera"; + "WEB.DE Suche" = "webde"; + "Wikipedia (en)" = "wikipedia"; + "Wikipedia (nn)" = "wikipedia-NN"; + "Wikipedia (nb)" = "wikipedia-NO"; + "Wikipedia (af)" = "wikipedia-af"; + "Biquipedia (an)" = "wikipedia-an"; + "ويكيبيديا (ar)" = "wikipedia-ar"; + "Wikipedia (ast)" = "wikipedia-ast"; + "Vikipediya (az)" = "wikipedia-az"; + "Вікіпедыя (be)" = "wikipedia-be"; + "Вікіпэдыя (be-tarask)" = "wikipedia-be-tarask"; + "Уикипедия (bg)" = "wikipedia-bg"; + "উইকিপিডিয়া (bn)" = "wikipedia-bn"; + "Wikipedia (br)" = "wikipedia-br"; + "Wikipedia (bs)" = "wikipedia-bs"; + "Viquipèdia (ca)" = "wikipedia-ca"; + "Wicipedia (cy)" = "wikipedia-cy"; + "Wikipedie (cs)" = "wikipedia-cz"; + "Wikipedia (da)" = "wikipedia-da"; + "Wikipedia (de)" = "wikipedia-de"; + "Wikipedija (dsb)" = "wikipedia-dsb"; + "Βικιπαίδεια (el)" = "wikipedia-el"; + "Vikipedio (eo)" = "wikipedia-eo"; + "Wikipedia (es)" = "wikipedia-es"; + "Vikipeedia (et)" = "wikipedia-et"; + "Wikipedia (eu)" = "wikipedia-eu"; + "ویکی‌پدیا (fa)" = "wikipedia-fa"; + "Wikipedia (fi)" = "wikipedia-fi"; + "Wikipédia (fr)" = "wikipedia-fr"; + "Wikipedy (fy)" = "wikipedia-fy-NL"; + "Vicipéid (ga)" = "wikipedia-ga-IE"; + "Uicipeid (gd)" = "wikipedia-gd"; + "Wikipedia (gl)" = "wikipedia-gl"; + "Vikipetã (gn)" = "wikipedia-gn"; + "વિકિપીડિયા (gu)" = "wikipedia-gu"; + "ויקיפדיה" = "wikipedia-he"; + "विकिपीडिया (hi)" = "wikipedia-hi"; + "Wikipedija (hr)" = "wikipedia-hr"; + "Wikipedija (hsb)" = "wikipedia-hsb"; + "Wikipédia (hu)" = "wikipedia-hu"; + "Վիքիպեդիա (hy)" = "wikipedia-hy"; + "Wikipedia (ia)" = "wikipedia-ia"; + "Wikipedia (id)" = "wikipedia-id"; + "Wikipedia (is)" = "wikipedia-is"; + "Wikipedia (it)" = "wikipedia-it"; + "Wikipedia (ja)" = "wikipedia-ja"; + "ვიკიპედია (ka)" = "wikipedia-ka"; + "Wikipedia (kab)" = "wikipedia-kab"; + "Уикипедия (kk)" = "wikipedia-kk"; + "វិគីភីឌា (km)" = "wikipedia-km"; + "ವಿಕಿಪೀಡಿಯ (kn)" = "wikipedia-kn"; + "위키백과 (ko)" = "wikipedia-kr"; + "Wikipedia (lij)" = "wikipedia-lij"; + "ວິກິພີເດຍ (lo)" = "wikipedia-lo"; + "Vikipedija (lt)" = "wikipedia-lt"; + "Vikipedeja (ltg)" = "wikipedia-ltg"; + "Vikipēdija (lv)" = "wikipedia-lv"; + "Википедија (mk)" = "wikipedia-mk"; + "विकिपीडिया (mr)" = "wikipedia-mr"; + "Wikipedia (ms)" = "wikipedia-ms"; + "ဝီကီပီးဒီးယား (my)" = "wikipedia-my"; + "विकिपिडिया (ne)" = "wikipedia-ne"; + "Wikipedia (nl)" = "wikipedia-nl"; + "Wikipèdia (oc)" = "wikipedia-oc"; + "ਵਿਕੀਪੀਡੀਆ (pa)" = "wikipedia-pa"; + "Wikipedia (pl)" = "wikipedia-pl"; + "Wikipédia (pt)" = "wikipedia-pt"; + "Wikipedia (rm)" = "wikipedia-rm"; + "Wikipedia (ro)" = "wikipedia-ro"; + "Википедия (ru)" = "wikipedia-ru"; + "විකිපීඩියා (si)" = "wikipedia-si"; + "Wikipédia (sk)" = "wikipedia-sk"; + "Wikipedija (sl)" = "wikipedia-sl"; + "Wikipedia (sq)" = "wikipedia-sq"; + "Википедија (sr)" = "wikipedia-sr"; + "Wikipedia (sv)" = "wikipedia-sv-SE"; + "விக்கிப்பீடியா (ta)" = "wikipedia-ta"; + "వికీపీడియా (te)" = "wikipedia-te"; + "วิกิพีเดีย" = "wikipedia-th"; + "Wikipedia (tl)" = "wikipedia-tl"; + "Vikipedi (tr)" = "wikipedia-tr"; + "Вікіпедія (uk)" = "wikipedia-uk"; + "ویکیپیڈیا (ur)" = "wikipedia-ur"; + "Vikipediya (uz)" = "wikipedia-uz"; + "Wikipedia (vi)" = "wikipedia-vi"; + "Wikipedia (wo)" = "wikipedia-wo"; + "维基百科" = "wikipedia-zh-CN"; + "Wikipedia (zh)" = "wikipedia-zh-TW"; + "ವಿಕ್ಷನರಿ (kn)" = "wiktionary-kn"; + "Wikiccionari (oc)" = "wiktionary-oc"; + "விக்சனரி (ta)" = "wiktionary-ta"; + "విక్షనరీ (te)" = "wiktionary-te"; + "Wolne Lektury" = "wolnelektury-pl"; + "Yahoo! JAPAN" = "yahoo-jp"; + "Yahoo!オークション" = "yahoo-jp-auctions"; + "YouTube" = "youtube"; + + # Derived from https://searchfox.org/mozilla-central/rev/e3f42ec9320748b2aab3d474d1e47075def9000c/toolkit/components/search/SearchSettings.sys.mjs#32-44 + "Wikipedia (hy)" = "wikipedia-hy"; + "Wikipedia (kn)" = "wikipedia-kn"; + "Vikipēdija" = "wikipedia-lv"; + "Wikipedia (no)" = "wikipedia-NO"; + "Wikipedia (el)" = "wikipedia-el"; + "Wikipedia (lt)" = "wikipedia-lt"; + "Wikipedia (my)" = "wikipedia-my"; + "Wikipedia (pa)" = "wikipedia-pa"; + "Wikipedia (pt)" = "wikipedia-pt"; + "Wikipedia (si)" = "wikipedia-si"; + "Wikipedia (tr)" = "wikipedia-tr"; + }; + + migrateEngineNameToIdV7 = engine: + if builtins.hasAttr engine engineNameToId then + warn "Search engines are now referenced by id instead of by name, use '${ + engineNameToId.${engine} + }' instead of '${engine}'" engineNameToId.${engine} + else + engine; in { imports = [ (pkgs.path + "/nixos/modules/misc/meta.nix") ]; @@ -182,8 +350,10 @@ in { default = mkOption { type = with types; nullOr str; + apply = engine: + if engine != null then migrateEngineNameToIdV7 engine else null; default = null; - example = "DuckDuckGo"; + example = "ddg"; description = '' The default search engine used in the address bar and search bar. @@ -192,8 +362,10 @@ in { privateDefault = mkOption { type = with types; nullOr str; + apply = engine: + if engine != null then migrateEngineNameToIdV7 engine else null; default = null; - example = "DuckDuckGo"; + example = "ddg"; description = '' The default search engine used in the Private Browsing. ''; @@ -201,8 +373,9 @@ in { order = mkOption { type = with types; uniq (listOf str); + apply = builtins.map migrateEngineNameToIdV7; default = [ ]; - example = [ "DuckDuckGo" "Google" ]; + example = [ "ddg" "google" ]; description = '' The order the search engines are listed in. Any engines that aren't included in this list will be listed after these in an @@ -212,10 +385,17 @@ in { engines = mkOption { type = with types; attrsOf (attrsOf jsonFormat.type); + + apply = mapAttrs' (name: value: { + name = migrateEngineNameToIdV7 name; + inherit value; + }); + default = { }; example = literalExpression '' { - "Nix Packages" = { + nix-packages = { + name = "Nix Packages"; urls = [{ template = "https://search.nixos.org/packages"; params = [ @@ -228,15 +408,16 @@ in { definedAliases = [ "@np" ]; }; - "NixOS Wiki" = { + nixos-wiki = { + name = "NixOS Wiki"; urls = [{ template = "https://wiki.nixos.org/index.php?search={searchTerms}"; }]; iconUpdateURL = "https://wiki.nixos.org/favicon.png"; updateInterval = 24 * 60 * 60 * 1000; # every day definedAliases = [ "@nw" ]; }; - "Bing".metaData.hidden = true; - "Google".metaData.alias = "@g"; # builtin engines only support specifying one additional alias + bing.metaData.hidden = true; + google.metaData.alias = "@g"; # builtin engines only support specifying one additional alias } ''; diff --git a/tests/modules/programs/firefox/profiles/search/default.nix b/tests/modules/programs/firefox/profiles/search/default.nix index 689c58439..a2a9a526f 100644 --- a/tests/modules/programs/firefox/profiles/search/default.nix +++ b/tests/modules/programs/firefox/profiles/search/default.nix @@ -21,6 +21,84 @@ in { profiles = { search = { id = 0; + search = { + force = true; + default = "google"; + privateDefault = "ddg"; + order = [ "nix-packages" "nixos-wiki" ]; + engines = { + nix-packages = { + name = "Nix Packages"; + + urls = [{ + template = "https://search.nixos.org/packages"; + params = [ + { + name = "type"; + value = "packages"; + } + { + name = "query"; + value = "{searchTerms}"; + } + ]; + }]; + + icon = + "/run/current-system/sw/share/icons/hicolor/scalable/apps/nix-snowflake.svg"; + + definedAliases = [ "@np" ]; + }; + + nixos-wiki = { + name = "NixOS Wiki"; + + urls = [{ + template = + "https://wiki.nixos.org/index.php?search={searchTerms}"; + }]; + iconUpdateURL = "https://wiki.nixos.org/favicon.png"; + updateInterval = 24 * 60 * 60 * 1000; + definedAliases = [ "@nw" ]; + }; + + bing.metaData.hidden = true; + google.metaData.alias = "@g"; + }; + }; + }; + + searchWithoutDefault = { + id = 1; + search = { + force = true; + order = [ "google" "nix-packages" ]; + engines = { + nix-packages = { + name = "Nix Packages"; + + urls = [{ + template = "https://search.nixos.org/packages"; + params = [ + { + name = "type"; + value = "packages"; + } + { + name = "query"; + value = "{searchTerms}"; + } + ]; + }]; + + definedAliases = [ "@np" ]; + }; + }; + }; + }; + + migrateSearchV7 = { + id = 2; search = { force = true; default = "Google"; @@ -53,7 +131,7 @@ in { template = "https://wiki.nixos.org/index.php?search={searchTerms}"; }]; - iconUpdateURL = "https://wiki.nixos.org/favicon.png"; + iconUpdateURL = "https://wiki.nixos.org/favicon.ico"; updateInterval = 24 * 60 * 60 * 1000; definedAliases = [ "@nw" ]; }; @@ -64,32 +142,6 @@ in { }; }; - searchWithoutDefault = { - id = 1; - search = { - force = true; - order = [ "Google" "Nix Packages" ]; - engines = { - "Nix Packages" = { - urls = [{ - template = "https://search.nixos.org/packages"; - params = [ - { - name = "type"; - value = "packages"; - } - { - name = "query"; - value = "{searchTerms}"; - } - ]; - }]; - - definedAliases = [ "@np" ]; - }; - }; - }; - }; }; } // { nmt.script = let @@ -106,8 +158,8 @@ in { f end; walk(if type == "object" then - if has("hash") then .hash = null else . end | - if has("privateHash") then .privateHash = null else . end + if has("defaultEngineIdHash") then .defaultEngineIdHash = "@hash@" else . end | + if has("privateDefaultEngineIdHash") then .privateDefaultEngineIdHash = "@privateHash@" else . end else . end)' ''; @@ -131,6 +183,10 @@ in { assertFirefoxSearchContent \ home-files/${cfg.configPath}/searchWithoutDefault/search.json.mozlz4 \ ${withName ./expected-search-without-default.json} + + assertFirefoxSearchContent \ + home-files/${cfg.configPath}/migrateSearchV7/search.json.mozlz4 \ + ${withName ./expected-migrate-search-v7.json} ''; }); } diff --git a/tests/modules/programs/firefox/profiles/search/expected-migrate-search-v7.json b/tests/modules/programs/firefox/profiles/search/expected-migrate-search-v7.json new file mode 100644 index 000000000..850afc6e3 --- /dev/null +++ b/tests/modules/programs/firefox/profiles/search/expected-migrate-search-v7.json @@ -0,0 +1,79 @@ +{ + "engines": [ + { + "_definedAliases": [ + "@np" + ], + "_iconURL": "file:///run/current-system/sw/share/icons/hicolor/scalable/apps/nix-snowflake.svg", + "_isAppProvided": false, + "_loadPath": "[home-manager]/programs.librewolf.profiles.migrateSearchV7.search.engines.\"Nix Packages\"", + "_metaData": { + "order": 1 + }, + "_name": "Nix Packages", + "_urls": [ + { + "params": [ + { + "name": "type", + "value": "packages" + }, + { + "name": "query", + "value": "{searchTerms}" + } + ], + "template": "https://search.nixos.org/packages" + } + ], + "id": "Nix Packages" + }, + { + "_definedAliases": [ + "@nw" + ], + "_iconURL": "https://wiki.nixos.org/favicon.ico", + "_iconUpdateURL": "https://wiki.nixos.org/favicon.ico", + "_isAppProvided": false, + "_loadPath": "[home-manager]/programs.librewolf.profiles.migrateSearchV7.search.engines.\"NixOS Wiki\"", + "_metaData": { + "order": 2 + }, + "_name": "NixOS Wiki", + "_updateInterval": 86400000, + "_urls": [ + { + "template": "https://wiki.nixos.org/index.php?search={searchTerms}" + } + ], + "id": "NixOS Wiki" + }, + { + "_isAppProvided": true, + "_metaData": { + "hidden": true + }, + "id": "bing" + }, + { + "_isAppProvided": true, + "_metaData": {}, + "id": "ddg" + }, + { + "_isAppProvided": true, + "_metaData": { + "alias": "@g" + }, + "id": "google" + } + ], + "metaData": { + "defaultEngineId": "google", + "defaultEngineIdHash": "@hash@", + "privateDefaultEngineId": "ddg", + "privateDefaultEngineIdHash": "@privateHash@", + "useSavedOrder": true + }, + "version": 7 +} diff --git a/tests/modules/programs/firefox/profiles/search/expected-search-without-default.json b/tests/modules/programs/firefox/profiles/search/expected-search-without-default.json index ec1ce14c8..2ba4691d9 100644 --- a/tests/modules/programs/firefox/profiles/search/expected-search-without-default.json +++ b/tests/modules/programs/firefox/profiles/search/expected-search-without-default.json @@ -5,14 +5,14 @@ "_metaData": { "order": 1 }, - "_name": "Google" + "id": "google" }, { "_definedAliases": [ "@np" ], "_isAppProvided": false, - "_loadPath": "[home-manager]/programs.@name@.profiles.searchWithoutDefault.search.engines.\"Nix Packages\"", + "_loadPath": "[home-manager]/programs.@name@.profiles.searchWithoutDefault.search.engines.nix-packages", "_metaData": { "order": 2 }, @@ -31,11 +31,12 @@ ], "template": "https://search.nixos.org/packages" } - ] + ], + "id": "nix-packages" } ], "metaData": { "useSavedOrder": true }, - "version": 6 + "version": 7 } diff --git a/tests/modules/programs/firefox/profiles/search/expected-search.json b/tests/modules/programs/firefox/profiles/search/expected-search.json index a661cf3fe..8688aea15 100644 --- a/tests/modules/programs/firefox/profiles/search/expected-search.json +++ b/tests/modules/programs/firefox/profiles/search/expected-search.json @@ -6,7 +6,7 @@ ], "_iconURL": "file:///run/current-system/sw/share/icons/hicolor/scalable/apps/nix-snowflake.svg", "_isAppProvided": false, - "_loadPath": "[home-manager]/programs.@name@.profiles.search.search.engines.\"Nix Packages\"", + "_loadPath": "[home-manager]/programs.@name@.profiles.search.search.engines.nix-packages", "_metaData": { "order": 1 }, @@ -25,7 +25,8 @@ ], "template": "https://search.nixos.org/packages" } - ] + ], + "id": "nix-packages" }, { "_definedAliases": [ @@ -34,7 +35,7 @@ "_iconURL": "https://wiki.nixos.org/favicon.png", "_iconUpdateURL": "https://wiki.nixos.org/favicon.png", "_isAppProvided": false, - "_loadPath": "[home-manager]/programs.@name@.profiles.search.search.engines.\"NixOS Wiki\"", + "_loadPath": "[home-manager]/programs.@name@.profiles.search.search.engines.nixos-wiki", "_metaData": { "order": 2 }, @@ -44,34 +45,35 @@ { "template": "https://wiki.nixos.org/index.php?search={searchTerms}" } - ] + ], + "id": "nixos-wiki" }, { "_isAppProvided": true, "_metaData": { "hidden": true }, - "_name": "Bing" + "id": "bing" }, { "_isAppProvided": true, "_metaData": {}, - "_name": "DuckDuckGo" + "id": "ddg" }, { "_isAppProvided": true, "_metaData": { "alias": "@g" }, - "_name": "Google" + "id": "google" } ], "metaData": { - "current": "Google", - "hash": null, - "private": "DuckDuckGo", - "privateHash": null, + "defaultEngineId": "google", + "defaultEngineIdHash": "@hash@", + "privateDefaultEngineId": "ddg", + "privateDefaultEngineIdHash": "@privateHash@", "useSavedOrder": true }, - "version": 6 + "version": 7 } diff --git a/tests/modules/programs/firefox/profiles/shared-path.nix b/tests/modules/programs/firefox/profiles/shared-path.nix index b0936bfaa..1955d9fe3 100644 --- a/tests/modules/programs/firefox/profiles/shared-path.nix +++ b/tests/modules/programs/firefox/profiles/shared-path.nix @@ -27,11 +27,11 @@ in { }; search = { force = true; - default = "Google"; - privateDefault = "DuckDuckGo"; + default = "google"; + privateDefault = "ddg"; engines = { - "Bing".metaData.hidden = true; - "Google".metaData.alias = "@g"; + bing.metaData.hidden = true; + google.metaData.alias = "@g"; }; }; settings = {