nix: add support for nix profile

PR #2833

Co-authored-by: David Arnold <dar@xoe.solutions>
Co-authored-by: Florian Franzen <Florian.Franzen@gmail.com>
This commit is contained in:
Moises Nessim 2021-11-10 13:23:33 -05:00 committed by Robert Helgesson
parent 171702dd88
commit 2f58d0a3de
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
6 changed files with 101 additions and 52 deletions

View file

@ -11,6 +11,23 @@ export TEXTDOMAINDIR=@OUT@/share/locale
# shellcheck disable=1091 # shellcheck disable=1091
source @HOME_MANAGER_LIB@ 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() { function setVerboseAndDryRun() {
if [[ -v VERBOSE ]]; then if [[ -v VERBOSE ]]; then
export VERBOSE_ARG="--verbose" export VERBOSE_ARG="--verbose"
@ -370,8 +387,9 @@ function doExpireGenerations() {
} }
function doListPackages() { function doListPackages() {
setNixProfileCommands
local outPath 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 if [[ -n "$outPath" ]] ; then
nix-store -q --references "$outPath" | sed 's/[^-]*-//' nix-store -q --references "$outPath" | sed 's/[^-]*-//'
else else
@ -446,6 +464,7 @@ function doShowNews() {
function doUninstall() { function doUninstall() {
setVerboseAndDryRun setVerboseAndDryRun
setNixProfileCommands
_i 'This will remove Home Manager from your system.' _i 'This will remove Home Manager from your system.'
@ -463,7 +482,7 @@ function doUninstall() {
HOME_MANAGER_CONFIG="$(mktemp --tmpdir home-manager.XXXXXXXXXX)" HOME_MANAGER_CONFIG="$(mktemp --tmpdir home-manager.XXXXXXXXXX)"
echo "{ lib, ... }: { home.file = lib.mkForce {}; }" > "$HOME_MANAGER_CONFIG" echo "{ lib, ... }: { home.file = lib.mkForce {}; }" > "$HOME_MANAGER_CONFIG"
doSwitch doSwitch
$DRY_RUN_CMD nix-env -e home-manager-path || true $DRY_RUN_CMD $REMOVE_CMD home-manager-path || true
rm "$HOME_MANAGER_CONFIG" rm "$HOME_MANAGER_CONFIG"
$DRY_RUN_CMD rm $VERBOSE_ARG -r \ $DRY_RUN_CMD rm $VERBOSE_ARG -r \
"${XDG_DATA_HOME:-$HOME/.local/share}/home-manager" "${XDG_DATA_HOME:-$HOME/.local/share}/home-manager"

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Home Manager\n" "Project-Id-Version: Home Manager\n"
"Report-Msgid-Bugs-To: https://github.com/nix-community/home-manager/issues\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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -18,23 +18,23 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\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" msgid "No configuration file found at %s"
msgstr "" msgstr ""
#: home-manager/home-manager:62 #: home-manager/home-manager:79
msgid "No configuration file found. Please create one at %s" msgid "No configuration file found. Please create one at %s"
msgstr "" msgstr ""
#: home-manager/home-manager:105 #: home-manager/home-manager:122
msgid "Can't inspect options of a flake configuration" msgid "Can't inspect options of a flake configuration"
msgstr "" msgstr ""
#: home-manager/home-manager:145 #: home-manager/home-manager:162
msgid "Can't instantiate a flake configuration" msgid "Can't instantiate a flake configuration"
msgstr "" msgstr ""
#: home-manager/home-manager:220 #: home-manager/home-manager:237
msgid "" msgid ""
"There is %d unread and relevant news item.\n" "There is %d unread and relevant news item.\n"
"Read it by running the command \"%s news\"." "Read it by running the command \"%s news\"."
@ -44,92 +44,92 @@ msgid_plural ""
msgstr[0] "" msgstr[0] ""
msgstr[1] "" msgstr[1] ""
#: home-manager/home-manager:234 #: home-manager/home-manager:251
msgid "Unknown \"news.display\" setting \"%s\"." msgid "Unknown \"news.display\" setting \"%s\"."
msgstr "" msgstr ""
#: home-manager/home-manager:241 #: home-manager/home-manager:258
#, sh-format #, sh-format
msgid "Please set the $EDITOR environment variable" msgid "Please set the $EDITOR environment variable"
msgstr "" msgstr ""
#: home-manager/home-manager:256 #: home-manager/home-manager:273
msgid "Cannot run build in read-only directory" msgid "Cannot run build in read-only directory"
msgstr "" msgstr ""
#: home-manager/home-manager:338 #: home-manager/home-manager:355
msgid "No generation with ID %s" msgid "No generation with ID %s"
msgstr "" msgstr ""
#: home-manager/home-manager:340 #: home-manager/home-manager:357
msgid "Cannot remove the current generation %s" msgid "Cannot remove the current generation %s"
msgstr "" msgstr ""
#: home-manager/home-manager:342 #: home-manager/home-manager:359
msgid "Removing generation %s" msgid "Removing generation %s"
msgstr "" msgstr ""
#: home-manager/home-manager:368 #: home-manager/home-manager:385
msgid "No generations to expire" msgid "No generations to expire"
msgstr "" msgstr ""
#: home-manager/home-manager:378 #: home-manager/home-manager:396
msgid "No home-manager packages seem to be installed." msgid "No home-manager packages seem to be installed."
msgstr "" msgstr ""
#: home-manager/home-manager:435 #: home-manager/home-manager:453
msgid "Unknown argument %s" msgid "Unknown argument %s"
msgstr "" msgstr ""
#: home-manager/home-manager:450 #: home-manager/home-manager:469
msgid "This will remove Home Manager from your system." msgid "This will remove Home Manager from your system."
msgstr "" msgstr ""
#: home-manager/home-manager:453 #: home-manager/home-manager:472
msgid "This is a dry run, nothing will actually be uninstalled." msgid "This is a dry run, nothing will actually be uninstalled."
msgstr "" msgstr ""
#: home-manager/home-manager:457 #: home-manager/home-manager:476
msgid "Really uninstall Home Manager?" msgid "Really uninstall Home Manager?"
msgstr "" msgstr ""
#: home-manager/home-manager:462 #: home-manager/home-manager:481
msgid "Switching to empty Home Manager configuration..." msgid "Switching to empty Home Manager configuration..."
msgstr "" msgstr ""
#: home-manager/home-manager:474 #: home-manager/home-manager:493
msgid "Yay!" msgid "Yay!"
msgstr "" msgstr ""
#: home-manager/home-manager:481 #: home-manager/home-manager:500
msgid "Remove all Home Manager generations?" msgid "Remove all Home Manager generations?"
msgstr "" msgstr ""
#: home-manager/home-manager:488 #: home-manager/home-manager:507
msgid "All generations are now eligible for garbage collection." msgid "All generations are now eligible for garbage collection."
msgstr "" msgstr ""
#: home-manager/home-manager:491 #: home-manager/home-manager:510
msgid "Leaving generations but they may still be garbage collected." msgid "Leaving generations but they may still be garbage collected."
msgstr "" msgstr ""
#: home-manager/home-manager:495 #: home-manager/home-manager:514
msgid "Home Manager is uninstalled but your home.nix is left untouched." msgid "Home Manager is uninstalled but your home.nix is left untouched."
msgstr "" msgstr ""
#: home-manager/home-manager:646 #: home-manager/home-manager:673
msgid "%s: unknown option '%s'" msgid "%s: unknown option '%s'"
msgstr "" msgstr ""
#: home-manager/home-manager:647 #: home-manager/home-manager:674
msgid "Run '%s --help' for usage help" msgid "Run '%s --help' for usage help"
msgstr "" msgstr ""
#: home-manager/home-manager:681 #: home-manager/home-manager:708
msgid "expire-generations expects one argument, got %d." msgid "expire-generations expects one argument, got %d."
msgstr "" msgstr ""
#: home-manager/home-manager:703 #: home-manager/home-manager:730
msgid "Unknown command: %s" msgid "Unknown command: %s"
msgstr "" msgstr ""

View file

@ -259,8 +259,18 @@ in
cleanOldGen cleanOldGen
if [[ ! -v oldGenPath || "$oldGenPath" != "$newGenPath" ]] ; then if [[ ! -v oldGenPath || "$oldGenPath" != "$newGenPath" ]] ; then
_i "Creating profile generation %s" "$newGenNum" _i "Creating profile generation %s" $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" $DRY_RUN_CMD ln -Tsf $VERBOSE_ARG "$newGenPath" "$newGenGcPath"
else else
_i "No change so reusing latest profile generation %s" "$oldGenNum" _i "No change so reusing latest profile generation %s" "$oldGenNum"

View file

@ -578,17 +578,36 @@ in
if config.submoduleSupport.externalPackageInstall if config.submoduleSupport.externalPackageInstall
then then
'' ''
if nix-env -q | grep '^home-manager-path$'; then if [[ -e "$nixProfilePath"/manifest.json ]] ; then
$DRY_RUN_CMD nix-env -e home-manager-path 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 fi
'' ''
else 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 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 exit 1
fi fi
unset INSTALL_CMD LIST_CMD REMOVE_CMD_SYNTAX
'' ''
); );

View file

@ -5,6 +5,7 @@ function setupVars() {
local profilesPath="$nixStateDir/profiles/per-user/$USER" local profilesPath="$nixStateDir/profiles/per-user/$USER"
local gcPath="$nixStateDir/gcroots/per-user/$USER" local gcPath="$nixStateDir/gcroots/per-user/$USER"
declare -gr nixProfilePath="$profilesPath/profile"
declare -gr genProfilePath="$profilesPath/home-manager" declare -gr genProfilePath="$profilesPath/home-manager"
declare -gr newGenPath="@GENERATION_DIR@"; declare -gr newGenPath="@GENERATION_DIR@";
declare -gr newGenGcPath="$gcPath/current-home" declare -gr newGenGcPath="$gcPath/current-home"

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Home Manager Modules\n" "Project-Id-Version: Home Manager Modules\n"
"Report-Msgid-Bugs-To: https://github.com/nix-community/home-manager/issues\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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -29,35 +29,35 @@ msgstr ""
msgid "Creating profile generation %s" msgid "Creating profile generation %s"
msgstr "" msgstr ""
#: modules/files.nix:266 #: modules/files.nix:276
msgid "No change so reusing latest profile generation %s" msgid "No change so reusing latest profile generation %s"
msgstr "" msgstr ""
#: modules/home-environment.nix:589 #: modules/home-environment.nix:607
msgid "" 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" "\n"
"Perhaps there is a conflict with a package that was installed using\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" "\n"
" nix-env -q\n" " %s\n"
"\n" "\n"
"and if there is a conflicting package you can remove it with\n" "and if there is a conflicting package you can remove it with\n"
"\n" "\n"
" nix-env -e {package name}\n" " %s\n"
"\n" "\n"
"Then try activating your Home Manager configuration again." "Then try activating your Home Manager configuration again."
msgstr "" msgstr ""
#: modules/home-environment.nix:620 #: modules/home-environment.nix:639
msgid "Activating %s" msgid "Activating %s"
msgstr "" msgstr ""
#: modules/lib-bash/activation-init.sh:30 #: modules/lib-bash/activation-init.sh:31
msgid "Sanity checking oldGenNum and oldGenPath" msgid "Sanity checking oldGenNum and oldGenPath"
msgstr "" msgstr ""
#: modules/lib-bash/activation-init.sh:33 #: modules/lib-bash/activation-init.sh:34
msgid "" msgid ""
"The previous generation number and path are in conflict! These\n" "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" "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!" "and trying home-manager switch again. Good luck!"
msgstr "" msgstr ""
#: modules/lib-bash/activation-init.sh:50 #: modules/lib-bash/activation-init.sh:51
msgid "Starting Home Manager activation" msgid "Starting Home Manager activation"
msgstr "" msgstr ""
#: modules/lib-bash/activation-init.sh:54 #: modules/lib-bash/activation-init.sh:55
msgid "Sanity checking Nix" msgid "Sanity checking Nix"
msgstr "" msgstr ""
#: modules/lib-bash/activation-init.sh:60 #: modules/lib-bash/activation-init.sh:61
msgid "This is a dry run" msgid "This is a dry run"
msgstr "" msgstr ""
#: modules/lib-bash/activation-init.sh:63 #: modules/lib-bash/activation-init.sh:64
msgid "This is a live run" msgid "This is a live run"
msgstr "" msgstr ""
#: modules/lib-bash/activation-init.sh:68 #: modules/lib-bash/activation-init.sh:69
msgid "Using Nix version: %s" msgid "Using Nix version: %s"
msgstr "" msgstr ""
#: modules/lib-bash/activation-init.sh:71 #: modules/lib-bash/activation-init.sh:72
msgid "Activation variables:" msgid "Activation variables:"
msgstr "" msgstr ""