From d1fbf16ac6b383656562bd60fe47dba2945d5f59 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 18:11:08 +0300 Subject: [PATCH 01/46] add note for #!/bin/sh --- scripts/install-nix-from-closure.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index ab20774bbf0..dd16887df9d 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -1,4 +1,8 @@ #!/bin/sh +## NOTE: If you would decide to change shebang to `/usr/bin/env sh` +## - investigate the question more to be really sure, +## as `/usr/bin/env sh` usage is less portable then `/bin/sh`, +## at least - using env is much less secure. set -e From b07ce08d62d49f110149354136913c541aaae7b6 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 18:31:05 +0300 Subject: [PATCH 02/46] add 'Main information' --- scripts/install-nix-from-closure.sh | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index dd16887df9d..ebd8e613c67 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -4,6 +4,42 @@ ## as `/usr/bin/env sh` usage is less portable then `/bin/sh`, ## at least - using env is much less secure. + +############################### +### Main information +############################### +{ + +# +# Nix installation script +# Shell script for POSIX-comapible environments +# +############################### +# +# Upstream URL: +# https://github.com/NixOS/nix/blob/master/scripts/install-nix-from-closure.sh +# +# Currently script follows POSIX.1-2017 (POSIX is simultaneously the +# IEEE Std 1003.1™-2017 and the +# The Open Group Technical Standard Base Specifications, Issue 7) +# POSIX standard is accessible at: +# http://pubs.opengroup.org/onlinepubs/9699919799 +# +# Script strives to be fully transactional, as much as shell script can be. +# That means that only after all required checks script starts to do changes. +# And if script not succeeds in some action - it catches the error and +# rolls back, if that is possible with a shell script. +# +# If you foud a way to improve the script - let us know. +# +# +# Additional notes: +# `/bin/sh -u` is not possible to do, because many Docker environments +# have unset USER variable. +# +true # so {...} body has some code, shell will not permit otherwise + +} set -e dest="/nix" From b76493ad81d79655bc8ae2f95b3ecbc1488b1ec5 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Thu, 16 Aug 2018 21:04:42 +0300 Subject: [PATCH 03/46] add 'Documentation' --- scripts/install-nix-from-closure.sh | 75 +++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index ebd8e613c67..e5cbdd6c687 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -39,6 +39,81 @@ # true # so {...} body has some code, shell will not permit otherwise +} + + +############################### +### Documentation +############################### +{ + +# +# Installer consist of: +# 1. Setup environment +# 2. Main constants +# 3. CLI control constants +# 4. CLI output functions +# 5. Program stage functions +# 6. Main function +# 7. Invocation of the main function (aka "Start of the script") +# +# + +# Special things about this script +# +# 1) Script tries to be fully POSIX compatible, +# code heavy follows that requirement. +# +# 2) Notice, Warning, Error, ErrorRevert level massages have a special +# functions. That is done to be: +# * uniformal in output +# * proper color highlihtgting +# * proper message classification +# * informative for the user +# * convinient for use in the code +# * code readability +# * less function invocations +# * to have an extendable and editable output system in a shell script +# all at the same time. +# +# Message body starts from a new line. +# And has 4 spaces from the left. Always. +# +# Code example: +############################### +# +# notice " +# +# Install executed for ROOT. +# +# Doing classic Linux package manager mode. +# In Nix this mode is called: single-user mode for root. +# +# Nix can do multi-user mode, and manage package trees for users +# independently. +# " +# +############################### +# +# This is the best balance of code simplicity and code readability found so far. +# +# Output of the example (in a green color): +############################### +# +#./install: Notice: +# +# Install executed for ROOT. +# +# Doing classic Linux package manager mode. +# In Nix this mode is called: single-user mode for root. +# +# Nix can do multi-user mode, and manage package trees for users +# independently. +# +############################### +# +true # so {...} body has some code, shell will not permit otherwise + } set -e From 3c9847d8da05471d6470c22aa61a208dfcfdea32 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Thu, 16 Aug 2018 19:27:13 +0300 Subject: [PATCH 04/46] add 'Setup environment' rm set -e --- scripts/install-nix-from-closure.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index e5cbdd6c687..c373379d16a 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -115,8 +115,22 @@ true # so {...} body has some code, shell will not permit otherwise true # so {...} body has some code, shell will not permit otherwise } -set -e + +############################### +### Setup environment +############################### +{ + +# Set the character collating sequence to be numeric ASCII/C standard. +readonly LC_COLLATE=C +# Set the character set to be the standard one-byte ASCII. +readonly LANG=C +# Set default umask; to be non-restrictive and friendly to others. +# umask obviously can heavy influence Nix work, and functioning of packages. +umask 022 + +} dest="/nix" self="$(dirname "$0")" nix="@nix@" From 09f40785ba515be88bb71a991e32c9d67b1bbd24 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 18:51:29 +0300 Subject: [PATCH 05/46] add 'Main constants' --- scripts/install-nix-from-closure.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index c373379d16a..e36ae027c2d 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -130,6 +130,27 @@ readonly LANG=C # umask obviously can heavy influence Nix work, and functioning of packages. umask 022 +} + + +############################### +### Main constants +############################### +{ + +# NOTE: If you are changing the destanation from `/nix` to something other - +# please read the link: +# https://nixos.org/nixos/nix-pills/install-on-your-running-system.html#idm140737316619344 +# TL;DR: Destination path change - changes the package hashes. +# Nix relies on hashes to determine packages. +# Local hashes & the central binary cache repository hashes not going to match. +# That mean that Nix going to compile packages from sources. +readonly dest='/nix' +readonly self="$(dirname "$(realpath "$0")")" +readonly nix="@nix@" +readonly cacert="@cacert@" +readonly appname="$0" + } dest="/nix" self="$(dirname "$0")" From 3e652a4ccb6f9c09469638833dfe4251b88b9973 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 18:51:56 +0300 Subject: [PATCH 06/46] add 'CLI control constants' --- scripts/install-nix-from-closure.sh | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index e36ae027c2d..ad21b4d5ff0 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -151,6 +151,44 @@ readonly nix="@nix@" readonly cacert="@cacert@" readonly appname="$0" +} + + +############################### +### CLI control constants +############################### +{ + +# This chain is created to output color for normal circumstances. +# If CI desides to report to the processes that they run in a terminal, +# while at the same time CI not took effort to parse the terminal color +# commands, and also not took effort to have according to it's possibilities +# $TERM configuration record in a termcap DB - well it is really on a CI. + +# If the file descriptor reports a terminal - then try to use correct DB colors +if test -t ; then + # If terminal reports a terminfo/termcap DB of colors available - use DB. + # Else - use literal color codes. + if tput colors > /dev/null 2>&1 ; then + # use tput and terminfo DB + readonly red=$(tput setaf 1) + readonly green=$(tput setaf 2) + readonly yellow=$(tput setaf 3) + readonly blue=$(tput setaf 4) + readonly bold=$(tput smso) + readonly reset=$(tput sgr0) # Reset to default output + else + # tput is not present on some systems (Alpine Linux), + # this trick allows to store, not 'codes' - literal term command symbol. + readonly red=$(printf '\033[1;31m') + readonly green=$(printf '\033[1;32m') + readonly yellow=$(printf '\033[1;33m') + readonly blue=$(printf '\033[1;34m') + readonly bold=$(printf '\033[1m') + readonly reset=$(printf '\033[0;m') # Reset to default output + fi +fi + } dest="/nix" self="$(dirname "$0")" From a87ca35e0ad8e82dc2a4e1ab505e0775a01e9c0a Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 18:52:43 +0300 Subject: [PATCH 07/46] add 'CLI output functions' --- scripts/install-nix-from-closure.sh | 100 ++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 4 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index ad21b4d5ff0..d23636df89a 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -190,12 +190,104 @@ if test -t ; then fi } -dest="/nix" -self="$(dirname "$0")" -nix="@nix@" -cacert="@cacert@" +############################### +### CLI output functions +############################### +{ + +# NOTE: Script output corresponds to the classification of massages +# [RFC 5424](https://tools.ietf.org/html/rfc5424) - "The Syslog Protocol" +# Standard holds an industy-wide agreed criterias for messages. +# For example, systemd/journald messages fully correspond to RFC 5424. + +# NOTE: Unified output function +# Every message in the script gets eventually printed by this function +print() { + # Using `printf`, because it is more portable than `echo`. + + # Would take message from "$message", or from the first argument. + # So using it for both flaxibly chaining for any functions and both + # a as simple: + # print 'Output message to user' + # - are both possible at the same time. + if [ -z "$message" ]; then + message="$1" + fi + if [ -z "$color" ]; then + color="$reset" + fi + if [ -z "$prefix" ]; then + prefix='Info' + fi + + # This line makes all prints in script + # Form of message: + # Application: Prefix: Body of message + printf '%s%s: %s: %s%s\n' "$color" "$appname" "$prefix" "$message" "$reset" + + # At this lines, output of message is done. So unset print variables. + unset color + unset prefix + unset message +} + +# Since 'info' name is already taken by the well known Unix textinfo reader - +# just use 'print' for Info level messages + +notice() { + message="$1" + color="$green" + prefix='Notice' + print +} + +warning() { + message="$1" + color="$yellow" + prefix='Warning' + >&2 print +} + +# NOTE: 'error' throws the exit signal +error() { + message="$1" + exitSig="$2" + color="$red" + prefix='Error' + >&2 print + if [ -z "$exitSig" ]; then + exit 1 + fi +} + +# NOTE: 'errorRevert' is a function to print messages in 'error' form +# when script catches an error and during a revert. +# It does not throw exit signal, so we can keep add to the error message +# before we decide in the end to launch the 'error' that throws exit signal. +errorRevert() { + message="$1" + color="$red" + prefix='Error' + >&2 print +} + +contactUs() { + print ' + + To search/open bugreports: https://github.com/nixos/nix/issues + + To contact the team and community: + - IRC: #nixos on irc.freenode.net + - Twitter: @nixos_org + + Matrix community rooms: https://matrix.to/#/@nix:matrix.org + https://matrix.to/#/@nixos:matrix.org + ' +} + +} if ! [ -e "$self/.reginfo" ]; then echo "$0: incomplete installer (.reginfo is missing)" >&2 fi From bb52676a49fda27fcc5368d8bd7fe07222197e3e Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 18:53:22 +0300 Subject: [PATCH 08/46] add 'Checking requirements' stub --- scripts/install-nix-from-closure.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index d23636df89a..01a6b55afea 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -287,6 +287,19 @@ contactUs() { ' } +} + + +############################### +### Checking requirements +############################### +{ + +checkingRequirements() { + # NOTE: This function - checks only, - do not make any changes to the system. + # And becouse of that and POSIx - it can be universally reused. +} + } if ! [ -e "$self/.reginfo" ]; then echo "$0: incomplete installer (.reginfo is missing)" >&2 From 9a28898a00b24d33b418432b8927f6cf7c04034b Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 19:17:47 +0300 Subject: [PATCH 09/46] Checking requirements: add checkBundle() --- scripts/install-nix-from-closure.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 01a6b55afea..98a4e6265a7 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -296,15 +296,20 @@ contactUs() { { checkingRequirements() { - # NOTE: This function - checks only, - do not make any changes to the system. - # And becouse of that and POSIx - it can be universally reused. -} +# NOTE: This function - checks only, - do not make any changes to the system. +# And becouse of that and POSIx - it can be universally reused. + + checkBundle() { + if ! [ -e "$self/.reginfo" ]; then + error " + Installer is incomplete ('$self/.reginfo' is missing) + " + fi + } } -if ! [ -e "$self/.reginfo" ]; then - echo "$0: incomplete installer (.reginfo is missing)" >&2 -fi +} if [ -z "$USER" ]; then echo "$0: \$USER is not set" >&2 exit 1 From 97ee05a22845f0d37c7abea3ceba4da7c077403b Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 12:45:41 +0300 Subject: [PATCH 10/46] Checking requirements: add checkEnv() stub --- scripts/install-nix-from-closure.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 98a4e6265a7..a55968a3bd1 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -307,6 +307,9 @@ checkingRequirements() { " fi } + + checkEnv() { + } } } From ccbd36067245e86dfea047acc13764f721e3c747 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 17:54:59 +0300 Subject: [PATCH 11/46] Checking requirements: checkEnv(): add check USER=root --- scripts/install-nix-from-closure.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index a55968a3bd1..f4947d3e5ef 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -309,6 +309,24 @@ checkingRequirements() { } checkEnv() { + + if [ "$(id -u)" -eq 0 ]; then + # TODO: At least merge single/multiuser, + # when https://github.com/NixOS/nix/issues/1559 solved. + # TODO: Reword after scripts integration and option switching + # becomes clear. + notice " + + Install executed for ROOT. + + Doing systemwide root install - this is classic Linux package manager mode. + In Nix this mode is called: single-user mode for root. + + Nix has a multi-user mode. + That is the main Nix mode, + it allows users manage their own independent trees of packages. + " + fi } } From 077d77389937faca6d3ec87f9159105acb4d526c Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Thu, 16 Aug 2018 18:40:26 +0300 Subject: [PATCH 12/46] rm check legacy Root --- scripts/install-nix-from-closure.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index f4947d3e5ef..e0d23ed048e 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -388,9 +388,6 @@ if [ "$INSTALL_MODE" = "daemon" ]; then exit 0 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 From 742a02c6cf5c6d1202b4b100f03e59897a5dee2e Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 17:55:16 +0300 Subject: [PATCH 13/46] Checking requirements: checkEnv(): add check [ -z "$USER" ] --- scripts/install-nix-from-closure.sh | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index e0d23ed048e..5f7a4ae73f3 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -327,15 +327,21 @@ checkingRequirements() { it allows users manage their own independent trees of packages. " fi + + # In case USER is not set + # Example: running inside container + if [ -z "$USER" ]; then + notice " + + Environment variable USER is not set. + " + readonly USER="$(id -u -n)" # id is POSIX + print "Detected username: $USER" + fi } } } -if [ -z "$USER" ]; then - echo "$0: \$USER is not set" >&2 - exit 1 -fi - if [ -z "$HOME" ]; then echo "$0: \$HOME is not set" >&2 exit 1 From 9e58356e3f6070ca22f0c99b31aee7a6d1a6effd Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 17:56:58 +0300 Subject: [PATCH 14/46] Checking requirements: checkEnv(): add check [ -z "$HOME" ] --- scripts/install-nix-from-closure.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 5f7a4ae73f3..644bbce19a7 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -338,15 +338,18 @@ checkingRequirements() { readonly USER="$(id -u -n)" # id is POSIX print "Detected username: $USER" fi + + if [ -z "$HOME" ]; then + error " + + Environment variable HOME is not set. + " + fi + } } } -if [ -z "$HOME" ]; then - echo "$0: \$HOME is not set" >&2 - exit 1 -fi - # macOS support for 10.10 or higher if [ "$(uname -s)" = "Darwin" ]; then if [ $(($(sw_vers -productVersion | cut -d '.' -f 2))) -lt 10 ]; then From 53a6fe2203f6ac1c050c40948e7cb22fdf97f8c9 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 13:23:26 +0300 Subject: [PATCH 15/46] Checking requirements: add checkHome() stub --- scripts/install-nix-from-closure.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 644bbce19a7..3f7afe4f4f8 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -347,6 +347,9 @@ checkingRequirements() { fi } + + checkHome() { + } } } From 79322b53469824ca77eaf300c6cf1ede878c9efa Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 17:59:01 +0300 Subject: [PATCH 16/46] Checking requirements: checkHome(): add check [ ! -e "$HOME" ] --- scripts/install-nix-from-closure.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 3f7afe4f4f8..93066d35a3c 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -349,6 +349,13 @@ checkingRequirements() { } checkHome() { + + if [ ! -e "$HOME" ]; then + error " + + Home directory '$HOME' does not exist. + " + fi } } From c853fe70dd372d2322e439c446375c5423981e49 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 17:59:20 +0300 Subject: [PATCH 17/46] Checking requirements: checkHome(): add check [ ! -d "$HOME" ] --- scripts/install-nix-from-closure.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 93066d35a3c..e037637ce1f 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -354,6 +354,14 @@ checkingRequirements() { error " Home directory '$HOME' does not exist. + " + fi + + # -d also resolves symbolic soft links if they point to directory + if [ ! -d "$HOME" ]; then + error " + + Home directory '$HOME' is not a directory, nor a link to one. " fi } From a494de960db6deefc429bac50b0a4e4c8a962fe4 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 17:59:52 +0300 Subject: [PATCH 18/46] Checking requirements: checkHome(): add check [ ! -w "$HOME" ] --- scripts/install-nix-from-closure.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index e037637ce1f..4d876b456d1 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -364,6 +364,14 @@ checkingRequirements() { Home directory '$HOME' is not a directory, nor a link to one. " fi + + if [ ! -w "$HOME" ]; then + error " + + Home directory '$HOME' is not writable for user '$USER'. No deal. + " + fi + } } From 22f0cd3b785150ee7fca3aeae37f64d900aa6ff6 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 13:45:23 +0300 Subject: [PATCH 19/46] Checking requirements: checkHome(): add check HOME owned by USER --- scripts/install-nix-from-closure.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 4d876b456d1..de76698eee4 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -369,6 +369,18 @@ checkingRequirements() { error " Home directory '$HOME' is not writable for user '$USER'. No deal. + " + fi + + # POSIX: `ls` is only able. No `test -O`, `find` can do this in POSIX + # AWK is more portable then `cut -d'c' -fN` + if [ "$(ls -ld "$HOME" | awk '{print $3}')" != "$USER" ]; then + contactUs # Let's get particular reports and solutions + error " + + Home directory '$HOME' is not owned by user '$USER'. + If you have legitimate case, please file a bug with description. + We gather information on particular cases. " fi From 9f4d72612ff4a1d9ac54a2af838d5a42e0b597c1 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 13:45:41 +0300 Subject: [PATCH 20/46] Checking requirements: checkHome(): add check [ ! -x "$HOME" ] --- scripts/install-nix-from-closure.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index de76698eee4..228ac3d2a2a 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -381,6 +381,14 @@ checkingRequirements() { Home directory '$HOME' is not owned by user '$USER'. If you have legitimate case, please file a bug with description. We gather information on particular cases. + " + fi + + if [ ! -x "$HOME" ]; then + error " + + Home directory '$HOME' is not marked as executable for user '$USER', + how then we are going to go into it? " fi From 47eb7585fde45e5b17e6608eeed9578df9c3d984 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 13:56:15 +0300 Subject: [PATCH 21/46] Checking requirements: add checkDest() stub --- scripts/install-nix-from-closure.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 228ac3d2a2a..6559f7dec9a 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -393,6 +393,9 @@ checkingRequirements() { fi } + + checkDest() { + } } } From adf55e0a08df1dce40adbafbf3ecf9c7daf83cc4 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 18:00:29 +0300 Subject: [PATCH 22/46] Checking requirements: checkDest(): add check [ -e "$dest" ] --- scripts/install-nix-from-closure.sh | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 6559f7dec9a..8a31322524d 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -395,6 +395,17 @@ checkingRequirements() { } checkDest() { + + if [ -e "$dest" ]; then + # Destination directory exist. Nix is/was installed before, be cautious. + + # Once more -d also resolves soft links to their targets + if [ ! -d "$dest" ]; then + error " + + Destination '$dest' exists, but is not a directory, nor a link to one. + " + fi } } @@ -449,14 +460,6 @@ 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 From de89e2b0812020ac0863b37caf92ab7246c35941 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 13:58:15 +0300 Subject: [PATCH 23/46] Checking requirements: checkDest(): add check [ ! -w $dest] --- scripts/install-nix-from-closure.sh | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 8a31322524d..227920678dc 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -404,6 +404,25 @@ checkingRequirements() { error " Destination '$dest' exists, but is not a directory, nor a link to one. + " + fi + + if [ ! -w $dest ]; then + # Do not mindlessly help a user to chroot the directory + # - that is a disservice. + # Let user who does not know - at least a chanse to search and read + # on the topic. + # Person needs to know/find command themselves, and know about + # consequences. + error " + + Destination directory '$dest' exists, but is not writable for user '$USER'. + + To enable multi-user support see: + http://nixos.org/nix/manual/#ssec-multi-user + + To nevertheless do a single-user install for '$USER': + recursively set user '$USER' as owner for '$dest' directory. " fi } @@ -461,10 +480,6 @@ fi echo "performing a single-user installation of Nix..." >&2 -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 From 1b03edc94d9444d19c96151d047622a7048206fd Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 14:00:27 +0300 Subject: [PATCH 24/46] Checking requirements: checkDest(): add message --- scripts/install-nix-from-closure.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 227920678dc..283a5f9e010 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -425,6 +425,14 @@ checkingRequirements() { recursively set user '$USER' as owner for '$dest' directory. " fi + + # If checks are OK + warning " + + Destination directory '$dest' already exists. Skipping creation of '$dest'. + " + + fi } } From 3d5e882460cd9a4eda9b26011f152614edea2158 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 17:41:01 +0300 Subject: [PATCH 25/46] Checking requirements: checkDest(): add if [ -e "$dest"/store ] block --- scripts/install-nix-from-closure.sh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 283a5f9e010..7f3fe53af60 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -433,6 +433,35 @@ checkingRequirements() { " fi + + if [ -e "$dest"/store ]; then + + if [ ! -d "$dest"/store ]; then + error " + + Store directory '$dest/store' exists and it's not a directory nor a link + to one. + " + fi + + if [ ! -w "$dest"/store ]; then + error " + + Store directory '$dest/store' exists, but is not writable for user '$USER'. + + 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 + + To nevertheless do a single-user install for '$USER': + recursively set user '$USER' as owner for '$dest/store' directory. + " + fi + + fi + } } From ff4e9aebbec6c7b0926bea4678ff046d05a827a7 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 17:46:52 +0300 Subject: [PATCH 26/46] Checking requirements: add mainChechingRequirements() --- scripts/install-nix-from-closure.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 7f3fe53af60..4fd557f8c34 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -463,6 +463,19 @@ checkingRequirements() { fi } + + # Invocation of functions + mainCheckingRequirements() { + + checkBundle + + checkEnv + + checkHome + + checkDest + + } } } From 764d314dc9148acb95932abbd964986cc3252a9c Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 17:47:42 +0300 Subject: [PATCH 27/46] Checking requirements: add execution start --- scripts/install-nix-from-closure.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 4fd557f8c34..8ac460e9951 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -476,6 +476,10 @@ checkingRequirements() { checkDest } + + # Execute the whole checkingRequirements() + mainCheckingRequirements + } } From e9ab789f86c81cad5368df1bdfbce659e40b41bb Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sun, 30 Dec 2018 01:58:01 +0200 Subject: [PATCH 28/46] add 'Check installer mode' rm old check system/mode rm old check key mode --- scripts/install-nix-from-closure.sh | 70 +++++++++++++++-------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 8ac460e9951..f9587966781 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -482,6 +482,43 @@ checkingRequirements() { } +} + + +############################### +### Check installer mode +############################### +{ + +checkInstallerMode() { + # Determine what mode of installer should be used + if [ "$(uname -s)" = "Darwin" ]; then + echo "Note: a multi-user installation is possible. See https://nixos.org/nix/manual/#sect-multi-user-installation" >&2 + elif [ "$(uname -s)" = "Linux" ] && [ -e /run/systemd/system ]; then + echo "Note: a multi-user installation is possible. See https://nixos.org/nix/manual/#sect-multi-user-installation" >&2 + fi + + INSTALL_MODE=no-daemon + # Trivially handle the --daemon / --no-daemon options + if [ "x${1:-}" = "x--no-daemon" ]; then + readonly INSTALL_MODE=no-daemon + elif [ "x${1:-}" = "x--daemon" ]; then + readonly INSTALL_MODE=daemon + elif [ "x${1:-}" != "x" ]; then + error ' + + Nix Installer [--daemon|--no-daemon] + --daemon: Force the installer to use the Daemon + based installer, even though it may not + work. + + --no-daemon: Simple, single-user installation that does not require root + and that is trivial to uninstall + (default) + ' + fi +} + } # macOS support for 10.10 or higher if [ "$(uname -s)" = "Darwin" ]; then @@ -491,39 +528,6 @@ if [ "$(uname -s)" = "Darwin" ]; then fi fi -# Determine if we could use the multi-user installer or not -if [ "$(uname -s)" = "Darwin" ]; then - echo "Note: a multi-user installation is possible. See https://nixos.org/nix/manual/#sect-multi-user-installation" >&2 -elif [ "$(uname -s)" = "Linux" ] && [ -e /run/systemd/system ]; then - echo "Note: a multi-user installation is possible. See https://nixos.org/nix/manual/#sect-multi-user-installation" >&2 -fi - -INSTALL_MODE=no-daemon -# Trivially handle the --daemon / --no-daemon options -if [ "x${1:-}" = "x--no-daemon" ]; then - INSTALL_MODE=no-daemon -elif [ "x${1:-}" = "x--daemon" ]; then - INSTALL_MODE=daemon -elif [ "x${1:-}" != "x" ]; then - ( - echo "Nix Installer [--daemon|--no-daemon]" - - 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 "" - ) >&2 - exit -fi - if [ "$INSTALL_MODE" = "daemon" ]; then printf '\e[1;31mSwitching to the Daemon-based Installer\e[0m\n' exec "$self/install-multi-user" From 3644f8cb48590390417403591bcffec2d1a1914a Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sun, 30 Dec 2018 01:59:34 +0200 Subject: [PATCH 29/46] add 'Exec installer mode' rm old check macOS bridge rm old bridge to daemon systemd --- scripts/install-nix-from-closure.sh | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index f9587966781..f154ce75048 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -520,21 +520,24 @@ checkInstallerMode() { } } -# macOS support for 10.10 or higher -if [ "$(uname -s)" = "Darwin" ]; then - if [ $(($(sw_vers -productVersion | cut -d '.' -f 2))) -lt 10 ]; then - echo "$0: macOS $(sw_vers -productVersion) is not supported, upgrade to 10.10 or higher" - exit 1 - fi -fi -if [ "$INSTALL_MODE" = "daemon" ]; then - printf '\e[1;31mSwitching to the Daemon-based Installer\e[0m\n' - exec "$self/install-multi-user" - exit 0 -fi +############################### +### Exec installer mode +############################### +{ +execInstallerMode() { + if [ "$INSTALL_MODE" = "daemon" ]; then + notice ' + + Entering daemon-based installer + ' + exec "$self/install-multi-user" + fi +} + +} echo "performing a single-user installation of Nix..." >&2 From 688870bef240aeef8060cb21492a499f5d5e0cdc Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 18:59:57 +0300 Subject: [PATCH 30/46] add 'Install Nix' stub --- scripts/install-nix-from-closure.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index f154ce75048..14fd81438ef 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -537,6 +537,17 @@ execInstallerMode() { fi } +} + + +############################### +### Install Nix +############################### +{ + +installNix() { +} + } echo "performing a single-user installation of Nix..." >&2 From 0ccb8aa8d4aa113db76f25ca377058fbce13ac0c Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 19:00:39 +0300 Subject: [PATCH 31/46] Install Nix: add description --- scripts/install-nix-from-closure.sh | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 14fd81438ef..0d879257782 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -546,6 +546,42 @@ execInstallerMode() { { installNix() { + + # In this function, if some action makes any change to the system, + # or error is possible - that needs to be wrapped in a function. + # For example, having func(){...}. + # Inside func() you start with setup of traps, before errors/actions happen. + # So when catch happen - according revert functions be launched: + # 'trap funcRevertOnCondition condition'. + # And funcRevertOnCondition is described right after the trap. + # + # Once more, now in code. How all actions operate inside this function: + #************************************************** + # + # installNix() { + # + # func() { + # + # trap 'funcRevertOnCondition $LINENO $?' condition + # funcRevertOnCondition() { + # errorRevert "Received Error signal from line $1" + # # revert action + # >&2 contactUs # This function gives contact information + # exit "$?" + # } + # + # # action of functions + # # 'exit SIG # to the trap that catches exit SIG' + # } + # + # func + # + # } + # + #************************************************** + + # Nix is pure, and claims to be transactional. + # So show people clean transactional installation. } } From 348000f67a0adc60dc95039fe6a6b266811aa9f5 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 03:47:08 +0300 Subject: [PATCH 32/46] Install Nix: add createDest() --- scripts/install-nix-from-closure.sh | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 0d879257782..19365062c50 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -582,6 +582,54 @@ installNix() { # Nix is pure, and claims to be transactional. # So show people clean transactional installation. + + createDest() { + + trap destCreateRevert 1 + + destCreateRevert() { + # all means failed, Exit! + error " + + Can't get root access to create directory '$dest'. All variants failed. + Exiting... + " + } + + # Shell parses variable with current user, so value under sudo/su not changes. + # It is const, but don't move it above USER set/checks. + readonly cmd="mkdir -m 0755 $dest && chown $USER $dest" + + # Trying to create $dest by all means + + # If user has rights to successfully execute $cmd. Else -> try to elevate rights section + if sh -c "$cmd" > /dev/null 2>&1 ; then + print "Creating $dest directory by running $cmd using $USER rights" + else + if command -v sudo > /dev/null 2>&1 ; then + print "Creating $dest directory by running $cmd using sudo:" + # Using sudo to create directory + if ! sudo sh -c "$cmd" && print "Exiting privileged mode." ; then + # It is not Warning by rules, by logging rules goes to stderr + >&2 notice " + + Could not succefully execute command with 'sudo'. + " + fi + else + print "'sudo' seems not installed" + fi + + fi + + # If dest still not created (with user or sudo) - use su to access root + if [ ! -d "$dest" ] ; then + print 'Trying to get privileges using su and root password' + if ! su root sh -c "$cmd" && print 'Exiting priviliged mode.'; then + exit 1 # Look into the trap + fi + fi + } } } From 07cf67acb2356c559b3560ff993ccfc457ff1f1d Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 03:47:45 +0300 Subject: [PATCH 33/46] Install Nix: add createStore() rm old mkdir $dest/store --- scripts/install-nix-from-closure.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 19365062c50..270063e8852 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -630,15 +630,28 @@ installNix() { fi fi } -} - -} -echo "performing a single-user installation of Nix..." >&2 + createStore() { + trap createStoreRevert 1 + createStoreRevert() { + error " + Script tried to create '$dest/store', but it already exists. + We do checks '$dest/store' existence before installation. + Probaly something went wrong with the installation script. -mkdir -p $dest/store + Please: + * gather information of your setup conditions + * search does BUG exists + * make further actions + " + } + mkdir -p "$dest"/store + } +} +} +echo "performing a single-user installation of Nix..." >&2 printf "copying Nix to %s..." "${dest}/store" >&2 for i in $(cd "$self/store" >/dev/null && echo ./*); do From 945e6e6e63017ef7c976aa4a98f25ea6cb0a07f1 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 03:48:11 +0300 Subject: [PATCH 34/46] Install Nix: add copyNix() rm old message copying Nix rm old store fill-up --- scripts/install-nix-from-closure.sh | 47 ++++++++++++++++++----------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 270063e8852..788132c6f8d 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -648,28 +648,39 @@ installNix() { } mkdir -p "$dest"/store } + + copyNix() { + print "Copying Nix to '$dest/store': " + + for object in $(cd "$self/store" >/dev/null && echo ./*); do + printf '.' + get="$self/store/$object" + tmp="$dest/store/$object.$$" ## $$ - is process PID + put="$dest/store/$object" + + # If $dest/store/$object.$$ exists - remove. Very certain - it is a directory from previous cycle. + if [ -e "$tmp" ]; then + rm -rf "$tmp" + fi + # If $put does not exist - populate it. + if ! [ -e "$put" ]; then + # Copy $get to $tmp + cp -Rp "$get" "$tmp" + # Remove write permissions from $tmp recursively + chmod -R a-w "$tmp" + # Place $tmp as $put + mv "$tmp" "$put" + else + print "Already exists, skipping: '$put'" + fi + + done + printf '\n' + } } } echo "performing a single-user installation of Nix..." >&2 -printf "copying Nix to %s..." "${dest}/store" >&2 - -for i in $(cd "$self/store" >/dev/null && echo ./*); do - printf "." >&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 From 9787e74ac9782b9ce9e75a9598d0562d04c454ef Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 03:48:30 +0300 Subject: [PATCH 35/46] Install Nix: add initializeDB() rm old message installing Nix database rm old nix-store --init initialization rm old ! nix-store --load-db --- scripts/install-nix-from-closure.sh | 31 +++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 788132c6f8d..f37046d1562 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -677,21 +677,30 @@ installNix() { done printf '\n' } + + initializeDB() { + notice " + + Initializing Nix database... + " + if ! "$nix/bin/nix-store" --init; then + error " + + Failed to initialize the Nix database + " + fi + + if ! "$nix/bin/nix-store" --load-db < "$self/.reginfo"; then + error " + + Unable to register valid paths + " + fi + } } } echo "performing a single-user installation of Nix..." >&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 From 6fc195967dc3e059297bdabb05461731168c14da Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 03:48:55 +0300 Subject: [PATCH 36/46] Install Nix: add setupProfile() rm old sourcing nix.sh rm old ! nix-env -i --- scripts/install-nix-from-closure.sh | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index f37046d1562..ea187ec5998 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -697,17 +697,25 @@ installNix() { " fi } + + setupProfile() { + # Modifying current runtime environment variables to use Nix. + # Persistent changes are configured later. + print "Launching $nix/etc/profile.d/nix.sh" + # shellcheck source=/dev/null + . "$nix/etc/profile.d/nix.sh" + print "Left $nix/etc/profile.d/nix.sh" + if ! "$nix/bin/nix-env" -i "$nix"; then + error " + + Unable to install Nix into your default profile + " + fi + } } } echo "performing a single-user installation of Nix..." >&2 -. "$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 "$NIX_SSL_CERT_FILE" ] || ! [ -f "$NIX_SSL_CERT_FILE" ]; then $nix/bin/nix-env -i "$cacert" From cd94065d24e0fe79235d726dcad6944d28717181 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 03:49:18 +0300 Subject: [PATCH 37/46] Install Nix: add installCerts() rm old install certificates --- scripts/install-nix-from-closure.sh | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index ea187ec5998..8afde2813ba 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -712,16 +712,28 @@ installNix() { " fi } + + installCerts() { + # Install an SSL certificate bundle. + trap 'installCertsRevert $LINENO $?' INT TERM ABRT QUIT + subscribeChannelsRevert() { + errorRevert "Received Error signal from line $1" + errorRevert "Reverting installation of certificate bundle" + "$nix/bin/nix-env" --uninstall "$cacert" + error " + + Changes reverted. + " "$2" + } + print 'Installing SSL certificate bundle.' + "$nix/bin/nix-env" --install "$cacert" + readonly NIX_SSL_CERT_FILE="$HOME/.nix-profile/etc/ssl/certs/ca-bundle.crt" + export NIX_SSL_CERT_FILE + } } } echo "performing a single-user installation of Nix..." >&2 -# Install an SSL certificate bundle. -if [ -z "$NIX_SSL_CERT_FILE" ] || ! [ -f "$NIX_SSL_CERT_FILE" ]; then - $nix/bin/nix-env -i "$cacert" - export NIX_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 From fc634106a66762c394631f851477f9296280d0a1 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 03:49:40 +0300 Subject: [PATCH 38/46] Install Nix: add subscribeChannels() rm old add a nix-channel --- scripts/install-nix-from-closure.sh | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 8afde2813ba..7cfe6911a7a 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -730,14 +730,27 @@ installNix() { readonly NIX_SSL_CERT_FILE="$HOME/.nix-profile/etc/ssl/certs/ca-bundle.crt" export NIX_SSL_CERT_FILE } + + subscribeChannels() { + # Subscribe the user to the Nixpkgs channel and fetch it. + trap 'subscribeChannelsRevert $LINENO $?' INT TERM ABRT QUIT + subscribeChannelsRevert() { + errorRevert "Received Error signal from line $1" + errorRevert "Reverting channels subscriptions: nixpkgs" + "$nix"/bin/nix-channel --remove nixpkgs + error " + + Changes reverted. + " "$2" + } + readonly channelUrl='https://nixos.org/channels/nixpkgs-unstable' + print "Subscribing to channel: nixpkgs=$channelUrl" + "$nix"/bin/nix-channel --add "$channelUrl" + } } } echo "performing a single-user installation of Nix..." >&2 -# 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 From ca02ac4509909896c97f1fb72c771ddbcd322178 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 03:50:04 +0300 Subject: [PATCH 39/46] Install Nix: updateRepos() rm old channel update --- scripts/install-nix-from-closure.sh | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 7cfe6911a7a..32d1680c5ab 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -747,14 +747,24 @@ installNix() { print "Subscribing to channel: nixpkgs=$channelUrl" "$nix"/bin/nix-channel --add "$channelUrl" } + + updateRepos() { + # Subscribe the user to the Nixpkgs channel and fetch it. + trap 'updateReposRevert $LINENO $?' INT TERM ABRT QUIT + updateReposRevert() { + error " + + Received Error signal from line $1 + " "$2" + } + print 'Pulling information from channel: nixpkgs' + # nix-channel should be fully transactional, so no 'nix-channel --rollback' + "$nix"/bin/nix-channel --update nixpkgs + } } } echo "performing a single-user installation of Nix..." >&2 -if [ -z "$_NIX_INSTALLER_TEST" ]; then - $nix/bin/nix-channel --update nixpkgs -fi - added= if [ -z "$NIX_INSTALLER_NO_MODIFY_PROFILE" ]; then From 9f75558d013736a2873bc6924d18e2722dc0594c Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 20:16:22 +0300 Subject: [PATCH 40/46] Install Nix: add mainInstallNix() - invocation of functions rm old message 'performing ... installation', which was to STDERR btw --- scripts/install-nix-from-closure.sh | 42 ++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 32d1680c5ab..74814578795 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -761,10 +761,50 @@ installNix() { # nix-channel should be fully transactional, so no 'nix-channel --rollback' "$nix"/bin/nix-channel --update nixpkgs } + + # Wrap function invocations process into function + mainInstallNix() { + + # TODO: Determine to write single/multi user after bug #1559 is resolved + notice ' + + Performing a single-user installation of Nix: + ' + + if [ ! -d "$dest" ] ; then + createDest + fi + + createStore + + copyNix + + initializeDB + + setupProfile + + + if [ -z "$NIX_SSL_CERT_FILE" ] || [ ! -f "$NIX_SSL_CERT_FILE" ]; then + installCerts + fi + + if ! "$nix"/bin/nix-channel --list | grep -q '^nixpkgs '; then + subscribeChannels + fi + + if [ -z "$_NIX_INSTALLER_TEST" ]; then + updateRepos + fi + + notice ' + + Installation finished. 8) + ' + + } } } -echo "performing a single-user installation of Nix..." >&2 added= if [ -z "$NIX_INSTALLER_NO_MODIFY_PROFILE" ]; then From edcc68be42e4ac983c1c5f7b0ff66a34d515b7be Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Fri, 17 Aug 2018 03:51:01 +0300 Subject: [PATCH 41/46] Install Nix: add execution start --- scripts/install-nix-from-closure.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 74814578795..70459efc9fc 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -802,6 +802,9 @@ installNix() { ' } + + # Invoke main function for installNix + mainInstallNix } } From 18bb415a086ffe5908ccbc973a6ddb026dc1fcf5 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 20:18:23 +0300 Subject: [PATCH 42/46] add 'Postinstall' stub --- scripts/install-nix-from-closure.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 70459efc9fc..3fe81d92cea 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -807,6 +807,17 @@ installNix() { mainInstallNix } +} + + +############################### +### Postinstall +############################### +{ + +postinstall() { +} + } added= if [ -z "$NIX_INSTALLER_NO_MODIFY_PROFILE" ]; then From 3563425b15040ddb80a81108ed1b4956f35a3f34 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Sat, 18 Aug 2018 19:54:51 +0300 Subject: [PATCH 43/46] Postinstall: add modification of user profile rm old modify profile --- scripts/install-nix-from-closure.sh | 45 ++++++++++++++++------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 3fe81d92cea..2908ff28b1e 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -816,29 +816,34 @@ installNix() { { postinstall() { -} -} -added= -if [ -z "$NIX_INSTALLER_NO_MODIFY_PROFILE" ]; then - - # Make the shell source nix.sh during login. - p=$HOME/.nix-profile/etc/profile.d/nix.sh - - 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 + notice ' -fi + Launching postinstall tasks: + ' + added=0 + if [ -z "$NIX_INSTALLER_NO_MODIFY_PROFILE" ]; then + # Make shell to source nix.sh during login. + readonly nix_profile="$HOME/.nix-profile/etc/profile.d/nix.sh" + for file in .bash_profile .bash_login .profile; do + user_profile="$HOME/$file" + if [ -w "$user_profile" ]; then + if ! grep -q "$nix_profile" "$user_profile"; then + notice " + + Modifying $user_profile... + " + printf 'if [ -e %s ]; then . %s; fi # added by Nix installer\n'\ + "$nix_profile" "$nix_profile" >> "$user_profile" + fi + added=1 + break + fi + done + fi +} +} if [ -z "$added" ]; then cat >&2 < Date: Sat, 18 Aug 2018 19:57:00 +0300 Subject: [PATCH 44/46] Postinstall: add message: installation finished, runtime env info rm old profile messages --- scripts/install-nix-from-closure.sh | 42 +++++++++++++++++------------ 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 2908ff28b1e..56299619d57 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -841,27 +841,35 @@ postinstall() { fi done fi -} -} -if [ -z "$added" ]; then - cat >&2 <&2 < Date: Sat, 18 Aug 2018 19:09:32 +0300 Subject: [PATCH 45/46] add 'Main function' --- scripts/install-nix-from-closure.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 56299619d57..776dde79a25 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -873,3 +873,18 @@ postinstall() { } } + + +############################### +### Main function +############################### +main() { + + checkingRequirements + checkInstallerMode "$@" + execInstallerMode + installNix + postinstall + contactUs + +} From 721cb5fc4224b0ce17fe0dbd30d63bc36ece517a Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Thu, 16 Aug 2018 21:05:23 +0300 Subject: [PATCH 46/46] add execution start --- scripts/install-nix-from-closure.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/install-nix-from-closure.sh b/scripts/install-nix-from-closure.sh index 776dde79a25..f6a4d4f9fcc 100644 --- a/scripts/install-nix-from-closure.sh +++ b/scripts/install-nix-from-closure.sh @@ -888,3 +888,9 @@ main() { contactUs } + + +############################### +### Start of the script +############################### +main "$@"