Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firefox: fix search engine icons #6505

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 51 additions & 17 deletions modules/programs/firefox/profiles/search.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ let
"name"
"isAppProvided"
"loadPath"
"hasPreferredIcon"
"updateInterval"
"updateURL"
"iconUpdateURL"
"iconURL"
"iconMapObj"
"metaData"
"orderHint"
Expand All @@ -26,23 +23,25 @@ let
searchForm = "__searchForm";
};

# Convenience to specify absolute path to icon
iconUrl = icon: if hasPrefix "/" icon then "file://${icon}" else icon;

processCustomEngineInput = input:
(removeAttrs input [ "icon" ]) // optionalAttrs (input ? icon) {
# Convenience to specify absolute path to icon
iconURL = "file://${input.icon}";
} // (optionalAttrs (input ? iconUpdateURL) {
# Convenience to default iconURL to iconUpdateURL so
# the icon is immediately downloaded from the URL
iconURL = input.iconURL or input.iconUpdateURL;
} // {
let
iconMapObj = mapAttrs (name: iconUrl) (optionalAttrs (input ? icon) {
# Convenience to specify single icon instead of map
"16" = input.icon;
} // (input.iconMapObj or { }));
in (removeAttrs input [ "icon" "iconMapObj" ])
// optionalAttrs (iconMapObj != { }) { inherit iconMapObj; } // {
# Required for custom engine configurations, loadPaths
# are unique identifiers that are generally formatted
# like: [source]/path/to/engine.xml
loadPath = "[home-manager]/${
concatStringsSep "." (map strings.escapeNixIdentifier
(modulePath ++ [ "engines" input.name ]))
}";
});
};

processEngineInput = name: input:
let
Expand All @@ -55,7 +54,11 @@ let
in if requiredInput.isAppProvided then
requiredInput
else
processCustomEngineInput (input // requiredInput);
pipe (input // requiredInput) [
migrateEngineToV11
migrateEngineToV12
processCustomEngineInput
];

buildEngineConfig = name: input:
mapAttrs' (name: value: {
Expand Down Expand Up @@ -89,7 +92,7 @@ let
};

settings = {
version = 6;
version = 12;
engines = sortEngineConfigs (mapAttrs buildEngineConfig engineInput);

metaData = optionalAttrs (config.default != null) {
Expand Down Expand Up @@ -155,6 +158,37 @@ let
mozlz4a <(echo "$json") "$out"
fi
'';

migrateEngineToV11 = engine:
engine // {
iconMapObj = mapAttrs' (name: value:
let nameToIntResult = builtins.tryEval (toInt name);
in {
name = if nameToIntResult.success then
name
else
let size = toString (builtins.fromJSON name).width;
in warn
"JSON object names for iconMapObj are deprecated, use ${size} instead of ${name}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"JSON object names for iconMapObj are deprecated, use ${size} instead of ${name}"
"JSON object names for `iconMapObj` are deprecated, use `${size}` instead of `${name}`"

size;

inherit value;
}) (engine.iconMapObj or { });
};

migrateEngineToV12 = engine:
throwIf (engine ? hasPreferredIcon) "hasPreferredIcon has been removed"
(removeAttrs engine [ "iconURL" "iconUpdateURL" ]) // {
iconMapObj = optionalAttrs (engine ? iconURL) {
"16" = warn "iconURL is deprecated, use icon = ${
strings.escapeNixString engine.iconURL
} instead" engine.iconURL;
Comment on lines +183 to +185
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"16" = warn "iconURL is deprecated, use icon = ${
strings.escapeNixString engine.iconURL
} instead" engine.iconURL;
"16" = warn "`iconURL` is deprecated, use `icon = ${
strings.escapeNixString engine.iconURL
}` instead" engine.iconURL;

} // optionalAttrs (engine ? iconUpdateURL) {
"16" = warn "iconUpdateURL is deprecated, use icon = ${
strings.escapeNixString engine.iconUpdateURL
} instead" engine.iconUpdateURL;
} // (engine.iconMapObj or { });
Comment on lines +187 to +190
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"16" = warn "iconUpdateURL is deprecated, use icon = ${
strings.escapeNixString engine.iconUpdateURL
} instead" engine.iconUpdateURL;
} // (engine.iconMapObj or { });
"16" = warn "`iconUpdateURL` is deprecated, use `icon = ${
strings.escapeNixString engine.iconUpdateURL
}` instead" engine.iconUpdateURL;
} // (engine.iconMapObj or { });

};
in {
imports = [ (pkgs.path + "/nixos/modules/misc/meta.nix") ];

Expand Down Expand Up @@ -229,8 +263,8 @@ in {
};

"NixOS Wiki" = {
urls = [{ template = "https://wiki.nixos.org/index.php?search={searchTerms}"; }];
iconUpdateURL = "https://wiki.nixos.org/favicon.png";
urls = [{ template = "https://wiki.nixos.org/w/index.php?search={searchTerms}"; }];
iconMapObj."16" = "https://wiki.nixos.org/favicon.png";
updateInterval = 24 * 60 * 60 * 1000; # every day
definedAliases = [ "@nw" ];
};
Expand All @@ -245,7 +279,7 @@ in {
only have {var}`metaData` specified will be treated as builtin
to ${appName}.

See [SearchEngine.jsm](https://searchfox.org/mozilla-central/rev/669329e284f8e8e2bb28090617192ca9b4ef3380/toolkit/components/search/SearchEngine.jsm#1138-1177)
See [SearchEngine.jsm](https://searchfox.org/mozilla-central/rev/e3f42ec9320748b2aab3d474d1e47075def9000c/toolkit/components/search/SearchEngine.sys.mjs#890-923)
in ${appName}'s source for available options. We maintain a
mapping to let you specify all options in the referenced link
without underscores, but it may fall out of date with future
Expand Down
48 changes: 46 additions & 2 deletions tests/modules/programs/firefox/profiles/search/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ in {
"NixOS Wiki" = {
urls = [{
template =
"https://wiki.nixos.org/index.php?search={searchTerms}";
"https://wiki.nixos.org/w/index.php?search={searchTerms}";
}];
iconUpdateURL = "https://wiki.nixos.org/favicon.png";
iconMapObj."16" = "https://wiki.nixos.org/favicon.png";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was inherited from the previous code, but while it's being updated anyways:
this url is actually a 404, it's favicon.ico

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yep thanks for catching that!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iconMapObj."16" = "https://wiki.nixos.org/favicon.png";
iconMapObj."16" = "https://wiki.nixos.org/favicon.ico";

updateInterval = 24 * 60 * 60 * 1000;
definedAliases = [ "@nw" ];
};
Expand Down Expand Up @@ -92,6 +92,46 @@ in {
};
};
};

migrateIcons = {
id = 2;
search = {
force = true;
engines = {
"Nix Packages" = {
urls = [{
template = "https://search.nixos.org/packages";
params = [
{
name = "type";
value = "packages";
}
{
name = "query";
value = "{searchTerms}";
}
];
}];

iconURL = "https://search.nixos.org/favicon.png";
iconUpdateURL = "https://search.nixos.org/favicon.png";
definedAliases = [ "@np" ];
};

"NixOS Wiki" = {
urls = [{
template =
"https://wiki.nixos.org/w/index.php?search={searchTerms}";
}];
iconMapObj."{\"width\":16,\"height\":16}" =
"https://wiki.nixos.org/favicon.png";
updateInterval = 24 * 60 * 60 * 1000;
definedAliases = [ "@nw" ];
};
};
};

};
};
} // {
nmt.script = let
Expand Down Expand Up @@ -133,6 +173,10 @@ in {
assertFirefoxSearchContent \
home-files/${cfg.configPath}/searchWithoutDefault/search.json.mozlz4 \
${withName ./expected-search-without-default.json}

assertFirefoxSearchContent \
home-files/${cfg.configPath}/migrateIcons/search.json.mozlz4 \
${withName ./expected-migrate-icons.json}
'';
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"engines": [
{
"_definedAliases": [
"@np"
],
"_iconMapObj": {
"16": "https://search.nixos.org/favicon.png"
},
"_isAppProvided": false,
"_loadPath": "[home-manager]/programs.@[email protected].\"Nix Packages\"",
"_metaData": {},
"_name": "Nix Packages",
"_urls": [
{
"params": [
{
"name": "type",
"value": "packages"
},
{
"name": "query",
"value": "{searchTerms}"
}
],
"template": "https://search.nixos.org/packages"
}
]
},
{
"_definedAliases": [
"@nw"
],
"_iconMapObj": {
"16": "https://wiki.nixos.org/favicon.png"
},
"_isAppProvided": false,
"_loadPath": "[home-manager]/programs.@[email protected].\"NixOS Wiki\"",
"_metaData": {},
"_name": "NixOS Wiki",
"_updateInterval": 86400000,
"_urls": [
{
"template": "https://wiki.nixos.org/w/index.php?search={searchTerms}"
}
]
}
],
"metaData": {
"useSavedOrder": false
},
"version": 12
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@
"metaData": {
"useSavedOrder": true
},
"version": 6
"version": 12
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"_definedAliases": [
"@np"
],
"_iconURL": "file:///run/current-system/sw/share/icons/hicolor/scalable/apps/nix-snowflake.svg",
"_iconMapObj": {
"16": "file:///run/current-system/sw/share/icons/hicolor/scalable/apps/nix-snowflake.svg"
},
"_isAppProvided": false,
"_loadPath": "[home-manager]/programs.@[email protected].\"Nix Packages\"",
"_metaData": {
Expand All @@ -31,8 +33,9 @@
"_definedAliases": [
"@nw"
],
"_iconURL": "https://wiki.nixos.org/favicon.png",
"_iconUpdateURL": "https://wiki.nixos.org/favicon.png",
"_iconMapObj": {
"16": "https://wiki.nixos.org/favicon.png"
},
"_isAppProvided": false,
"_loadPath": "[home-manager]/programs.@[email protected].\"NixOS Wiki\"",
"_metaData": {
Expand All @@ -42,7 +45,7 @@
"_updateInterval": 86400000,
"_urls": [
{
"template": "https://wiki.nixos.org/index.php?search={searchTerms}"
"template": "https://wiki.nixos.org/w/index.php?search={searchTerms}"
}
]
},
Expand Down Expand Up @@ -73,5 +76,5 @@
"privateHash": null,
"useSavedOrder": true
},
"version": 6
"version": 12
}