Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zellij should launch the shell as a login shell #1434

Open
hferreiro opened this issue May 24, 2022 · 20 comments
Open

zellij should launch the shell as a login shell #1434

hferreiro opened this issue May 24, 2022 · 20 comments
Labels
config Improvements to the configuration system help wanted Extra attention is needed

Comments

@hferreiro
Copy link

I don't know if this is intended or not, but I noticed that when running tmux I have bash completions enabled, and with zellij they aren't. This looks to be enabled by loading /etc/profile at startup.

@SpyrosRoum
Copy link
Contributor

I believe the shell is responsible for sourcing /etc/profile so I'm not sure how tmux or zellij are relevant, unless tmux loads it on its own for some reason.

You could try sourcing /etc/profile in your bashrc/zshrc maybe?

@hferreiro
Copy link
Author

After some research I found that the issue is whether the shell is run as a login shell or an interactive shell. It appears that tmux runs bash as a login shell, and that's why /etc/profile is sourced. zellij appears to run bash as an interactive shell.

The consequence is surprising behavior as, for example, bash completion won't work when running zellij.

@hferreiro hferreiro changed the title /etc/profile is not loaded when running zellij zellij should launch the shell as a login shell May 30, 2022
@imsnif
Copy link
Member

imsnif commented May 30, 2022

Thanks for looking into this @hferreiro - I also agree this should be the behaviour (unless someone else is aware of a reason for this not to be?)

Putting a Help Wanted here in case anyone wants to work on this.

@imsnif imsnif added the help wanted Extra attention is needed label May 30, 2022
@a-kenji
Copy link
Contributor

a-kenji commented May 30, 2022

I also agree this should be the behaviour (unless someone else is aware of a reason for this not to be?)

By convention a login shell should by default be run once per login, unless explicitly specified by a user.
It is used to automatically run programs that should only be run once per login.
The interactive shell is used to run programs that should be run once per shell.

If we break with this convention users could have performance problems, or other unintended behaviors.

@imsnif
Copy link
Member

imsnif commented May 30, 2022

Hmm - I see your point @a-kenji - I'm just wondering about the definition of a "login shell". When I open a new terminal emulator window, is it considered a login shell?

@a-kenji
Copy link
Contributor

a-kenji commented May 30, 2022

When I open a new terminal emulator window, is it considered a login shell?

It depends on what you configured your shell to be. On most systems it wouldn't and shouldn't be a login shell, at least by default. Because the commands that are sourced are by convention supposed to be run once, at the start of the login and not every time a new instance of the shell is opened.

If we ssh into a machine, then the login shell should be run exactly once - if the user wasn't logged in, but on opening a second pane it should open an interactive shell again.

@imsnif
Copy link
Member

imsnif commented May 30, 2022

I see. This does check out with some local testing I've done. tmux does seem to open a login shell though (afaict in each separate pane according to shopt -q login_shell)... any guesses as to why?

@hferreiro
Copy link
Author

gnome-terminal launches a login shell. I guess probably most terminal emulators, otherwise shell completion wouldn't work.

@a-kenji
Copy link
Contributor

a-kenji commented May 30, 2022

tmux does seem to open a login shell though (afaict in each separate pane according to shopt -q login_shell)... any guesses as to why?

No, I couldn't figure that out yet.

I have found this so far:
https://www.gridbugs.org/daily/tmux-runs-a-login-shell-by-default/
https://www.gridbugs.org/make-sure-your-terminal-emulator-runs-in-the-expected-environment/

gnome-terminal launches a login shell. I guess probably most terminal emulators, otherwise shell completion wouldn't work.

@hferreiro, Not everyone uses /etc/profile as a location for completion. Completion works for me also in interactive shells. Also I doubt that gnome-terminal would launch a login shell.

I know for a fact that foot and alacritty don't spawn shells as a login shell.

Edit: It seems that gnome-terminal doesn't run a login shell by default:
https://help.gnome.org/users/gnome-terminal/stable/pref-login-shell.html.en

@hferreiro
Copy link
Author

My bad, you're right. It appears the completion being missing is because the system I use zellij in is Debian where /etc/profile.d/* isn't sourced by interactive shells, while on Fedora they are, and that's why I assumed that gnome-terminal run login shells.

@imsnif
Copy link
Member

imsnif commented May 31, 2022

Cool, thanks for setting us straight @a-kenji ! So can we close this?

@a-kenji
Copy link
Contributor

a-kenji commented Jun 1, 2022

I think so?

There are legitimate reasons to run the shell as a login shell, but I think the user should opt in to that. Maybe we can add a way to surface this setting easily to a user.

@a-kenji a-kenji added config Improvements to the configuration system and removed suspected bug labels Jun 1, 2022
@hferreiro
Copy link
Author

I tried setting default_shell to -bash and bash --login but that failed.

@a-kenji
Copy link
Contributor

a-kenji commented Jun 2, 2022

@hferreiro,
Yes, currently we only allow the path to a default shell.
What you should be able to do is wrap your shell and then specify the path, but I can't try it out at the moment.

david-davenport added a commit to david-davenport/.dotfile that referenced this issue Jun 29, 2022
There are some issues with using the GNU Stow `--dotfile` argument when
the path to be stowed contains multiple levels of `dot-` style named
files. This is the start of work to rename these files to be plain
dotfiles.

Additional Changes

- Several additions to nvim, git, and bash configurations made over
  time.
- Added .inputrc
- Removed .bash_history (should never have been included)
- Lines from .profile were copied to .bashrc to accomodate issues with
  loading .profile in zellij

See: Zellij GH Issue <zellij-org/zellij#1434>
schrotthaufen pushed a commit to schrotthaufen/dotfiles that referenced this issue Feb 2, 2023
Sourcing ~/.bash_profile in ~/.bashrc is neccessary, because zellij
doesn't launch a login shell.
See zellij-org/zellij#1434 for reference
@jcayzac
Copy link

jcayzac commented Jun 11, 2024

As a workaround I ended up writing this small bash-login wrapper script, which is in my $PATH. Don't forget to chmod +x it.

#!/usr/bin/env bash
exec /usr/bin/env -i HOME="$HOME" SHELL="$SHELL" PATH="$PATH" TERM="$TERM" USER="$USER" "$BASH" -il

…and in ~/.config/zellij/config.kdl:

default_shell "bash-login"

@zachvalenta
Copy link

zachvalenta commented Jun 23, 2024

There are legitimate reasons to run the shell as a login shell, but I think the user should opt in to that. Maybe we can add a way to surface this setting easily to a user.

@a-kenji Just an upvote for this. I want my .zprofile sourced on shell init otherwise I'm losing my prompt/aliases/env var.

@imsnif Could this be worked around by adding a new CLI or config option to enable running command(s) on pane startup (in my case,source $HOME/.zprofile)

@zachvalenta
Copy link

I tried to work around this with pane commands but no luck, as the source command was not available:

layout {
    pane
    pane split_direction="vertical" {
        pane command="source" {
            args "$HOME/.zprofile"
        }
    }
}

Some UNIX commands were available, however, like echo:

layout {
    pane
    pane split_direction="vertical" {
        pane command="echo" {
            args "hello"
        }
    }
}

@jokeyrhyme
Copy link

I spent a more time than I'd like to admit trying to make the concept of "login shell" useful on my system, and came to the conclusion that it's practically worthless, due to inconsistencies in how the user's SHELL is called by the various components that start it: https://gitlab.com/jokeyrhyme/dotfiles/-/issues/129#note_1575856312

This is in no way the fault of zellij, but we cannot depend on exactly one login shell per user session, because it might never run, and it might run more than once

I'd recommend removing any assumption about login shells from your configuration completely, and coming up with an alternative mechanism for guaranteeing that something happens exactly once per user session

Either that, or making such configuration idempotent so that it can be run every time your shell launches without breaking anything

@zachvalenta
Copy link

coming up with an alternative mechanism for guaranteeing that something happens exactly once per user session

To be clear, what I'm going for is the option to use login shells in new panes/tabs in the manner of tmux, iTerm, et al.

@zachvalenta
Copy link

I got around this by sourcing my profile in .zshenv, although the downside is that now my profile is being sourced twice when I'm outside Zellij.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
config Improvements to the configuration system help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants