2023-03-04 10:39:02 +01:00
# Moves the existing profile from /nix to ~ to match changed behavior in Nix
# 2.14. See https://github.com/NixOS/nix/pull/5226.
#
# Note, this function is intentionally unused for now. There remains a few open
# questions about backwards compatibility and support from
# `nix-collect-garbage`.
function migrateProfile( ) {
declare -r stateHome = " ${ XDG_STATE_HOME :- $HOME /.local/state } "
declare -r hmStateDir = " $stateHome /home-manager "
declare -r nixStateDir = " ${ NIX_STATE_DIR :- /nix/var/nix } "
declare -r newProfilesDir = " $hmStateDir /profiles "
declare -r oldProfilesDir = " $nixStateDir /profiles/per-user/ $USER "
if [ [ ! -d $newProfilesDir ] ] ; then
_i 'Migrating profiles from %s to %s' " $oldProfilesDir " " $newProfilesDir "
mkdir -p " $newProfilesDir "
for p in " $oldProfilesDir " /home-manager-*; do
declare -r name = " ${ p ##*/ } "
nix-store --realise " $p " --add-root " $newProfilesDir / $name " > /dev/null
done
cp -P " $oldProfilesDir /home-manager " " $newProfilesDir "
fi
rm " $oldProfilesDir " /home-manager-*
}
2017-01-16 20:26:42 +01:00
function setupVars( ) {
2023-03-04 10:39:02 +01:00
declare -r nixStateDir = " ${ NIX_STATE_DIR :- /nix/var/nix } "
declare -r globalProfilesDir = " $nixStateDir /profiles/per-user/ $USER "
declare -r globalGcrootsDir = " $nixStateDir /gcroots/per-user/ $USER "
declare -r stateHome = " ${ XDG_STATE_HOME :- $HOME /.local/state } "
declare -r hmStateDir = " $stateHome /home-manager "
declare -r hmGcrootsDir = " $hmStateDir /gcroots "
2017-01-16 20:26:42 +01:00
2023-03-04 10:39:02 +01:00
# If the global profiles path exists or we can create it, then place the HM
# profile there. Otherwise place it in the HM data directory. We prefer to
# use the global location since it makes it visible to
# `nix-collect-garbage`.
#
# In the future we may perform a one-shot migration to the new location.
#
# shellcheck disable=2174
2023-03-19 23:32:23 +01:00
if [ [ -d " $globalProfilesDir " ] ] || mkdir -m 0755 -p " $globalProfilesDir " 2>/dev/null; then
2023-03-04 10:39:02 +01:00
declare -r hmProfilesDir = " $globalProfilesDir "
else
declare -r hmProfilesDir = " $hmStateDir /profiles "
mkdir -m 0755 -p " $hmProfilesDir "
fi
declare -gr genProfilePath = " $hmProfilesDir /home-manager "
2021-07-27 23:50:33 +02:00
declare -gr newGenPath = "@GENERATION_DIR@" ;
2023-03-04 10:39:02 +01:00
declare -gr newGenGcPath = " $hmGcrootsDir /current-home "
declare -gr legacyGenGcPath = " $globalGcrootsDir /current-home "
2020-05-16 18:09:09 +02:00
2023-03-04 10:39:02 +01:00
declare greatestGenNum
2017-01-16 20:26:42 +01:00
greatestGenNum = $( \
2020-05-16 18:09:09 +02:00
nix-env --list-generations --profile " $genProfilePath " \
| tail -1 \
| sed -E 's/ *([[:digit:]]+) .*/\1/' )
2017-01-16 20:26:42 +01:00
2018-03-13 20:49:45 +01:00
if [ [ -n $greatestGenNum ] ] ; then
2021-07-27 23:50:33 +02:00
declare -gr oldGenNum = $greatestGenNum
declare -gr newGenNum = $(( oldGenNum + 1 ))
2017-01-16 20:26:42 +01:00
else
2021-07-27 23:50:33 +02:00
declare -gr newGenNum = 1
2017-01-16 20:26:42 +01:00
fi
2023-03-04 10:39:02 +01:00
if [ [ -e $genProfilePath ] ] ; then
declare -g oldGenPath
oldGenPath = " $( readlink -e " $genProfilePath " ) "
2017-01-16 20:26:42 +01:00
fi
2022-01-02 11:44:53 +01:00
$VERBOSE_RUN _i "Sanity checking oldGenNum and oldGenPath"
2018-03-13 20:49:45 +01:00
if [ [ -v oldGenNum && ! -v oldGenPath
|| ! -v oldGenNum && -v oldGenPath ] ] ; then
2022-01-02 11:44:53 +01: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 :- } " \
2023-03-04 10:39:02 +01:00
" $hmProfilesDir " " $hmGcrootsDir "
2018-03-13 20:49:45 +01:00
exit 1
fi
2017-01-16 20:26:42 +01:00
}
2017-03-25 21:48:17 +01:00
if [ [ -v VERBOSE ] ] ; then
2017-01-21 12:27:50 +01:00
export VERBOSE_ECHO = echo
export VERBOSE_ARG = "--verbose"
2022-01-02 11:44:53 +01:00
export VERBOSE_RUN = ""
2017-01-21 12:27:50 +01:00
else
export VERBOSE_ECHO = true
2017-03-25 21:48:17 +01:00
export VERBOSE_ARG = ""
2022-01-02 11:44:53 +01:00
export VERBOSE_RUN = true
2017-01-21 12:27:50 +01:00
fi
2021-04-30 01:29:39 +02:00
_i "Starting Home Manager activation"
2018-03-13 20:49:45 +01:00
2020-05-20 22:17:37 +02:00
# Verify that we can connect to the Nix store and/or daemon. This will
# also create the necessary directories in profiles and gcroots.
2022-01-02 11:44:53 +01:00
$VERBOSE_RUN _i "Sanity checking Nix"
2020-05-20 22:17:37 +02:00
nix-build --expr '{}' --no-out-link
2018-03-13 20:49:45 +01:00
setupVars
2017-03-25 21:48:17 +01:00
if [ [ -v DRY_RUN ] ] ; then
2022-01-02 11:44:53 +01:00
_i "This is a dry run"
2017-01-21 12:27:50 +01:00
export DRY_RUN_CMD = echo
2023-03-04 10:39:02 +01:00
export DRY_RUN_NULL = /dev/stdout
2017-01-16 20:26:42 +01:00
else
2022-01-02 11:44:53 +01:00
$VERBOSE_RUN _i "This is a live run"
2017-03-25 21:48:17 +01:00
export DRY_RUN_CMD = ""
2023-03-04 10:39:02 +01:00
export DRY_RUN_NULL = /dev/null
2017-01-16 20:26:42 +01:00
fi
2018-02-27 20:31:03 +01:00
if [ [ -v VERBOSE ] ] ; then
2022-01-02 11:44:53 +01:00
_i 'Using Nix version: %s' " $( nix-env --version) "
2018-02-27 20:31:03 +01:00
fi
2022-01-02 11:44:53 +01:00
$VERBOSE_RUN _i "Activation variables:"
2017-03-29 00:10:30 +02: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
2017-01-21 12:27:50 +01:00
$VERBOSE_ECHO " newGenPath= $newGenPath "
2017-03-29 00:10:30 +02:00
$VERBOSE_ECHO " newGenNum= $newGenNum "
2017-02-05 23:00:04 +01:00
$VERBOSE_ECHO " genProfilePath= $genProfilePath "
2023-03-04 10:39:02 +01:00
$VERBOSE_ECHO " newGenGcPath= $newGenGcPath "
$VERBOSE_ECHO " legacyGenGcPath= $legacyGenGcPath "