From 99561277e31caeffa7fc9d722650247511ad3bf7 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Sat, 29 Aug 2026 15:44:43 +0200 Subject: Refactoring to a C Binary --- .gitignore | 9 ++------- Containerfile | 28 ---------------------------- Makefile | 32 +++++++++++++++++++++----------- container/Containerfile | 28 ++++++++++++++++++++++++++++ container/init | 10 ++++++++++ dbx | 33 --------------------------------- dbx-create | 45 --------------------------------------------- dbx-enter | 11 ----------- dbx-init | 12 ------------ dbx-proxy | 17 ----------------- dbx-start | 23 ----------------------- dbx-stop | 15 --------------- 12 files changed, 61 insertions(+), 202 deletions(-) delete mode 100644 Containerfile create mode 100644 container/Containerfile create mode 100755 container/init delete mode 100755 dbx delete mode 100755 dbx-create delete mode 100755 dbx-enter delete mode 100755 dbx-init delete mode 100755 dbx-proxy delete mode 100755 dbx-start delete mode 100755 dbx-stop diff --git a/.gitignore b/.gitignore index 0f8e6ab..33d3062 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,2 @@ -.container -custom/extra-packages -custom/post-install -custom/skel/ -ssh/*.config -ssh/*.known_hosts -ssh/id_* +dbx +src/*.o diff --git a/Containerfile b/Containerfile deleted file mode 100644 index 9f250dd..0000000 --- a/Containerfile +++ /dev/null @@ -1,28 +0,0 @@ -FROM docker.io/library/debian:13 - -# Flag that we are a devbox -RUN touch /run/.dbxenv - -# Remove container-specific APT configurations, and install required packages -RUN rm /etc/apt/apt.conf.d/docker-* && \ - apt-get update && \ - apt-get install -y \ - locales \ - openssh-server \ - sudo \ - tini \ - && \ - apt-get clean && \ - rm -dr /var/lib/apt/lists/* - -# Configure locale with UTF-8 support -RUN update-locale LANG=C.UTF-8 LC_ALL=C.UTF-8 - -# Configure SSH server -COPY ssh/id_ed25519.pub /etc/ssh/authorized_keys -RUN echo "StreamLocalBindUnlink yes" >> /etc/ssh/sshd_config && \ - echo "AuthorizedKeysFile /etc/ssh/authorized_keys" >> /etc/ssh/sshd_config - -# Set up the entrypoint -COPY dbx-init /sbin/init -ENTRYPOINT ["/sbin/init"] diff --git a/Makefile b/Makefile index 7377817..6f6f205 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,26 @@ -DBX_IMAGE ?= dbx +CC = cc +CFLAGS = -Wall -Wextra -O2 +LDFLAGS = + +SRCS = $(shell find src -name '*.c') +OBJS = $(patsubst %.c,%.o,$(SRCS)) + +CONTAINER = $(if $(findstring Darwin,$(shell uname -s)),container,podman) +IMAGE = dbx-next .PHONY: all -all: .container ssh/id_ed25519 ; +all: dbx ; + +dbx: $(OBJS) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -.PHONY: install -install: all - mkdir -p $(HOME)/.local/bin - ln -s $(PWD)/dbx $(HOME)/.local/bin/dbx +%.o: %.c src/dbx.h + $(CC) $(CFLAGS) -c -o $@ $< -.container: Containerfile dbx-init ssh/id_ed25519.pub - container build --pull --tag $(DBX_IMAGE) . - touch $@ +.PHONY: container +container: container/Containerfile container/init + $(CONTAINER) build --tag $(IMAGE) container -ssh/id_% ssh/id_%.pub: - ssh-keygen -C "$$(id -un)@$$(hostname)" -f ssh/id_$* -N "" -t $* +.PHONY: clean +clean: + rm -f dbx src/*.o diff --git a/container/Containerfile b/container/Containerfile new file mode 100644 index 0000000..2498b12 --- /dev/null +++ b/container/Containerfile @@ -0,0 +1,28 @@ +FROM docker.io/library/debian:13 + +# Flag that we are a devbox +RUN touch /run/.dbxenv + +# Remove container-specific APT configurations, and install required packages +RUN rm /etc/apt/apt.conf.d/docker-* && \ + apt-get update && \ + apt-get install -y \ + locales \ + openssh-server \ + sudo \ + tini \ + && \ + apt-get clean && \ + rm -dr /var/lib/apt/lists/* + +# Configure locale with UTF-8 support +RUN update-locale LANG=C.UTF-8 LC_ALL=C.UTF-8 + +# Configure SSH server +RUN echo "StreamLocalBindUnlink yes" >> /etc/ssh/sshd_config && \ + echo "AuthorizedKeysFile /etc/ssh/authorized_keys" >> /etc/ssh/sshd_config && \ + touch /etc/ssh/authorized_keys + +# Set up the entrypoint +COPY init /sbin/init +ENTRYPOINT ["/sbin/init"] diff --git a/container/init b/container/init new file mode 100755 index 0000000..24b6629 --- /dev/null +++ b/container/init @@ -0,0 +1,10 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello + +if [ ! -f /run/.dbxenv ]; then + echo "ERROR: not in a devbox" >&2 + exit 1 +fi + +exec /usr/bin/tini /usr/sbin/sshd -- -D -e diff --git a/dbx b/dbx deleted file mode 100755 index 3402133..0000000 --- a/dbx +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: GPL-3.0-only -# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello - -set -euo pipefail - -root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" - -usage() { - cat < - -set -euo pipefail - -root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -machine="${DBX_MACHINE:-dbx}" -image="${DBX_IMAGE:-dbx}" - -if ! container system status &>/dev/null; then - container system start >/dev/null -fi - -if container machine inspect "$machine" &>/dev/null; then - echo "already created" - exit -fi - -container machine create \ - --name="$machine" \ - --no-boot \ - --home-mount=none \ - "$image" \ - >/dev/null - -# Perform the initial setup for the devbox -"$root/dbx-start" -if [ -f "$root/custom/extra-packages" ]; then - container machine run --name="$machine" --root \ - apt-get update - container machine run --name="$machine" --root \ - apt-get install -y $(cat "$root/custom/extra-packages") -fi -if [ -d "$root/custom/skel" ]; then - home="$(container machine run --name="$machine" bash -c 'echo $HOME')" - scp -F "$root/ssh/$machine.config" -r \ - "$root/custom/skel/." "$machine:$home" -fi -if [ -x "$root/custom/post-install" ]; then - scp -F "$root/ssh/$machine.config" -r \ - "$root/custom/post-install" "$machine:/tmp/post-install" - container machine run --name="$machine" /tmp/post-install - container machine run --name="$machine" rm /tmp/post-install -fi diff --git a/dbx-enter b/dbx-enter deleted file mode 100755 index 49078b4..0000000 --- a/dbx-enter +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: GPL-3.0-only -# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello - -set -euo pipefail - -root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -machine="${DBX_MACHINE:-dbx}" - -"$root/dbx-start" -exec ssh "$machine" -F "$root/ssh/$machine.config" diff --git a/dbx-init b/dbx-init deleted file mode 100755 index 3877c92..0000000 --- a/dbx-init +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: GPL-3.0-only -# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello - -set -euo pipefail - -if [[ ! -f /run/.dbxenv ]]; then - echo "ERROR: not in a devbox" >&2 - exit 1 -fi - -exec tini /usr/sbin/sshd -- -D -e diff --git a/dbx-proxy b/dbx-proxy deleted file mode 100755 index 67e0456..0000000 --- a/dbx-proxy +++ /dev/null @@ -1,17 +0,0 @@ -#!/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 - -root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -machine="${DBX_MACHINE:-dbx}" -port="$1" - -"$root/dbx-start" -exec ssh "$machine" -F "$root/ssh/$machine.config" \ - -L "localhost:$port:localhost:$port" -N diff --git a/dbx-start b/dbx-start deleted file mode 100755 index 1b602e1..0000000 --- a/dbx-start +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: GPL-3.0-only -# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello - -set -euo pipefail - -root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -machine="${DBX_MACHINE:-dbx}" - -if ! container system status &>/dev/null; then - container system start >/dev/null -fi - -container machine run --name="$machine" true >/dev/null - -# The IP address assigned to the machine changes on restarts, so update the SSH -# configuration with the current value. -ip="$(container machine inspect "$machine" | jq -r '.[0].ipAddress')" -cat "$root/ssh/config.in" \ - | sed -e 's|$ROOT|'"$root"'|g' \ - | sed -e 's|$MACHINE|'"$machine"'|g' \ - | sed -e 's|$IP|'"$ip"'|g' \ - >"$root/ssh/$machine.config" diff --git a/dbx-stop b/dbx-stop deleted file mode 100755 index dcef4a2..0000000 --- a/dbx-stop +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: GPL-3.0-only -# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello - -set -euo pipefail - -machine="${DBX_MACHINE:-dbx}" - -if ! container system status &>/dev/null; then - exit 0 -fi - -container machine stop "$machine" &>/dev/null \ - || true -container system stop >/dev/null -- cgit v1.2.3