diff --git a/home-manager/home-manager b/home-manager/home-manager index fc141062..42210ed1 100644 --- a/home-manager/home-manager +++ b/home-manager/home-manager @@ -10,6 +10,23 @@ function errorEcho() { echo $* >&2 } +function removeByName() { + nix profile list \ + | { grep "$1" || test $? = 1; } \ + | cut -d ' ' -f 4 \ + | xargs -t $DRY_RUN_CMD nix profile remove $VERBOSE_ARG +} + +function setNixProfileCommands() { + if [[ -e ~/.nix-profile/manifest.json ]] ; then + LIST_OUTPATH_CMD="nix profile list" + REMOVE_CMD="removeByName" + else + LIST_OUTPATH_CMD="nix-env -q --outpath" + REMOVE_CMD="nix-env -q" + fi +} + function setVerboseAndDryRun() { if [[ -v VERBOSE ]]; then export VERBOSE_ARG="--verbose" @@ -371,8 +388,9 @@ function doExpireGenerations() { } function doListPackages() { + setNixProfileCommands local outPath - outPath="$(nix-env -q --out-path | grep -o '/.*home-manager-path$')" + outPath="$($LIST_OUTPATH_CMD | grep -o '/.*home-manager-path$')" if [[ -n "$outPath" ]] ; then nix-store -q --references "$outPath" | sed 's/[^-]*-//' else @@ -447,6 +465,7 @@ function doShowNews() { function doUninstall() { setVerboseAndDryRun + setNixProfileCommands echo "This will remove Home Manager from your system." @@ -464,7 +483,7 @@ function doUninstall() { HOME_MANAGER_CONFIG="$(mktemp --tmpdir home-manager.XXXXXXXXXX)" echo "{ lib, ... }: { home.file = lib.mkForce {}; }" > "$HOME_MANAGER_CONFIG" doSwitch - $DRY_RUN_CMD nix-env -e home-manager-path || true + $DRY_RUN_CMD $REMOVE_CMD home-manager-path || true rm "$HOME_MANAGER_CONFIG" $DRY_RUN_CMD rm $VERBOSE_ARG -r \ "${XDG_DATA_HOME:-$HOME/.local/share}/home-manager" diff --git a/modules/files.nix b/modules/files.nix index 8db317fb..8e8d26a5 100644 --- a/modules/files.nix +++ b/modules/files.nix @@ -260,7 +260,17 @@ in if [[ ! -v oldGenPath || "$oldGenPath" != "$newGenPath" ]] ; then echo "Creating profile generation $newGenNum" - $DRY_RUN_CMD nix-env $VERBOSE_ARG --profile "$genProfilePath" --set "$newGenPath" + if [[ -e "$genProfilePath"/manifest.json ]] ; then + # Remove all packages from "$genProfilePath" + # `nix profile remove '.*' --profile "$genProfilePath"` was not working, so here is a workaround: + nix profile list --profile "$genProfilePath" \ + | cut -d ' ' -f 4 \ + | xargs -t $DRY_RUN_CMD nix profile remove $VERBOSE_ARG --profile "$genProfilePath" + $DRY_RUN_CMD nix profile install $VERBOSE_ARG --profile "$genProfilePath" "$newGenPath" + else + $DRY_RUN_CMD nix-env $VERBOSE_ARG --profile "$genProfilePath" --set "$newGenPath" + fi + $DRY_RUN_CMD ln -Tsf $VERBOSE_ARG "$newGenPath" "$newGenGcPath" else echo "No change so reusing latest profile generation $oldGenNum" diff --git a/modules/home-environment.nix b/modules/home-environment.nix index dd8b9817..6d880818 100644 --- a/modules/home-environment.nix +++ b/modules/home-environment.nix @@ -578,30 +578,49 @@ in if config.submoduleSupport.externalPackageInstall then '' - if nix-env -q | grep '^home-manager-path$'; then - $DRY_RUN_CMD nix-env -e home-manager-path + if [[ -e "$nixProfilePath"/manifest.json ]] ; then + nix profile list \ + | { grep 'home-manager-path$' || test $? = 1; } \ + | awk -F ' ' '{ print $4 }' \ + | cut -d ' ' -f 4 \ + | xargs -t $DRY_RUN_CMD nix profile remove $VERBOSE_ARG + else + if nix-env -q | grep '^home-manager-path$'; then + $DRY_RUN_CMD nix-env -e home-manager-path + fi fi '' else '' - if ! $DRY_RUN_CMD nix-env -i ${cfg.path} ; then + if [[ -e "$nixProfilePath"/manifest.json ]] ; then + INSTALL_CMD="nix profile install" + LIST_CMD="nix profile list" + REMOVE_CMD_SYNTAX='nix profile remove {number | store path}' + else + INSTALL_CMD="nix-env -i" + LIST_CMD="nix-env -q" + REMOVE_CMD_SYNTAX='nix-env -e {package name}' + fi + + if ! $DRY_RUN_CMD $INSTALL_CMD ${cfg.path} ; then cat <