feat(home/shell): inject SOPS GitHub token into Nix on-demand

This commit is contained in:
kiskaadee committed 2026-11-04 05:54:24 -05:00
1 parent a9aa41ff02
commit f1184b28da
2 files changed
+25 -1

No files matched your search

+15 -1
View File
@@ -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() {
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
if [[ "${1:-}" == "flake" && "${2:-}" == "check" && "$*" != *"--log-format"* && "$*" != *"-L"* ]]; then
command nix flake check -L "${@:3}"
else
command nix "$@"
fi
fi
}
+10
View File
@@ -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:-}"
}