From ed2f2e13cded4d611a92fa228033576c4f976cfd Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Thu, 14 May 2026 12:23:01 +0200 Subject: Started Adapting for Apple Containers --- .gitignore | 2 +- Makefile | 17 ++++----------- README.md | 4 ++-- dbx | 27 ++++++++++++------------ dbx-create | 55 ++++++++++++++++++++++++++++++++---------------- dbx-enter | 20 ++++-------------- dbx-init | 32 +++------------------------- dbx-link | 30 -------------------------- dbx-proxy | 16 ++++++++++++++ dbx-proxy.go | 69 ------------------------------------------------------------ dbx-run | 35 ------------------------------ dbx-start | 8 +++++++ 12 files changed, 88 insertions(+), 227 deletions(-) delete mode 100755 dbx-link create mode 100755 dbx-proxy delete mode 100644 dbx-proxy.go delete mode 100755 dbx-run create mode 100755 dbx-start diff --git a/.gitignore b/.gitignore index 91e31d1..b0bfd82 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -dbx-proxy +ssh/id_* diff --git a/Makefile b/Makefile index 52d8950..86aa085 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,11 @@ -GO := docker run -it --rm --security-opt label=disable -v $(PWD):/dbx -w /dbx docker.io/library/golang go - .PHONY: all -all: dbx-proxy ; +all: ; + @ echo "install with 'make install'" .PHONY: install install: all mkdir -p $(HOME)/.local/bin ln -s $(PWD)/dbx $(HOME)/.local/bin/dbx -dbx-proxy: dbx-proxy.go - $(GO) build -o $@ $< - -.PHONY: fmt -fmt: - $(GO) fmt dbx-proxy.go - -.PHONY: clean -clean: - rm -f dbx-proxy +ssh/id_% ssh/id_%.pub: + ssh-keygen -C "$$(id -un)@$$(hostname)" -f ssh/id_$* -N "" -t $* diff --git a/README.md b/README.md index 013037b..a7d1faa 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Containerized Development Environments -`dbx` is a set of scripts for managing a local containerized development environment. It works by creating a long-lived container named `dbx` with only access to a development root directory, where you can install various developer tools and dependencies without losing sleep at night. +`dbx` is a set of scripts for managing a local containerized development environment. It works by creating a long-lived container named `dbx` that that has no access to the host where you can install various developer tools and dependencies without losing sleep at night. It uses Apple `container` tool, which creates a dedicated virtual machine for the `dbx` container, providing increased isolation and security. The host connects to the container via SSH, which allows any development tools with remote editing capabilities to be used (such as Emacs, Zed or Visual Studio Code). -At its core, it is extremely similar to Toolbx and Distrobox, but with as reduced permissions as possible for improved security. For example, unlike the aforementioned tools, `dbx` keeps SELinux enforcement; meaning that the `dbx` user cannot access other files if it manages to escape the namespace. In general, it does not try to create a convenient container that is seamlessly integrated with the host, but rather a restricted sandbox where you can install development CLI tools, and reduce `npm install` risk. +At its core, it is similar to Toolbx and Distrobox, but with improved isolation and security. In general, it does not try to create a convenient container that is seamlessly integrated with the host, but rather a restricted sandbox where you can install development CLI tools, and reduce `npm install` risk. diff --git a/dbx b/dbx index 7d65003..261a2a2 100755 --- a/dbx +++ b/dbx @@ -3,18 +3,17 @@ set -euo pipefail -r="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +R="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" usage() { - cat </dev/null +#if ! id $DBX_USER &>/dev/null; then +# useradd \ +# --comment "$DBX_USER_COMMENT" \ +# --create-home \ +# --groups sudo \ +# --shell /usr/bin/bash \ +# --uid 1000 \ +# --user-group \ +# $DBX_USER +#fi +# if container list --all | grep $C >/dev/null; then +# echo "already created" +# exit +# fi +# podman create \ +# --hostname dbx \ +# --label manager=dbx \ +# --name "$c" \ +# --user root:root \ +# --userns keep-id \ +# --volume "$d:$d:z" \ +# --volume "$r/dbx-init:/usr/local/bin/dbx-init:ro,z" \ +# registry.fedoraproject.org/fedora-toolbox:43 \ +# dbx-init "$(id -u)" "$(id -g)" +# podman start "$c" >/dev/null diff --git a/dbx-enter b/dbx-enter index 7852fa9..9ed911d 100755 --- a/dbx-enter +++ b/dbx-enter @@ -3,20 +3,8 @@ set -euo pipefail -c="dbx" -d="$HOME/Developer" +R="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +C="dbx" -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 +container start $C >/dev/null +exec ssh -F "$R/ssh/config" $C diff --git a/dbx-init b/dbx-init index 0cf9ac3..f0ac1c1 100755 --- a/dbx-init +++ b/dbx-init @@ -3,34 +3,8 @@ set -euo pipefail -if [[ ! -f /run/.containerenv ]]; then - echo "ERROR: not in a container" >&2 +if [[ ! -f /run/.dbxenv ]]; then + echo "ERROR: not in a devbox" >&2 fi -h="/dbx" -u="${1:-1000}" -g="${2:-1000}" - -dnf install -y \ - fish \ - fzf \ - netcat \ - ripgrep \ - tini \ - vim-enhanced - -if [[ ! -d "$h" ]]; then - usermod \ - --home "$h" \ - --groups wheel \ - --password "" \ - --shell $(command -v fish) \ - $(id -nu "$u") - - mkdir --parents "$h" - chown --recursive "$u:$g" "$h" -fi - -echo "=== READY TO ROLL! ===" -touch /run/.dbxenv -exec tini sleep -- infinity +exec tini sshd -- -D -e diff --git a/dbx-link b/dbx-link deleted file mode 100755 index cab4ecb..0000000 --- a/dbx-link +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: GPL-3.0-only - -set -euo pipefail - -if [[ $# -ne 1 ]]; then - echo "ERROR: missing command to link" >&2 - exit 1 -fi - -r="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -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 b/dbx-proxy new file mode 100755 index 0000000..2eae026 --- /dev/null +++ b/dbx-proxy @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: GPL-3.0-only + +set -euo pipefail + +if [[ $# -ne 1 ]]; then + echo "ERROR: missing port to proxy" >&2 + exit 1 +fi + +R="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +C="dbx" + +port="$1" +container start "$C" >/dev/null +exec ssh -F "$R/ssh/config" -L "localhost:$port:localhost:$port" -N $C diff --git a/dbx-proxy.go b/dbx-proxy.go deleted file mode 100644 index 84688af..0000000 --- a/dbx-proxy.go +++ /dev/null @@ -1,69 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-only -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 deleted file mode 100755 index 1afec9c..0000000 --- a/dbx-run +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: GPL-3.0-only - -set -euo pipefail - -if [[ $# -lt 1 ]]; then - echo "ERROR: missing command to run" >&2 - exit 1 -fi - -c="dbx" -d="$HOME/Developer" - -workdir="$(realpath "$PWD")" -if [[ "$workdir" != "$d"* ]]; then - workdir="$d" -fi - -tty="tty" -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)" \ - --workdir "$workdir" \ - "$c" \ - fish -c "exec '$cmd'"' $argv' -- "$@" diff --git a/dbx-start b/dbx-start new file mode 100755 index 0000000..b333fe7 --- /dev/null +++ b/dbx-start @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: GPL-3.0-only + +set -euo pipefail + +C="dbx" + +container start $C >/dev/null -- cgit v1.2.3