Skip to content

Commit

Permalink
Totally revamp the way we handle .bashrc.
Browse files Browse the repository at this point in the history
Summary:
I have spent some time groveling through the bash manpages, and am
clearer now on the difference between .bashrc and .bash_profile --
basically, one is used for login shells and one for non-login shells.

We want our code to run in both contexts, so I have moved everything
to .bash_profile, which I now source from .bashrc.  (I actually only
source .bash_profile.khan, to keep any existing .bash_profile stuff
out of it, since maybe the user *does* want that stuff to be
login-only.)

I also separate out .bash_profile -- which is bash-specific -- from
.profile -- which runs on any sh-compatible shell, including zsh.
This should make it easier for zsh users to do their setup.

Test Plan:
I created a new user on my machine, and ran
   make
in the khan-dotfiles directory.  It created all the files the way they
should be.  I then ran
   bash
   git rb
and saw that the git alias was picked up; that is, the git command
didn't say 'rb not found'.  I then ran
   bash --login
   git rb
and saw that it was picked up as well (it wasn't, before this change).

Reviewers: marcos, alpert

Reviewed By: alpert

Subscribers: ayman

Differential Revision: http://phabricator.khanacademy.org/D13071
  • Loading branch information
csilvers committed Sep 16, 2014
1 parent ee59c40 commit 2bc3d04
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 79 deletions.
39 changes: 39 additions & 0 deletions .bash_profile.khan
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
################################################################################
# Khan Academy specific .bash_profile
#
# The difference between .bash_profile and .profile is that the latter
# is called for all sh-compatible shells. So we put bashisms here
# and non-bashisms in .profile.
#
# According to the bash manpage, if both .bash_profile and .profile
# exist, bash only reads the first one. So we have to source .profile
# manually.

if [ -s ~/.profile ]; then
source ~/.profile
fi

# Activate Python2.7 virtualenv
source ~/.virtualenv/khan27/bin/activate

# Figure out what directory we're *really* in (following symlinks).
# We need this because *-completion.bash are siblings to this script.
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # follow symlinks
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # resolve relative symlink
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# Enable autocompletion for git
source "$DIR/git-completion.bash"

# Make sure git (and other apps) prefer 'vim' to 'vi'.
: ${EDITOR:=vim}

# Set a high limit for open file descriptors per shell.
# The default on OS X is 256; we increase it to 1024--the default value of
# `sysctl kern.maxfilesperproc`, which `ulimit -n` must not exceed.
ulimit -S -n 1024
46 changes: 12 additions & 34 deletions .bashrc.khan
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
################################################################################
# Khan Academy specific bashrc

# Add Phabricator bins to PATH
export PATH="$HOME/khan/devtools/arcanist/khan-bin:$PATH"

# Add frankenserver bins to PATH
export PATH="$HOME/khan/webapp/third_party/frankenserver:$PATH"

# Add dotfiles scripts to PATH
export PATH="$HOME/khan/devtools/khan-dotfiles/bin:$PATH"

# Activate Python2.7 virtualenv
source ~/.virtualenv/khan27/bin/activate

# Figure out what directory we're *really* in (following symlinks).
# We need this because *-completion.bash are siblings to this script.
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # follow symlinks
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # resolve relative symlink
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# Enable autocompletion for git
source "$DIR/git-completion.bash"

# Make sure git (and other apps) prefer 'vim' to 'vi'.
: ${EDITOR:=vim}

# Set a high limit for open file descriptors per shell.
# The default on OS X is 256; we increase it to 1024--the default value of
# `sysctl kern.maxfilesperproc`, which `ulimit -n` must not exceed.
ulimit -S -n 1024
#
# We actually store all our rc-code in .profile and .bash_profile.
# The difference between .*profile and .bashrc is that the former
# are loaded for login shells and the latter for non-login shells.
# (For more info, see http://hacktux.com/bash/bashrc/bash_profile.)
#
# We want our code to run for both types of shells, so we just include
# .bash_profile from here. We source only .bash_profile.khan in case
# you have code in your .bashrc_profile that you *don't* want executed
# for non-login shells.

source ~/.bash_profile.khan
51 changes: 51 additions & 0 deletions .profile.khan
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
################################################################################
# Khan Academy specific .profile
#
# The difference between .bash_profile and .profile is that the latter
# is called for all sh-compatible shells. So we put non-bashisms here
# and bashisms in .bash_profile.

# Add Phabricator bins to PATH
export PATH="$HOME/khan/devtools/arcanist/khan-bin:$PATH"

# Add frankenserver bins to PATH
export PATH="$HOME/khan/webapp/third_party/frankenserver:$PATH"

# Add dotfiles scripts to PATH
export PATH="$HOME/khan/devtools/khan-dotfiles/bin:$PATH"

# Make sure git (and other apps) prefer 'vim' to 'vi'.
: ${EDITOR:=vim}

# Set a high limit for open file descriptors per shell.
# The default on OS X is 256; we increase it to 1024--the default value of
# `sysctl kern.maxfilesperproc`, which `ulimit -n` must not exceed.
ulimit -S -n 1024


# Mac-specific stuff.
if [ `uname -s` = Darwin ]; then
# We need /usr/local/bin to come before /usr/bin on the path,
# to pick up brew files we install.
if ! echo "$PATH" | egrep -q '(:|^)/usr/local/bin/?:(.*:)?/usr/bin/?(:|$)'
then
# This replaces /usr/bin with /usr/local/bin:/usr/bin
PATH=`echo $PATH | sed -E 's,(^|:)(/usr/bin/?(:|$)),\1/usr/local/bin:\2,'`
fi

# Ideally we'd put /usr/local/sbin right before /usr/sbin, but
# there's so little in it we figure it's ok to put it first.
PATH=/usr/local/sbin:$PATH
export PATH

# Numpy/etc use flags clang doesn't know about. This is only
# needed for mavericks.
if expr "`sw_vers -productVersion`" : 10.9 >/dev/null; then
CPPFLAGS="-Qunused-arguments $CPPFLAGS"
CFLAGS="-Qunused-arguments $CFLAGS"
# This ARCHFLAGS is needed until we have pyobjc 3.0, according to
# https://bitbucket.org/ronaldoussoren/pyobjc/issue/66/cannot-locate-a-working-compiler-error
ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future $ARCHFLAGS"
export CPPFLAGS CFLAGS ARCHFLAGS
fi
fi
12 changes: 12 additions & 0 deletions bash_profile.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
################################################################################
# Local .bash_profile.
# Feel free to add whatever you like after the 'bash_profile.khan' line.
#
# Note that .bash_profile.khan automatically sources ~/.profile if it
# exists, so you don't have to worry that the addition of this file
# "hides" .profile (bash won't load a .profile file if it sees a
# .bash_profile file instead).

if [ -s ~/.bash_profile.khan ]; then
source ~/.bash_profile.khan
fi
51 changes: 7 additions & 44 deletions mac-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,51 +38,14 @@ error () {
printf "\r\033[2K [\033[0;31mFAIL\033[0m] $1\n"
}

update_path() {
# We need /usr/local/bin to come before /usr/bin on the path,
# to pick up brew files we install.
if ! echo "$PATH" | egrep -q '(:|^)/usr/local/bin/?:(.*:)?/usr/bin/?(:|$)'
then
# This replaces /usr/bin with /usr/local/bin:/usr/bin
PATH=`echo $PATH | sed -E 's,(^|:)(/usr/bin/?(:|$)),\1/usr/local/bin:\2,'`
# Make this path update work in the future too.
path_update=`cat<<'EOF'
echo $PATH | sed -E 's,(^|:)(/usr/bin/?(:|$)),\1/usr/local/bin:\2,'
EOF`
else
path_update=''
fi

# Ideally we'd put /usr/local/sbin right before /usr/sbin, but
# there's so little in it we figure it's ok to put it first.
export PATH=/usr/local/sbin:$PATH
# Put these in shell config file too.
# Test whether it's already in a config file (sorry zsh users who
# aren't using .profile). We follow the same order bash does:
if [ -f ~/.bash_profile ]; then
PROFILE_FILE="$HOME/.bash_profile"
elif [ -f ~/.bash_login ]; then
PROFILE_FILE="$HOME/.bash_login"
else
PROFILE_FILE="$HOME/.profile"
fi
if ! grep -q "export PATH=.*/usr/local/sbin" \
~/.bash_profile ~/.bash_login ~/.profile; then
echo 'export PATH=/usr/local/sbin:$PATH' >> "$PROFILE_FILE"
fi
# Numpy/etc use flags clang doesn't know about. This is only
# needed for mavericks.
if expr "`sw_vers -productVersion`" : 10.9 >/dev/null && \
! grep -q -e "-Qunused-arguments" \
~/.bash_profile ~/.bash_login ~/.profile; then
echo 'export CPPFLAGS="-Qunused-arguments $CPPFLAGS"' >> "$PROFILE_FILE"
echo 'export CFLAGS="-Qunused-arguments $CFLAGS"' >> "$PROFILE_FILE"
fi
if [ -n "$path_update" ]; then
echo "# Put /usr/local/bin right before /usr/bin" >> "$PROFILE_FILE"
echo 'PATH=`'"$path_update"'`' >> "$PROFILE_FILE"
fi
update_path() {
# We need /usr/local/bin to come before /usr/bin on the path, to
# pick up brew files we install. To do this, we just source
# .profile.khan, which does this for us (and the new user).
# (This assumes you're running mac-setup.sh from the khan-dotfiles
# directory.)
. .profile.khan
}

maybe_generate_ssh_keys () {
Expand Down
7 changes: 7 additions & 0 deletions profile.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
################################################################################
# Local .profile.
# Feel free to add whatever you like after the profile.khan' line.

if [ -s ~/.profile.khan ]; then
. ~/.profile.khan
fi
2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ install_dotfiles() {
# They all have the property they 'include' khan-specific code.
for file in *.default; do
dest="$ROOT/.`echo "$file" | sed s/.default$//`" # foo.default -> .foo
ka_version=.`echo "$file" | sed s/default/khan/` # .bashrc.khan
ka_version=.`echo "$file" | sed s/default/khan/` # .bashrc.khan, etc.
if [ ! -e "$dest" ]; then
cp -f "$file" "$dest"
elif ! fgrep -q "$ka_version" "$dest"; then
Expand Down

0 comments on commit 2bc3d04

Please sign in to comment.