Skip to content

feat: tiered GitHub token context switching - #971

Merged
JacobPEvans-personal merged 2 commits into
mainfrom
feat/gh-token-switching
Apr 10, 2026
Merged

JacobPEvans-personal merged 2 commits into
mainfrom
feat/gh-token-switching

Conversation

@JacobPEvans-personal

@JacobPEvans-personal JacobPEvans-personal commented Apr 10, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Adds tiered GitHub token context switching via three shell functions (restricted → private → admin) with security gates for escalated access
  • Token tier defaults to restricted on shell startup; escalation to private/admin requires keychain unlock (password prompt), placing a security boundary between AI tooling and elevated GitHub access
  • Decouples GITHUB_TOKEN (gh CLI) from GITHUB_PERSONAL_ACCESS_TOKEN (Claude's GitHub MCP), enabling independent token management for different consumers

Changes

  • hosts/macbook-m4/gh-token-switching.zsh (new) — Shell functions for tiered token switching: gh-restricted, gh-private, gh-admin, gh-token-status, and internal _gh_switch_token helper with comprehensive error handling
  • hosts/macbook-m4/home.nix — Restructured shell initContent to source the new token-switching module; adds per-tier keychain variable exports; defaults to restricted token on shell startup
  • lib/user-config.nix — Added github.tokens section with per-tier attributes (service, keychain) for DRY token configuration
  • overlays/direnv-darwin-fix.nix (new) — Temporary overlay skipping broken direnv test-fish target on darwin (upstream issue: Content-addressed derivation fails to build on aarch64-darwin NixOS/nix#6065)
  • modules/darwin/common.nix — Imports the new direnv-darwin-fix overlay

Test Plan

  • Run nix flake check — verifies flake syntax and overlay imports
  • Run sudo darwin-rebuild switch --flake . — applies configuration; verify no errors
  • Open new shell and confirm gh-token-status shows restricted token
  • Run gh-private and verify keychain password prompt appears
  • Confirm GITHUB_TOKEN is set from the elevate-access keychain after unlock
  • Run gh-restricted and verify token reverts to automation keychain token
  • Run gh-admin and verify admin token is set after keychain unlock
  • Verify GITHUB_PERSONAL_ACCESS_TOKEN remains unchanged across all tier switches
  • Test gh CLI commands in restricted mode (gh repo list) — should only see public repos
  • Test gh CLI commands in private mode — should see all repos

Related

  • NixOS/nix#6065 — direnv test-fish fails on darwin due to Mach-O signature corruption (workaround via overlay)
  • NixOS/nix#15638 — RewritingSink Mach-O signature bug root cause
  • NixOS/nixpkgs#507531 — direnv darwin-specific test handling (tracking upstream)

Add gh-restricted, gh-private, gh-admin functions that switch
GITHUB_TOKEN by reading tiered PATs from macOS Keychain. Defaults
to restricted on shell startup; escalation gated by keychain password.

Restricted uses automation.keychain-db (AI accessible).
Private and admin use elevate-access.keychain-db (user unlock required).

Centralizes token configuration in lib/user-config.nix under
github.tokens with per-tier service + keychain attributes for DRY.

Includes temporary direnv darwin overlay tracking NixOS/nix#6065:
Mach-O signature corruption causes fish test SIGKILL. Remove when
NixOS/nix#15638 lands.
Copilot AI review requested due to automatic review settings April 10, 2026 00:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a tiered GitHub token “context switching” workflow for the macbook-m4 shell environment (defaulting to least privilege on startup), and introduces a temporary darwin overlay to keep direnv checks passing by skipping the test-fish target impacted by a Nix daemon Mach-O signing corruption bug.

Changes:

  • Add gh-restricted, gh-private, gh-admin, and gh-token-status zsh functions that set GITHUB_TOKEN by reading tiered PATs from macOS Keychain.
  • Centralize per-tier token metadata (service name + keychain DB) in lib/user-config.nix and wire the new switching into zsh init with a restricted default.
  • Add a temporary nixpkgs overlay on darwin to override direnv’s checkPhase to exclude test-fish.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
overlays/direnv-darwin-fix.nix Adds a darwin-only overlay to skip direnv’s test-fish while keeping other tests.
modules/darwin/common.nix Enables the new overlay in the darwin nixpkgs overlay list.
lib/user-config.nix Introduces github.tokens per-tier service/keychain config for DRY token switching.
hosts/macbook-m4/home.nix Persists keychain helper + vars for runtime switching; sources switching script; defaults new shells to restricted.
hosts/macbook-m4/gh-token-switching.zsh Implements the gh-* token switching/status functions.

Need me to fix this? Tag @copilot in a reply to request an automatic fix.
Do NOT tag me just to say thanks — only tag if you want me to make changes.

Comment thread hosts/macbook-m4/gh-token-switching.zsh

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a tiered GitHub token context switching mechanism for macOS, allowing users to switch between restricted, private, and admin PATs stored in the macOS Keychain. It also includes a temporary Nix overlay to fix a direnv build issue on Darwin caused by a known Nix daemon bug. One improvement was identified regarding the shell script: error messages and setup instructions should be redirected to stderr to avoid polluting stdout during command substitution or pipelining.

Comment thread hosts/macbook-m4/gh-token-switching.zsh Outdated
- gh-token-switching.zsh: call security directly to distinguish missing
  entries from locked/access-denied/empty failures; route errors to stderr;
  add REQUIRES contract comment listing expected env vars
- home.nix: unset _get_keychain_secret and _KC_AI_DB after init since the
  switching functions no longer need them at runtime
- direnv-darwin-fix.nix: use lib.optionalAttrs instead of if/then/else
@JacobPEvans-personal
JacobPEvans-personal merged commit cf83afc into main Apr 10, 2026
12 checks passed
@JacobPEvans-personal
JacobPEvans-personal deleted the feat/gh-token-switching branch April 10, 2026 01:04
This was referenced Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants