From d3ef8257abc14c9fb5cc48209384651850b4f658 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Wed, 15 Apr 2026 15:29:23 +0500 Subject: [PATCH] github actions --- .github/workflows/dependency-updates.yml | 89 ++++++++++++++++++++++++ scripts/check-nixpkgs-relevance.sh | 87 +++++++++++++++++++++++ scripts/update-tg-ws-proxy.sh | 73 +++++++++++++++++++ 3 files changed, 249 insertions(+) create mode 100644 .github/workflows/dependency-updates.yml create mode 100755 scripts/check-nixpkgs-relevance.sh create mode 100755 scripts/update-tg-ws-proxy.sh diff --git a/.github/workflows/dependency-updates.yml b/.github/workflows/dependency-updates.yml new file mode 100644 index 0000000..f6b8133 --- /dev/null +++ b/.github/workflows/dependency-updates.yml @@ -0,0 +1,89 @@ +name: Dependency updates + +on: + schedule: + - cron: "0 6 * * 1" + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + +concurrency: + group: dependency-updates + cancel-in-progress: false + +jobs: + update-dependencies: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@v16 + + - name: Decide whether nixpkgs should be updated + id: nixpkgs_gate + run: | + set -euo pipefail + chmod +x scripts/check-nixpkgs-relevance.sh + scripts/check-nixpkgs-relevance.sh >> "$GITHUB_OUTPUT" + + - name: Update flake inputs + run: | + set -euo pipefail + if [[ "${{ steps.nixpkgs_gate.outputs.should_update_nixpkgs }}" == "true" ]]; then + nix flake lock --update-input nixpkgs + else + echo "Skipping nixpkgs update: watched package versions on nixos-unstable are unchanged." + fi + nix flake lock --update-input zapret + + - name: Update tg-ws-proxy pin + run: | + set -euo pipefail + chmod +x scripts/update-tg-ws-proxy.sh + scripts/update-tg-ws-proxy.sh + + - name: Detect dependency file changes + id: changes + run: | + set -euo pipefail + if git diff --quiet -- flake.lock pkgs/tg-ws-proxy.nix; then + echo "has_changes=false" >> "$GITHUB_OUTPUT" + else + echo "has_changes=true" >> "$GITHUB_OUTPUT" + fi + + - name: Run flake checks + if: steps.changes.outputs.has_changes == 'true' + run: nix flake check + + - name: Create or update pull request + if: steps.changes.outputs.has_changes == 'true' + uses: peter-evans/create-pull-request@v7 + with: + branch: ci/update-dependencies + delete-branch: false + commit-message: "chore(deps): refresh flakes and external pins" + title: "chore(deps): refresh flakes and external pins" + labels: | + dependencies + nix + body: | + ## Automated dependency update + + This PR is managed by CI and the same branch is reused across runs. + + ### Included updates + - `flake.lock` updates (`zapret` always, `nixpkgs` only when watched package versions change on `nixos-unstable`) + - `pkgs/tg-ws-proxy.nix` pin refresh (`rev` + `hash`) + + ### Nixpkgs gate result + - should update nixpkgs: `${{ steps.nixpkgs_gate.outputs.should_update_nixpkgs }}` + - changed watched attrs: `${{ steps.nixpkgs_gate.outputs.nixpkgs_changed_watch_attrs }}` + + - name: No dependency changes + if: steps.changes.outputs.has_changes != 'true' + run: echo "No dependency updates detected." diff --git a/scripts/check-nixpkgs-relevance.sh b/scripts/check-nixpkgs-relevance.sh new file mode 100755 index 0000000..830139a --- /dev/null +++ b/scripts/check-nixpkgs-relevance.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +set -euo pipefail + +repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" +cd "$repo_root" + +if [[ ! -f flake.nix ]]; then + echo "flake.nix not found at repository root" >&2 + exit 1 +fi + +if ! command -v nix >/dev/null 2>&1; then + echo "nix command is required" >&2 + exit 1 +fi + +if ! command -v jq >/dev/null 2>&1; then + echo "jq command is required" >&2 + exit 1 +fi + +system="${NIXPKGS_WATCH_SYSTEM:-x86_64-linux}" +default_watch_attrs="sing-box,proxychains-ng,sing-geosite,sing-geoip,nftables,iproute2,iptables,ipset,python3,python3Packages.requests,python3Packages.websockets,python3Packages.cryptography,gtk3,libayatana-appindicator" +watch_attrs_csv="${NIXPKGS_WATCH_ATTRS:-$default_watch_attrs}" + +IFS=',' read -r -a watch_attrs <<< "$watch_attrs_csv" +watch_attrs_json="$( + printf '%s\n' "${watch_attrs[@]}" | + sed '/^[[:space:]]*$/d' | + jq -R . | + jq -s . +)" + +if [[ "$(jq 'length' <<< "$watch_attrs_json")" -eq 0 ]]; then + echo "watch attribute list is empty" >&2 + exit 1 +fi + +eval_versions() { + local ref="$1" + + REF="$ref" SYSTEM="$system" WATCH_ATTRS_JSON="$watch_attrs_json" nix eval --json --impure --expr ' +let + flake = builtins.getFlake (builtins.getEnv "REF"); + watched = builtins.fromJSON (builtins.getEnv "WATCH_ATTRS_JSON"); + system = builtins.getEnv "SYSTEM"; + pkgs = flake.legacyPackages.${system}; + lib = pkgs.lib; + versionOf = attrPath: + let value = lib.attrByPath (lib.splitString "." attrPath) null pkgs; + in + if value == null then null + else if builtins.isAttrs value && value ? version then value.version + else null; +in +lib.genAttrs watched versionOf +' +} + +current_versions="$(eval_versions "path:${repo_root}")" +latest_versions="$(eval_versions "github:NixOS/nixpkgs/nixos-unstable")" + +changes_json="$( + jq -n \ + --argjson current "$current_versions" \ + --argjson latest "$latest_versions" \ + ' + [($latest | keys[]) as $k + | select(($current[$k] // null) != ($latest[$k] // null)) + | { + attr: $k, + current: ($current[$k] // null), + latest: ($latest[$k] // null) + }] + ' +)" + +if jq -e 'length > 0' <<< "$changes_json" >/dev/null; then + changed_attrs="$(jq -r '[.[].attr] | join(",")' <<< "$changes_json")" + echo "Detected nixpkgs updates for watched attrs: ${changed_attrs}" >&2 + echo "should_update_nixpkgs=true" + echo "nixpkgs_changed_watch_attrs=${changed_attrs}" +else + echo "No watched nixpkgs package version changes detected on nixos-unstable." >&2 + echo "should_update_nixpkgs=false" + echo "nixpkgs_changed_watch_attrs=" +fi diff --git a/scripts/update-tg-ws-proxy.sh b/scripts/update-tg-ws-proxy.sh new file mode 100755 index 0000000..dda8a78 --- /dev/null +++ b/scripts/update-tg-ws-proxy.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +set -euo pipefail + +repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" +cd "$repo_root" + +target_file="pkgs/tg-ws-proxy.nix" + +if [[ ! -f "$target_file" ]]; then + echo "Cannot find ${target_file}" >&2 + exit 1 +fi + +if ! command -v curl >/dev/null 2>&1; then + echo "curl command is required" >&2 + exit 1 +fi + +if ! command -v jq >/dev/null 2>&1; then + echo "jq command is required" >&2 + exit 1 +fi + +if ! command -v nix >/dev/null 2>&1; then + echo "nix command is required" >&2 + exit 1 +fi + +latest_tag="$( + curl -fsSL "https://api.github.com/repos/Flowseal/tg-ws-proxy/releases/latest" | + jq -r '.tag_name // empty' +)" + +if [[ -z "$latest_tag" ]]; then + latest_tag="$( + curl -fsSL "https://api.github.com/repos/Flowseal/tg-ws-proxy/tags?per_page=1" | + jq -r '.[0].name // empty' + )" +fi + +if [[ -z "$latest_tag" ]]; then + echo "Unable to resolve latest tg-ws-proxy tag" >&2 + exit 1 +fi + +archive_url="https://github.com/Flowseal/tg-ws-proxy/archive/refs/tags/${latest_tag}.tar.gz" +latest_hash="$( + nix store prefetch-file --json --unpack "$archive_url" | jq -r '.hash' +)" + +if [[ -z "$latest_hash" || "$latest_hash" == "null" ]]; then + echo "Unable to compute tg-ws-proxy source hash for ${latest_tag}" >&2 + exit 1 +fi + +current_rev="$(sed -n -E 's/^[[:space:]]*rev = "([^"]+)";/\1/p' "$target_file" | head -n1)" +current_hash="$(sed -n -E 's/^[[:space:]]*hash = "([^"]+)";/\1/p' "$target_file" | head -n1)" + +if [[ -z "$current_rev" || -z "$current_hash" ]]; then + echo "Could not parse current rev/hash from ${target_file}" >&2 + exit 1 +fi + +if [[ "$current_rev" == "$latest_tag" && "$current_hash" == "$latest_hash" ]]; then + echo "tg-ws-proxy is already up to date (${latest_tag})" + exit 0 +fi + +sed -i -E "s|(^[[:space:]]*rev = \").*(\";)|\1${latest_tag}\2|" "$target_file" +sed -i -E "s|(^[[:space:]]*hash = \").*(\";)|\1${latest_hash}\2|" "$target_file" + +echo "Updated tg-ws-proxy pin: ${current_rev} -> ${latest_tag}" +echo "Updated tg-ws-proxy hash: ${current_hash} -> ${latest_hash}"