diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index 22886d9..503e062 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -13,10 +13,7 @@ jobs: runs-on: job-v2 outputs: any_change: ${{ steps.filter.outputs.workflow == 'true' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'}} - fedora: ${{ steps.filter.outputs.fedora == 'true' }} - go: ${{ steps.filter.outputs.go == 'true' }} - rust: ${{ steps.filter.outputs.rust == 'true' }} - dotnet: ${{ steps.filter.outputs.dotnet == 'true' }} + base: ${{ steps.filter.outputs.base == 'true' }} steps: - uses: actions/checkout@v4 - uses: https://github.com/dorny/paths-filter@v3 @@ -24,14 +21,11 @@ jobs: with: filters: | workflow: ['.gitea/workflows/build.yaml'] - fedora: ['containers/fedora/**'] - go: ['containers/go/**'] - rust: ['containers/rust/**'] - dotnet: ['containers/dotnet/**'] + base: ['containers/base/**'] base-image: needs: [changes] - if: ${{ needs.changes.outputs.fedora == 'true' || needs.changes.outputs.any_change == 'true' }} + if: ${{ needs.changes.outputs.base == 'true' || needs.changes.outputs.any_change == 'true' }} runs-on: job-v2 steps: - uses: actions/checkout@v4 @@ -45,10 +39,10 @@ jobs: id: build uses: job79/buildah-build@65b3793a1370c1ccd74a5c0d090d70eb9637a4ef with: - image: maurice/fedora + image: maurice/base tags: ${{ github.ref_name }} - context: ./containers/fedora - containerfiles: ./containers/fedora/Containerfile + context: ./containers/base + containerfiles: ./containers/base/Containerfile - name: Push uses: redhat-actions/push-to-registry@v2 with: @@ -61,51 +55,51 @@ jobs: --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 + # 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/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/containers/fedora/config/bashrc b/container/config/bashrc similarity index 84% rename from containers/fedora/config/bashrc rename to container/config/bashrc index c1191d0..36cd0f3 100644 --- a/containers/fedora/config/bashrc +++ b/container/config/bashrc @@ -5,6 +5,9 @@ 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" @@ -13,7 +16,7 @@ export EDITOR=hx \ 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 +# === Git aliases === alias gc="git commit -m" alias ga="git add -A" alias gf="git fetch" 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/containers/fedora/config/git b/container/config/git similarity index 100% rename from containers/fedora/config/git rename to container/config/git diff --git a/containers/fedora/config/helix/config.toml b/container/config/helix/config.toml similarity index 100% rename from containers/fedora/config/helix/config.toml rename to container/config/helix/config.toml 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/android/Containerfile b/containers/android/Containerfile deleted file mode 100644 index ad61e86..0000000 --- a/containers/android/Containerfile +++ /dev/null @@ -1,20 +0,0 @@ -FROM git.plabble.org/maurice/dotnet:main -ARG studio_version="2025.1.4.8" -ARG download_uri="https://redirector.gvt1.com/edgedl/android/studio/ide-zips/${studio_version}/android-studio-${studio_version}-linux.tar.gz" - -USER root - -RUN --mount=type=cache,id=dnf-cache,target=/var/cache/libdnf5 \ - dnf -y --setopt=keepcache=1 install libXext libXrender libXtst libX11 freetype freetype-devel mesa-libGLU -USER user - -RUN curl -Lo /tmp/studio.tar.gz "${download_uri}" && \ - mkdir /home/user/AndroidStudio && \ - mkdir /home/user/Android && \ - tar -zxf /tmp/studio.tar.gz -C /home/user/AndroidStudio && \ - rm /tmp/studio.tar.gz && \ - mv /home/user/AndroidStudio/android-studio*/* /home/user/AndroidStudio - -RUN echo 'alias studio="$HOME/AndroidStudio/bin/studio"' >> /home/user/.bashrc -RUN echo 'export ANDROID_HOME="$HOME/Android/Sdk"' >> /home/user/.bashrc -RUN echo 'export PATH="$ANDROID_HOME/platform-tools:$PATH"' >> /home/user/.bashrc diff --git a/containers/dotnet/Containerfile b/containers/dotnet/Containerfile deleted file mode 100644 index 0d95101..0000000 --- a/containers/dotnet/Containerfile +++ /dev/null @@ -1,11 +0,0 @@ -FROM git.plabble.org/maurice/fedora:main - -USER root -RUN --mount=type=cache,id=dnf-cache,target=/var/cache/libdnf5 \ - dnf -y --setopt=keepcache=1 install dotnet-sdk-9.0 dotnet-sdk-10.0 - -COPY install-roslyn.sh /tmp/install-roslyn.sh -RUN chmod +x /tmp/install-roslyn.sh && /tmp/install-roslyn.sh - -USER user -RUN dotnet tool install --global dotnet-ef diff --git a/containers/dotnet/install-roslyn.sh b/containers/dotnet/install-roslyn.sh deleted file mode 100644 index 865c354..0000000 --- a/containers/dotnet/install-roslyn.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -version="v1.39.15" -arch="linux-x64" -link="https://github.com/OmniSharp/omnisharp-roslyn/releases/download/$version/omnisharp-$arch-net6.0.tar.gz" - -curl -Lo /tmp/omnisharp.tar.gz $link -mkdir /home/user/.omnisharp -tar -zxf /tmp/omnisharp.tar.gz -C /home/user/.omnisharp/ -chown -R user:user /home/user/.omnisharp \ No newline at end of file diff --git a/containers/fedora/Containerfile b/containers/fedora/Containerfile deleted file mode 100644 index 43332e8..0000000 --- a/containers/fedora/Containerfile +++ /dev/null @@ -1,31 +0,0 @@ -FROM quay.io/fedora/fedora:43 - -# === install system packages === -RUN --mount=type=cache,id=dnf-cache,target=/var/cache/libdnf5 \ - dnf update -y && \ - dnf -y --setopt=keepcache=1 install procps ping bash-completion glibc-langpack-en \ - host-spawn dbus-launch \ - git pnpm helix zip rsync - -# === 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 - -# === Add host entry for podman === -RUN ln -s /usr/local/bin/host /usr/local/bin/podman - -USER user -WORKDIR /home/user -RUN mkdir .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/ -COPY config/bin /usr/local/bin - -# === Setup PNPM & install language servers === -RUN /bin/bash -c "SHELL=/bin/bash pnpm setup && . /home/user/.bashrc && pnpm i -g deno bash-language-server vscode-langservers-extracted dockerfile-language-server-nodejs \ - typescript typescript-language-server" - -VOLUME /home/user diff --git a/containers/fedora/config.sh b/containers/fedora/config.sh deleted file mode 100644 index 70ff6b5..0000000 --- a/containers/fedora/config.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -arg "-p 5173:5173 --network dev-php" 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/helix/languages.toml b/containers/fedora/config/helix/languages.toml deleted file mode 100644 index f1dbd9a..0000000 --- a/containers/fedora/config/helix/languages.toml +++ /dev/null @@ -1,12 +0,0 @@ -# C# -[language-server.omnisharp] -command = "/home/user/.omnisharp/OmniSharp" -args = [ "--languageserver" ] - -# Rust -[language-server.rust-analyzer.config.check] -command = "clippy" - -[[language]] -name = "rust" -formatter = { command = "rustfmt" } \ No newline at end of file diff --git a/containers/flutter/Containerfile b/containers/flutter/Containerfile deleted file mode 100644 index 50ec34f..0000000 --- a/containers/flutter/Containerfile +++ /dev/null @@ -1,11 +0,0 @@ -FROM git.plabble.org/maurice/android:main -ARG flutter_version="3.35.7-stable" -ARG download_uri="https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_${flutter_version}.tar.xz" - -RUN mkdir /home/user/Flutter && \ - curl -Lo /tmp/flutter.tar.xz "${download_uri}" && \ - tar -xf /tmp/flutter.tar.xz -C /home/user/Flutter && \ - rm /tmp/flutter.tar.xz - -RUN echo 'export PATH="$HOME/Flutter/flutter/bin:$PATH"' >> /home/user/.bashrc -RUN echo 'export FLUTTER_ROOT="$HOME/Flutter/flutter"' >> /home/user/.bashrc \ No newline at end of file diff --git a/containers/go/Containerfile b/containers/go/Containerfile deleted file mode 100644 index 9275aa2..0000000 --- a/containers/go/Containerfile +++ /dev/null @@ -1,7 +0,0 @@ -FROM git.plabble.org/maurice/fedora:main - -USER root -RUN --mount=type=cache,id=dnf-cache,target=/var/cache/libdnf5 \ - dnf -y --setopt=keepcache=1 install go - -USER user \ No newline at end of file diff --git a/containers/rider/Containerfile b/containers/rider/Containerfile deleted file mode 100644 index ad47b70..0000000 --- a/containers/rider/Containerfile +++ /dev/null @@ -1,16 +0,0 @@ -FROM git.plabble.org/maurice/dotnet:main -ARG rider_version="2025.3.2" -ARG download_uri="https://download.jetbrains.com/rider/JetBrains.Rider-${rider_version}.tar.gz" - -USER root -RUN --mount=type=cache,id=dnf-cache,target=/var/cache/libdnf5 \ - dnf -y --setopt=keepcache=1 install libXext libXrender libXtst libX11 freetype freetype-devel -USER user - -RUN curl -Lo /tmp/rider.tar.gz "${download_uri}" && \ - mkdir /home/user/Rider && \ - tar -zxf /tmp/rider.tar.gz -C /home/user/Rider && \ - rm /tmp/rider.tar.gz && \ - mv /home/user/Rider/JetBrains*/* /home/user/Rider - -RUN echo 'alias rider="$HOME/Rider/bin/rider"' >> /home/user/.bashrc diff --git a/containers/rider/config.sh b/containers/rider/config.sh deleted file mode 100644 index d4037b2..0000000 --- a/containers/rider/config.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -#arg "-p 8080:8080" -#arg "-p 8081:8081" -arg "--network scripts_dev-php" diff --git a/containers/rust/Containerfile b/containers/rust/Containerfile deleted file mode 100644 index e9c23b0..0000000 --- a/containers/rust/Containerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM git.plabble.org/maurice/fedora:main - -RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y -RUN . '/home/user/.cargo/env' && rustup component add rust-analyzer - -# Raspberry Pi Pico tools -RUN sudo dnf install -y \ - systemd-devel \ - pkg-config \ - && dnf clean all -RUN . "$HOME/.cargo/env" && cargo install elf2uf2-rs --locked -RUN . "$HOME/.cargo/env" && rustup target add thumbv6m-none-eabi diff --git a/devc.sh b/devc.sh index 24cbb68..9ef7ac4 100755 --- a/devc.sh +++ b/devc.sh @@ -1,124 +1,82 @@ #!/bin/bash # =============================================== # -# devc.sh v2.0; job79, maurice # -# 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" ;; - -kvm) # Enable KVM - arg "--device /dev/kvm" ;; - -usb) # Enable USB access - arg "--device /dev/bus/usb" ;; - -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. - ;; - -net) # Enable network dev- - arg "--network dev-$name" - ;; - -mnt) # Mount directory. - shift - arg "-w /workdir/" - arg "-v $1:/workdir/$([ ! -d "$1" ] && echo $1)" - ;; - -mdf) # Mount dev folder - arg "-v $HOME/dev:/home/user/dev" - ;; - -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 network create --ignore "dev-$name" - 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 "$@" diff --git a/example-bashrc b/example-bashrc deleted file mode 100644 index 5d2a215..0000000 --- a/example-bashrc +++ /dev/null @@ -1,28 +0,0 @@ -# Tools -pbcopy() { curl -sF "content=<-" "https://paste.plabble.org/$2?lang=$1" && echo; } -alias code="flatpak run com.visualstudio.code" -alias random="cat /dev/urandom | tr -dc '[:alpha:]' | fold -w ${1:-20} | head -n 1" - -# Dev containers -export DEVC_REGISTRY="git.plabble.org/maurice" - -function devc() { - "$HOME/dev/devcontainer/devc.sh" "$@" -mdf -} - -alias rider="DEVC_COMMAND=/home/user/Rider/bin/rider devc rider -x11 -host-spawn -net" -alias flutter="devc flutter -x11 -host-spawn -kvm -usb -gpu" -alias androidstudio="DEVC_COMMAND=/home/user/AndroidStudio/bin/studio devc android -x11 -host-spawn -kvm -usb -gpu" - -function hx() { - arg="${1:-.}" - DEVC_COMMAND="hx $arg" devc fedora -mnt $arg -} - -# 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"