From 280721186ab75a76537713ec310306f0eba3e407 Mon Sep 17 00:00:00 2001 From: Chuang Zhu Date: Sun, 12 Nov 2023 18:11:12 +0800 Subject: [PATCH] firefox: support setting a separate default search engine in private browsing (#4114) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * firefox: support setting a separate default search engine in private browing Ref: https://searchfox.org/mozilla-central/rev/05d1afda588e54be73e31ef4e022dde91f1ed97a/toolkit/components/search/SearchSettings.sys.mjs#548 * Update modules/programs/firefox.nix --------- Co-authored-by: Naïm Favier --- modules/programs/firefox.nix | 30 ++++++++++++++++--- .../profile-settings-expected-search.json | 6 ++-- .../programs/firefox/profile-settings.nix | 3 +- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/modules/programs/firefox.nix b/modules/programs/firefox.nix index e63a72670..6a3ab656c 100644 --- a/modules/programs/firefox.nix +++ b/modules/programs/firefox.nix @@ -444,6 +444,15 @@ in { ''; }; + privateDefault = mkOption { + type = with types; nullOr str; + default = null; + example = "DuckDuckGo"; + description = '' + The default search engine used in the Private Browsing. + ''; + }; + order = mkOption { type = with types; uniq (listOf str); default = [ ]; @@ -675,8 +684,8 @@ in { }; "${profilesPath}/${profile.path}/search.json.mozlz4" = mkIf - (profile.search.default != null || profile.search.order != [ ] - || profile.search.engines != { }) { + (profile.search.default != null || profile.search.privateDefault != null + || profile.search.order != [ ] || profile.search.engines != { }) { force = profile.search.force; source = let settings = { @@ -767,12 +776,18 @@ in { # engine if it's not in profile.search.engines ${profile.search.default} = profile.search.engines.${profile.search.default} or { }; + } // { + ${profile.search.privateDefault} = + profile.search.engines.${profile.search.privateDefault} or { }; }; in sortEngineConfigs (mapAttrs buildEngineConfig engineInput); metaData = optionalAttrs (profile.search.default != null) { current = profile.search.default; hash = "@hash@"; + } // optionalAttrs (profile.search.privateDefault != null) { + private = profile.search.privateDefault; + privateHash = "@privateHash@"; } // { useSavedOrder = profile.search.order != [ ]; }; @@ -794,14 +809,21 @@ in { profile.path + profile.search.default + disclaimer "Firefox" else null; + + privateSalt = if profile.search.privateDefault != null then + profile.path + profile.search.privateDefault + + disclaimer "Firefox" + else + null; in pkgs.runCommand "search.json.mozlz4" { nativeBuildInputs = with pkgs; [ mozlz4a openssl ]; json = builtins.toJSON settings; - inherit salt; + inherit salt privateSalt; } '' if [[ -n $salt ]]; then export hash=$(echo -n "$salt" | openssl dgst -sha256 -binary | base64) - mozlz4a <(substituteStream json search.json.in --subst-var hash) "$out" + export privateHash=$(echo -n "$privateSalt" | openssl dgst -sha256 -binary | base64) + mozlz4a <(substituteStream json search.json.in --subst-var hash --subst-var privateHash) "$out" else mozlz4a <(echo "$json") "$out" fi diff --git a/tests/modules/programs/firefox/profile-settings-expected-search.json b/tests/modules/programs/firefox/profile-settings-expected-search.json index ceee27ee6..f17a0b91e 100644 --- a/tests/modules/programs/firefox/profile-settings-expected-search.json +++ b/tests/modules/programs/firefox/profile-settings-expected-search.json @@ -67,8 +67,10 @@ } ], "metaData": { - "current": "DuckDuckGo", - "hash": "BWvqUiaCuMJ20lbymFf2dqzWyl1cgm1LZhhdWNEp0Cc=", + "current": "Google", + "hash": "IRmKVSQlAYnvLO8kdZyNUR/g5hzVn4++T0PLPGm+kZk=", + "private": "DuckDuckGo", + "privateHash": "BWvqUiaCuMJ20lbymFf2dqzWyl1cgm1LZhhdWNEp0Cc=", "useSavedOrder": true }, "version": 6 diff --git a/tests/modules/programs/firefox/profile-settings.nix b/tests/modules/programs/firefox/profile-settings.nix index 6529a1bb2..94fa9436e 100644 --- a/tests/modules/programs/firefox/profile-settings.nix +++ b/tests/modules/programs/firefox/profile-settings.nix @@ -75,7 +75,8 @@ id = 3; search = { force = true; - default = "DuckDuckGo"; + default = "Google"; + privateDefault = "DuckDuckGo"; order = [ "Nix Packages" "NixOS Wiki" ]; engines = { "Nix Packages" = {