From 26f172dd393ec3408312e7bcd9cdf04a666c1f29 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Mon, 23 Mar 2026 10:22:06 +0100 Subject: Link Devbox Commands and Proxy TCP Connections Improve DX by adding a utility to create linked executables that run in a devbox (for example, editors can natively run LSP servers) and proxy container TCP connections to the host. The proxying works over stdin and stdout, so it does not require the container to publish ports (and know what ports need to be published beforehand). It was written in Go (since writing this in Bash was too unruly). I want to port the whole project to OCaml once I have time :) --- .gitignore | 1 + dbx | 6 ++++-- dbx-create | 2 +- dbx-enter | 18 ++++++++++++++-- dbx-link | 30 ++++++++++++++++++++++++++ dbx-proxy.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dbx-run | 19 ++++++++++------- 7 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 .gitignore create mode 100755 dbx-link create mode 100644 dbx-proxy.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..91e31d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dbx-proxy diff --git a/dbx b/dbx index a5b7327..5c2bd1f 100755 --- a/dbx +++ b/dbx @@ -11,15 +11,17 @@ dbx - containerized development environments USAGE: dbx create creates a devbox if one does not already exist dbx [enter] enters the devbox in the current directory + dbx link CMD creates a host link to a devbox command + dbx proxy PORT proxies a container port to the host dbx run CMD [ARGS...] runs a command within the devbox EOF } cmd="${1:-enter}" -shift +shift || true case "$#:$cmd" in - 0:create|0:enter|[1-9]:run|[1-9][0-9]:run) + 0:create|0:enter|1:link|1:proxy|[1-9]:run|[1-9][0-9]:run) "$r/dbx-$cmd" "$@" ;; *) diff --git a/dbx-create b/dbx-create index 8a35097..a87a77b 100755 --- a/dbx-create +++ b/dbx-create @@ -7,7 +7,7 @@ r="$HOME/.dbx" c="dbx" d="$HOME/Developer/dbxroot" -if podman container exists dbx; then +if podman container exists "$c"; then echo "already created" exit fi diff --git a/dbx-enter b/dbx-enter index f4661d9..9095c9b 100755 --- a/dbx-enter +++ b/dbx-enter @@ -3,6 +3,20 @@ set -euo pipefail -r="$HOME/.dbx" +c="dbx" +d="$HOME/Developer/dbxroot" -"$r/dbx-run" fish --login +workdir="$(realpath "$PWD")" +if [[ "$workdir" != "$d"* ]]; then + workdir="$d" +fi + +podman start "$c" >/dev/null +exec podman exec \ + --detach-keys "" \ + --interactive \ + --tty \ + --user "$(id -u)" \ + --workdir "$workdir" \ + "$c" \ + fish --login diff --git a/dbx-link b/dbx-link new file mode 100755 index 0000000..297ba02 --- /dev/null +++ b/dbx-link @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: 0BSD + +set -euo pipefail + +if [[ $# -ne 1 ]]; then + echo "ERROR: missing command to link" >&2 + exit 1 +fi + +r="$HOME/.dbx" +c="dbx" +p="$HOME/.local/share/dbx/bin" + +cmd="$1" +link="$p/$cmd" +if [[ -e "$link" ]]; then + echo "already linked" + exit +fi + +if ! "$r/dbx-run" which "$cmd" &>/dev/null; then + echo "ERROR: command '$cmd' not found" + exit 1 +fi + +mkdir -p "$p" +echo "#!/bin/sh" > "$link" +echo "exec '$r/dbx-run' '$cmd' "'"$@"' >> "$link" +chmod +x "$link" diff --git a/dbx-proxy.go b/dbx-proxy.go new file mode 100644 index 0000000..6e5048a --- /dev/null +++ b/dbx-proxy.go @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: 0BSD +package main + +import ( + "bufio" + "log" + "net" + "os" + "os/exec" +) + +const C string = "dbx" + +func main() { + if len(os.Args) != 2 { + log.Fatal("ERROR: invalid command arguments") + } + + port := os.Args[1] + addr := "localhost:" + port + conn, err := net.Listen("tcp", addr) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + for { + client, err := conn.Accept() + if err != nil { + log.Fatal(err) + } + go handleClient(client, port) + } +} + +func handleClient(client net.Conn, port string) error { + log.Print("connected ", client.RemoteAddr()) + defer client.Close() + + netcat := exec.Command("podman", "exec", "--interactive", "--user=1000", C, "netcat", "-N", "localhost", port) + netcat.Stdin = client + netcat.Stdout = client + + stderr, err := netcat.StderrPipe() + if err != nil { + log.Print(err) + return err + } + defer stderr.Close() + + if err := netcat.Start(); err != nil { + log.Print(err) + return err + } + + scanner := bufio.NewScanner(stderr) + for scanner.Scan() { + log.Print("netcat 2>", scanner.Text()) + } + + client.Close() + if err := netcat.Wait(); err != nil { + log.Print(err) + return err + } + + log.Print("disconnected ", client.RemoteAddr()) + return nil +} diff --git a/dbx-run b/dbx-run index b8a2e52..2e3535d 100755 --- a/dbx-run +++ b/dbx-run @@ -11,22 +11,25 @@ fi c="dbx" d="$HOME/Developer/dbxroot" -w="$(realpath "$PWD")" -if [[ "$w" != "$d"* ]]; then - w="$d" +workdir="$(realpath "$PWD")" +if [[ "$workdir" != "$d"* ]]; then + workdir="$d" fi tty="tty" -if [[ ! -t 0 ]]; then +if [[ ! -t 0 || ! -t 1 ]]; then tty="tty=false" fi +cmd="$1" +shift + podman start "$c" >/dev/null exec podman exec \ --detach-keys "" \ --interactive \ --"$tty" \ - --user "$(id -u):$(id -g)" \ - --workdir "$w" \ - dbx \ - "$@" + --user "$(id -u)" \ + --workdir "$workdir" \ + "$c" \ + fish -c "exec '$cmd'"' $argv' -- "$@" -- cgit v1.2.3