Skip to content

Commit 27b6094

Browse files
committed
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.
1 parent 8434914 commit 27b6094

6 files changed

Lines changed: 36 additions & 47 deletions

File tree

home-manager/po/home-manager.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: Home Manager\n"
1010
"Report-Msgid-Bugs-To: https://github.com/nix-community/home-manager/issues\n"
11-
"POT-Creation-Date: 2026-01-20 21:51+0100\n"
11+
"POT-Creation-Date: 2026-01-21 09:53+0100\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"

modules/home-environment.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -847,10 +847,10 @@ in
847847
${builtins.readFile ./lib-bash/activation-init.sh}
848848
849849
if [[ ! -v SKIP_SANITY_CHECKS ]]; then
850-
checkUsername ${lib.escapeShellArg config.home.username}
851-
checkHomeDirectory ${lib.escapeShellArg config.home.homeDirectory}
850+
checkStringEq USER "$USER" ${lib.escapeShellArg config.home.username}
851+
checkPathEq HOME "$HOME" ${lib.escapeShellArg config.home.homeDirectory}
852852
${lib.optionalString (config.home.uid != null) ''
853-
checkUid ${toString config.home.uid}
853+
checkStringEq UID "$(id -u)" ${toString config.home.uid}
854854
''}
855855
fi
856856

modules/lib-bash/activation-init.sh

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -99,33 +99,28 @@ function nixProfileRemove() {
9999
fi
100100
}
101101

102-
function checkUsername() {
103-
local expectedUser="$1"
104-
105-
if [[ "$USER" != "$expectedUser" ]]; then
106-
_iError 'USER is set to "%s" but we expect "%s"' "$USER" "$expectedUser"
107-
exit 1
108-
fi
109-
}
110-
111-
function checkHomeDirectory() {
112-
local expectedHome="$1"
113-
114-
if ! [[ $HOME -ef $expectedHome ]]; then
115-
_iError 'HOME is set to "%s" but we expect "%s"' "$HOME" "$expectedHome"
116-
exit 1
117-
fi
102+
function checkStringEq() {
103+
local name="$1"
104+
local actual="$2"
105+
local expected="$3"
106+
107+
if [[ "$actual" != "$expected" ]]; then
108+
# translators: For example: HOME is "/home/foo", expected "/home/bar"
109+
_iError '%s is "%s", expected "%s"' "$name" "$actual" "$expected"
110+
exit 1
111+
fi
118112
}
119113

120-
function checkUid() {
121-
local expectedUid="$1"
122-
local actualUid
123-
actualUid="$(id -u)"
114+
function checkPathEq() {
115+
local name="$1"
116+
local actual="$2"
117+
local expected="$3"
124118

125-
if [[ "$actualUid" != "$expectedUid" ]]; then
126-
_iError 'UID is "%s" but we expect "%s"' "$actualUid" "$expectedUid"
127-
exit 1
128-
fi
119+
if ! [[ "$actual" -ef "$expected" ]]; then
120+
# translators: For example: HOME is "/home/foo", expected "/home/bar"
121+
_iError '%s is "%s", expected "%s"' "$name" "$actual" "$expected"
122+
exit 1
123+
fi
129124
}
130125

131126
# Note, the VERBOSE_ECHO variable is deprecated and should not be used inside

modules/po/hm-modules.pot

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: Home Manager Modules\n"
1010
"Report-Msgid-Bugs-To: https://github.com/nix-community/home-manager/issues\n"
11-
"POT-Creation-Date: 2026-01-20 21:51+0100\n"
11+
"POT-Creation-Date: 2026-01-21 09:53+0100\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -65,38 +65,32 @@ msgstr ""
6565
msgid "Could not find suitable profile directory, tried %s and %s"
6666
msgstr ""
6767

68-
#: modules/lib-bash/activation-init.sh:106
69-
msgid "USER is set to \"%s\" but we expect \"%s\""
68+
#. translators: For example: HOME is "/home/foo", expected "/home/bar"
69+
#: modules/lib-bash/activation-init.sh:109
70+
#: modules/lib-bash/activation-init.sh:121
71+
msgid "%s is \"%s\", expected \"%s\""
7072
msgstr ""
7173

72-
#: modules/lib-bash/activation-init.sh:115
73-
msgid "HOME is set to \"%s\" but we expect \"%s\""
74-
msgstr ""
75-
76-
#: modules/lib-bash/activation-init.sh:126
77-
msgid "UID is \"%s\" but we expect \"%s\""
78-
msgstr ""
79-
80-
#: modules/lib-bash/activation-init.sh:143
74+
#: modules/lib-bash/activation-init.sh:138
8175
msgid "Starting Home Manager activation"
8276
msgstr ""
8377

84-
#: modules/lib-bash/activation-init.sh:147
78+
#: modules/lib-bash/activation-init.sh:142
8579
msgid "Sanity checking Nix"
8680
msgstr ""
8781

88-
#: modules/lib-bash/activation-init.sh:160
82+
#: modules/lib-bash/activation-init.sh:155
8983
msgid "This is a dry run"
9084
msgstr ""
9185

92-
#: modules/lib-bash/activation-init.sh:164
86+
#: modules/lib-bash/activation-init.sh:159
9387
msgid "This is a live run"
9488
msgstr ""
9589

96-
#: modules/lib-bash/activation-init.sh:170
90+
#: modules/lib-bash/activation-init.sh:165
9791
msgid "Using Nix version: %s"
9892
msgstr ""
9993

100-
#: modules/lib-bash/activation-init.sh:173
94+
#: modules/lib-bash/activation-init.sh:168
10195
msgid "Activation variables:"
10296
msgstr ""

tests/modules/home-environment/uid-null.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
# home.uid defaults to null, so checkUid should not be called in the activation script
33

44
nmt.script = ''
5-
assertFileNotRegex activate "checkUid [0-9]+"
5+
assertFileNotRegex activate "checkStringEq UID"
66
'';
77
}

tests/modules/home-environment/uid.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
home.uid = 1000;
33

44
nmt.script = ''
5-
assertFileContains activate "checkUid 1000"
5+
assertFileContains activate 'checkStringEq UID "$(id -u)" 1000'
66
'';
77
}

0 commit comments

Comments
 (0)