From 2f58d0a3de97f4c20efcc6ba00878acfd7b5665d Mon Sep 17 00:00:00 2001 From: Moises Nessim Date: Wed, 10 Nov 2021 13:23:33 -0500 Subject: [PATCH] nix: add support for `nix profile` PR #2833 Co-authored-by: David Arnold Co-authored-by: Florian Franzen --- home-manager/home-manager | 23 ++++++++++-- home-manager/po/home-manager.pot | 56 ++++++++++++++--------------- modules/files.nix | 14 ++++++-- modules/home-environment.nix | 27 +++++++++++--- modules/lib-bash/activation-init.sh | 1 + modules/po/hm-modules.pot | 32 ++++++++--------- 6 files changed, 101 insertions(+), 52 deletions(-) diff --git a/home-manager/home-manager b/home-manager/home-manager index 43c92442..29e4df94 100644 --- a/home-manager/home-manager +++ b/home-manager/home-manager @@ -11,6 +11,23 @@ export TEXTDOMAINDIR=@OUT@/share/locale # shellcheck disable=1091 source @HOME_MANAGER_LIB@ +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" @@ -370,8 +387,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 @@ -446,6 +464,7 @@ function doShowNews() { function doUninstall() { setVerboseAndDryRun + setNixProfileCommands _i 'This will remove Home Manager from your system.' @@ -463,7 +482,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/home-manager/po/home-manager.pot b/home-manager/po/home-manager.pot index e37bd685..003a344b 100644 --- a/home-manager/po/home-manager.pot +++ b/home-manager/po/home-manager.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Home Manager\n" "Report-Msgid-Bugs-To: https://github.com/nix-community/home-manager/issues\n" -"POT-Creation-Date: 2022-01-02 11:55+0100\n" +"POT-Creation-Date: 2022-03-26 15:08+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -18,23 +18,23 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" -#: home-manager/home-manager:43 +#: home-manager/home-manager:60 msgid "No configuration file found at %s" msgstr "" -#: home-manager/home-manager:62 +#: home-manager/home-manager:79 msgid "No configuration file found. Please create one at %s" msgstr "" -#: home-manager/home-manager:105 +#: home-manager/home-manager:122 msgid "Can't inspect options of a flake configuration" msgstr "" -#: home-manager/home-manager:145 +#: home-manager/home-manager:162 msgid "Can't instantiate a flake configuration" msgstr "" -#: home-manager/home-manager:220 +#: home-manager/home-manager:237 msgid "" "There is %d unread and relevant news item.\n" "Read it by running the command \"%s news\"." @@ -44,92 +44,92 @@ msgid_plural "" msgstr[0] "" msgstr[1] "" -#: home-manager/home-manager:234 +#: home-manager/home-manager:251 msgid "Unknown \"news.display\" setting \"%s\"." msgstr "" -#: home-manager/home-manager:241 +#: home-manager/home-manager:258 #, sh-format msgid "Please set the $EDITOR environment variable" msgstr "" -#: home-manager/home-manager:256 +#: home-manager/home-manager:273 msgid "Cannot run build in read-only directory" msgstr "" -#: home-manager/home-manager:338 +#: home-manager/home-manager:355 msgid "No generation with ID %s" msgstr "" -#: home-manager/home-manager:340 +#: home-manager/home-manager:357 msgid "Cannot remove the current generation %s" msgstr "" -#: home-manager/home-manager:342 +#: home-manager/home-manager:359 msgid "Removing generation %s" msgstr "" -#: home-manager/home-manager:368 +#: home-manager/home-manager:385 msgid "No generations to expire" msgstr "" -#: home-manager/home-manager:378 +#: home-manager/home-manager:396 msgid "No home-manager packages seem to be installed." msgstr "" -#: home-manager/home-manager:435 +#: home-manager/home-manager:453 msgid "Unknown argument %s" msgstr "" -#: home-manager/home-manager:450 +#: home-manager/home-manager:469 msgid "This will remove Home Manager from your system." msgstr "" -#: home-manager/home-manager:453 +#: home-manager/home-manager:472 msgid "This is a dry run, nothing will actually be uninstalled." msgstr "" -#: home-manager/home-manager:457 +#: home-manager/home-manager:476 msgid "Really uninstall Home Manager?" msgstr "" -#: home-manager/home-manager:462 +#: home-manager/home-manager:481 msgid "Switching to empty Home Manager configuration..." msgstr "" -#: home-manager/home-manager:474 +#: home-manager/home-manager:493 msgid "Yay!" msgstr "" -#: home-manager/home-manager:481 +#: home-manager/home-manager:500 msgid "Remove all Home Manager generations?" msgstr "" -#: home-manager/home-manager:488 +#: home-manager/home-manager:507 msgid "All generations are now eligible for garbage collection." msgstr "" -#: home-manager/home-manager:491 +#: home-manager/home-manager:510 msgid "Leaving generations but they may still be garbage collected." msgstr "" -#: home-manager/home-manager:495 +#: home-manager/home-manager:514 msgid "Home Manager is uninstalled but your home.nix is left untouched." msgstr "" -#: home-manager/home-manager:646 +#: home-manager/home-manager:673 msgid "%s: unknown option '%s'" msgstr "" -#: home-manager/home-manager:647 +#: home-manager/home-manager:674 msgid "Run '%s --help' for usage help" msgstr "" -#: home-manager/home-manager:681 +#: home-manager/home-manager:708 msgid "expire-generations expects one argument, got %d." msgstr "" -#: home-manager/home-manager:703 +#: home-manager/home-manager:730 msgid "Unknown command: %s" msgstr "" diff --git a/modules/files.nix b/modules/files.nix index 21749649..a7fd67f5 100644 --- a/modules/files.nix +++ b/modules/files.nix @@ -259,8 +259,18 @@ in cleanOldGen if [[ ! -v oldGenPath || "$oldGenPath" != "$newGenPath" ]] ; then - _i "Creating profile generation %s" "$newGenNum" - $DRY_RUN_CMD nix-env $VERBOSE_ARG --profile "$genProfilePath" --set "$newGenPath" + _i "Creating profile generation %s" $newGenNum + 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 _i "No change so reusing latest profile generation %s" "$oldGenNum" diff --git a/modules/home-environment.nix b/modules/home-environment.nix index af32cc50..b06ae438 100644 --- a/modules/home-environment.nix +++ b/modules/home-environment.nix @@ -578,17 +578,36 @@ 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 echo - _iError $'Oops, nix-env failed to install your new Home Manager profile!\n\nPerhaps there is a conflict with a package that was installed using\n"nix-env -i"? Try running\n\n nix-env -q\n\nand if there is a conflicting package you can remove it with\n\n nix-env -e {package name}\n\nThen try activating your Home Manager configuration again.' + _iError $'Oops, Nix failed to install your new Home Manager profile!\n\nPerhaps there is a conflict with a package that was installed using\n"%s"? Try running\n\n %s\n\nand if there is a conflicting package you can remove it with\n\n %s\n\nThen try activating your Home Manager configuration again.' "$INSTALL_CMD" "$LIST_CMD" "$REMOVE_CMD_SYNTAX" exit 1 fi + unset INSTALL_CMD LIST_CMD REMOVE_CMD_SYNTAX '' ); diff --git a/modules/lib-bash/activation-init.sh b/modules/lib-bash/activation-init.sh index 7a1be0f3..aadc88dd 100755 --- a/modules/lib-bash/activation-init.sh +++ b/modules/lib-bash/activation-init.sh @@ -5,6 +5,7 @@ function setupVars() { local profilesPath="$nixStateDir/profiles/per-user/$USER" local gcPath="$nixStateDir/gcroots/per-user/$USER" + declare -gr nixProfilePath="$profilesPath/profile" declare -gr genProfilePath="$profilesPath/home-manager" declare -gr newGenPath="@GENERATION_DIR@"; declare -gr newGenGcPath="$gcPath/current-home" diff --git a/modules/po/hm-modules.pot b/modules/po/hm-modules.pot index c4ade9aa..7bff64af 100644 --- a/modules/po/hm-modules.pot +++ b/modules/po/hm-modules.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Home Manager Modules\n" "Report-Msgid-Bugs-To: https://github.com/nix-community/home-manager/issues\n" -"POT-Creation-Date: 2022-01-02 11:55+0100\n" +"POT-Creation-Date: 2022-03-26 15:08+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -29,35 +29,35 @@ msgstr "" msgid "Creating profile generation %s" msgstr "" -#: modules/files.nix:266 +#: modules/files.nix:276 msgid "No change so reusing latest profile generation %s" msgstr "" -#: modules/home-environment.nix:589 +#: modules/home-environment.nix:607 msgid "" -"Oops, nix-env failed to install your new Home Manager profile!\n" +"Oops, Nix failed to install your new Home Manager profile!\n" "\n" "Perhaps there is a conflict with a package that was installed using\n" -"\"nix-env -i\"? Try running\n" +"\"%s\"? Try running\n" "\n" -" nix-env -q\n" +" %s\n" "\n" "and if there is a conflicting package you can remove it with\n" "\n" -" nix-env -e {package name}\n" +" %s\n" "\n" "Then try activating your Home Manager configuration again." msgstr "" -#: modules/home-environment.nix:620 +#: modules/home-environment.nix:639 msgid "Activating %s" msgstr "" -#: modules/lib-bash/activation-init.sh:30 +#: modules/lib-bash/activation-init.sh:31 msgid "Sanity checking oldGenNum and oldGenPath" msgstr "" -#: modules/lib-bash/activation-init.sh:33 +#: modules/lib-bash/activation-init.sh:34 msgid "" "The previous generation number and path are in conflict! These\n" "must be either both empty or both set but are now set to\n" @@ -73,26 +73,26 @@ msgid "" "and trying home-manager switch again. Good luck!" msgstr "" -#: modules/lib-bash/activation-init.sh:50 +#: modules/lib-bash/activation-init.sh:51 msgid "Starting Home Manager activation" msgstr "" -#: modules/lib-bash/activation-init.sh:54 +#: modules/lib-bash/activation-init.sh:55 msgid "Sanity checking Nix" msgstr "" -#: modules/lib-bash/activation-init.sh:60 +#: modules/lib-bash/activation-init.sh:61 msgid "This is a dry run" msgstr "" -#: modules/lib-bash/activation-init.sh:63 +#: modules/lib-bash/activation-init.sh:64 msgid "This is a live run" msgstr "" -#: modules/lib-bash/activation-init.sh:68 +#: modules/lib-bash/activation-init.sh:69 msgid "Using Nix version: %s" msgstr "" -#: modules/lib-bash/activation-init.sh:71 +#: modules/lib-bash/activation-init.sh:72 msgid "Activation variables:" msgstr ""