diff --git a/fish/functions/mchsh.fish b/fish/functions/mchsh.fish index 7ca2cac3..48c65786 100644 --- a/fish/functions/mchsh.fish +++ b/fish/functions/mchsh.fish @@ -22,13 +22,8 @@ function mchsh -d "Wrapper around 'machinectl shell'" set mchsh_args -l end - # If the current working directory starts with '/home', it needs to be - # translated into a path that the container can use. - if string match -qr ^$HOME $PWD - set container_folder (string replace $HOME /run/host$HOME $PWD) - else - set container_folder $PWD - end - - machinectl shell -q $user_host $SHELL -C "cd $container_folder" $mchsh_args + # Use fish's --init-commands to drop into the current working directory, + # instead of $HOME, which is not really useful if working on the host and + # wanting to drop into the guest to work. + machinectl shell -q $user_host $SHELL -C 'cd '(nspawn_adjust_path $PWD) $mchsh_args end diff --git a/fish/functions/nspawn_adjust_path.fish b/fish/functions/nspawn_adjust_path.fish new file mode 100644 index 00000000..96ad4158 --- /dev/null +++ b/fish/functions/nspawn_adjust_path.fish @@ -0,0 +1,11 @@ +#!/usr/bin/env fish +# SPDX-License-Identifier: MIT +# Copyright (C) 2024 Nathan Chancellor + +function nspawn_adjust_path -d "Translate $HOME paths into /run/host$HOME paths" + if string match -qr ^$HOME $argv[1] + set prefix /run/host + end + + printf '%s%s\n' $prefix $argv[1] +end