diff options
| -rw-r--r-- | .gitignore | 7 | ||||
| -rw-r--r-- | Containerfile | 59 | ||||
| -rw-r--r-- | Makefile | 11 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | custom/README.md | 10 | ||||
| -rwxr-xr-x | dbx | 9 | ||||
| -rwxr-xr-x | dbx-create | 38 | ||||
| -rwxr-xr-x | dbx-enter | 9 | ||||
| -rwxr-xr-x | dbx-init | 1 | ||||
| -rwxr-xr-x | dbx-proxy | 16 | ||||
| -rwxr-xr-x | dbx-start | 15 | ||||
| -rwxr-xr-x | dbx-stop | 15 | ||||
| -rw-r--r-- | ssh/config.in | 10 |
13 files changed, 96 insertions, 106 deletions
@@ -1,5 +1,6 @@ .container -custom/* -!custom/README.md +custom/extra-packages +custom/post-install +custom/skel/ +ssh/*.config ssh/id_* -ssh/config diff --git a/Containerfile b/Containerfile index 84ba12e..24698e9 100644 --- a/Containerfile +++ b/Containerfile @@ -1,8 +1,5 @@ FROM docker.io/library/debian:13 -ARG USER=dbx -EXPOSE 22/tcp - # Flag that we are a devbox RUN touch /run/.dbxenv @@ -10,62 +7,18 @@ RUN touch /run/.dbxenv RUN rm /etc/apt/apt.conf.d/docker-* && \ apt-get update && \ apt-get install -y \ - bash-completion \ - bzip2 \ - curl \ - git \ - gnupg \ - htop \ - less \ - man-db \ - manpages \ - nano \ - openssh-client \ openssh-server \ - procps \ sudo \ tini \ - tree \ - vim \ - wget \ - zip \ && \ apt-get clean && \ rm -dr /var/lib/apt/lists/* -# Passwordless sudo -RUN sed -i -e 's/ ALL$/ NOPASSWD:ALL/' /etc/sudoers - -# Configure automatic removal of stale forwarded sockets -RUN echo "StreamLocalBindUnlink yes" >> /etc/ssh/sshd_config - -# Create and configure the devbox user -RUN useradd \ - --create-home \ - --groups sudo \ - --shell /usr/bin/bash \ - --uid 1000 \ - --user-group \ - ${USER} - -# Configure SSH login -RUN mkdir -m 700 /home/${USER}/.ssh && chown 1000:1000 /home/${USER}/.ssh -COPY --chmod=600 --chown=1000:1000 ssh/id_ed25519.pub /home/${USER}/.ssh/authorized_keys - -# User customizations -COPY --chown=1000:1000 custom/ /home/${USER}/ -RUN rm /home/${USER}/README.md -RUN if [ -f /home/${USER}/extra-packages ]; then \ - apt-get update && \ - apt-get install -y $(cat /home/${USER}/extra-packages | xargs) && \ - apt-get clean && \ - rm -dr /var/lib/apt/lists/* /home/${USER}/extra-packages; \ - fi -RUN if [ -x /home/${USER}/post-install ]; then \ - su - ${USER} /home/${USER}/post-install && \ - rm /home/${USER}/post-install; \ - fi +# 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 /usr/local/bin/dbx-init -ENTRYPOINT ["dbx-init"] +COPY dbx-init /sbin/init +ENTRYPOINT ["/sbin/init"] @@ -1,5 +1,7 @@ +DBX_IMAGE ?= dbx + .PHONY: all -all: .container ssh/config ; +all: .container ssh/id_ed25519 ; .PHONY: install install: all @@ -7,13 +9,8 @@ install: all ln -s $(PWD)/dbx $(HOME)/.local/bin/dbx .container: Containerfile dbx-init ssh/id_ed25519.pub - container build --build-arg USER=$(USER) --pull --tag dbx . + container build --pull --tag $(DBX_IMAGE) . touch $@ -ssh/config: ssh/config.in ssh/id_ed25519.pub - cat $< \ - | sed -e 's|$$PWD|$(PWD)|' \ - > $@ - ssh/id_% ssh/id_%.pub: ssh-keygen -C "$$(id -un)@$$(hostname)" -f ssh/id_$* -N "" -t $* @@ -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` 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). +`dbx` is a set of scripts for managing a local containerized development environment. It works by creating a long-lived container machine 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 machine, 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 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/custom/README.md b/custom/README.md index 04d6b03..5d9a8a5 100644 --- a/custom/README.md +++ b/custom/README.md @@ -1,11 +1,15 @@ # Customizations -This directory includes customizations that are not tracked by Git. All files in this directory will be copied over the devbox user's home directory (overwriting any files that may exist there from `/etc/skel`), allowing for easy customization of the devbox user. +This directory includes customizations that are not tracked by Git that can be used to configure the devbox on its first run. + +## Skeleton Directory + +All files in the `skel` directory will be copied over the devbox user's home directory (overwriting any files that may exist there from `/etc/skel`), allowing for easy customization of the devbox user. ## Extra Packages -A special `extra-packages` file can be added to this directory to include additional packages to install when building the container. +A special `extra-packages` file can be added to this directory to include additional packages to install on first run. ## Post Installation Script -A special `post-install` executable can be added to this directory which will be run as a final step when building the container. This allows you to include a one-time setup with the development container image. The post install script is run as the devbox user (with `sudo` access). +A special `post-install` executable can be added to this directory which will be run as a final step when creating the devbox. The post install script is run as the devbox user (with passwordless `sudo` access). @@ -1,9 +1,10 @@ #!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> set -euo pipefail -R="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" usage() { cat <<EOF @@ -12,8 +13,8 @@ dbx - containerized development environments USAGE: dbx create creates the devbox dbx [enter] enters the devbox - dbx proxy PORT proxies a devbox port to the host dbx start starts the devbox + dbx stop stops the devbox EOF } @@ -21,8 +22,8 @@ cmd="${1:-enter}" shift || true case "$#:$cmd" in - 0:create|0:enter|1:proxy|0:start) - "$R/dbx-$cmd" "$@" + 0:create|0:enter|0:start|0:stop) + "$root/dbx-$cmd" "$@" ;; *) usage @@ -1,23 +1,45 @@ #!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> set -euo pipefail -C="dbx" +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 list --all | grep $C >/dev/null; then +if container machine inspect "$machine" &>/dev/null; then echo "already created" exit fi -container create \ - --cpus 4 \ - --memory 8G \ - --name $C \ - --publish 127.0.0.1:4222:22 \ - $C \ +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 @@ -1,10 +1,11 @@ #!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> set -euo pipefail -R="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -C="dbx" +root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +machine="${DBX_MACHINE:-dbx}" -"$R/dbx-start" -exec ssh -F "$R/ssh/config" $C +"$root/dbx-start" +exec ssh -F "$root/ssh/$machine.config" "$machine" @@ -1,5 +1,6 @@ #!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> set -euo pipefail diff --git a/dbx-proxy b/dbx-proxy deleted file mode 100755 index b889f7f..0000000 --- a/dbx-proxy +++ /dev/null @@ -1,16 +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 - -R="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -C="dbx" - -port="$1" -"$R/dbx-start" -exec ssh -F "$R/ssh/config" -L "localhost:$port:localhost:$port" -N $C @@ -1,12 +1,23 @@ #!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> set -euo pipefail -C="dbx" +root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +machine="${DBX_MACHINE:-dbx}" if ! container system status &>/dev/null; then container system start >/dev/null fi -container start $C >/dev/null +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 new file mode 100755 index 0000000..dcef4a2 --- /dev/null +++ b/dbx-stop @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> + +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 diff --git a/ssh/config.in b/ssh/config.in index 6e0e420..294754e 100644 --- a/ssh/config.in +++ b/ssh/config.in @@ -1,6 +1,6 @@ -Host dbx - HostName localhost - Port 4222 +Host $MACHINE + HostName $IP IdentitiesOnly yes - IdentityFile $PWD/ssh/id_ed25519 - NoHostAuthenticationForLocalhost yes + IdentityFile $ROOT/ssh/id_ed25519 + StrictHostKeyChecking no + UserKnownHostsFile /dev/null |
