terraform-aws-access/.envrc

78 lines
2.1 KiB
Bash

#!/bin/env sh
nf () {
nix --extra-experimental-features nix-command --extra-experimental-features flakes "$@"
}
get_repo_basename() {
basename "$(git rev-parse --show-toplevel)"
}
cleanup() {
echo "Cleaning Up..."
basename="$(get_repo_basename)"
if [ -z "$basename" ]; then echo "basename not found"; exit 1; fi
echo "basename is $basename..."
nix-collect-garbage --delete-older-than 30d
if ! grep -q "$(n profile list | grep '^Name:' | grep "$basename")"; then
echo 'Installing Nix Profile...'
nf profile install . --priority 6
fi
nf profile upgrade "$basename"
echo 'Saving Nix Profile...'
nf develop --profile /tmp/"$basename"-nix-env --command bash -c "echo done"
}
if ! which "$0" | grep -q nix; then
print 'Entering Environment...'
basename="$(get_repo_basename)"
if [ -z "$basename" ]; then echo "basename not found"; exit 1; fi
echo "basename is $basename..."
print 'Updating Nix Cache...'
nf flake update
nf profile upgrade "$basename"
print 'Starting...'
# --impure allows Nix to reuse previously built paths
# --ignore-environment ignores the environment variables and paths to tools not installed by nix
nf develop \
--ignore-environment \
--impure \
--keep HOME \
--keep SSH_AUTH_SOCK \
--keep GITHUB_TOKEN \
--keep AWS_ROLE \
--keep AWS_REGION \
--keep AWS_DEFAULT_REGION \
--keep AWS_ACCESS_KEY_ID \
--keep AWS_SECRET_ACCESS_KEY \
--keep AWS_SESSION_TOKEN \
--keep TERM \
--keep XDG_DATA_DIRS \
/tmp/"$basename"-nix-env \
--command bash -c "bash --rcfile .envrc"
print 'Exiting Dev Environment...'
cleanup
else
# this is run inside the dev environment so we can make assumptions about what is available
echo 'Setting up dev environment...'
. .functions
. .variables
. .rcs
. .aliases
if [ -z "$SSH_AUTH_SOCK" ]; then eval "$(ssh-agent -s)"; ssh-add; fi
if ! env | grep -q 'AWS'; then
echo 'Unable to find AWS authentication information in the environment, please make sure you authenticate with AWS.'
echo 'Try using the "aws" cli included in the environment.'
fi
fi