dotfiles

My PC Dotfiles & Website

scripts/packages.sh (4413B)


  1#! /usr/bin/env nix
  2#! nix shell nixpkgs#bash nixpkgs#coreutils nixpkgs#jq -c bash
  3
  4unset NIX_PATH
  5set -euo pipefail
  6
  7COMMIT_PREFIX="chore(packages): Update"
  8
  9# Script to Automatically Update Packages #
 10# Logic
 11update() {
 12  if [[ "${1:-""}" != "" ]]
 13  then
 14    t1="$(mktemp)"; trap 'rm ${t1}' EXIT;
 15    t2="$(mktemp)"; trap 'rm ${t2}' EXIT;
 16    metadata="${1}"
 17    pkg="$(dirname "${metadata}")"
 18    pkgname="$(basename "${pkg}")"
 19
 20    if [[ ! -f "${metadata}" ]]
 21    then
 22      exit 0
 23    fi
 24
 25    # Extraction
 26    # Each *meta.nix file tracks one source
 27    nix eval -f "${metadata}" --json > "${t1}" 2>/dev/null
 28    rev="$(cat "${t1}" | jq -r .rev)"
 29    sha256="$(cat "${t1}" | jq -r .sha256)"
 30    repo="$(cat "${t1}" | jq -r .repo)"
 31    branch="$(cat "${t1}" | jq -r .branch)"          # Optional
 32    release="$(cat "${t1}" | jq -r .release)"        # Optional
 33    regex="$(jq -r '.regex // "."' "${t1}")"         # Optional
 34    cargoHash="$(cat "${t1}" | jq -r .cargoHash)"    # Optional
 35    vendorHash="$(cat "${t1}" | jq -r .vendorHash)"  # Optional
 36    skip="$(cat "${t1}" | jq -r .skip)"              # Optional
 37    upattr="$(cat "${t1}" | jq -r .upattr)";         # Optional
 38    if [[ "${upattr}" == "null" ]]
 39    then
 40      upattr="${pkgname}"
 41    fi
 42
 43    if [[ "${skip}" == "true" ]]
 44    then
 45      echo "Skipping (Pinned to '${rev}')"
 46      exit 0
 47    fi
 48
 49    # Latest Revision
 50    if [[ "${repo}" != "null" ]]
 51    then
 52      if [[ "${release}" == "true" ]]
 53      then
 54        newrev="$(git ls-remote --refs --sort="version:refname" --tags "${repo}" | cut -d/ -f3- | grep -E "${regex}" | tail -n1)"
 55      else
 56        if [[ "${branch}" == "null" ]]
 57        then
 58          branch="main"
 59        fi
 60        newrev="$(git ls-remote "${repo}" "refs/heads/${branch}" | awk '{ print $1}')"
 61      fi
 62    fi
 63
 64    if [[ -z "${newrev:-}" ]]
 65    then
 66      echo "Could not determine the latest revision for '${pkgname}'" >&2
 67      exit 1
 68    fi
 69
 70    # Early Quit
 71    if [[ "${rev}" == "${newrev}" && "${FORCE_RECHECK:-""}" != "true" ]]
 72    then
 73      echo "'${pkgname}' Already Up to Date ('${rev}')"
 74      exit 0
 75    fi
 76
 77    echo "Updating '${pkgname}'..."
 78    echo "Bumping '${rev}' ---> '${newrev}'"
 79
 80    # SHA
 81    fakeHash="sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
 82    sed -i "s|${rev}|${newrev}|" "${metadata}"
 83    sed -i "s|${sha256}|${fakeHash}|" "${metadata}"
 84    nix build --no-link "..#${upattr}" &> "${t2}" || true
 85    newHash="$(cat "${t2}" | grep 'got:' | cut -d':' -f2 | tr -d ' ' || true)"
 86    if [[ "${newHash}" == "sha256" ]]
 87    then
 88      newHash="$(cat "${t2}" | grep 'got:' | cut -d':' -f3 | tr -d ' ' || true)"
 89    fi
 90
 91    newHash="$(nix hash convert --hash-algo sha256 "${newHash}")"
 92    sed -i "s|${fakeHash}|${newHash}|" "${metadata}"
 93
 94    # Cargo SHA
 95    if [[ "${cargoHash}" != "null" ]]
 96    then
 97      sed -i "s|${cargoHash}|${fakeHash}|" "${metadata}"
 98      nix build --no-link "..#${upattr}" &> "${t2}" || true
 99      newcargoHash="$(cat "${t2}" | grep 'got:' | cut -d':' -f2 | tr -d ' ' || true)"
100      if [[ "${newcargoHash}" == "sha256" ]]
101        then
102        newcargoHash="$(cat "${t2}" | grep 'got:' | cut -d':' -f3 | tr -d ' ' || true)"
103      fi
104      newcargoHash="$(nix hash convert --hash-algo sha256 "${newcargoHash}")"
105      sed -i "s|${fakeHash}|${newcargoHash}|" "${metadata}"
106    fi
107
108    # Vendor SHA
109    if [[ "${vendorHash}" != "null" ]]
110    then
111      sed -i "s|${vendorHash}|${fakeHash}|" "${metadata}"
112      nix build --no-link "..#${upattr}" &> "${t2}" || true
113      newvendorHash="$(cat "${t2}" | grep 'got:' | cut -d':' -f2 | tr -d ' ' || true)"
114      if [[ "${newvendorHash}" == "sha256" ]]
115      then
116        newvendorHash="$(cat "${t2}" | grep 'got:' | cut -d':' -f3 | tr -d ' ' || true)"
117      fi
118      newvendorHash="$(nix hash convert --hash-algo sha256 "${newvendorHash}")"
119      sed -i "s|${fakeHash}|${newvendorHash}|" "${metadata}"
120    fi
121
122    # Commit
123    git diff-index --quiet HEAD "${pkg}" || \
124      git commit -q -n "${pkg}" -m "${COMMIT_PREFIX} \`${pkgname}\`" -m "${rev} ---> ${newrev}"
125    echo -e "Finished Updating '${pkgname}' ('${rev}' ---> '${newrev}')\n"
126    exit 0
127  fi
128}
129export -f update
130
131# Execution
132main() { 
133  echo "Checking for Updates..."
134}
135
136while true
137do
138  (main)
139  find . -mindepth 2 -maxdepth 2 -type f -name '*meta.nix' -exec bash -c 'update "$0"' {} \;
140  res=$?
141  if (( res != 255 ))
142  then
143    break
144  fi
145done