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
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"

View File

@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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 ""

View File

@ -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"

View File

@ -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
''
);

View File

@ -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"

View File

@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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 ""