From fd9b0ecef4142a62b45404700ba1cff488f84a73 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Thu, 23 May 2024 13:31:11 +0200 Subject: refactor(modules/home): Setup as "normal" NixOS module --- modules/home/conf/firefox/scripts/default.nix | 29 ++++++++ .../home/conf/firefox/scripts/extract_cookies.sh | 77 ++++++++++++++++++++++ modules/home/conf/firefox/scripts/unzip_mozlz4.py | 44 +++++++++++++ .../home/conf/firefox/scripts/update_extensions.sh | 18 +++++ 4 files changed, 168 insertions(+) create mode 100644 modules/home/conf/firefox/scripts/default.nix create mode 100755 modules/home/conf/firefox/scripts/extract_cookies.sh create mode 100755 modules/home/conf/firefox/scripts/unzip_mozlz4.py create mode 100755 modules/home/conf/firefox/scripts/update_extensions.sh (limited to 'modules/home/conf/firefox/scripts') diff --git a/modules/home/conf/firefox/scripts/default.nix b/modules/home/conf/firefox/scripts/default.nix new file mode 100644 index 00000000..1127662b --- /dev/null +++ b/modules/home/conf/firefox/scripts/default.nix @@ -0,0 +1,29 @@ +{ + pkgs, + sysLib, + ... +}: let + unzip_mozlz4 = pkgs.stdenv.mkDerivation { + name = "unzip_mozlz4"; + propagatedBuildInputs = [ + (pkgs.python3.withPackages (pythonPackages: + with pythonPackages; [ + lz4 + ])) + ]; + dontUnpack = true; + installPhase = "install -Dm755 ${./unzip_mozlz4.py} $out/bin/unzip_mozlz4"; + }; + extract_cookies = sysLib.writeShellScript { + name = "extract_cookies"; + src = ./extract_cookies.sh; + dependencies = with pkgs; [ + bash + sqlite + mktemp + coreutils + ]; + }; +in { + inherit unzip_mozlz4 extract_cookies; +} diff --git a/modules/home/conf/firefox/scripts/extract_cookies.sh b/modules/home/conf/firefox/scripts/extract_cookies.sh new file mode 100755 index 00000000..e3d50d43 --- /dev/null +++ b/modules/home/conf/firefox/scripts/extract_cookies.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +# copied from https://superuser.com/a/1239036 +# extract_cookies.sh: +# +# Convert from Firefox's cookies.sqlite format to Netscape cookies, +# which can then be used by wget and curl. (Why don't wget and curl +# just use libsqlite if it's installed? Mysteries abound.) +# +# Note: This script reads directly from the standard cookie jar file, +# which means cookies which are kept only in memory ("session cookies") +# will not be extracted. You will need an extension to do that. + +# USAGE: +# +# $ extract_cookies.sh > /tmp/cookies.txt +# or +# $ extract_cookies.sh ~/.mozilla/firefox/*default*/cookies.sqlite > /tmp/cookies.txt + +# USING WITH WGET: +# $ wget --load-cookies=/tmp/cookies.txt http://example.com + +# USING WITH CURL: +# $ curl --cookie /tmp/cookies.txt http://example.com + +# Note: If you do not specify an SQLite filename, this script will +# intelligently find it for you. +# +# A) Usually it will check all profiles under ~/.mozilla/firefox/ and +# use the cookies.sqlite that was updated most recently. +# +# B) If you've redirected stdin (with < or |) , then that will be used. + +# HISTORY: I believe this is circa 2010 from: +# http://slacy.com/blog/2010/02/using-cookies-sqlite-in-wget-or-curl/ +# However, that site is down now. + +# Cleaned up by Hackerb9 (2017) to be more robust and require less typing. + +cleanup() { + rm -f "$TMPFILE" + exit 0 +} +trap cleanup EXIT INT QUIT TERM + +if [ "$#" -ge 1 ]; then + SQLFILE="$1" +else + SQLFILE="$HOME/.mozilla/firefox/default/cookies.sqlite" +fi + +if ! [ -r "$SQLFILE" ]; then + echo "Error. File $SQLFILE is not readable." >&2 + exit 1 +fi + +# We have to copy cookies.sqlite, because FireFox has a lock on it +TMPFILE=$(mktemp /tmp/cookies.sqlite.XXXXXXXXXX) +cat "$SQLFILE" >>"$TMPFILE" + +# This is the format of the sqlite database: +# CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT, path TEXT,expiry INTEGER, lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER); + +echo "# Netscape HTTP Cookie File" +sqlite3 -separator $'\t' "$TMPFILE" < ") diff --git a/modules/home/conf/firefox/scripts/update_extensions.sh b/modules/home/conf/firefox/scripts/update_extensions.sh new file mode 100755 index 00000000..86bd843c --- /dev/null +++ b/modules/home/conf/firefox/scripts/update_extensions.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +tmp=$(mktemp) +cat <"$tmp" + darkreader:navbar + keepassxc-browser:navbar + vhack-libredirect:navbar + torproject-snowflake:navbar + tridactyl-vim:menupanel + ublock-origin:menupanel +EOF + +# The bin is provided in the devshell; +# The cat execution should be unquoted; +# shellcheck disable=SC2046 +generate_extensions $(cat "$tmp") >"$(dirname "$0")"/../config/extensions/extensions.json + +rm "$tmp" -- cgit 1.4.1