2014-10-07 17:40:31 +02:00
#!/bin/sh
2012-05-23 00:36:54 +02:00
2014-02-10 16:35:59 +01:00
set -e
2012-05-23 00:36:54 +02:00
2014-02-10 16:35:59 +01:00
dest = "/nix"
self = " $( dirname " $0 " ) "
nix = "@nix@"
2014-12-10 16:05:08 +01:00
cacert = "@cacert@"
2014-02-10 16:35:59 +01:00
2017-01-24 19:59:55 +01:00
2016-12-02 14:18:50 +01:00
if ! [ -e " $self /.reginfo " ] ; then
2014-02-10 16:35:59 +01:00
echo " $0 : incomplete installer (.reginfo is missing) " >& 2
fi
2019-07-25 15:39:44 +02:00
if [ -z " $USER " ] && ! USER = $( id -u -n) ; then
2014-02-10 16:35:59 +01:00
echo " $0 : \$USER is not set " >& 2
exit 1
fi
2017-07-09 19:07:28 +02:00
if [ -z " $HOME " ] ; then
echo " $0 : \$HOME is not set " >& 2
exit 1
fi
2018-12-20 20:05:14 +01:00
# macOS support for 10.12.6 or higher
2017-07-09 19:07:28 +02:00
if [ " $( uname -s) " = "Darwin" ] ; then
2020-09-08 06:53:31 +02:00
IFS = '.' read macos_major macos_minor macos_patch << EOF
$( 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 20:05:14 +01:00
echo " $0 : macOS $( sw_vers -productVersion) is not supported, upgrade to 10.12.6 or higher "
2017-07-09 19:07:28 +02:00
exit 1
fi
2018-04-19 19:37:54 +02:00
fi
2017-07-09 19:07:28 +02:00
2018-09-01 14:51:32 +02:00
# Determine if we could use the multi-user installer or not
2018-04-19 19:37:54 +02:00
if [ " $( uname -s) " = "Darwin" ] ; then
2018-09-01 14:51:32 +02:00
echo "Note: a multi-user installation is possible. See https://nixos.org/nix/manual/#sect-multi-user-installation" >& 2
2020-07-07 04:59:18 +02:00
elif [ " $( uname -s) " = "Linux" ] ; then
2018-09-01 14:51:32 +02:00
echo "Note: a multi-user installation is possible. See https://nixos.org/nix/manual/#sect-multi-user-installation" >& 2
2018-04-19 19:37:54 +02:00
fi
2018-09-01 14:51:32 +02:00
INSTALL_MODE = no-daemon
2019-12-15 16:43:43 +01:00
CREATE_DARWIN_VOLUME = 0
2020-05-12 18:58:13 +02:00
# handle the command line flags
2019-12-15 16:43:43 +01:00
while [ $# -gt 0 ] ; do
case $1 in
--daemon)
INSTALL_MODE = daemon; ;
--no-daemon)
INSTALL_MODE = no-daemon; ;
--no-channel-add)
2020-05-26 15:49:26 +02:00
export NIX_INSTALLER_NO_CHANNEL_ADD = 1; ;
2020-02-26 10:50:40 +01:00
--daemon-user-count)
2020-05-26 15:49:26 +02:00
export NIX_USER_COUNT = $2
2020-02-26 10:50:40 +01:00
shift; ;
2019-12-15 16:43:43 +01:00
--no-modify-profile)
NIX_INSTALLER_NO_MODIFY_PROFILE = 1; ;
2020-05-15 04:59:10 +02:00
--darwin-use-unencrypted-nix-store-volume)
2019-12-15 16:43:43 +01:00
CREATE_DARWIN_VOLUME = 1; ;
2020-05-25 17:31:46 +02:00
--nix-extra-conf-file)
2020-05-26 15:49:26 +02:00
export NIX_EXTRA_CONF = " $( cat $2 ) "
2020-05-25 17:31:46 +02:00
shift; ;
2019-12-15 16:43:43 +01:00
*)
(
2020-05-26 15:49:26 +02:00
echo "Nix Installer [--daemon|--no-daemon] [--daemon-user-count INT] [--no-channel-add] [--no-modify-profile] [--darwin-use-unencrypted-nix-store-volume] [--nix-extra-conf-file FILE]"
2019-12-15 16:43:43 +01: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."
echo " See https://nixos.org/nix/manual/#sect-multi-user-installation"
echo ""
echo " --no-daemon: Simple, single-user installation that does not require root and is"
echo " trivial to uninstall."
echo " (default)"
echo ""
echo " --no-channel-add: Don't add any channels. nixpkgs-unstable is installed by default."
echo ""
echo " --no-modify-profile: Skip channel installation. When not provided nixpkgs-unstable"
echo " is installed by default."
echo ""
2020-05-26 15:49:26 +02:00
echo " --daemon-user-count: Number of build users to create. Defaults to 32."
echo ""
echo " --nix-extra-conf-file: Path to nix.conf to prepend when installing /etc/nix.conf"
echo ""
2019-12-15 16:43:43 +01:00
) >& 2
2020-05-15 04:59:10 +02:00
# darwin and Catalina+
2020-09-08 06:53:31 +02:00
if [ " $( uname -s) " = "Darwin" ] && { [ " $macos_major " -gt 10 ] || { [ " $macos_major " -eq 10 ] && [ " $macos_minor " -gt 14 ] ; } ; } ; then
2019-12-15 16:43:43 +01:00
(
2020-05-15 04:59:10 +02:00
echo " --darwin-use-unencrypted-nix-store-volume: Create an APFS volume for the Nix"
echo " store and mount it at /nix. This is the recommended way to create"
echo " /nix with a read-only / on macOS >=10.15."
echo " See: https://nixos.org/nix/manual/#sect-macos-installation"
2019-12-15 16:43:43 +01:00
echo ""
) >& 2
fi
exit; ;
esac
2020-05-12 18:58:13 +02:00
shift
done
2018-03-28 15:16:13 +02:00
2020-02-13 12:57:35 +01:00
if [ " $( uname -s) " = "Darwin" ] ; then
if [ " $CREATE_DARWIN_VOLUME " = 1 ] ; then
printf '\e[1;31mCreating volume and mountpoint /nix.\e[0m\n'
" $self /create-darwin-volume.sh "
fi
fix xpath and conditional bugs; xpath -> xmllint
- xpath -> xmllint: xpath's cli interface changed in Big Sur
rather than add conditional logic for picking the correct
syntax for xpath, I'm changing to xmllint --xpath, which
appears to be consistent across versions I've tested...
- /plist/dict/key[text()='Writable']/following-sibling::true[1]
doesn't do quite what's expected. It was written to try to
select a <true /> node paired with the Writable key, but it
will also select the *next* <true /> node that appears even
if it was paired with another key.
- I think there's also a logic bug in the conditionals here.
I'm not sure anyone ever actuall saw it, thanks to the xpath
bug, though. With the xpath fix, this conditional passes if /nix
does not exist, / IS writable, and the version is Catalina+.
I think it meant to test for /nix does not exist, / is NOT
writable, and the version is Catalina+. I reworked this lightly
to make it a little clearer at the code level.
2020-09-08 07:07:53 +02:00
writable = " $( diskutil info -plist / | xmllint --xpath "name(/plist/dict/key[text()='Writable']/following-sibling::*[1])" -) "
if ! [ -e $dest ] && [ " $writable " = "false" ] ; then
2020-02-13 12:57:35 +01:00
(
echo ""
echo "Installing on macOS >=10.15 requires relocating the store to an apfs volume."
2020-06-23 23:51:09 +02:00
echo "Use sh <(curl -L https://nixos.org/nix/install) --darwin-use-unencrypted-nix-store-volume or run the preparation steps manually."
2020-05-15 04:59:10 +02:00
echo "See https://nixos.org/nix/manual/#sect-macos-installation"
2020-02-13 12:57:35 +01:00
echo ""
) >& 2
2020-03-07 12:01:40 +01:00
exit 1
2020-02-13 12:57:35 +01:00
fi
2019-12-15 16:43:43 +01:00
fi
2018-04-19 19:37:54 +02:00
if [ " $INSTALL_MODE " = "daemon" ] ; then
2020-07-07 22:08:23 +02:00
printf '\e[1;31mSwitching to the Multi-user Installer\e[0m\n'
2018-03-28 15:16:13 +02:00
exec " $self /install-multi-user "
2017-07-09 19:07:28 +02:00
exit 0
fi
2014-02-10 16:35:59 +01:00
if [ " $( id -u) " -eq 0 ] ; then
2014-11-18 14:49:42 +01:00
printf '\e[1;31mwarning: installing Nix as root is not supported by this script!\e[0m\n'
2014-02-10 16:35:59 +01:00
fi
echo "performing a single-user installation of Nix..." >& 2
if ! [ -e $dest ] ; then
cmd = " mkdir -m 0755 $dest && chown $USER $dest "
2016-05-18 21:02:48 +02:00
echo " directory $dest does not exist; creating it by running ' $cmd ' using sudo " >& 2
2014-02-10 16:35:59 +01:00
if ! sudo sh -c " $cmd " ; then
2017-07-30 13:27:57 +02:00
echo " $0 : please manually run ' $cmd ' as root to create $dest " >& 2
2014-02-10 16:35:59 +01:00
exit 1
fi
fi
if ! [ -w $dest ] ; then
2020-03-11 19:41:22 +01: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/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
2014-02-10 16:35:59 +01:00
exit 1
fi
mkdir -p $dest /store
2016-12-02 14:18:50 +01:00
printf "copying Nix to %s..." " ${ dest } /store " >& 2
2014-02-10 16:35:59 +01:00
2016-12-02 14:18:50 +01:00
for i in $( cd " $self /store " >/dev/null && echo ./*) ; do
printf "." >& 2
2014-02-10 16:35:59 +01: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 06:30:38 +01:00
cp -RPp " $self /store/ $i " " $i_tmp "
2014-10-07 17:40:31 +02:00
chmod -R a-w " $i_tmp "
chmod +w " $i_tmp "
2014-02-10 16:35:59 +01:00
mv " $i_tmp " " $dest /store/ $i "
2014-10-07 17:40:31 +02:00
chmod -w " $dest /store/ $i "
2014-02-10 16:35:59 +01:00
fi
done
echo "" >& 2
2016-12-02 14:18:50 +01:00
if ! " $nix /bin/nix-store " --load-db < " $self /.reginfo " ; then
2014-02-10 16:35:59 +01:00
echo " $0 : unable to register valid paths " >& 2
exit 1
fi
2012-05-23 00:36:54 +02:00
2016-12-02 14:18:50 +01:00
. " $nix /etc/profile.d/nix.sh "
2014-02-10 16:35:59 +01:00
2016-12-02 14:18:50 +01:00
if ! " $nix /bin/nix-env " -i " $nix " ; then
2014-02-10 16:35:59 +01:00
echo " $0 : unable to install Nix into your default profile " >& 2
2012-05-23 00:36:54 +02:00
exit 1
fi
2014-12-13 16:53:21 +01:00
# Install an SSL certificate bundle.
2016-12-02 14:18:50 +01:00
if [ -z " $NIX_SSL_CERT_FILE " ] || ! [ -f " $NIX_SSL_CERT_FILE " ] ; then
2014-12-13 16:53:21 +01:00
$nix /bin/nix-env -i " $cacert "
2016-10-13 17:09:10 +02:00
export NIX_SSL_CERT_FILE = " $HOME /.nix-profile/etc/ssl/certs/ca-bundle.crt "
2014-12-13 16:53:21 +01:00
fi
2014-02-10 10:50:29 +01:00
# Subscribe the user to the Nixpkgs channel and fetch it.
2020-05-12 12:13:40 +02:00
if [ -z " $NIX_INSTALLER_NO_CHANNEL_ADD " ] ; then
if ! $nix /bin/nix-channel --list | grep -q "^nixpkgs " ; then
$nix /bin/nix-channel --add https://nixos.org/channels/nixpkgs-unstable
fi
if [ -z " $_NIX_INSTALLER_TEST " ] ; then
if ! $nix /bin/nix-channel --update nixpkgs; then
echo "Fetching the nixpkgs channel failed. (Are you offline?)"
echo "To try again later, run \"nix-channel --update nixpkgs\"."
fi
2019-02-13 11:28:28 +01:00
fi
2014-11-18 14:49:42 +01:00
fi
2014-02-10 10:50:29 +01:00
2014-02-10 16:35:59 +01:00
added =
2019-10-19 05:25:18 +02:00
p = $HOME /.nix-profile/etc/profile.d/nix.sh
2016-11-03 18:02:29 +01: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
if ! grep -q " $p " " $fn " ; then
echo " modifying $fn ... " >& 2
2020-07-18 17:23:43 +02:00
echo -e " \nif [ -e $p ]; then . $p ; fi # added by Nix installer " >> " $fn "
2016-11-03 18:02:29 +01:00
fi
added = 1
break
2014-02-10 16:35:59 +01:00
fi
2016-11-03 18:02:29 +01:00
done
2019-12-15 21:11:14 +01:00
for i in .zshenv .zshrc; do
fn = " $HOME / $i "
if [ -w " $fn " ] ; then
if ! grep -q " $p " " $fn " ; then
echo " modifying $fn ... " >& 2
2020-07-18 17:23:43 +02:00
echo -e " \nif [ -e $p ]; then . $p ; fi # added by Nix installer " >> " $fn "
2019-12-15 21:11:14 +01:00
fi
added = 1
break
fi
done
2016-11-03 18:02:29 +01:00
fi
2014-02-10 16:35:59 +01:00
if [ -z " $added " ] ; then
cat >& 2 <<EOF
Installation finished! To ensure that the necessary environment
2012-05-23 00:36:54 +02:00
variables are set, please add the line
2014-02-10 16:35:59 +01:00
. $p
2012-05-23 00:36:54 +02:00
to your shell profile ( e.g. ~/.profile) .
EOF
2014-02-10 16:35:59 +01: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 10:50:29 +01:00
fi