From 3ad750a3d977b3472a8a67acb7aa3613be7f70bb Mon Sep 17 00:00:00 2001 From: ene Date: Sat, 11 Feb 2023 22:24:30 +0100 Subject: Fix(home-manager): Improve Firefox configuration --- home-manager/firefox/default.nix | 152 +++++++++++++------------ home-manager/firefox/extensions.json | 30 ----- home-manager/firefox/generate-extension.py | 41 ------- home-manager/firefox/scripts/unzip_mozlz4.py | 43 +++++++ home-manager/firefox/scripts/update_extensions | 20 ++++ home-manager/firefox/settings/extensions.json | 37 ++++++ home-manager/firefox/settings/override.js | 2 +- home-manager/firefox/unzip_mozlz4.py | 43 ------- 8 files changed, 183 insertions(+), 185 deletions(-) delete mode 100644 home-manager/firefox/extensions.json delete mode 100755 home-manager/firefox/generate-extension.py create mode 100755 home-manager/firefox/scripts/unzip_mozlz4.py create mode 100755 home-manager/firefox/scripts/update_extensions create mode 100644 home-manager/firefox/settings/extensions.json delete mode 100755 home-manager/firefox/unzip_mozlz4.py (limited to 'home-manager/firefox') diff --git a/home-manager/firefox/default.nix b/home-manager/firefox/default.nix index af627a21..88fad227 100644 --- a/home-manager/firefox/default.nix +++ b/home-manager/firefox/default.nix @@ -5,17 +5,32 @@ lib, stdenv, strip_js_comments, + generate_extensions, user_js, + system, ... }: let - user_js_override = "./settings/override.js"; - user_js = pkgs.runCommand "user.js" {} '' - mkdir $out; - ${strip_js_comments}/bin "${user_js}/user.js" > $out/user.js; - cat ${user_js_override} >> $out/user.js; - sed 's/user_pref(\(.*\)",\(.*\));/\1" = \2;/' $out/user.js > $out/user.nix; + userChrome = builtins.readFile ./chrome/userChrome.css; + + user_js_override = pkgs.writeText "user.override.js" (builtins.readFile ./settings/override.js); + user_js_nix = pkgs.runCommand "user.js" {} '' + mkdir $out; + cat "${user_js}/user.js" > $out/user.js; + cat "${user_js_override}" >> $out/user.js; + + "${strip_js_comments.app.${system}.default.program}" $out/user.js > $out/user_clean.js; + # echo "{" > $out/user.nix.tmp + # sed 's/user_pref(\(.*\)",\(.*\));/\1" = \2;/' $out/user_clean.js >> $out/user.nix.tmp; + # echo "}" >> $out/user.nix.tmp + # awk '!/"_user.js.parrot"/' $out/user.nix.tmp >> $out/user.nix; # delete duplicate keys ''; + extensions = builtins.map buildFirefoxXpiAddon ( + lib.attrValues ( + lib.importJSON ./settings/extensions.json + ) + ); + # source: https://gitlab.com/rycee/nur-expressions/-/blob/master/pkgs/firefox-addons/default.nix buildFirefoxXpiAddon = { pname, @@ -23,13 +38,13 @@ addonId, url, sha256, - meta, +#meta, ... }: - builtins.stdenv.mkDerivation { + pkgs.stdenv.mkDerivation { name = "${pname}-${version}"; - inherit meta; +#inherit meta; src = builtins.fetchurl {inherit url sha256;}; @@ -43,68 +58,64 @@ ''; }; - settings = - pkgs.lib.recursiveUpdate - { - # Allow my custom css - "toolkit.legacyUserProfileCustomizations.stylesheets" = true; - - # might improve performance TODO - "gfx.webrender.all" = true; - - # disable updates (pretty pointless with nix) - "extensions.update.autoUpdateDefault" = false; - "extensions.update.enabled" = false; - "app.update.channel" = "default"; - - "browser.ctrlTab.recentlyUsedOrder" = false; - - "browser.download.useDownloadDir" = true; - "browser.download.dir" = "${config.user.soisha.home}/media/downloads"; - "browser.download.folderList" = 2; # TODO - "browser.download.viewableInternally.typeWasRegistered.svg" = true; - "browser.download.viewableInternally.typeWasRegistered.webp" = true; - "browser.download.viewableInternally.typeWasRegistered.xml" = true; - - # TODO what does this do? - "browser.search.widget.inNavBar" = true; - - "browser.shell.checkDefaultBrowser" = false; - "browser.tabs.loadInBackground" = true; - "browser.urlbar.placeholderName" = "Brave"; - - "general.autoScroll" = true; - - # Set the tabs and bookmarks - "browser.tabs.inTitlebar" = 1; - "browser.toolbars.bookmarks.visibility" = "always"; - - # Theme - "extensions.activeThemeID" = "firefox-alpenglow@mozilla.org"; - "extensions.extensions.activeThemeID" = "firefox-alpenglow@mozilla.org"; - - # highlight all entries when searching - "findbar.highlightAll" = true; - - # TODO - #"extensions.webcompat.enable_picture_in_picture_overrides" = true; - #"extensions.webcompat.enable_shims" = true; - #"extensions.webcompat.perform_injections" = true; - #"extensions.webcompat.perform_ua_overrides" = true; - - # onlykey / copied from a yubikey config - #"security.webauth.u2f" = true; - #"security.webauth.webauthn" = true; - #"security.webauth.webauthn_enable_softtoken" = true; - #"security.webauth.webauthn_enable_usbtoken" = true; - } - (builtins.readFile - "${user_js}/user.nix"); + settings = { + # Allow my custom css + "toolkit.legacyUserProfileCustomizations.stylesheets" = true; + + # might improve performance TODO + "gfx.webrender.all" = true; + + # disable updates (pretty pointless with nix) + "extensions.update.autoUpdateDefault" = false; + "extensions.update.enabled" = false; + "app.update.channel" = "default"; + + "browser.ctrlTab.recentlyUsedOrder" = false; + + "browser.download.useDownloadDir" = true; + "browser.download.dir" = "${config.home.homeDirectory}/media/downloads"; + "browser.download.folderList" = 2; # TODO + "browser.download.viewableInternally.typeWasRegistered.svg" = true; + "browser.download.viewableInternally.typeWasRegistered.webp" = true; + "browser.download.viewableInternally.typeWasRegistered.xml" = true; + + # TODO what does this do? + "browser.search.widget.inNavBar" = true; + + "browser.shell.checkDefaultBrowser" = false; + "browser.tabs.loadInBackground" = true; + "browser.urlbar.placeholderName" = "Brave"; + + "general.autoScroll" = true; + + # Set the tabs and bookmarks + "browser.tabs.inTitlebar" = 1; + "browser.toolbars.bookmarks.visibility" = "always"; + + # Theme + "extensions.activeThemeID" = "firefox-alpenglow@mozilla.org"; + "extensions.extensions.activeThemeID" = "firefox-alpenglow@mozilla.org"; + + # highlight all entries when searching + "findbar.highlightAll" = true; + + # TODO + #"extensions.webcompat.enable_picture_in_picture_overrides" = true; + #"extensions.webcompat.enable_shims" = true; + #"extensions.webcompat.perform_injections" = true; + #"extensions.webcompat.perform_ua_overrides" = true; + + # onlykey / copied from a yubikey config + #"security.webauth.u2f" = true; + #"security.webauth.webauthn" = true; + #"security.webauth.webauthn_enable_softtoken" = true; + #"security.webauth.webauthn_enable_usbtoken" = true; + }; in { home.packages = [ pkgs.firefox-wayland ]; - sessionVariables = { + home.sessionVariables = { # improve touch input & make scrolling smother MOZ_USE_XINPUT2 = "1"; # improve wayland support @@ -124,13 +135,13 @@ in { programs.firefox = { enable = true; package = pkgs.firefox; - extensions = builtins.map buildFirefoxXpiAddon (lib.attrValues (lib.importJSON ./extensions.json)); - profile."default" = { + profiles."default" = { + inherit extensions; isDefault = true; id = 0; name = "default"; - userChrome = builtins.readFile "./chrome/userChrome.css"; + inherit userChrome; search = { default = "Brave Search"; @@ -215,6 +226,7 @@ in { } ]; inherit settings; + extraConfig = builtins.readFile "${user_js_nix}/user.js"; }; }; } diff --git a/home-manager/firefox/extensions.json b/home-manager/firefox/extensions.json deleted file mode 100644 index a7673612..00000000 --- a/home-manager/firefox/extensions.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "darkreader": { - "pname": "darkreader", - "version": "4.9.62", - "addonId": "addon@darkreader.org", - "url": "https://addons.mozilla.org/firefox/downloads/file/4053589/darkreader-4.9.62.xpi", - "sha256": "sha256:e537a2cee45ed7c26f79ecd3ed362620e3f00d24c158532a58e163a63a3d60cc" - }, - "firenvim": { - "pname": "firenvim", - "version": "0.2.14", - "addonId": "firenvim@lacamb.re", - "url": "https://addons.mozilla.org/firefox/downloads/file/4026386/firenvim-0.2.14.xpi", - "sha256": "sha256:a8c495a59e30eaabbb3fcd188db9b5e28b40bffefe41a3f0fa22ecc58c80c2b6" - }, - "keepassxc-browser": { - "pname": "keepassxc-browser", - "version": "1.8.4", - "addonId": "keepassxc-browser@keepassxc.org", - "url": "https://addons.mozilla.org/firefox/downloads/file/4045866/keepassxc_browser-1.8.4.xpi", - "sha256": "sha256:cc39aa058cb8915cfc88424e2e1cebe3ccfc3f95d7bddb2abd0c4905d2b17719" - }, - "simple-tab-groups": { - "pname": "simple-tab-groups", - "version": "4.7.2.1", - "addonId": "simple-tab-groups@drive4ik", - "url": "https://addons.mozilla.org/firefox/downloads/file/3873608/simple_tab_groups-4.7.2.1.xpi", - "sha256": "sha256:75077589098ca62c00b86cf9554c6120bf8dc04c5f916fe26f84915f5147b2a4" - } -} \ No newline at end of file diff --git a/home-manager/firefox/generate-extension.py b/home-manager/firefox/generate-extension.py deleted file mode 100755 index cfb73542..00000000 --- a/home-manager/firefox/generate-extension.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python -# source: https://github.com/etu/nixconfig/blob/ba47d577c8bfb4a1c06927c34ece34118f4a0460/modules/graphical/firefox/generate.py - -from concurrent.futures import ThreadPoolExecutor -import json -import os -import requests - -EXTENSIONS = sorted([ - "darkreader", - "firenvim", - "keepassxc-browser", - "simple-tab-groups", -]) - -def index_ext(ext: str): - print(f"Indexing {ext}...") - - resp = requests.get(f"https://addons.mozilla.org/api/v5/addons/addon/{ext}/").json() - rel = resp["current_version"] - - if not rel["file"]["hash"].startswith("sha256:"): - raise ValueError("Unhandled hash type") - - return { - "pname": ext, - "version": rel["version"], - "addonId": resp["guid"], - "url": rel["file"]["url"], - "sha256": rel["file"]["hash"], - } - -if __name__ == "__main__": - outfile = os.path.dirname(os.path.realpath(__file__)) + "/extensions.json" - - with ThreadPoolExecutor() as e: - extensions = {ext: e.submit(index_ext, ext) for ext in EXTENSIONS} - extensions = {k: v.result() for k, v in extensions.items()} - - with open(outfile, "w") as f: - json.dump(extensions, f, indent=2) diff --git a/home-manager/firefox/scripts/unzip_mozlz4.py b/home-manager/firefox/scripts/unzip_mozlz4.py new file mode 100755 index 00000000..311fd214 --- /dev/null +++ b/home-manager/firefox/scripts/unzip_mozlz4.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +# source: https://unix.stackexchange.com/a/497861 +# Command-line tool to decompress mozLz4 files used for example by Firefox to store various kinds of session backup information. +# Works in both Python 2.7.15 and 3.6.7, as of version 2.1.6 of the LZ4 Python bindings at pypi.org/project/lz4. +# To use in another script, simply cut and paste the import statement and the mozlz4_to_text() function (lines 8 to 17). + +import lz4.block # pip install lz4 --user + + +def mozlz4_to_text(filepath): + # Given the path to a "mozlz4", "jsonlz4", "baklz4" etc. file, + # return the uncompressed text. + bytestream = open(filepath, "rb") + bytestream.read(8) # skip past the b"mozLz40\0" header + valid_bytes = bytestream.read() + text = lz4.block.decompress(valid_bytes) + return text + + +def main(args): + # Given command-line arguments of an input filepath for a ".mozlz4" file + # and optionally an output filepath, write the decompressed text to the + # output filepath. + # Default output filepath is the input filepath minus the last three characters + # (e.g. "foo.jsonlz4" becomes "foo.json") + filepath_in = args[0] + if len(args) < 2: + filepath_out = filepath_in[:-3] + else: + filepath_out = args[1] + text = mozlz4_to_text(filepath_in) + with open(filepath_out, "wb") as outfile: + outfile.write(text) + print("Wrote decompressed text to {}".format(filepath_out)) + + +if __name__ == "__main__": + import sys + args = sys.argv[1:] + if args and not args[0] in ("--help", "-h"): + main(args) + else: + print("Usage: mozlz4.py ") diff --git a/home-manager/firefox/scripts/update_extensions b/home-manager/firefox/scripts/update_extensions new file mode 100755 index 00000000..97f7c0ba --- /dev/null +++ b/home-manager/firefox/scripts/update_extensions @@ -0,0 +1,20 @@ +#!/bin/sh +# shellcheck disable=SC2086 +# shellcheck source=/dev/null +. ~/.local/lib/shell/lib + +tmp=$(mktmp) +cat << EOF > $tmp + darkreader + firenvim + keepassxc-browser + simple-tab-groups + ublock-origin +EOF + + +nix shell git+https://codeberg.org/ene/generate_moz_extension.git -c generate_extensions $(cat $tmp) > "$(dirname $0)"/../settings/extensions.json + + + +if [ -d /tmp/LIB_FILE_TEMP_DIR/ ];then rm -r /tmp/LIB_FILE_TEMP_DIR/; fi diff --git a/home-manager/firefox/settings/extensions.json b/home-manager/firefox/settings/extensions.json new file mode 100644 index 00000000..057810bc --- /dev/null +++ b/home-manager/firefox/settings/extensions.json @@ -0,0 +1,37 @@ +{ + "darkreader": { + "addonId": "addon@darkreader.org", + "pname": "darkreader", + "sha256": "sha256:e537a2cee45ed7c26f79ecd3ed362620e3f00d24c158532a58e163a63a3d60cc", + "url": "https://addons.mozilla.org/firefox/downloads/file/4053589/darkreader-4.9.62.xpi", + "version": "4.9.62" + }, + "firenvim": { + "addonId": "firenvim@lacamb.re", + "pname": "firenvim", + "sha256": "sha256:a8c495a59e30eaabbb3fcd188db9b5e28b40bffefe41a3f0fa22ecc58c80c2b6", + "url": "https://addons.mozilla.org/firefox/downloads/file/4026386/firenvim-0.2.14.xpi", + "version": "0.2.14" + }, + "keepassxc-browser": { + "addonId": "keepassxc-browser@keepassxc.org", + "pname": "keepassxc-browser", + "sha256": "sha256:cc39aa058cb8915cfc88424e2e1cebe3ccfc3f95d7bddb2abd0c4905d2b17719", + "url": "https://addons.mozilla.org/firefox/downloads/file/4045866/keepassxc_browser-1.8.4.xpi", + "version": "1.8.4" + }, + "simple-tab-groups": { + "addonId": "simple-tab-groups@drive4ik", + "pname": "simple-tab-groups", + "sha256": "sha256:75077589098ca62c00b86cf9554c6120bf8dc04c5f916fe26f84915f5147b2a4", + "url": "https://addons.mozilla.org/firefox/downloads/file/3873608/simple_tab_groups-4.7.2.1.xpi", + "version": "4.7.2.1" + }, + "ublock-origin": { + "addonId": "uBlock0@raymondhill.net", + "pname": "ublock-origin", + "sha256": "sha256:6bf8af5266353fab5eabdc7476de026e01edfb7901b0430c5e539f6791f1edc8", + "url": "https://addons.mozilla.org/firefox/downloads/file/4047353/ublock_origin-1.46.0.xpi", + "version": "1.46.0" + } +} diff --git a/home-manager/firefox/settings/override.js b/home-manager/firefox/settings/override.js index 1f92735b..ca03e8ed 100644 --- a/home-manager/firefox/settings/override.js +++ b/home-manager/firefox/settings/override.js @@ -56,7 +56,7 @@ user_pref("browser.safebrowsing.downloads.remote.enabled", true); // 0403 user_pref("network.prefetch-next", true); // 0601 // enable ipv6 because the rest of the system uses it -user_pref("network.dns.disableIPv6", true); // 0701 +user_pref("network.dns.disableIPv6", false); // 0701 // TRR only user_pref("network.trr.mode", 3); // 0710 diff --git a/home-manager/firefox/unzip_mozlz4.py b/home-manager/firefox/unzip_mozlz4.py deleted file mode 100755 index 311fd214..00000000 --- a/home-manager/firefox/unzip_mozlz4.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/python -# source: https://unix.stackexchange.com/a/497861 -# Command-line tool to decompress mozLz4 files used for example by Firefox to store various kinds of session backup information. -# Works in both Python 2.7.15 and 3.6.7, as of version 2.1.6 of the LZ4 Python bindings at pypi.org/project/lz4. -# To use in another script, simply cut and paste the import statement and the mozlz4_to_text() function (lines 8 to 17). - -import lz4.block # pip install lz4 --user - - -def mozlz4_to_text(filepath): - # Given the path to a "mozlz4", "jsonlz4", "baklz4" etc. file, - # return the uncompressed text. - bytestream = open(filepath, "rb") - bytestream.read(8) # skip past the b"mozLz40\0" header - valid_bytes = bytestream.read() - text = lz4.block.decompress(valid_bytes) - return text - - -def main(args): - # Given command-line arguments of an input filepath for a ".mozlz4" file - # and optionally an output filepath, write the decompressed text to the - # output filepath. - # Default output filepath is the input filepath minus the last three characters - # (e.g. "foo.jsonlz4" becomes "foo.json") - filepath_in = args[0] - if len(args) < 2: - filepath_out = filepath_in[:-3] - else: - filepath_out = args[1] - text = mozlz4_to_text(filepath_in) - with open(filepath_out, "wb") as outfile: - outfile.write(text) - print("Wrote decompressed text to {}".format(filepath_out)) - - -if __name__ == "__main__": - import sys - args = sys.argv[1:] - if args and not args[0] in ("--help", "-h"): - main(args) - else: - print("Usage: mozlz4.py ") -- cgit 1.4.1