2020-10-12 00:22:58 +00:00
#!/usr/bin/env bash
function setupVars( ) {
local nixStateDir = " ${ NIX_STATE_DIR :- /nix/var/nix } "
local profilesPath = " $nixStateDir /profiles/per-user/ $USER "
local gcPath = " $nixStateDir /gcroots/per-user/ $USER "
2023-01-10 09:35:00 +00:00
declare -gr nixProfilePath = " $profilesPath /profile "
2021-11-04 16:42:44 +00:00
declare -gr genProfilePath = " $profilesPath /home-manager "
declare -gr newGenPath = "@GENERATION_DIR@" ;
declare -gr newGenGcPath = " $gcPath /current-home "
2020-10-12 00:22:58 +00:00
local greatestGenNum
greatestGenNum = $( \
nix-env --list-generations --profile " $genProfilePath " \
| tail -1 \
| sed -E 's/ *([[:digit:]]+) .*/\1/' )
if [ [ -n $greatestGenNum ] ] ; then
2021-11-04 16:42:44 +00:00
declare -gr oldGenNum = $greatestGenNum
declare -gr newGenNum = $(( oldGenNum + 1 ))
2020-10-12 00:22:58 +00:00
else
2021-11-04 16:42:44 +00:00
declare -gr newGenNum = 1
2020-10-12 00:22:58 +00:00
fi
if [ [ -e $profilesPath /home-manager ] ] ; then
oldGenPath = " $( readlink -e " $profilesPath /home-manager " ) "
2021-11-04 16:42:44 +00:00
declare -gr oldGenPath
2020-10-12 00:22:58 +00:00
fi
2023-01-10 09:35:00 +00:00
$VERBOSE_RUN _i "Sanity checking oldGenNum and oldGenPath"
2020-10-12 00:22:58 +00:00
if [ [ -v oldGenNum && ! -v oldGenPath
|| ! -v oldGenNum && -v oldGenPath ] ] ; then
2023-01-10 09:35:00 +00:00
_i $'The previous generation number and path are in conflict! These\nmust be either both empty or both set but are now set to\n\n \'%s\' and \'%s\'\n\nIf you don\'t mind losing previous profile generations then\nthe easiest solution is probably to run\n\n rm %s/home-manager*\n rm %s/current-home\n\nand trying home-manager switch again. Good luck!' \
" ${ oldGenNum :- } " " ${ oldGenPath :- } " \
" $profilesPath " " $gcPath "
2020-10-12 00:22:58 +00:00
exit 1
fi
}
if [ [ -v VERBOSE ] ] ; then
export VERBOSE_ECHO = echo
export VERBOSE_ARG = "--verbose"
2023-01-10 09:35:00 +00:00
export VERBOSE_RUN = ""
2020-10-12 00:22:58 +00:00
else
export VERBOSE_ECHO = true
export VERBOSE_ARG = ""
2023-01-10 09:35:00 +00:00
export VERBOSE_RUN = true
2020-10-12 00:22:58 +00:00
fi
2023-01-10 09:35:00 +00:00
_i "Starting Home Manager activation"
2020-10-12 00:22:58 +00:00
# Verify that we can connect to the Nix store and/or daemon. This will
# also create the necessary directories in profiles and gcroots.
2023-01-10 09:35:00 +00:00
$VERBOSE_RUN _i "Sanity checking Nix"
2020-10-12 00:22:58 +00:00
nix-build --expr '{}' --no-out-link
setupVars
if [ [ -v DRY_RUN ] ] ; then
2023-01-10 09:35:00 +00:00
_i "This is a dry run"
2020-10-12 00:22:58 +00:00
export DRY_RUN_CMD = echo
else
2023-01-10 09:35:00 +00:00
$VERBOSE_RUN _i "This is a live run"
2020-10-12 00:22:58 +00:00
export DRY_RUN_CMD = ""
fi
if [ [ -v VERBOSE ] ] ; then
2023-01-10 09:35:00 +00:00
_i 'Using Nix version: %s' " $( nix-env --version) "
2020-10-12 00:22:58 +00:00
fi
2023-01-10 09:35:00 +00:00
$VERBOSE_RUN _i "Activation variables:"
2020-10-12 00:22:58 +00:00
if [ [ -v oldGenNum ] ] ; then
$VERBOSE_ECHO " oldGenNum= $oldGenNum "
$VERBOSE_ECHO " oldGenPath= $oldGenPath "
else
$VERBOSE_ECHO " oldGenNum undefined (first run?)"
$VERBOSE_ECHO " oldGenPath undefined (first run?)"
fi
$VERBOSE_ECHO " newGenPath= $newGenPath "
$VERBOSE_ECHO " newGenNum= $newGenNum "
$VERBOSE_ECHO " newGenGcPath= $newGenGcPath "
$VERBOSE_ECHO " genProfilePath= $genProfilePath "