1{ pkgs, ... }:
2# Drop-in replacement for macOS open(1) that, inside a VS Code integrated
3# terminal, routes http(s) URLs into the built-in Simple Browser via the
4# companion `simple-browser-bridge` extension's uri handler. Every other
5# invocation (non-vscode terminal, files, apps, no `code` on PATH) falls
6# through to the real /usr/bin/open with identical semantics.
7#
8# Named `open` so it shadows the system binary on PATH and is therefore
9# honored by programs that spawn `open` directly (e.g. a dev TUI's
10# "open browser" action), not just interactive shells.
11pkgs.writeShellApplication {
12 name = "open";
13 runtimeInputs = with pkgs; [ nodejs ];
14 text = ''
15 if [ "''${TERM_PROGRAM:-}" = "vscode" ] && command -v code >/dev/null 2>&1; then
16 for arg in "$@"; do
17 case "$arg" in
18 http://* | https://*)
19 encoded=$(node -e 'process.stdout.write(encodeURIComponent(process.argv[1]))' "$arg")
20 code --open-url "vscode://clover.simple-browser-bridge/open?url=$encoded"
21 exit 0
22 ;;
23 esac
24 done
25 fi
26 exec /usr/bin/open "$@"
27 '';
28}