on master help sign in
nix-bundle/install-nix-from-closure.sh
128 lines 3.3 kB history raw
1#!/bin/sh
3set -e
5dest="/nix"
6self="."
7nix="@nix@"
8cacert="@cacert@"
10if ! [ -e $self/.reginfo ]; then
11 echo "$0: incomplete installer (.reginfo is missing)" >&2
12 exit 1
13fi
15if [ -z "$USER" ]; then
16 echo "$0: \$USER is not set" >&2
17 exit 1
18fi
20if [ "$(id -u)" -eq 0 ]; then
21 printf '\e[1;31mwarning: installing Nix as root is not supported by this script!\e[0m\n'
22fi
24echo "performing a single-user installation of Nix..." >&2
26if ! [ -e $dest ]; then
27 cmd="mkdir -m 0755 $dest && chown $USER $dest"
28 echo "directory $dest does not exist; creating it by running '$cmd' using sudo" >&2
29 if ! sudo sh -c "$cmd"; then
30 echo "$0: please manually run ‘$cmd’ as root to create $dest" >&2
31 exit 1
32 fi
33fi
35if ! [ -w $dest ]; then
36 echo "$0: directory $dest exists, but is not writable by you. This could indicate that another user has already performed a single-user installation of Nix on this system. If you wish to enable multi-user support see http://nixos.org/nix/manual/#ssec-multi-user. If you wish to continue with a single-user install for $USER please run ‘chown -R $USER $dest’ as root." >&2
37 exit 1
38fi
40mkdir -p $dest/store
42echo -n "copying Nix to $dest/store..." >&2
44for i in $(cd $self/store >/dev/null && echo *); do
45 echo -n "." >&2
46 i_tmp="$dest/store/$i.$$"
47 if [ -e "$i_tmp" ]; then
48 rm -rf "$i_tmp"
49 fi
50 if ! [ -e "$dest/store/$i" ]; then
51 cp -Rp "$self/store/$i" "$i_tmp"
52 chmod -R a-w "$i_tmp"
53 chmod +w "$i_tmp"
54 mv "$i_tmp" "$dest/store/$i"
55 chmod -w "$dest/store/$i"
56 fi
57done
58echo "" >&2
60echo "initialising Nix database..." >&2
61if ! $nix/bin/nix-store --init; then
62 echo "$0: failed to initialize the Nix database" >&2
63 exit 1
64fi
66if ! $nix/bin/nix-store --load-db < $self/.reginfo; then
67 echo "$0: unable to register valid paths" >&2
68 exit 1
69fi
71. $nix/etc/profile.d/nix.sh
73if ! $nix/bin/nix-env -i "$nix"; then
74 echo "$0: unable to install Nix into your default profile" >&2
75 exit 1
76fi
78# Install an SSL certificate bundle.
79if [ -z "$SSL_CERT_FILE" -o ! -f "$SSL_CERT_FILE" ]; then
80 $nix/bin/nix-env -i "$cacert"
81 export SSL_CERT_FILE="$HOME/.nix-profile/etc/ssl/certs/ca-bundle.crt"
82fi
84# Subscribe the user to the Nixpkgs channel and fetch it.
85if ! $nix/bin/nix-channel --list | grep -q "^nixpkgs "; then
86 $nix/bin/nix-channel --add https://nixos.org/channels/nixpkgs-unstable
87fi
88if [ -z "$_NIX_INSTALLER_TEST" ]; then
89 $nix/bin/nix-channel --update nixpkgs
90fi
92# Make the shell source nix.sh during login.
93p=$HOME/.nix-profile/etc/profile.d/nix.sh
95added=
96for i in .bash_profile .bash_login .profile; do
97 fn="$HOME/$i"
98 if [ -w "$fn" ]; then
99 if ! grep -q "$p" "$fn"; then
100 echo "modifying $fn..." >&2
101 echo "if [ -e $p ]; then . $p; fi # added by Nix installer" >> $fn
102 fi
103 added=1
104 break
105 fi
106done
108if [ -z "$added" ]; then
109 cat >&2 <<EOF
111Installation finished! To ensure that the necessary environment
112variables are set, please add the line
114 . $p
116to your shell profile (e.g. ~/.profile).
117EOF
118else
119 cat >&2 <<EOF
121Installation finished! To ensure that the necessary environment
122variables are set, either log in again, or type
124 . $p
126in your shell.
127EOF
128fi