home-manager: refactor activation checks

This is mainly to unify the error messages for more convenient and
consistent translation. Also allows somewhat more convenience if
additional checks are needed.
This commit is contained in:
Robert Helgesson
2026-01-21 09:54:37 +01:00
parent 8434914a03
commit 27b60942b7
6 changed files with 35 additions and 46 deletions

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: 2026-01-20 21:51+0100\n"
"POT-Creation-Date: 2026-01-21 09:53+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"

View File

@@ -847,10 +847,10 @@ in
${builtins.readFile ./lib-bash/activation-init.sh}
if [[ ! -v SKIP_SANITY_CHECKS ]]; then
checkUsername ${lib.escapeShellArg config.home.username}
checkHomeDirectory ${lib.escapeShellArg config.home.homeDirectory}
checkStringEq USER "$USER" ${lib.escapeShellArg config.home.username}
checkPathEq HOME "$HOME" ${lib.escapeShellArg config.home.homeDirectory}
${lib.optionalString (config.home.uid != null) ''
checkUid ${toString config.home.uid}
checkStringEq UID "$(id -u)" ${toString config.home.uid}
''}
fi

View File

@@ -99,33 +99,28 @@ function nixProfileRemove() {
fi
}
function checkUsername() {
local expectedUser="$1"
function checkStringEq() {
local name="$1"
local actual="$2"
local expected="$3"
if [[ "$USER" != "$expectedUser" ]]; then
_iError 'USER is set to "%s" but we expect "%s"' "$USER" "$expectedUser"
exit 1
fi
if [[ "$actual" != "$expected" ]]; then
# translators: For example: HOME is "/home/foo", expected "/home/bar"
_iError '%s is "%s", expected "%s"' "$name" "$actual" "$expected"
exit 1
fi
}
function checkHomeDirectory() {
local expectedHome="$1"
function checkPathEq() {
local name="$1"
local actual="$2"
local expected="$3"
if ! [[ $HOME -ef $expectedHome ]]; then
_iError 'HOME is set to "%s" but we expect "%s"' "$HOME" "$expectedHome"
exit 1
fi
}
function checkUid() {
local expectedUid="$1"
local actualUid
actualUid="$(id -u)"
if [[ "$actualUid" != "$expectedUid" ]]; then
_iError 'UID is "%s" but we expect "%s"' "$actualUid" "$expectedUid"
exit 1
fi
if ! [[ "$actual" -ef "$expected" ]]; then
# translators: For example: HOME is "/home/foo", expected "/home/bar"
_iError '%s is "%s", expected "%s"' "$name" "$actual" "$expected"
exit 1
fi
}
# Note, the VERBOSE_ECHO variable is deprecated and should not be used inside

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: 2026-01-20 21:51+0100\n"
"POT-Creation-Date: 2026-01-21 09:53+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"
@@ -65,38 +65,32 @@ msgstr ""
msgid "Could not find suitable profile directory, tried %s and %s"
msgstr ""
#: modules/lib-bash/activation-init.sh:106
msgid "USER is set to \"%s\" but we expect \"%s\""
#. translators: For example: HOME is "/home/foo", expected "/home/bar"
#: modules/lib-bash/activation-init.sh:109
#: modules/lib-bash/activation-init.sh:121
msgid "%s is \"%s\", expected \"%s\""
msgstr ""
#: modules/lib-bash/activation-init.sh:115
msgid "HOME is set to \"%s\" but we expect \"%s\""
msgstr ""
#: modules/lib-bash/activation-init.sh:126
msgid "UID is \"%s\" but we expect \"%s\""
msgstr ""
#: modules/lib-bash/activation-init.sh:143
#: modules/lib-bash/activation-init.sh:138
msgid "Starting Home Manager activation"
msgstr ""
#: modules/lib-bash/activation-init.sh:147
#: modules/lib-bash/activation-init.sh:142
msgid "Sanity checking Nix"
msgstr ""
#: modules/lib-bash/activation-init.sh:160
#: modules/lib-bash/activation-init.sh:155
msgid "This is a dry run"
msgstr ""
#: modules/lib-bash/activation-init.sh:164
#: modules/lib-bash/activation-init.sh:159
msgid "This is a live run"
msgstr ""
#: modules/lib-bash/activation-init.sh:170
#: modules/lib-bash/activation-init.sh:165
msgid "Using Nix version: %s"
msgstr ""
#: modules/lib-bash/activation-init.sh:173
#: modules/lib-bash/activation-init.sh:168
msgid "Activation variables:"
msgstr ""

View File

@@ -2,6 +2,6 @@
# home.uid defaults to null, so checkUid should not be called in the activation script
nmt.script = ''
assertFileNotRegex activate "checkUid [0-9]+"
assertFileNotRegex activate "checkStringEq UID"
'';
}

View File

@@ -2,6 +2,6 @@
home.uid = 1000;
nmt.script = ''
assertFileContains activate "checkUid 1000"
assertFileContains activate 'checkStringEq UID "$(id -u)" 1000'
'';
}