2014-10-07 18:40:31 +03:00
#!/bin/sh
2012-05-23 01:36:54 +03:00
2014-02-10 17:35:59 +02:00
set -e
2012-05-23 01:36:54 +03:00
2020-09-30 18:48:49 +03:00
umask 0022
2014-02-10 17:35:59 +02:00
dest = "/nix"
self = " $( dirname " $0 " ) "
nix = "@nix@"
2014-12-10 17:05:08 +02:00
cacert = "@cacert@"
2014-02-10 17:35:59 +02:00
2017-01-24 20:59:55 +02:00
2016-12-02 15:18:50 +02:00
if ! [ -e " $self /.reginfo " ] ; then
2014-02-10 17:35:59 +02:00
echo " $0 : incomplete installer (.reginfo is missing) " >& 2
fi
2019-07-25 16:39:44 +03:00
if [ -z " $USER " ] && ! USER = $( id -u -n) ; then
2014-02-10 17:35:59 +02:00
echo " $0 : \$USER is not set " >& 2
exit 1
fi
2017-07-09 20:07:28 +03:00
if [ -z " $HOME " ] ; then
echo " $0 : \$HOME is not set " >& 2
exit 1
fi
2018-12-20 21:05:14 +02:00
# macOS support for 10.12.6 or higher
2017-07-09 20:07:28 +03:00
if [ " $( uname -s) " = "Darwin" ] ; then
2020-11-28 00:42:15 +02:00
IFS = '.' read -r macos_major macos_minor macos_patch << EOF
2020-09-08 07:53:31 +03:00
$( sw_vers -productVersion)
EOF
if [ " $macos_major " -lt 10 ] || { [ " $macos_major " -eq 10 ] && [ " $macos_minor " -lt 12 ] ; } || { [ " $macos_minor " -eq 12 ] && [ " $macos_patch " -lt 6 ] ; } ; then
# patch may not be present; command substitution for simplicity
2018-12-20 21:05:14 +02:00
echo " $0 : macOS $( sw_vers -productVersion) is not supported, upgrade to 10.12.6 or higher "
2017-07-09 20:07:28 +03:00
exit 1
fi
2018-04-19 20:37:54 +03:00
fi
2017-07-09 20:07:28 +03:00
2018-09-01 15:51:32 +03:00
# Determine if we could use the multi-user installer or not
2020-11-28 00:42:15 +02:00
if [ " $( uname -s) " = "Linux" ] ; then
2021-12-06 17:42:57 +02:00
echo "Note: a multi-user installation is possible. See https://nixos.org/manual/nix/stable/installation/installing-binary.html#multi-user-installation" >& 2
2018-04-19 20:37:54 +03:00
fi
2020-11-28 00:42:15 +02:00
case " $( uname -s) " in
"Darwin" )
INSTALL_MODE = daemon; ;
*)
INSTALL_MODE = no-daemon; ;
esac
# space-separated string
ACTIONS =
2020-05-12 19:58:13 +03:00
# handle the command line flags
2019-12-15 17:43:43 +02:00
while [ $# -gt 0 ] ; do
case $1 in
--daemon)
2020-11-28 00:42:15 +02:00
INSTALL_MODE = daemon
ACTIONS = " ${ ACTIONS } install "
; ;
2019-12-15 17:43:43 +02:00
--no-daemon)
2020-11-28 00:42:15 +02:00
if [ " $( uname -s) " = "Darwin" ] ; then
printf '\e[1;31mError: --no-daemon installs are no-longer supported on Darwin/macOS!\e[0m\n' >& 2
exit 1
fi
INSTALL_MODE = no-daemon
# intentional tail space
ACTIONS = " ${ ACTIONS } install "
; ;
# --uninstall)
# # intentional tail space
# ACTIONS="${ACTIONS}uninstall "
# ;;
2022-10-26 02:43:00 +03:00
--yes)
export NIX_INSTALLER_YES = 1; ;
2019-12-15 17:43:43 +02:00
--no-channel-add)
2020-05-26 16:49:26 +03:00
export NIX_INSTALLER_NO_CHANNEL_ADD = 1; ;
2020-02-26 11:50:40 +02:00
--daemon-user-count)
2020-05-26 16:49:26 +03:00
export NIX_USER_COUNT = $2
2020-02-26 11:50:40 +02:00
shift; ;
2019-12-15 17:43:43 +02:00
--no-modify-profile)
NIX_INSTALLER_NO_MODIFY_PROFILE = 1; ;
2020-05-15 05:59:10 +03:00
--darwin-use-unencrypted-nix-store-volume)
2020-11-28 00:42:15 +02:00
{
echo "Warning: the flag --darwin-use-unencrypted-nix-store-volume"
echo " is no longer needed and will be removed in the future."
echo ""
} >& 2; ;
2020-05-25 18:31:46 +03:00
--nix-extra-conf-file)
2020-11-28 00:42:15 +02:00
# shellcheck disable=SC2155
export NIX_EXTRA_CONF = " $( cat " $2 " ) "
2020-05-25 18:31:46 +03:00
shift; ;
2019-12-15 17:43:43 +02:00
*)
2020-11-28 00:42:15 +02:00
{
2022-10-26 02:43:00 +03:00
echo "Nix Installer [--daemon|--no-daemon] [--daemon-user-count INT] [--yes] [--no-channel-add] [--no-modify-profile] [--nix-extra-conf-file FILE]"
2019-12-15 17:43:43 +02:00
echo "Choose installation method."
echo ""
echo " --daemon: Installs and configures a background daemon that manages the store,"
echo " providing multi-user support and better isolation for local builds."
echo " Both for security and reproducibility, this method is recommended if"
echo " supported on your platform."
2021-12-06 17:42:57 +02:00
echo " See https://nixos.org/manual/nix/stable/installation/installing-binary.html#multi-user-installation"
2019-12-15 17:43:43 +02:00
echo ""
echo " --no-daemon: Simple, single-user installation that does not require root and is"
echo " trivial to uninstall."
echo " (default)"
echo ""
2022-10-26 02:43:00 +03:00
echo " --yes: Run the script non-interactively, accepting all prompts."
echo ""
2019-12-15 17:43:43 +02:00
echo " --no-channel-add: Don't add any channels. nixpkgs-unstable is installed by default."
echo ""
2021-07-23 20:51:37 +03:00
echo " --no-modify-profile: Don't modify the user profile to automatically load nix."
2019-12-15 17:43:43 +02:00
echo ""
2020-05-26 16:49:26 +03:00
echo " --daemon-user-count: Number of build users to create. Defaults to 32."
echo ""
2021-07-23 20:56:30 +03:00
echo " --nix-extra-conf-file: Path to nix.conf to prepend when installing /etc/nix/nix.conf"
2020-05-26 16:49:26 +03:00
echo ""
2020-11-06 12:53:00 +02:00
if [ -n " ${ INVOKED_FROM_INSTALL_IN :- } " ] ; then
echo " --tarball-url-prefix URL: Base URL to download the Nix tarball from."
fi
2020-11-28 00:42:15 +02:00
} >& 2
2019-12-15 17:43:43 +02:00
exit; ;
esac
2020-05-12 19:58:13 +03:00
shift
done
2018-03-28 16:16:13 +03:00
2018-04-19 20:37:54 +03:00
if [ " $INSTALL_MODE " = "daemon" ] ; then
2020-07-07 23:08:23 +03:00
printf '\e[1;31mSwitching to the Multi-user Installer\e[0m\n'
2020-11-28 00:42:15 +02:00
exec " $self /install-multi-user " $ACTIONS # let ACTIONS split
2017-07-09 20:07:28 +03:00
exit 0
fi
2014-02-10 17:35:59 +02:00
if [ " $( id -u) " -eq 0 ] ; then
2014-11-18 15:49:42 +02:00
printf '\e[1;31mwarning: installing Nix as root is not supported by this script!\e[0m\n'
2014-02-10 17:35:59 +02:00
fi
echo "performing a single-user installation of Nix..." >& 2
2021-11-18 19:33:13 +02:00
if ! [ -e " $dest " ] ; then
2014-02-10 17:35:59 +02:00
cmd = " mkdir -m 0755 $dest && chown $USER $dest "
2016-05-18 22:02:48 +03:00
echo " directory $dest does not exist; creating it by running ' $cmd ' using sudo " >& 2
2014-02-10 17:35:59 +02:00
if ! sudo sh -c " $cmd " ; then
2017-07-30 14:27:57 +03:00
echo " $0 : please manually run ' $cmd ' as root to create $dest " >& 2
2014-02-10 17:35:59 +02:00
exit 1
fi
fi
2021-11-18 19:33:13 +02:00
if ! [ -w " $dest " ] ; then
2021-12-06 17:42:57 +02:00
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 https://nixos.org/manual/nix/stable/installation/multi-user.html. If you wish to continue with a single-user install for $USER please run 'chown -R $USER $dest ' as root. " >& 2
2014-02-10 17:35:59 +02:00
exit 1
fi
2022-07-12 17:09:25 +03:00
# The auto-chroot code in openFromNonUri() checks for the
# non-existence of /nix/var/nix, so we need to create it here.
mkdir -p " $dest /store " " $dest /var/nix "
2014-02-10 17:35:59 +02:00
2016-12-02 15:18:50 +02:00
printf "copying Nix to %s..." " ${ dest } /store " >& 2
2021-01-21 18:30:26 +02:00
# Insert a newline if no progress is shown.
if [ ! -t 0 ] ; then
echo ""
fi
2014-02-10 17:35:59 +02:00
2016-12-02 15:18:50 +02:00
for i in $( cd " $self /store " >/dev/null && echo ./*) ; do
2021-01-21 18:30:26 +02:00
if [ -t 0 ] ; then
printf "." >& 2
fi
2014-02-10 17:35:59 +02:00
i_tmp = " $dest /store/ $i . $$ "
if [ -e " $i_tmp " ] ; then
rm -rf " $i_tmp "
fi
if ! [ -e " $dest /store/ $i " ] ; then
2019-12-22 07:30:38 +02:00
cp -RPp " $self /store/ $i " " $i_tmp "
2014-10-07 18:40:31 +03:00
chmod -R a-w " $i_tmp "
chmod +w " $i_tmp "
2014-02-10 17:35:59 +02:00
mv " $i_tmp " " $dest /store/ $i "
2014-10-07 18:40:31 +03:00
chmod -w " $dest /store/ $i "
2014-02-10 17:35:59 +02:00
fi
done
echo "" >& 2
2016-12-02 15:18:50 +02:00
if ! " $nix /bin/nix-store " --load-db < " $self /.reginfo " ; then
2014-02-10 17:35:59 +02:00
echo " $0 : unable to register valid paths " >& 2
exit 1
fi
2012-05-23 01:36:54 +03:00
2020-11-28 00:42:15 +02:00
# shellcheck source=./nix-profile.sh.in
2016-12-02 15:18:50 +02:00
. " $nix /etc/profile.d/nix.sh "
2014-02-10 17:35:59 +02:00
2021-11-17 22:35:21 +02:00
NIX_LINK = " $HOME /.nix-profile "
2016-12-02 15:18:50 +02:00
if ! " $nix /bin/nix-env " -i " $nix " ; then
2014-02-10 17:35:59 +02:00
echo " $0 : unable to install Nix into your default profile " >& 2
2012-05-23 01:36:54 +03:00
exit 1
fi
2014-12-13 17:53:21 +02:00
# Install an SSL certificate bundle.
2016-12-02 15:18:50 +02:00
if [ -z " $NIX_SSL_CERT_FILE " ] || ! [ -f " $NIX_SSL_CERT_FILE " ] ; then
2021-11-18 19:33:13 +02:00
" $nix /bin/nix-env " -i " $cacert "
2021-11-17 22:35:21 +02:00
export NIX_SSL_CERT_FILE = " $NIX_LINK /etc/ssl/certs/ca-bundle.crt "
2014-12-13 17:53:21 +02:00
fi
2014-02-10 11:50:29 +02:00
# Subscribe the user to the Nixpkgs channel and fetch it.
2020-05-12 13:13:40 +03:00
if [ -z " $NIX_INSTALLER_NO_CHANNEL_ADD " ] ; then
2021-11-18 19:33:13 +02:00
if ! " $nix /bin/nix-channel " --list | grep -q "^nixpkgs " ; then
" $nix /bin/nix-channel " --add https://nixos.org/channels/nixpkgs-unstable
2020-05-12 13:13:40 +03:00
fi
if [ -z " $_NIX_INSTALLER_TEST " ] ; then
2021-11-18 19:33:13 +02:00
if ! " $nix /bin/nix-channel " --update nixpkgs; then
2020-05-12 13:13:40 +03:00
echo "Fetching the nixpkgs channel failed. (Are you offline?)"
echo "To try again later, run \"nix-channel --update nixpkgs\"."
fi
2019-02-13 12:28:28 +02:00
fi
2014-11-18 15:49:42 +02:00
fi
2014-02-10 11:50:29 +02:00
2014-02-10 17:35:59 +02:00
added =
2022-09-07 21:58:25 +03:00
p =
2021-11-17 22:35:21 +02:00
p_sh = $NIX_LINK /etc/profile.d/nix.sh
p_fish = $NIX_LINK /etc/profile.d/nix.fish
2016-11-03 19:02:29 +02:00
if [ -z " $NIX_INSTALLER_NO_MODIFY_PROFILE " ] ; then
# Make the shell source nix.sh during login.
for i in .bash_profile .bash_login .profile; do
fn = " $HOME / $i "
if [ -w " $fn " ] ; then
2022-09-07 21:58:25 +03:00
if ! grep -q " $p_sh " " $fn " ; then
2016-11-03 19:02:29 +02:00
echo " modifying $fn ... " >& 2
2022-09-07 21:58:25 +03:00
printf '\nif [ -e %s ]; then . %s; fi # added by Nix installer\n' " $p_sh " " $p_sh " >> " $fn "
2016-11-03 19:02:29 +02:00
fi
added = 1
2022-09-07 21:58:25 +03:00
p = ${ p_sh }
2016-11-03 19:02:29 +02:00
break
2014-02-10 17:35:59 +02:00
fi
2016-11-03 19:02:29 +02:00
done
2019-12-15 22:11:14 +02:00
for i in .zshenv .zshrc; do
fn = " $HOME / $i "
if [ -w " $fn " ] ; then
2022-09-07 21:58:25 +03:00
if ! grep -q " $p_sh " " $fn " ; then
2019-12-15 22:11:14 +02:00
echo " modifying $fn ... " >& 2
2022-09-07 21:58:25 +03:00
printf '\nif [ -e %s ]; then . %s; fi # added by Nix installer\n' " $p_sh " " $p_sh " >> " $fn "
2019-12-15 22:11:14 +02:00
fi
added = 1
2022-09-07 21:58:25 +03:00
p = ${ p_sh }
2019-12-15 22:11:14 +02:00
break
fi
done
2022-09-07 21:58:25 +03:00
if [ -d " $HOME /.config/fish " ] ; then
fishdir = $HOME /.config/fish/conf.d
if [ ! -d " $fishdir " ] ; then
mkdir -p " $fishdir "
fi
fn = " $fishdir /nix.fish "
echo " placing $fn ... " >& 2
printf '\nif test -e %s; . %s; end # added by Nix installer\n' " $p_fish " " $p_fish " > " $fn "
added = 1
p = ${ p_fish }
fi
else
p = ${ p_sh }
2016-11-03 19:02:29 +02:00
fi
2014-02-10 17:35:59 +02:00
if [ -z " $added " ] ; then
cat >& 2 <<EOF
Installation finished! To ensure that the necessary environment
2012-05-23 01:36:54 +03:00
variables are set, please add the line
2014-02-10 17:35:59 +02:00
. $p
2012-05-23 01:36:54 +03:00
to your shell profile ( e.g. ~/.profile) .
EOF
2014-02-10 17:35:59 +02:00
else
cat >& 2 <<EOF
Installation finished! To ensure that the necessary environment
variables are set, either log in again, or type
. $p
in your shell.
EOF
2014-02-10 11:50:29 +02:00
fi