home-manager: add repl subcommand

Similar to the `nixos-rebuild repl` command, `home-manager repl` will
launch the Nix read-evaluate-print-loop environment with the Home
Manager configuration loaded.

To make that more useful, also add the pkgs and options attributes from
the generated Home Manager configuration to the environment.

This doesn't currently work with flakes, because I don't use them and
I'm not confident I could safely test that function.
This commit is contained in:
Adam Dinwoodie 2024-07-15 22:46:20 +01:00
parent 36e2f9da91
commit 77326d1a2e
2 changed files with 30 additions and 2 deletions

View file

@ -634,6 +634,28 @@ function doBuild() {
presentNews presentNews
} }
function doRepl() {
setFlakeAttribute
if [[ -v FLAKE_CONFIG_URI ]]; then
_i 'home-manager repl does not (yet) support flakes' >&2
return 1
fi
setConfigFile
extraArgs=()
for p in "${EXTRA_NIX_PATH[@]}"; do
extraArgs+=(-I "$p")
done
exec nix repl \
--file '<home-manager/home-manager/home-manager.nix>' \
"${extraArgs[@]}" \
"${PASSTHROUGH_OPTS[@]}" \
--argstr confPath "$HOME_MANAGER_CONFIG" \
--argstr confAttr "$HOME_MANAGER_CONFIG_ATTRIBUTE"
}
function doSwitch() { function doSwitch() {
setWorkDir setWorkDir
@ -937,6 +959,9 @@ function doHelp() {
echo " Remove indicated generations. Use 'generations' command to" echo " Remove indicated generations. Use 'generations' command to"
echo " find suitable generation numbers." echo " find suitable generation numbers."
echo echo
echo " repl"
echo " Opens the configuration in \`nix repl\`"
echo
echo " expire-generations TIMESTAMP" echo " expire-generations TIMESTAMP"
echo " Remove generations older than TIMESTAMP where TIMESTAMP is" echo " Remove generations older than TIMESTAMP where TIMESTAMP is"
echo " interpreted as in the -d argument of the date tool. For" echo " interpreted as in the -d argument of the date tool. For"
@ -960,7 +985,7 @@ while [[ $# -gt 0 ]]; do
opt="$1" opt="$1"
shift shift
case $opt in case $opt in
build|init|instantiate|option|edit|expire-generations|generations|help|news|packages|remove-generations|switch|uninstall) build|init|instantiate|option|edit|expire-generations|generations|help|news|packages|remove-generations|repl|switch|uninstall)
COMMAND="$opt" COMMAND="$opt"
;; ;;
-A) -A)
@ -1105,6 +1130,9 @@ case $COMMAND in
packages) packages)
doListPackages doListPackages
;; ;;
repl)
doRepl
;;
news) news)
doShowNews --all doShowNews --all
;; ;;

View file

@ -15,4 +15,4 @@ let
check = check; check = check;
}; };
in { inherit (env) activationPackage config; } in { inherit (env) activationPackage config pkgs options; }