2023-04-07 14:01:45 +02:00
# Moves the existing profile from /nix or $XDG_STATE_HOME/home-manager to
# $XDG_STATE_HOME/nix to match changed behavior in Nix 2.14. See
# https://github.com/NixOS/nix/pull/5226.
2023-03-04 10:39:02 +01:00
function migrateProfile( ) {
declare -r stateHome = " ${ XDG_STATE_HOME :- $HOME /.local/state } "
2023-04-07 14:01:45 +02:00
declare -r userNixStateDir = " $stateHome /nix "
2023-03-04 10:39:02 +01:00
declare -r hmStateDir = " $stateHome /home-manager "
2023-04-07 14:01:45 +02:00
declare -r globalNixStateDir = " ${ NIX_STATE_DIR :- /nix/var/nix } "
declare -r globalProfilesDir = " $globalNixStateDir /profiles/per-user/ $USER "
if [ [ -e $globalProfilesDir /home-manager ] ] ; then
declare -r oldProfilesDir = " $globalProfilesDir "
elif [ [ -e $hmStateDir /profiles/home-manager ] ] ; then
declare -r oldProfilesDir = " $hmStateDir /profiles "
2023-03-04 10:39:02 +01:00
fi
2023-04-07 14:01:45 +02:00
declare -r newProfilesDir = " $userNixStateDir /profiles "
if [ [ -v oldProfilesDir && -e $newProfilesDir ] ] ; then
if [ [ ! -e $newProfilesDir /home-manager ] ] ; then
_i 'Migrating profile from %s to %s' " $oldProfilesDir " " $newProfilesDir "
for p in " $oldProfilesDir " /home-manager-*; do
declare name = " ${ p ##*/ } "
nix-store --realise " $p " --add-root " $newProfilesDir / $name " > /dev/null
done
cp -P " $oldProfilesDir /home-manager " " $newProfilesDir "
fi
rm " $oldProfilesDir /home-manager " " $oldProfilesDir " /home-manager-*
fi
2023-03-04 10:39:02 +01:00
}
2017-01-16 20:26:42 +01:00
function setupVars( ) {
2023-03-04 10:39:02 +01:00
declare -r stateHome = " ${ XDG_STATE_HOME :- $HOME /.local/state } "
2023-04-07 14:01:45 +02:00
declare -r userNixStateDir = " $stateHome /nix "
2024-01-06 00:22:27 +01:00
declare -gr hmStatePath = " $stateHome /home-manager "
declare -r hmGcrootsDir = " $hmStatePath /gcroots "
2017-01-16 20:26:42 +01:00
2023-04-07 14:01:45 +02:00
declare -r globalNixStateDir = " ${ NIX_STATE_DIR :- /nix/var/nix } "
declare -r globalProfilesDir = " $globalNixStateDir /profiles/per-user/ $USER "
declare -r globalGcrootsDir = " $globalNixStateDir /gcroots/per-user/ $USER "
# If the user Nix profiles path exists, then place the HM profile there.
# Otherwise, if the global Nix per-user state directory exists then use
# that. If neither exists, then we give up.
2023-03-04 10:39:02 +01:00
#
# shellcheck disable=2174
2023-04-07 14:01:45 +02:00
if [ [ -d $userNixStateDir /profiles ] ] ; then
declare -r profilesDir = " $userNixStateDir /profiles "
elif [ [ -d $globalProfilesDir ] ] ; then
declare -r profilesDir = " $globalProfilesDir "
2023-03-04 10:39:02 +01:00
else
2023-04-07 14:01:45 +02:00
_iError 'Could not find suitable profile directory, tried %s and %s' \
" $userNixStateDir /profiles " " $globalProfilesDir " >& 2
exit 1
2023-03-04 10:39:02 +01:00
fi
2024-01-06 00:22:27 +01:00
declare -gr hmDataPath = " ${ XDG_DATA_HOME :- $HOME /.local/share } /home-manager "
2023-04-07 14:01:45 +02:00
declare -gr genProfilePath = " $profilesDir /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
2024-01-23 22:59:26 +01:00
_iVerbose "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-04-07 14:01:45 +02:00
" $profilesDir " " $hmGcrootsDir "
2018-03-13 20:49:45 +01:00
exit 1
fi
2017-01-16 20:26:42 +01:00
}
2024-01-06 00:22:27 +01:00
# Helper used to list content of a `nix profile` profile.
function nixProfileList( ) {
# We attempt to use `--json` first (added in Nix 2.17). Otherwise attempt to
# parse the legacy output format.
{
nix profile list --json 2>/dev/null \
| jq -r --arg name " $1 " '.elements[].storePaths[] | select(endswith($name))'
} || {
nix profile list \
| { grep " $1 \$ " || test $? = 1; } \
| cut -d ' ' -f 4
}
}
# Helper used to remove a package from a Nix profile. Supports both `nix-env`
# and `nix profile`.
function nixProfileRemove( ) {
# We don't use `cfg.profileDirectory` here because it defaults to
# `/etc/profiles/per-user/<user>` which is constructed by NixOS or
# nix-darwin and won't require uninstalling `home-manager-path`.
if [ [ -e $HOME /.nix-profile/manifest.json \
|| -e ${ XDG_STATE_HOME :- $HOME /.local/state } /nix/profile/manifest.json ] ] ; then
2024-01-22 22:40:55 +01:00
nixProfileList " $1 " | xargs -rt $DRY_RUN_CMD nix profile remove $VERBOSE_ARG
2024-01-06 00:22:27 +01:00
else
2024-01-15 17:25:23 +01:00
if nix-env -q | grep -q " ^ $1 $" ; then
2024-02-29 00:18:40 +01:00
run --quiet nix-env -e " $1 "
2024-01-15 17:25:23 +01:00
fi
2024-01-06 00:22:27 +01:00
fi
}
2023-05-26 15:11:22 +02:00
function checkUsername( ) {
local expectedUser = " $1 "
if [ [ " $USER " != " $expectedUser " ] ] ; then
_iError 'Error: USER is set to "%s" but we expect "%s"' " $USER " " $expectedUser "
exit 1
fi
}
function checkHomeDirectory( ) {
local expectedHome = " $1 "
if ! [ [ $HOME -ef $expectedHome ] ] ; then
_iError 'Error: HOME is set to "%s" but we expect "%s"' " $HOME " " $expectedHome "
exit 1
fi
}
2024-01-23 22:59:26 +01:00
# Note, the VERBOSE_ECHO variable is deprecated and should not be used inside
# the Home Manager project. It is provided here for backwards compatibility.
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.
2024-01-23 22:59:26 +01:00
_iVerbose "Sanity checking Nix"
2024-03-22 19:44:27 +01:00
nix-build --quiet --expr '{}' --no-out-link
2020-05-20 22:17:37 +02:00
2023-04-13 19:03:41 +02:00
# Also make sure that the Nix profiles path is created.
2023-04-14 09:29:06 +02:00
nix-env -q > /dev/null 2>& 1 || true
2023-04-13 19:03:41 +02:00
2023-04-07 14:01:45 +02:00
migrateProfile
2018-03-13 20:49:45 +01:00
setupVars
2024-01-13 23:15:00 +01:00
# Note, the DRY_RUN_CMD and DRY_RUN_NULL variables are deprecated and should not
# be used inside the Home Manager project. They are provided here for backwards
# compatibility.
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
2024-01-23 22:59:26 +01:00
_iVerbose "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
2024-01-23 22:59:26 +01:00
_iVerbose "Activation variables:"
2017-03-29 00:10:30 +02:00
if [ [ -v oldGenNum ] ] ; then
2024-01-23 22:59:26 +01:00
verboseEcho " oldGenNum= $oldGenNum "
verboseEcho " oldGenPath= $oldGenPath "
2017-03-29 00:10:30 +02:00
else
2024-01-23 22:59:26 +01:00
verboseEcho " oldGenNum undefined (first run?)"
verboseEcho " oldGenPath undefined (first run?)"
2017-03-29 00:10:30 +02:00
fi
2024-01-23 22:59:26 +01:00
verboseEcho " newGenPath= $newGenPath "
verboseEcho " newGenNum= $newGenNum "
verboseEcho " genProfilePath= $genProfilePath "
verboseEcho " newGenGcPath= $newGenGcPath "
verboseEcho " legacyGenGcPath= $legacyGenGcPath "