From 96e958234d1afd880a36d9306e2b9d04431816f8 Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Wed, 15 Jun 2016 18:53:47 +0000 Subject: [PATCH] Add nix installer. Add self extracting nix installer. --- default.nix | 6 ++ install-nix-from-closure.sh | 128 ++++++++++++++++++++++++++++++++++++ nix-installer.nix | 14 ++-- 3 files changed, 138 insertions(+), 10 deletions(-) create mode 100644 install-nix-from-closure.sh diff --git a/default.nix b/default.nix index 1418043..74fa081 100644 --- a/default.nix +++ b/default.nix @@ -6,6 +6,7 @@ let makeself = callPackage ./makeself.nix {}; makedir = callPackage ./makedir.nix {}; + nix-installer = callPackage ./nix-installer.nix {}; makebootstrap = callPackage ./makebootstrap.nix { inherit makedir makeself; @@ -28,4 +29,9 @@ in { run = "/stage1.sh"; }; + nixInstaller = makebootstrap { + name = "nix-installer.sh"; + target = nix-installer; + run = "/install"; + }; } diff --git a/install-nix-from-closure.sh b/install-nix-from-closure.sh new file mode 100644 index 0000000..1105c8f --- /dev/null +++ b/install-nix-from-closure.sh @@ -0,0 +1,128 @@ +#!/bin/sh + +set -e + +dest="/nix" +self="." +nix="@nix@" +cacert="@cacert@" + +if ! [ -e $self/.reginfo ]; then + echo "$0: incomplete installer (.reginfo is missing)" >&2 + exit 1 +fi + +if [ -z "$USER" ]; then + echo "$0: \$USER is not set" >&2 + exit 1 +fi + +if [ "$(id -u)" -eq 0 ]; then + printf '\e[1;31mwarning: installing Nix as root is not supported by this script!\e[0m\n' +fi + +echo "performing a single-user installation of Nix..." >&2 + +if ! [ -e $dest ]; then + cmd="mkdir -m 0755 $dest && chown $USER $dest" + echo "directory $dest does not exist; creating it by running '$cmd' using sudo" >&2 + if ! sudo sh -c "$cmd"; then + echo "$0: please manually run ‘$cmd’ as root to create $dest" >&2 + exit 1 + fi +fi + +if ! [ -w $dest ]; then + 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 + exit 1 +fi + +mkdir -p $dest/store + +echo -n "copying Nix to $dest/store..." >&2 + +for i in $(cd $self/store >/dev/null && echo *); do + echo -n "." >&2 + i_tmp="$dest/store/$i.$$" + if [ -e "$i_tmp" ]; then + rm -rf "$i_tmp" + fi + if ! [ -e "$dest/store/$i" ]; then + cp -Rp "$self/store/$i" "$i_tmp" + chmod -R a-w "$i_tmp" + chmod +w "$i_tmp" + mv "$i_tmp" "$dest/store/$i" + chmod -w "$dest/store/$i" + fi +done +echo "" >&2 + +echo "initialising Nix database..." >&2 +if ! $nix/bin/nix-store --init; then + echo "$0: failed to initialize the Nix database" >&2 + exit 1 +fi + +if ! $nix/bin/nix-store --load-db < $self/.reginfo; then + echo "$0: unable to register valid paths" >&2 + exit 1 +fi + +. $nix/etc/profile.d/nix.sh + +if ! $nix/bin/nix-env -i "$nix"; then + echo "$0: unable to install Nix into your default profile" >&2 + exit 1 +fi + +# Install an SSL certificate bundle. +if [ -z "$SSL_CERT_FILE" -o ! -f "$SSL_CERT_FILE" ]; then + $nix/bin/nix-env -i "$cacert" + export SSL_CERT_FILE="$HOME/.nix-profile/etc/ssl/certs/ca-bundle.crt" +fi + +# Subscribe the user to the Nixpkgs channel and fetch it. +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 + $nix/bin/nix-channel --update nixpkgs +fi + +# Make the shell source nix.sh during login. +p=$HOME/.nix-profile/etc/profile.d/nix.sh + +added= +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 + echo "if [ -e $p ]; then . $p; fi # added by Nix installer" >> $fn + fi + added=1 + break + fi +done + +if [ -z "$added" ]; then + cat >&2 <&2 <