Skip to content

Commit

Permalink
Bring nix-profile.sh in line with NixOS
Browse files Browse the repository at this point in the history
Use the same logic as NixOS' profile and environment setup. Closes NixOS#414
  • Loading branch information
wmertens committed Jan 21, 2015
1 parent f46e329 commit a77b88c
Showing 1 changed file with 73 additions and 22 deletions.
95 changes: 73 additions & 22 deletions scripts/nix-profile.sh.in
Original file line number Diff line number Diff line change
@@ -1,31 +1,82 @@
if [ -n "$HOME" ]; then
NIX_LINK="$HOME/.nix-profile"
if [ -n "$HOME" ] && [ -n "$USER" ]; then
# Set up the per-user profile.
# This part should be kept in sync with nixpkgs:nixos/modules/programs/shell.nix

# Set the default profile.
: ${NIX_LINK:=$HOME/.nix-profile}

: ${NIX_USER_PROFILE_DIR:=@localstatedir@/nix/profiles/per-user/$USER}

savedpath="$PATH"
export PATH=@coreutils@

mkdir -m 0755 -p "$NIX_USER_PROFILE_DIR"

if [ "$(stat --printf '%u' "$NIX_USER_PROFILE_DIR")" != "$(id -u)" ]; then
echo "Nix: WARNING: bad ownership on "$NIX_USER_PROFILE_DIR", should be $(id -u)" >&2
fi

if [ -w "$HOME" ]; then
if ! [ -L "$NIX_LINK" ]; then
echo "creating $NIX_LINK" >&2
_NIX_DEF_LINK=@localstatedir@/nix/profiles/default
@coreutils@/ln -s "$_NIX_DEF_LINK" "$NIX_LINK"
echo "Nix: creating $NIX_LINK" >&2
if [ "$USER" != root ]; then
if ! ln -s "$NIX_USER_PROFILE_DIR"/profile "$NIX_LINK"; then
echo "Nix: WARNING: could not create $NIX_LINK -> $NIX_USER_PROFILE_DIR/profile"
else
# Root installs in the system-wide profile by default.
ln -s @localstatedir@/nix/profiles/default "$NIX_LINK"
fi
fi
fi

# Subscribe the user to the unstable Nixpkgs channel by default.
if [ ! -e "$HOME"/.nix-channels ]; then
echo "https://nixos.org/channels/nixpkgs-unstable nixpkgs" > "$HOME"/.nix-channels
fi

export PATH=$NIX_LINK/bin:$NIX_LINK/sbin:$PATH
# Create the per-user garbage collector roots directory.
user_gcroots=@localstatedir@/nix/gcroots/per-user/"$USER"
mkdir -m 0755 -p "$user_gcroots"
if [ "$(stat --printf '%u' "$user_gcroots")" != "$(id -u)" ]; then
echo "Nix: WARNING: bad ownership on $user_gcroots, should be $(id -u)" >&2
fi
unset user_gcroots

# Subscribe the user to the Nixpkgs channel by default.
if [ ! -e $HOME/.nix-channels ]; then
echo "https://nixos.org/channels/nixpkgs-unstable nixpkgs" > $HOME/.nix-channels
# Set up a default Nix expression from which to install stuff.
nix_defexpr="$HOME"/.nix-defexpr
[ -L "$nix_defexpr" ] && rm -f "$nix_defexpr"
mkdir -m 0755 -p "$nix_defexpr"
if [ "$USER" != root ] && [ ! -L "$nix_defexpr"/channels_root ]; then
ln -s @localstatedir@/nix/profiles/per-user/root/channels "$nix_defexpr"/channels_root
fi
unset nix_defexpr
fi

# Set up environment.
# This part should be kept in sync with nixpkgs:nixos/modules/programs/environment.nix
export NIX_USER_PROFILE_DIR
export NIX_PROFILES="@localstatedir@/nix/profiles/default $NIX_USER_PROFILE_DIR"

# Append ~/.nix-defexpr/channels/nixpkgs to $NIX_PATH so that
# <nixpkgs> paths work when the user has fetched the Nixpkgs
# channel.
export NIX_PATH=${NIX_PATH:+$NIX_PATH:}nixpkgs=$HOME/.nix-defexpr/channels/nixpkgs

# Set $SSL_CERT_FILE so that Nixpkgs applications like curl work.
if [ -e /etc/ssl/certs/ca-bundle.crt ]; then # Fedora, NixOS
export SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt
elif [ -e /etc/ssl/certs/ca-certificates.crt ]; then # Ubuntu, Debian
export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
elif [ -e "$NIX_LINK/etc/ca-bundle.crt" ]; then # fall back to Nix profile
export SSL_CERT_FILE="$NIX_LINK/etc/ca-bundle.crt"
for i in $NIX_PROFILES; do
if [ -d "$i/lib/aspell" ]; then
export ASPELL_CONF="dict-dir $i/lib/aspell"
fi
done

# Set $SSL_CERT_FILE so that Nixpkgs applications like curl work.
if [ -n "SSL_CERT_FILE" ]; then
for i in /etc/ssl/certs/ca-bundle.crt /etc/ssl/certs/ca-certificates.crt "$NIX_LINK/etc/ca-bundle.crt"; do
if [ -e "$i" ]; then
export SSL_CERT_FILE="$i"
break
fi
done
fi

# Append ~/.nix-defexpr/channels/nixpkgs to $NIX_PATH so that
# <nixpkgs> paths work when the user has fetched the Nixpkgs
# channel.
export NIX_PATH="${NIX_PATH:+$NIX_PATH:}nixpkgs=$HOME/.nix-defexpr/channels/nixpkgs"

export PATH="$NIX_LINK/bin:$NIX_LINK/sbin:$savedpath"
unset savedpath
fi

0 comments on commit a77b88c

Please sign in to comment.