From e0a87d75e9083569f73efc47b58c0e3fa4f99382 Mon Sep 17 00:00:00 2001 From: Emil Karlson Date: Wed, 29 Sep 2021 09:39:17 +0300 Subject: [PATCH] firefox: add bookmarks support Generate bookmarks html file and hook it up in user.js. --- modules/programs/firefox.nix | 91 +++++++++++++++++-- ...rofile-settings-expected-bookmarks-user.js | 8 ++ .../profile-settings-expected-bookmarks.html | 12 +++ .../programs/firefox/profile-settings.nix | 28 ++++++ 4 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 tests/modules/programs/firefox/profile-settings-expected-bookmarks-user.js create mode 100644 tests/modules/programs/firefox/profile-settings-expected-bookmarks.html diff --git a/modules/programs/firefox.nix b/modules/programs/firefox.nix index 0d1af97cf..cca4e5054 100644 --- a/modules/programs/firefox.nix +++ b/modules/programs/firefox.nix @@ -40,15 +40,50 @@ let profilesIni = generators.toINI { } profiles; - mkUserJs = prefs: extraPrefs: '' - // Generated by Home Manager. + mkUserJs = prefs: extraPrefs: bookmarks: + let + prefs' = lib.optionalAttrs ({ } != bookmarks) { + "browser.bookmarks.file" = toString (firefoxBookmarksFile bookmarks); + "browser.places.importBookmarksHTML" = true; + } // prefs; + in '' + // Generated by Home Manager. - ${concatStrings (mapAttrsToList (name: value: '' - user_pref("${name}", ${builtins.toJSON value}); - '') prefs)} + ${concatStrings (mapAttrsToList (name: value: '' + user_pref("${name}", ${builtins.toJSON value}); + '') prefs')} - ${extraPrefs} - ''; + ${extraPrefs} + ''; + + firefoxBookmarksFile = bookmarks: + let + escapeXML = replaceStrings [ ''"'' "'" "<" ">" "&" ] [ + """ + "'" + "<" + ">" + "&" + ]; + mapper = _: entry: '' +
${escapeXML entry.name} + ''; + bookmarksEntries = lib.attrsets.mapAttrsToList mapper bookmarks; + in pkgs.writeText "firefox-bookmarks.html" '' + + + + Bookmarks +

Bookmarks Menu

+

+ ${concatStrings bookmarksEntries} +

+ ''; in { meta.maintainers = [ maintainers.rycee ]; @@ -197,6 +232,45 @@ in { ''; }; + bookmarks = mkOption { + type = types.attrsOf (types.submodule ({ config, name, ... }: { + options = { + name = mkOption { + type = types.str; + default = name; + description = "Bookmark name."; + }; + + keyword = mkOption { + type = types.nullOr types.str; + default = null; + description = "Bookmark search keyword."; + }; + + url = mkOption { + type = types.str; + description = "Bookmark url, use %s for search terms."; + }; + }; + })); + default = { }; + example = literalExample '' + { + wikipedia = { + keyword = "wiki"; + url = "https://en.wikipedia.org/wiki/Special:Search?search=%s&go=Go"; + }; + "kernel.org" = { + url = "https://www.kernel.org"; + }; + } + ''; + description = '' + Preloaded bookmarks. Note, this may silently overwrite any + previously existing bookmarks! + ''; + }; + path = mkOption { type = types.str; default = name; @@ -298,7 +372,8 @@ in { "${profilesPath}/${profile.path}/user.js" = mkIf (profile.settings != { } || profile.extraConfig != "") { - text = mkUserJs profile.settings profile.extraConfig; + text = + mkUserJs profile.settings profile.extraConfig profile.bookmarks; }; "${profilesPath}/${profile.path}/extensions" = diff --git a/tests/modules/programs/firefox/profile-settings-expected-bookmarks-user.js b/tests/modules/programs/firefox/profile-settings-expected-bookmarks-user.js new file mode 100644 index 000000000..922d3651e --- /dev/null +++ b/tests/modules/programs/firefox/profile-settings-expected-bookmarks-user.js @@ -0,0 +1,8 @@ +// Generated by Home Manager. + +user_pref("browser.bookmarks.file", "/nix/store/00000000000000000000000000000000-firefox-bookmarks.html"); +user_pref("browser.places.importBookmarksHTML", true); +user_pref("general.smoothScroll", false); + + + diff --git a/tests/modules/programs/firefox/profile-settings-expected-bookmarks.html b/tests/modules/programs/firefox/profile-settings-expected-bookmarks.html new file mode 100644 index 000000000..15ce487b0 --- /dev/null +++ b/tests/modules/programs/firefox/profile-settings-expected-bookmarks.html @@ -0,0 +1,12 @@ + + + +Bookmarks +

Bookmarks Menu

+

+

kernel.org +
wikipedia + +

diff --git a/tests/modules/programs/firefox/profile-settings.nix b/tests/modules/programs/firefox/profile-settings.nix index af4498681..605ff6692 100644 --- a/tests/modules/programs/firefox/profile-settings.nix +++ b/tests/modules/programs/firefox/profile-settings.nix @@ -12,6 +12,19 @@ with lib; id = 1; settings = { "general.smoothScroll" = false; }; }; + + profiles.bookmarks = { + id = 2; + settings = { "general.smoothScroll" = false; }; + bookmarks = { + wikipedia = { + keyword = "wiki"; + url = + "https://en.wikipedia.org/wiki/Special:Search?search=%s&go=Go"; + }; + "kernel.org" = { url = "https://www.kernel.org"; }; + }; + }; }; nixpkgs.overlays = [ @@ -39,6 +52,21 @@ with lib; assertFileContent \ home-files/.mozilla/firefox/test/user.js \ ${./profile-settings-expected-user.js} + + bookmarksUserJs=$(normalizeStorePaths \ + home-files/.mozilla/firefox/bookmarks/user.js) + + assertFileContent \ + $bookmarksUserJs \ + ${./profile-settings-expected-bookmarks-user.js} + + bookmarksFile="$(sed -n \ + '/browser.bookmarks.file/ {s|^.*\(/nix/store[^"]*\).*|\1|;p}' \ + $TESTED/home-files/.mozilla/firefox/bookmarks/user.js)" + + assertFileContent \ + $bookmarksFile \ + ${./profile-settings-expected-bookmarks.html} ''; }; }