From f1184b28da349eebeb47894b4ee89c5e98b3b7d6 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Wed, 4 Nov 2026 05:54:24 -0500 Subject: [PATCH] feat(home/shell): inject SOPS GitHub token into Nix on-demand --- home/shell/interactive.sh | 22 ++++++++++++++++++---- home/shell/secrets.sh | 10 ++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/home/shell/interactive.sh b/home/shell/interactive.sh index 094d179..8cfad78 100644 --- a/home/shell/interactive.sh +++ b/home/shell/interactive.sh @@ -19,11 +19,25 @@ if [[ $- == *i* ]]; then fi # --- Nix Command Ergonomics --- -# Ensure `nix flake check` always prints build and verification logs (-L) +# Ensure `nix flake check` always prints build and verification logs (-L), +# and inject GitHub personal access token via NIX_CONFIG on-demand to prevent API rate limiting. nix() { - if [[ "${1:-}" == "flake" && "${2:-}" == "check" && "$*" != *"--log-format"* && "$*" != *"-L"* ]]; then - command nix flake check -L "${@:3}" + local token="" + if declare -F _get_nix_gh_token >/dev/null 2>&1; then + token=$(_get_nix_gh_token) + fi + + if [[ -n "$token" ]]; then + if [[ "${1:-}" == "flake" && "${2:-}" == "check" && "$*" != *"--log-format"* && "$*" != *"-L"* ]]; then + NIX_CONFIG="access-tokens = github.com=${token}${NIX_CONFIG:+ $NIX_CONFIG}" command nix flake check -L "${@:3}" + else + NIX_CONFIG="access-tokens = github.com=${token}${NIX_CONFIG:+ $NIX_CONFIG}" command nix "$@" + fi else - command nix "$@" + if [[ "${1:-}" == "flake" && "${2:-}" == "check" && "$*" != *"--log-format"* && "$*" != *"-L"* ]]; then + command nix flake check -L "${@:3}" + else + command nix "$@" + fi fi } diff --git a/home/shell/secrets.sh b/home/shell/secrets.sh index 037a935..6d32b6b 100644 --- a/home/shell/secrets.sh +++ b/home/shell/secrets.sh @@ -59,3 +59,13 @@ hostinger() { command hostinger "$@" fi } + +# 4. _get_nix_gh_token: Extracts and caches the GitHub repo token in memory for Nix invocations. +# Decrypts only on first use and caches in the shell session variable __NIX_GH_TOKEN to avoid repeated SOPS latency. +_get_nix_gh_token() { + local secrets_file="${CONFIG_SECRETS_FILE:-$HOME/Config/secrets.yaml}" + if [[ -z "${__NIX_GH_TOKEN:-}" ]] && [[ -f "$secrets_file" ]] && command -v sops >/dev/null 2>&1; then + __NIX_GH_TOKEN=$(sops -d --extract '["github"]["gh_repo_token"]' "$secrets_file" 2>/dev/null) || true + fi + printf "%s" "${__NIX_GH_TOKEN:-}" +}