diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml new file mode 100644 index 0000000..503e062 --- /dev/null +++ b/.gitea/workflows/build.yaml @@ -0,0 +1,105 @@ +name: Build containers +on: + workflow_dispatch: + push: + branches: ["main"] + paths: ["containers/**", ".gitea/workflows/build.yaml"] + pull_request: + branches: ["main"] + paths: ["containers/**", ".gitea/workflows/build.yaml"] + +jobs: + changes: + runs-on: job-v2 + outputs: + any_change: ${{ steps.filter.outputs.workflow == 'true' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'}} + base: ${{ steps.filter.outputs.base == 'true' }} + steps: + - uses: actions/checkout@v4 + - uses: https://github.com/dorny/paths-filter@v3 + id: filter + with: + filters: | + workflow: ['.gitea/workflows/build.yaml'] + base: ['containers/base/**'] + + base-image: + needs: [changes] + if: ${{ needs.changes.outputs.base == 'true' || needs.changes.outputs.any_change == 'true' }} + runs-on: job-v2 + steps: + - uses: actions/checkout@v4 + - name: Log in + uses: redhat-actions/podman-login@v1 + with: + registry: git.plabble.org + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_TOKEN }} + - name: Build + id: build + uses: job79/buildah-build@65b3793a1370c1ccd74a5c0d090d70eb9637a4ef + with: + image: maurice/base + tags: ${{ github.ref_name }} + context: ./containers/base + containerfiles: ./containers/base/Containerfile + - name: Push + uses: redhat-actions/push-to-registry@v2 + with: + image: ${{ steps.build.outputs.image }} + tags: ${{ steps.build.outputs.tags }} + registry: git.plabble.org + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_TOKEN }} + extra-args: | + --compression-format=zstd + --compression-level=12 + + # dependent-images: + # needs: [changes, base-image] + # if: always() && needs.changes.result == 'success' && (needs.base-image.result == 'success' || needs.base-image.result == 'skipped') + # runs-on: job-v2 + # strategy: + # fail-fast: false + # matrix: + # container: [rust, dotnet, go] + # steps: + # - name: Check if build needed + # id: check + # run: | + # if [[ "${{ matrix.container }}" == "go" && "${{ needs.changes.outputs.go }}" == "true" ]]; then echo "run=true" >> $GITHUB_OUTPUT; fi + # if [[ "${{ matrix.container }}" == "rust" && "${{ needs.changes.outputs.rust }}" == "true" ]]; then echo "run=true" >> $GITHUB_OUTPUT; fi + # if [[ "${{ matrix.container }}" == "dotnet" && "${{ needs.changes.outputs.dotnet }}" == "true" ]]; then echo "run=true" >> $GITHUB_OUTPUT; fi + # if [[ "${{ needs.changes.outputs.any_change }}" == "true" ]]; then echo "run=true" >> $GITHUB_OUTPUT; fi + # - name: Clone repo + # if: steps.check.outputs.run == 'true' + # uses: actions/checkout@v4 + # - name: Log in + # if: steps.check.outputs.run == 'true' + # uses: redhat-actions/podman-login@v1 + # with: + # registry: git.plabble.org + # username: ${{ secrets.REGISTRY_USERNAME }} + # password: ${{ secrets.REGISTRY_TOKEN }} + # - name: Build + # id: build + # if: steps.check.outputs.run == 'true' + # uses: job79/buildah-build@65b3793a1370c1ccd74a5c0d090d70eb9637a4ef + # with: + # image: maurice/${{ matrix.container }} + # tags: ${{ github.ref_name }} + # context: ./containers/${{ matrix.container }} + # containerfiles: ./containers/${{ matrix.container }}/Containerfile + # build-args: TAG=${{ github.ref_name }} + # - name: Push + # if: steps.check.outputs.run == 'true' + # uses: redhat-actions/push-to-registry@v2 + # with: + # image: ${{ steps.build.outputs.image }} + # tags: ${{ steps.build.outputs.tags }} + # registry: git.plabble.org + # username: ${{ secrets.REGISTRY_USERNAME }} + # password: ${{ secrets.REGISTRY_TOKEN }} + # extra-args: | + # --compression-format=zstd + # --compression-level=12 diff --git a/.gitea/workflows/fedora.yaml b/.gitea/workflows/fedora.yaml deleted file mode 100644 index 90b73cb..0000000 --- a/.gitea/workflows/fedora.yaml +++ /dev/null @@ -1,60 +0,0 @@ -name: Build container -on: - push: - branches: ["main"] - paths: - - "containers/fedora/**" - - "containers/go/**" - - "containers/infra/**" - - ".gitea/workflows/fedora.yaml" - pull_request: - branches: ["main"] - paths: - - "containers/fedora/**" - - "containers/go/**" - - "containers/infra/**" - - ".gitea/workflows/fedora.yaml" - schedule: - - cron: "0 16 * * FRI" - -jobs: - fedora-build: - runs-on: job-latest - steps: - - name: Clone repo - uses: actions/checkout@v4 - - name: Setup Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Login to Registry - uses: docker/login-action@v3 - with: - registry: git.plabble.org - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_TOKEN }} - - name: Build and push fedora container - uses: docker/build-push-action@v5 - with: - context: ./containers/fedora - file: ./containers/fedora/Containerfile - push: true - tags: git.plabble.org/job79/fedora:${{ github.ref_name }} - build-args: TAG=${{ github.ref_name }} - outputs: type=image,oci-mediatypes=true,compression=zstd,compression-level=3,force-compression=true - - name: Build and push go container - uses: docker/build-push-action@v5 - with: - context: ./containers/go - file: ./containers/go/Containerfile - push: true - tags: git.plabble.org/job79/go:${{ github.ref_name }} - build-args: TAG=${{ github.ref_name }} - outputs: type=image,oci-mediatypes=true,compression=zstd,compression-level=3,force-compression=true - - name: Build and push infra container - uses: docker/build-push-action@v5 - with: - context: ./containers/infra - file: ./containers/infra/Containerfile - push: true - tags: git.plabble.org/job79/infra:${{ github.ref_name }} - build-args: TAG=${{ github.ref_name }} - outputs: type=image,oci-mediatypes=true,compression=zstd,compression-level=3,force-compression=true diff --git a/.gitea/workflows/kali.yaml b/.gitea/workflows/kali.yaml deleted file mode 100644 index 5d8e2cc..0000000 --- a/.gitea/workflows/kali.yaml +++ /dev/null @@ -1,37 +0,0 @@ -name: Build container -on: - push: - branches: ["main"] - paths: - - "containers/kali/**" - - ".gitea/workflows/kali.yaml" - pull_request: - branches: ["main"] - paths: - - "containers/kali/**" - - ".gitea/workflows/kali.yaml" - schedule: - - cron: "0 16 * * FRI" - -jobs: - kali-build: - runs-on: job-latest - steps: - - name: Clone repo - uses: actions/checkout@v4 - - name: Setup Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Login to Registry - uses: docker/login-action@v3 - with: - registry: git.plabble.org - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_TOKEN }} - - name: Build and push kali container - uses: docker/build-push-action@v5 - with: - context: ./containers/kali - file: ./containers/kali/Containerfile - push: true - tags: git.plabble.org/job79/kali:${{ github.ref_name }} - outputs: type=image,oci-mediatypes=true,compression=zstd,compression-level=3,force-compression=true diff --git a/.gitea/workflows/other.yaml b/.gitea/workflows/other.yaml new file mode 100644 index 0000000..6b15d84 --- /dev/null +++ b/.gitea/workflows/other.yaml @@ -0,0 +1,40 @@ +name: Build optional dev containers +on: + workflow_dispatch: + +jobs: + fedora-build: + runs-on: job-v2 + strategy: + max-parallel: 1 + matrix: + container: [rider] + steps: + - name: Clone repo + uses: actions/checkout@v4 + - name: Log in + uses: redhat-actions/podman-login@v1 + with: + registry: git.plabble.org + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_TOKEN }} + - name: Build ${{ matrix.container }} container + id: build_image + uses: job79/buildah-build@65b3793a1370c1ccd74a5c0d090d70eb9637a4ef + with: + image: maurice/${{ matrix.container }} + tags: ${{ github.ref_name }} + context: ./containers/${{ matrix.container }} + containerfiles: ./containers/${{ matrix.container }}/Containerfile + build-args: TAG=${{ github.ref_name }} + - name: Push ${{ matrix.container }} container + uses: redhat-actions/push-to-registry@v2 + with: + image: ${{ steps.build_image.outputs.image }} + tags: ${{ steps.build_image.outputs.tags }} + registry: git.plabble.org + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_TOKEN }} + extra-args: | + --compression-format=zstd + --compression-level=12 diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..4e56e4d --- /dev/null +++ b/build.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cd container && \ +podman build -t git.plabble.org/maurice/devc:main . \ No newline at end of file diff --git a/container/Containerfile b/container/Containerfile new file mode 100644 index 0000000..5d5a7ff --- /dev/null +++ b/container/Containerfile @@ -0,0 +1,107 @@ +FROM quay.io/fedora/fedora:44 + +# === install system packages === +RUN --mount=type=cache,id=dnf-cache,target=/var/cache/libdnf5 \ + dnf update -y && \ + dnf -y --setopt=keepcache=1 --setopt=install_weak_deps=False install \ + bash-completion git-core curl awk jq rg zip unzip which \ + host-spawn wl-copy gcc helix helix-themes rsync python3 python3-pip dotnet-sdk-10.0 openssl-devel + +# === setup user === +RUN useradd -ms /bin/bash user && \ + echo 'user ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/user && \ + mkdir -p /run/user/1000 && \ + chown user:user /run/user/1000 + +# === Add host entry for podman & XDG open === +RUN ln -s /usr/local/bin/host /usr/local/bin/podman && \ + ln -s /usr/local/bin/host /usr/local/bin/xdg-open + +COPY config/bin /usr/local/bin + +# === languages & SDKs === + +# Golang +RUN GO_VERSION=$(curl -sL "https://go.dev/VERSION?m=text" | head -n 1 | tr -d '\r\n') && \ + curl -sL "https://go.dev/dl/${GO_VERSION}.linux-amd64.tar.gz" | tar xz -C /usr/local && \ + ln -sf /usr/local/go/bin/go /usr/local/bin/go && \ + ln -sf /usr/local/go/bin/gofmt /usr/local/bin/gofmt && \ + export GOPATH=/usr/local/go && \ + go install golang.org/x/tools/gopls@latest && \ + ln -sf /usr/local/go/bin/gopls /usr/local/bin/gopls + +# Deno +RUN export DENO_INSTALL=/usr/local/deno && \ + curl -fsSL https://deno.land/install.sh | sh && \ + ln -sf /usr/local/deno/bin/deno /usr/local/bin/deno + +# Rust +RUN export RUSTUP_HOME=/usr/local/rust && \ + export CARGO_HOME=/usr/local/rust && \ + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path && \ + ln -sf /usr/local/rust/bin/rustc /usr/local/bin/rustc && \ + ln -sf /usr/local/rust/bin/cargo /usr/local/bin/cargo && \ + ln -sf /usr/local/rust/bin/rustup /usr/local/bin/rustup && \ + ln -sf /usr/local/rust/bin/rustfmt /usr/local/bin/rustfmt && \ + ln -sf /usr/local/rust/bin/clippy /usr/local/bin/clippy && \ + ln -sf /usr/local/rust/bin/rust-analyzer /usr/local/bin/rust-analyzer && \ + rustup component add rustfmt clippy rust-analyzer && \ + curl https://drager.github.io/wasm-pack/installer/init.sh -sSf | sh +# cargo install cargo-generate && \ +# cargo install cargo-expand + +# Dotnet +RUN dotnet tool install --global dotnet-ef && \ + mkdir -p /usr/local/bin/omnisharp && \ + curl -L https://github.com/OmniSharp/omnisharp-roslyn/releases/latest/download/omnisharp-linux-x64-net6.0.tar.gz \ + | tar xz -C /usr/local/bin/omnisharp && \ + chmod +x /usr/local/bin/omnisharp/OmniSharp + +# PHP +RUN --mount=type=cache,id=dnf-cache,target=/var/cache/libdnf5 \ + dnf -y install https://rpm.henderkes.com/static-php-1-0.noarch.rpm && \ + dnf -y module enable php-zts:static-8.5 && \ + dnf install -y \ + frankenphp \ + php-zts-dom \ + php-zts-xml \ + php-zts-tokenizer \ + php-zts-session \ + php-zts-posix \ + php-zts-fileinfo \ + php-zts-simplexml \ + php-zts-xmlreader \ + php-zts-xmlwriter \ + php-zts-soap \ + php-zts-gd \ + php-zts-pdo_mysql \ + php-zts-pcntl \ + php-zts-zip \ + php-zts-exif \ + php-zts-bcmath \ + php-zts-mysqli \ + php-zts-imagick \ + php-zts-grpc && \ + curl -fsSL https://getcomposer.org/installer | \ + php -- --install-dir=/usr/local/bin --filename=composer + +# === install language servers, tools === +RUN deno i -g -f -A --root /usr/local/bin npm:npm npm:bash-language-server npm:vscode-langservers-extracted npm:dockerfile-language-server-nodejs \ + npm:typescript npm:typescript-language-server + +USER user +WORKDIR /home/user +RUN mkdir -p .config .local .cache + +COPY --chown=user:user config/bashrc .bashrc +COPY --chown=user:user config/git .config/git/config +COPY --chown=user:user config/helix/ .config/helix/ + +# === Cleanup build dependencies === +USER root +RUN dnf remove -y openssl-devel && dnf clean all && \ + rm -rf /tmp/* && rm -rf /root/* + +USER user + +VOLUME /home/user diff --git a/container/config/bashrc b/container/config/bashrc new file mode 100644 index 0000000..36cd0f3 --- /dev/null +++ b/container/config/bashrc @@ -0,0 +1,25 @@ +. /etc/bashrc + +# === environment === +export EDITOR=hx \ + WAYLAND_DISPLAY=wayland-0 \ + XDG_RUNTIME_DIR=/run/user/1000 \ + SSH_AUTH_SOCK=/run/user/1000/ssh-auth-sock \ + CARGO_HOME=/usr/local/rust \ + RUSTUP_HOME=/usr/local/rust \ + GOPATH=/usr/local/go \ + PS1="\[\e[30;46m\] \h | \w \[\e[0;36m\]î‚°\[\e[m\] " \ + TZ="Europe/Amsterdam" \ + SHELL="/bin/bash" + +# === aliases and functions === +pbcopy() { curl -sF "content=<-" "https://paste.plabble.org/$2?lang=$1" && echo; } +alias random="cat /dev/urandom | tr -dc '[:alpha:]' | fold -w ${1:-20} | head -n 1" + +# === Git aliases === +alias gc="git commit -m" +alias ga="git add -A" +alias gf="git fetch" +alias gp="git pull" +alias gpp="git push" +alias gs="git status" \ No newline at end of file diff --git a/container/config/bin/host b/container/config/bin/host new file mode 100755 index 0000000..0610301 --- /dev/null +++ b/container/config/bin/host @@ -0,0 +1,6 @@ +#!/bin/bash +export DBUS_SESSION_BUS_ADDRESS="unix:path=/tmp/bus" + +cmd="${0##*/}" +[[ "$cmd" == "host" ]] && unset cmd +exec host-spawn -cwd "${PWD/#$HOME/$HOST_HOME}" ${cmd:+"$cmd"} "$@ \ No newline at end of file diff --git a/container/config/git b/container/config/git new file mode 100644 index 0000000..6ca2f0b --- /dev/null +++ b/container/config/git @@ -0,0 +1,12 @@ +[user] + email = maurict@pm.me + name = Maurice + signingKey = ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGL/am4HDyfV0OWwaI3CeKGypBdzNXOYSbcnm6tK/VB+ maurict@pm.me +[gpg] + format = ssh +[commit] + gpgSign = true +[push] + autoSetupRemote = true +[init] + defaultBranch = main \ No newline at end of file diff --git a/container/config/helix/config.toml b/container/config/helix/config.toml new file mode 100644 index 0000000..74e2046 --- /dev/null +++ b/container/config/helix/config.toml @@ -0,0 +1,7 @@ +theme = "dark_plus" + +[editor] +true-color = true + +[editor.file-picker] +hidden = false \ No newline at end of file diff --git a/container/config/helix/languages.toml b/container/config/helix/languages.toml new file mode 100644 index 0000000..6861f1e --- /dev/null +++ b/container/config/helix/languages.toml @@ -0,0 +1,33 @@ +# C# +[language-server.omnisharp] +command = "/usr/local/bin/omnisharp/OmniSharp" +args = [ "--languageserver" ] + +# Rust +[language-server.rust-analyzer.config.check] +command = "clippy" + +[language-server.deno-lsp] +command = "deno" +args = ["lsp"] +config.deno.enable = true + +[[language]] +name = "rust" +formatter = { command = "rustfmt" } + +# TS +[[language]] +name = "typescript" +roots = ["deno.json", "deno.jsonc", "package.json"] +file-types = ["ts", "tsx"] +auto-format = true +language-servers = ["deno-lsp"] + +# JS +[[language]] +name = "javascript" +roots = ["deno.json", "deno.jsonc", "package.json"] +file-types = ["js", "jsx"] +auto-format = true +language-servers = ["deno-lsp"] \ No newline at end of file diff --git a/containers/fedora/Containerfile b/containers/fedora/Containerfile deleted file mode 100644 index df54734..0000000 --- a/containers/fedora/Containerfile +++ /dev/null @@ -1,24 +0,0 @@ -FROM quay.io/fedora/fedora:43 - -# === install system packages === -RUN dnf update -y && \ - dnf copr enable -y atim/lazygit && \ - dnf -y install procps ping bash-completion glibc-langpack-en \ - host-spawn dbus-launch \ - zoxide git npm neovim awk jq unzip fd-find lazygit - -# === setup user === -RUN useradd -ms /bin/bash user && \ - usermod -aG wheel user && sed -i '/NOPASSWD/s/^#//g' /etc/sudoers && \ - mkdir -p /run/user/1000 && chown user:user /run/user/1000 - -USER user -WORKDIR /home/user -RUN mkdir .config .local .cache - -RUN git clone https://git.plabble.org/Job79/neovim-config.git .config/nvim -COPY --chown=user:user config/bashrc .bashrc -COPY --chown=user:user config/git .config/git/config -COPY config/bin /usr/local/bin - -VOLUME /home/user diff --git a/containers/fedora/config/bashrc b/containers/fedora/config/bashrc deleted file mode 100644 index c5cc3fa..0000000 --- a/containers/fedora/config/bashrc +++ /dev/null @@ -1,16 +0,0 @@ -. /etc/bashrc - -# === environment === -export EDITOR=nvim \ - WAYLAND_DISPLAY=wayland-0 \ - XDG_RUNTIME_DIR=/run/user/1000 \ - SSH_AUTH_SOCK=/run/user/1000/ssh-auth-sock \ - PS1="\[\e[30;46m\] \h | \w \[\e[0;36m\]î‚°\[\e[m\] " \ - TZ="Europe/Amsterdam" - -# === aliases and functions === -alias vi=nvim -post() { curl -sF "content=<-" "https://paste.plabble.org/$2?lang=$1" && echo; } - -eval "$(zoxide init --cmd cd bash)" -eval "$(fzf --bash)" diff --git a/containers/fedora/config/bin/host b/containers/fedora/config/bin/host deleted file mode 100755 index f8fe5ed..0000000 --- a/containers/fedora/config/bin/host +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -export DBUS_SESSION_BUS_ADDRESS=unix:path=/tmp/bus -host-spawn -cwd "${PWD/#$HOME/$HOST_HOME}" \ - $([ "$(basename "$0")" != "host" ] && echo "$(basename "$0")") "$@" diff --git a/containers/fedora/config/bin/podman b/containers/fedora/config/bin/podman deleted file mode 120000 index fe198d7..0000000 --- a/containers/fedora/config/bin/podman +++ /dev/null @@ -1 +0,0 @@ -host \ No newline at end of file diff --git a/containers/fedora/config/bin/xdg-open b/containers/fedora/config/bin/xdg-open deleted file mode 120000 index fe198d7..0000000 --- a/containers/fedora/config/bin/xdg-open +++ /dev/null @@ -1 +0,0 @@ -host \ No newline at end of file diff --git a/containers/fedora/config/git b/containers/fedora/config/git deleted file mode 100644 index d898cb2..0000000 --- a/containers/fedora/config/git +++ /dev/null @@ -1,12 +0,0 @@ -[user] - email = job@plabble.org - name = Job79 - signingKey = ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPsqM6ABAaCTQZ+llFXD3CXrYYuIHDEnvz8IBbXddYEc job@plabble.org -[gpg] - format = ssh -[commit] - gpgSign = true -[push] - autoSetupRemote = true -[init] - defaultBranch = main diff --git a/containers/go/Containerfile b/containers/go/Containerfile deleted file mode 100644 index cc4ada4..0000000 --- a/containers/go/Containerfile +++ /dev/null @@ -1,5 +0,0 @@ -ARG TAG -FROM git.plabble.org/job79/fedora:${TAG} -USER root -RUN dnf -y install go -USER user diff --git a/containers/go/config.sh b/containers/go/config.sh deleted file mode 100644 index 276f925..0000000 --- a/containers/go/config.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -arg "-v $HOME/Documents/containers/go:/home/user/projects" diff --git a/containers/infra/Containerfile b/containers/infra/Containerfile deleted file mode 100644 index 70f1418..0000000 --- a/containers/infra/Containerfile +++ /dev/null @@ -1,6 +0,0 @@ -ARG TAG -FROM git.plabble.org/job79/fedora:${TAG} -USER root -RUN dnf -y install kubectl openssl -RUN curl -sL https://talos.dev/install | sh -USER user diff --git a/containers/infra/config.sh b/containers/infra/config.sh deleted file mode 100644 index efcc191..0000000 --- a/containers/infra/config.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -arg "-v $HOME/Documents/containers/infra:/home/user/projects" diff --git a/containers/kali/Containerfile b/containers/kali/Containerfile deleted file mode 100644 index 42b4984..0000000 --- a/containers/kali/Containerfile +++ /dev/null @@ -1,17 +0,0 @@ -FROM docker.io/kalilinux/kali-rolling - -# === install system packages === -RUN apt update && \ - apt -y install iputils-ping sudo \ - zoxide git npm neovim gawk jq unzip fd-find lazygit - -# === setup user === -RUN useradd -ms /bin/bash user && \ - usermod -aG sudo user && sed -i 's/^%sudo\s\+ALL=(ALL:ALL)\s\+ALL$/%sudo\tALL=(ALL:ALL)\tNOPASSWD: ALL/' /etc/sudoers && \ - mkdir -p /run/user/1000 && chown user:user /run/user/1000 - -USER user -WORKDIR /home/user -RUN mkdir -p .config .local .cache -RUN git clone https://git.plabble.org/Job79/neovim-config.git .config/nvim -VOLUME /home/user diff --git a/devc.sh b/devc.sh index 0a86901..9ef7ac4 100755 --- a/devc.sh +++ b/devc.sh @@ -1,108 +1,82 @@ #!/bin/bash # =============================================== # -# devc.sh v2.0; job79 # -# Dev container enter script. Handles setting up # -# different dev containers, resuming sessions and # -# automatic container updates. # +# devc.sh v2.1; job79, maurice # +# Dev container entry script. # # =============================================== # -set -eu -log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-â—‹}" "$1"; } -arg() { echo -n " $@"; } +set -euo pipefail +log() { echo -e "\e[36mâ—‹\e[0m $1"; } +die() { echo -e "\e[31mx\e[0m $1" && exit 1; } -# run_args returns the podman run arguments required for -# starting a new container. +# default_args configures standard container options. default_args() { - arg "--name $name" - arg "--hostname $name" + run_opts+=( + "--name" "$name" + "--hostname" "$name" + "--network" "devc" + "--pull=newer" # Update image. + "--userns=keep-id" # Map host user. + "-v" "$name:/home/user:copy" # Persistent home volume. + "-v" "dnf-cache:/var/cache/libdnf5" # Cache dnf metadata. + ) - # Pull newer container image if available. - arg "--pull=newer" + # Unix sockets require SELinux label disable. + [[ -d /sys/fs/selinux ]] && run_opts+=("--security-opt" "label=disable") - # Use keep-id so the container user matches the host user. - arg "--userns=keep-id" + # Desktop integration (Wayland, SSH). + [[ -e "/run/user/$UID/wayland-0" ]] && run_opts+=("-v" "/run/user/$UID/wayland-0:/run/user/1000/wayland-0") + [[ -e "${SSH_AUTH_SOCK:-}" ]] && run_opts+=("-v" "$SSH_AUTH_SOCK:/run/user/1000/ssh-auth-sock") - # Disable selinux labeling so unix sockets can be mounted - # without problems. - arg "--security-opt label=disable" - - # Mount the wayland socket. Required to get the system - # clipboard (wl-copy) and gui applications working. - [ -e "/run/user/$UID/wayland-0" ] && arg "-v /run/user/$UID/wayland-0:/run/user/1000/wayland-0" - - # Mount the ssh socket to get ssh working. - [ -e "$SSH_AUTH_SOCK" ] && arg "-v $SSH_AUTH_SOCK:/run/user/1000/ssh-auth-sock" - - # Make the user home dir a volume so it survives container - # restarts. Use copy to keep the files from the image. - arg "-v $name:/home/user:copy" - - # If there is custom configuration for the container, load - # it here. - config_file="$(dirname "$(realpath "$0")")/containers/$name/config.sh" - [ -f "$config_file" ] && source "${config_file}" + # Load custom container config. + local config_file="${BASH_SOURCE[0]%/*}/container/config.sh" + [[ -f "$config_file" ]] && source "$config_file" || true } -# param_args returns the podman run arguments based on the -# arguments provided to this script. +# param_args parses CLI arguments into podman run options. param_args() { - while test $# -gt 0; do + while (($# > 0)); do case "$1" in - -gpu) # Enable gpu acceleration. - arg "--device /dev/dri" ;; - -host-spawn) # Enable spawning host commands from inside the container using host-spawn. - arg "-v /run/user/$UID/bus:/tmp/bus" - arg "-e HOST_HOME=$HOME" # Used to translate paths. - ;; - -x11) # Enable X11 support. - arg "-v /tmp/.X11-unix:/tmp/.X11-unix" - arg "-v $XAUTHORITY:/run/user/1000/.Xauthority:ro" - arg "-e DISPLAY=$DISPLAY" - arg "-e XAUTHORITY=/run/user/1000/.Xauthority" - ;; - *) # Use unknown arguments a podman arguments. - arg "$1" ;; + -gpu) run_opts+=("--device" "/dev/dri") ;; + -kvm) run_opts+=("--device" "/dev/kvm") ;; + -usb) run_opts+=("--device" "/dev/bus/usb") ;; + -host-spawn) run_opts+=("-v" "/run/user/$UID/bus:/tmp/bus" "-e" "HOST_HOME=$HOME") ;; + -container-sock) run_opts+=("-v" "$XDG_RUNTIME_DIR/podman/podman.sock:/var/run/docker.sock") ;; + -x11) run_opts+=("-v" "/tmp/.X11-unix:/tmp/.X11-unix" "-v" "$XAUTHORITY:/run/user/1000/.Xauthority:ro" "-e" "DISPLAY=$DISPLAY" "-e" "XAUTHORITY=/run/user/1000/.Xauthority") ;; + -mnt) shift && run_opts+=("-w" "/mnt/" "-v" "$1:/mnt/$([[ -d "$1" ]] || echo "file")") ;; + *) run_opts+=("$1") ;; esac shift done } -### MAIN ### -# Get the devcontainer name from the first argument. If not -# provided, use the last used name when possible. -if [[ $# -gt 0 ]] && [[ ${1:-} != -* ]]; then - image="$1" - [[ "$image" != *:* ]] && image="$image:main" - echo "$image" >"$HOME/.local/share/devc-previous-container" - shift -elif [ -f "$HOME/.local/share/devc-previous-container" ]; then - image=$(<"$HOME/.local/share/devc-previous-container") -else - log "no container name specified" 'x' 31 - exit 1 -fi -name="${image%:*}" +main() { + local state_file="$HOME/.local/share/devc-previous-container" + local image="devc" -# Get container registry from the DEVC_REGISTRY env -# variable. -if [ -n "${DEVC_REGISTRY:-}" ]; then - registry="$DEVC_REGISTRY" -else - log "registry unknown; set the DEVC_REGISTRY environment variable" 'x' 31 - exit 1 -fi + # Resolve container name (CLI arg > Last used > Error). + if [[ $image && $image != -* ]]; then + shift + [[ $image != *:* ]] && image+=":main" + echo "$image" >"$state_file" + elif [[ -f $state_file ]]; then + image=$(<$state_file) + else + die "no container name specified" + fi + local name="${image%:*}" -# Get container command from the DEVC_COMMAND env variable -# if set, else use bash -l. -if [ -z "${DEVC_COMMAND:-}" ]; then - DEVC_COMMAND="bash -l" -fi + # Start/Restart if not running or if arguments change configuration. + if [[ -z "$(podman ps -q -f name="^$name$" -f status=running)" ]] || (($# > 0)); then + log "starting $image..." -# When container is not running or arguments are provided, -# recreate it. -if [ "$(podman container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ] || [[ $# -gt 0 ]]; then - log "starting devcontainer..." - podman container rm -f -t 0 "$name" 1>/dev/null - podman run -td $(default_args) $(param_args $@) "$registry/$image" -fi + default_args + param_args "$@" -podman exec --detach-keys "ctrl-@" -it "$name" ${DEVC_COMMAND:-} + [[ -n "${DEVC_REGISTRY:-}" ]] || die "registry unknown; set the DEVC_REGISTRY environment variable" + podman network create devc --ignore > /dev/null 2>&1 + podman run --replace --stop-timeout 0 -td "${run_opts[@]}" "$DEVC_REGISTRY/$image" + fi + + exec podman exec --detach-keys "ctrl-@,ctrl-@" -it "$name" ${DEVC_COMMAND:-bash -l} +} + +main "$@"