-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path.envrc
More file actions
executable file
·138 lines (121 loc) · 5.45 KB
/
.envrc
File metadata and controls
executable file
·138 lines (121 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
# shellcheck shell=bash
################################################################################
# Portable direnv bootstrap for Nix flakes – NixOS, Ubuntu/Debian and Alpine
# Author: Benji (adapted)
################################################################################
set -euo pipefail
###############################################################################
# Helper functions
###############################################################################
warn() { printf '\e[33m%s\e[0m\n' "$*" >&2; }
fail() { printf '\e[31m%s\e[0m\n' "$*" >&2; exit 1; }
need() { command -v "$1" &>/dev/null || return 1; }
# Detect OS
if [ -r /etc/os-release ]; then
. /etc/os-release
else
fail "Unsupported system – no /etc/os-release"
fi
###############################################################################
# Minimal prerequisites (curl & git) – only if Nix is missing
###############################################################################
if ! need nix; then
pkgs=""
need curl || pkgs+=" curl"
need git || pkgs+=" git"
if [ -n "$pkgs" ]; then
if need sudo; then SUDO=sudo; else SUDO=""; fi
case "$ID" in
alpine) $SUDO apk add --no-cache $pkgs ;;
debian|ubuntu) $SUDO apt-get update -y && $SUDO apt-get install -y $pkgs ;;
naxos) ;; # already has nix
*) warn "Unknown distro $ID – please install:$pkgs";;
esac
fi
fi
###############################################################################
# Install Nix (if needed)
###############################################################################
if ! need nix; then
warn "Installing Nix package manager..."
case "$ID" in
alpine) sh <(curl -L https://nixos.org/nix/install) --no-daemon ;;
*) sh <(curl -L https://nixos.org/nix/install) --daemon ;;
esac
# Load Nix into current shell – works for both install modes
if [ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then
# shellcheck source=/dev/null
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
elif [ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]; then
# shellcheck source=/dev/null
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
fi
fi
###############################################################################
# Ensure helper CLIs via Nix (gh, vault, cachix)
###############################################################################
ensure_cli() {
local cmd="$1" attr="$2"
need "$cmd" || nix-env -iA "nixpkgs.$attr" >/dev/null
}
export NIXPKGS_ALLOW_UNFREE=1
ensure_cli gh gh
ensure_cli vault vault
ensure_cli cachix cachix
###############################################################################
# GitHub Token
###############################################################################
if [ -z "${GITHUB_TOKEN-}" ]; then
warn "Fetching GitHub token via GitHub CLI..."
GITHUB_TOKEN="$(gh auth token 2>/dev/null || true)"
[ -n "$GITHUB_TOKEN" ] || fail "No GITHUB_TOKEN found – set env var or run: gh auth login"
export GITHUB_TOKEN
else
warn "Using existing GITHUB_TOKEN"
fi
###############################################################################
# Vault login & secret retrieval
###############################################################################
export VAULT_ADDR=https://pki.devnw.com
if ! vault token lookup -no-print &>/dev/null; then
vault login -no-print -method=github token="$GITHUB_TOKEN" || warn "Vault login failed"
fi
OPENAI_API_KEY="$(vault kv get -mount=secret -field=token /dev/openai/actions-primary 2>/dev/null || echo '')"
export OPENAI_API_KEY
CACHIX_AUTH_TOKEN="$(vault kv get -mount=secret -field=token /cicd/gh/cachix 2>/dev/null || echo '')"
export CACHIX_AUTH_TOKEN
###############################################################################
# Nix configuration & Cachix
###############################################################################
NIX_CONFIG="experimental-features = nix-command flakes
access-tokens = github.com=${GITHUB_TOKEN}
trusted-users = root $(whoami)"
export NIX_CONFIG
# Configure trusted users in nix.conf if not already configured
if ! grep -q "trusted-users.*$(whoami)" /etc/nix/nix.conf 2>/dev/null; then
warn "Adding $(whoami) as trusted user for Nix..."
echo "trusted-users = root $(whoami)" | sudo tee -a /etc/nix/nix.conf >/dev/null
sudo pkill nix-daemon 2>/dev/null || true
sleep 2
fi
# Setup Cachix auth and binary cache
if [ -n "${CACHIX_AUTH_TOKEN-}" ]; then
cachix authtoken "$CACHIX_AUTH_TOKEN" >/dev/null 2>&1 || true
cachix use spyder >/dev/null 2>&1 || warn "Could not configure Cachix binary cache"
fi
###############################################################################
# Finally: load the flake environment
###############################################################################
export FLAKE_PATH=.# # override if desired
export FLAKE_ARGS='--impure --accept-flake-config'
source_up # direnv helper – walk up to load any parent .envrc
use flake "$FLAKE_PATH" $FLAKE_ARGS
###############################################################################
# Push newly built development shell to Cachix (non‑blocking)
###############################################################################
if [ -n "${CACHIX_AUTH_TOKEN-}" ]; then
# Push the development shell derivation to cachix
( nix develop --dry-run --json $FLAKE_ARGS $FLAKE_PATH 2>/dev/null | jq -r '.devShell' | cachix push spyder 2>/dev/null || warn "Cachix push failed" ) &
fi
# End of .envrc