diff options
| author | Arpit Chakladar <arpitchakladar+git@gmail.com> | 2026-08-13 21:21:02 +0530 |
|---|---|---|
| committer | Arpit Chakladar <arpitchakladar+git@gmail.com> | 2026-08-29 03:50:26 +0530 |
| commit | 3f281a9e5ed725050e4f7ae05d740fb78ea42a72 (patch) | |
| tree | 41a66e3dfca788cd4790eb7a22e4ea080340c270 /modules/web/chromium | |
| parent | 8ab2eadd2c079b0e0b648b48d08d4d6b7d1ba2ab (diff) | |
| download | home-manager-config-3f281a9e5ed725050e4f7ae05d740fb78ea42a72.tar.gz home-manager-config-3f281a9e5ed725050e4f7ae05d740fb78ea42a72.zip | |
refactor: using python scripts for preparing extensions for chromium
Diffstat (limited to 'modules/web/chromium')
| -rw-r--r-- | modules/web/chromium/browserpass.nix | 7 | ||||
| -rw-r--r-- | modules/web/chromium/extensions/inject_extension_key.py | 26 | ||||
| -rw-r--r-- | modules/web/chromium/extensions/lib.nix | 33 | ||||
| -rw-r--r-- | modules/web/chromium/patch_native_host.py | 21 |
4 files changed, 57 insertions, 30 deletions
diff --git a/modules/web/chromium/browserpass.nix b/modules/web/chromium/browserpass.nix index 6bef374..9186a7d 100644 --- a/modules/web/chromium/browserpass.nix +++ b/modules/web/chromium/browserpass.nix @@ -9,12 +9,13 @@ let patchedHost = pkgs.runCommand "com.github.browserpass.native.json" { - nativeBuildInputs = [ pkgs.jq ]; + nativeBuildInputs = [ pkgs.python3 ]; } '' - jq '.allowed_origins = ["chrome-extension://${config.web.chromium.extensions.browserpass.id}/"]' \ + python3 ${./patch_native_host.py} \ + ${config.web.chromium.extensions.browserpass.id} \ ${config.programs.browserpass.package}/lib/browserpass/hosts/chromium/com.github.browserpass.native.json \ - > $out + $out ''; in { diff --git a/modules/web/chromium/extensions/inject_extension_key.py b/modules/web/chromium/extensions/inject_extension_key.py new file mode 100644 index 0000000..f1b7055 --- /dev/null +++ b/modules/web/chromium/extensions/inject_extension_key.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import json +import re +import sys + +manifest_path = sys.argv[1] +ext_key = sys.argv[2] + +with open(manifest_path, "r", encoding="utf-8", errors="ignore") as f: + content = f.read() + +pattern = r"(\"[^\"]*?\")|//.*?$|/\*.*?\*/" +clean = re.sub( + pattern, + lambda m: m.group(1) if m.group(1) else "", + content, + flags=re.DOTALL | re.MULTILINE, +) + +clean = re.sub(r",\s*([\]}])", r"\1", clean) + +data = json.loads(clean) +data["key"] = ext_key + +with open(manifest_path, "w", encoding="utf-8") as f: + json.dump(data, f, indent=2) diff --git a/modules/web/chromium/extensions/lib.nix b/modules/web/chromium/extensions/lib.nix index 1197868..fed6846 100644 --- a/modules/web/chromium/extensions/lib.nix +++ b/modules/web/chromium/extensions/lib.nix @@ -201,33 +201,12 @@ rec { # Inject the public key into manifest.json if provided ${lib.optionalString (extensionKey != null) '' - if [ -f "$out/manifest.json" ]; then - echo "Injecting extension key into manifest.json to lock the extension ID..." - python3 -c ' - import json, sys, re - - manifest_path = sys.argv[1] - ext_key = sys.argv[2] - - with open(manifest_path, "r", encoding="utf-8", errors="ignore") as f: - content = f.read() - - # 1. Strip JS comments without corrupting URLs inside strings - pattern = r"(\"[^\"]*?\")|//.*?$|/\*.*?\*/" - clean = re.sub(pattern, lambda m: m.group(1) if m.group(1) else "", content, flags=re.DOTALL | re.MULTILINE) - - # 2. Strip trailing commas in JSON objects/arrays - clean = re.sub(r",\s*([\]}])", r"\1", clean) - - data = json.loads(clean) - data["key"] = ext_key - - with open(manifest_path, "w", encoding="utf-8") as f: - json.dump(data, f, indent=2) - ' "$out/manifest.json" "${extensionKey}" - else - echo "Warning: No manifest.json found in $out to inject the key!" >&2 - fi + if [ -f "$out/manifest.json" ]; then + echo "Injecting extension key into manifest.json to lock the extension ID..." + python3 ${./inject_extension_key.py} "$out/manifest.json" "${extensionKey}" + else + echo "Warning: No manifest.json found in $out to inject the key!" >&2 + fi ''} runHook postBuild diff --git a/modules/web/chromium/patch_native_host.py b/modules/web/chromium/patch_native_host.py new file mode 100644 index 0000000..38afb44 --- /dev/null +++ b/modules/web/chromium/patch_native_host.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +import argparse +import json + +parser = argparse.ArgumentParser( + description="Set allowed_origins in a native messaging host manifest" +) +parser.add_argument("extension_id", help="Chrome extension ID") +parser.add_argument("input", help="Path to the input JSON manifest") +parser.add_argument("output", help="Path to write the patched JSON manifest") +args = parser.parse_args() + +with open(args.input) as f: + manifest = json.load(f) + +origin = f"chrome-extension://{args.extension_id}/" +manifest["allowed_origins"] = [origin] + +with open(args.output, "w") as f: + json.dump(manifest, f, indent=2) + f.write("\n") |
