#!/bin/bash # ============================================================================== # nerdfont-install.sh # # Installs or updates one or more Nerd Fonts (https://www.nerdfonts.com) from # the latest ryanoasis/nerd-fonts GitHub release. # # Usage: # curl -fsSL https://curl-pipe-bash.watzinger.biz/nerdfont-install.sh | bash # # What it does: # - Installs FiraCode by default; other fonts can be passed as arguments # (`| bash -s -- JetBrainsMono Hack`), named as on # https://www.nerdfonts.com/font-downloads # - Resolves the latest Nerd Fonts release from GitHub # - Skips each font whose installed version already matches the release # - Downloads .tar.xz and verifies it against the release's # official SHA-256.txt # - Installs per-user to ~/.local/share/fonts/NerdFont/ (no sudo), # or system-wide to /usr/local/share/fonts/ when run as root # - Refreshes the fontconfig cache # - Cleans up temp files on exit, even on failure # # Requirements: curl, tar, xz, sha256sum, fc-cache (fontconfig) # ============================================================================== set -euo pipefail # ── Configuration ────────────────────────────────────────────────────────────── RELEASES_URL="https://github.com/ryanoasis/nerd-fonts/releases" DEFAULT_FONTS=(FiraCode) VERSION_FILE=".nerdfont-version" TMP_DIR="$(mktemp -d)" if [ "$EUID" -eq 0 ]; then FONT_ROOT="/usr/local/share/fonts" else FONT_ROOT="${XDG_DATA_HOME:-$HOME/.local/share}/fonts" fi # ── Cleanup on exit ──────────────────────────────────────────────────────────── cleanup() { rm -rf "$TMP_DIR" } trap cleanup EXIT # ── Helpers ──────────────────────────────────────────────────────────────────── err() { echo "[ERROR] $*" >&2; } info() { echo "[INFO] $*"; } # ── Dependency check ─────────────────────────────────────────────────────────── check_dependencies() { local missing=() for cmd in curl tar xz sha256sum fc-cache; do command -v "$cmd" &>/dev/null || missing+=("$cmd") done if [ ${#missing[@]} -gt 0 ]; then err "Missing required tools: ${missing[*]}" err "On Debian/Ubuntu: sudo apt install curl xz-utils fontconfig" err "On Fedora/RHEL: sudo dnf install curl xz fontconfig" exit 1 fi } # ── Resolve latest release and its checksums ─────────────────────────────────── get_latest_version() { info "Fetching latest Nerd Fonts release from GitHub..." # Follow the /releases/latest redirect to .../tag/vX.Y.Z instead of using # the GitHub API, which is rate-limited for unauthenticated requests. local latest_url latest_url=$(curl -fsSLI -o /dev/null -w '%{url_effective}' "$RELEASES_URL/latest") LATEST_VERSION="${latest_url##*/tag/}" if [[ ! "$LATEST_VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then err "Could not determine the latest Nerd Fonts release (got: $latest_url)." err "Check $RELEASES_URL manually." exit 1 fi info "Latest Nerd Fonts release: $LATEST_VERSION" CHECKSUMS="$TMP_DIR/SHA-256.txt" curl -fsSL "$RELEASES_URL/download/$LATEST_VERSION/SHA-256.txt" -o "$CHECKSUMS" } # ── Install a single font ────────────────────────────────────────────────────── install_font() { local font="$1" local asset="$font.tar.xz" local font_dir="$FONT_ROOT/${font}NerdFont" local installed_version="" [ -f "$font_dir/$VERSION_FILE" ] && installed_version=$(cat "$font_dir/$VERSION_FILE") if [ "$installed_version" = "$LATEST_VERSION" ]; then info "$font Nerd Font $installed_version is already the latest version - skipping." return 0 fi local expected_hash expected_hash=$(awk -v a="$asset" '$2 == a { print $1 }' "$CHECKSUMS") if [ -z "$expected_hash" ]; then err "No font named '$font' in Nerd Fonts $LATEST_VERSION." err "See https://www.nerdfonts.com/font-downloads for valid names (e.g. FiraCode, JetBrainsMono, Hack)." return 1 fi local tarball_path="$TMP_DIR/$asset" info "Downloading $asset ($LATEST_VERSION)..." curl -fsSL --retry 3 --connect-timeout 20 \ "$RELEASES_URL/download/$LATEST_VERSION/$asset" -o "$tarball_path" info "Verifying checksum..." echo "${expected_hash} ${tarball_path}" | sha256sum --check --status info "Checksum verified OK." # Replace the whole directory so files dropped in a newer release don't linger. rm -rf "$font_dir" mkdir -p "$font_dir" tar -xJf "$tarball_path" -C "$font_dir" echo "$LATEST_VERSION" > "$font_dir/$VERSION_FILE" info "$font Nerd Font ${installed_version:+$installed_version → }$LATEST_VERSION installed to $font_dir" FONTS_CHANGED=1 } # ── Main ─────────────────────────────────────────────────────────────────────── main() { local fonts=("$@") [ ${#fonts[@]} -gt 0 ] || fonts=("${DEFAULT_FONTS[@]}") check_dependencies get_latest_version FONTS_CHANGED=0 local failed=0 for font in "${fonts[@]}"; do install_font "$font" || failed=1 done if [ "$FONTS_CHANGED" -eq 1 ]; then info "Refreshing font cache..." fc-cache -f "$FONT_ROOT" fi [ "$failed" -eq 0 ] || exit 1 info "Done. Select e.g. \"${fonts[0]} Nerd Font Mono\" in your terminal's font settings." } main "$@"