Skip to content

Commit

Permalink
firefox: fix search engine icons
Browse files Browse the repository at this point in the history
  • Loading branch information
kira-bruneau committed Feb 20, 2025
1 parent 0c0b0ac commit dc489ed
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 26 deletions.
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}"
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;
} // optionalAttrs (engine ? iconUpdateURL) {
"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
50 changes: 47 additions & 3 deletions tests/modules/programs/firefox/profiles/search/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ in {
}];

icon =
"/run/current-system/sw/share/icons/hicolor/scalable/apps/nix-snowflake.svg";
"${pkgs.nixos-icons}/share/icons/hicolor/scalable/apps/nix-snowflake.svg";

definedAliases = [ "@np" ];
};

"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";
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": "@nixos-icons@/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
}

0 comments on commit dc489ed

Please sign in to comment.