zsh-workers
 help / color / mirror / code / Atom feed
* --init-file option
@ 2023-02-13 10:57 Kalmár Gergely
  2023-02-13 11:24 ` Roman Perepelitsa
  0 siblings, 1 reply; 8+ messages in thread
From: Kalmár Gergely @ 2023-02-13 10:57 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 923 bytes --]

Dear Zsh maintainers,

I was wondering if there is an equivalent to Bash's --init-file option or
fish shell's --init-command option in Zsh or if one can hack around to
achieve the same effect. I would need to be able to source a virtual
environment activation file after the Zsh startup files were processed (but
before a command is executed) in order to be able to start a shell or run
commands inside a Python virtual environment.

I maintain a tool for managing Python virtual environments (
https://docs.logikal.io/pyorbs/) and I had no problems with supporting Bash
and fish (https://github.com/logikal-io/pyorbs/blob/main/pyorbs/shell.py#L45),
but I'm not sure how to achieve the same behavior with Zsh. I'm aware of
zshi (https://github.com/romkatv/zshi/blob/master/zshi), but I'm wondering
if there's any other option that does not involve hacking around the zsh
startup files.

Thank you and kind regards,
Gergely

[-- Attachment #2: Type: text/html, Size: 1240 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: --init-file option
  2023-02-13 10:57 --init-file option Kalmár Gergely
@ 2023-02-13 11:24 ` Roman Perepelitsa
  2023-02-13 13:43   ` Kalmár Gergely
  0 siblings, 1 reply; 8+ messages in thread
From: Roman Perepelitsa @ 2023-02-13 11:24 UTC (permalink / raw)
  To: Kalmár Gergely; +Cc: zsh-workers

On Mon, Feb 13, 2023 at 11:58 AM Kalmár Gergely <kgregory89@gmail.com> wrote:
>
> I was wondering if there is an equivalent to Bash's --init-file
> option or fish shell's --init-command option in Zsh or if one can
> hack around to achieve the same effect. I would need to be able to
> source a virtual environment activation file after the Zsh startup
> files were processed (but before a command is executed) in order to
> be able to start a shell or run commands inside a Python virtual
> environment.

If you are executing a script or a command, then instead of this:

    zsh foo
    zsh -c 'foo'

you can do this:

    zsh -c 'source /path/to/your/file; foo'

If you need to start an interactive zsh that sources an extra file
after the usual startup files, and if you cannot modify any of the
standard startup files, then https://github.com/romkatv/zshi or
something like it is the only option. Disclaimer: It's my project but
I'm not using this code myself.

P.S.

This question is more suitable for zsh-users than zsh-workers.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: --init-file option
  2023-02-13 11:24 ` Roman Perepelitsa
@ 2023-02-13 13:43   ` Kalmár Gergely
  2023-02-13 20:28     ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Kalmár Gergely @ 2023-02-13 13:43 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1733 bytes --]

Thank you! Yes, I'm already using the -c 'source xxx; command' approach
with Bash/fish, and you are right, it should work with Zsh too of course.
Nonetheless, the problem of starting Zsh with an activated virtual
environment remains.

I suppose the next question is if the maintainers would consider adding
this feature to Zsh to support use cases like these, just as Bash/fish do
(which is why I thought writing to zsh-workers instead of zsh-users was
perhaps going to be more appropriate).

Best,
Gergely

On Mon, Feb 13, 2023 at 12:24 PM Roman Perepelitsa <
roman.perepelitsa@gmail.com> wrote:

> On Mon, Feb 13, 2023 at 11:58 AM Kalmár Gergely <kgregory89@gmail.com>
> wrote:
> >
> > I was wondering if there is an equivalent to Bash's --init-file
> > option or fish shell's --init-command option in Zsh or if one can
> > hack around to achieve the same effect. I would need to be able to
> > source a virtual environment activation file after the Zsh startup
> > files were processed (but before a command is executed) in order to
> > be able to start a shell or run commands inside a Python virtual
> > environment.
>
> If you are executing a script or a command, then instead of this:
>
>     zsh foo
>     zsh -c 'foo'
>
> you can do this:
>
>     zsh -c 'source /path/to/your/file; foo'
>
> If you need to start an interactive zsh that sources an extra file
> after the usual startup files, and if you cannot modify any of the
> standard startup files, then https://github.com/romkatv/zshi or
> something like it is the only option. Disclaimer: It's my project but
> I'm not using this code myself.
>
> P.S.
>
> This question is more suitable for zsh-users than zsh-workers.
>

[-- Attachment #2: Type: text/html, Size: 2335 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: --init-file option
  2023-02-13 13:43   ` Kalmár Gergely
@ 2023-02-13 20:28     ` Bart Schaefer
  2023-02-13 20:34       ` Roman Perepelitsa
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2023-02-13 20:28 UTC (permalink / raw)
  To: Kalmár Gergely; +Cc: zsh-workers

Create a directory "virtualenv" with the appropriate configuration in
virtualenv/{.zshenv,.zshrc,.zprofile,.zlogin} and use
  env ZDOTDIR=virtualenv zsh ...

most likely you want virtualenv/.zshrc to be "source $HOME/.zshrc ;
source xxx" and everything else to merely source the corresponding
$HOME file.

Adjust for absolute paths as needed.  A bit of extra work required if
you want to preserve an existing $ZDOTDIR before replacing it.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: --init-file option
  2023-02-13 20:28     ` Bart Schaefer
@ 2023-02-13 20:34       ` Roman Perepelitsa
  2023-02-13 20:59         ` Kalmár Gergely
  0 siblings, 1 reply; 8+ messages in thread
From: Roman Perepelitsa @ 2023-02-13 20:34 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Kalmár Gergely, zsh-workers

On Mon, Feb 13, 2023 at 9:29 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> Create a directory "virtualenv" with the appropriate configuration in
> virtualenv/{.zshenv,.zshrc,.zprofile,.zlogin} and use
>   env ZDOTDIR=virtualenv zsh ...
>
> most likely you want virtualenv/.zshrc to be "source $HOME/.zshrc ;
> source xxx" and everything else to merely source the corresponding
> $HOME file.
>
> Adjust for absolute paths as needed.  A bit of extra work required if
> you want to preserve an existing $ZDOTDIR before replacing it.

zshi essentially automates this process. It also takes some care of
restoring `ZDOTDIR` before sourcing the real user rc files and
checking `[[ -o rcs ]]` afterwards. There is still one thing that
breaks when using zshi (or doing the same thing manually): global rc
files see the wrong `ZDOTDIR`.

Roman.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: --init-file option
  2023-02-13 20:34       ` Roman Perepelitsa
@ 2023-02-13 20:59         ` Kalmár Gergely
  2023-02-13 21:46           ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Kalmár Gergely @ 2023-02-13 20:59 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Bart Schaefer, zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1446 bytes --]

Yes, I could do something similar as what zshi does, but that seems like a
suboptimal solution, because it would require a duplication (and indefinite
maintenance) of the startup file processing logic in an independent
codebase. As the logic is not trivial and has some potential pitfalls as
well, it seems to me that implementing the option within Zsh would be the
most sound (and perhaps simplest) solution from an engineering and
long-term maintenance perspective.

On Mon, Feb 13, 2023 at 9:34 PM Roman Perepelitsa <
roman.perepelitsa@gmail.com> wrote:

> On Mon, Feb 13, 2023 at 9:29 PM Bart Schaefer <schaefer@brasslantern.com>
> wrote:
> >
> > Create a directory "virtualenv" with the appropriate configuration in
> > virtualenv/{.zshenv,.zshrc,.zprofile,.zlogin} and use
> >   env ZDOTDIR=virtualenv zsh ...
> >
> > most likely you want virtualenv/.zshrc to be "source $HOME/.zshrc ;
> > source xxx" and everything else to merely source the corresponding
> > $HOME file.
> >
> > Adjust for absolute paths as needed.  A bit of extra work required if
> > you want to preserve an existing $ZDOTDIR before replacing it.
>
> zshi essentially automates this process. It also takes some care of
> restoring `ZDOTDIR` before sourcing the real user rc files and
> checking `[[ -o rcs ]]` afterwards. There is still one thing that
> breaks when using zshi (or doing the same thing manually): global rc
> files see the wrong `ZDOTDIR`.
>
> Roman.
>

[-- Attachment #2: Type: text/html, Size: 1915 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: --init-file option
  2023-02-13 20:59         ` Kalmár Gergely
@ 2023-02-13 21:46           ` Bart Schaefer
  2023-02-14 10:18             ` Kalmár Gergely
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2023-02-13 21:46 UTC (permalink / raw)
  To: Kalmár Gergely; +Cc: Roman Perepelitsa, zsh-workers

On Mon, Feb 13, 2023 at 12:59 PM Kalmár Gergely <kgregory89@gmail.com> wrote:
>
> Yes, I could do something similar as what zshi does, but that seems like a suboptimal solution, because it would require a duplication (and indefinite maintenance) of the startup file processing logic in an independent codebase.

I don't follow that at all.  You're keeping the file for --init-file
somewhere now (I presume), so why would pointing ZDOTDIR at it require
a new codebase?

Anyway "bash --init-file" replaces (both global and user) bashrc
rather than run the file after them, which doesn't match what you
originally asked for.  Does it  in fact do what you want?

Does your file need to create internal variables or functions, or is
it just populating the environment?


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: --init-file option
  2023-02-13 21:46           ` Bart Schaefer
@ 2023-02-14 10:18             ` Kalmár Gergely
  0 siblings, 0 replies; 8+ messages in thread
From: Kalmár Gergely @ 2023-02-14 10:18 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Roman Perepelitsa, zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1821 bytes --]

I meant keeping it in a codebase independent of Zsh's.

You are right, bash's --init-file does replace bashrc, so I have to source
that too in my activation script. For my use case I just want to start a
new shell that works the same to the user as when they start a vanilla
shell, with the exception that the virtual environment activation file is
sourced. The activation file sets/modifies a bunch of environment variables
(e.g. PATH, PYTHONHOME and PS1) and changes the working directory. To me
fish shell's --init-command is the most elegant approach, but I can also
live with the same approach as what bash does – in which case I only would
need to source .zshrc in my activation script and not meddle with ZDOTDIR
(which might have unintended side-effects, like what Roman mentioned
regarding the startup files seeing the modified ZDOTDIR instead of what the
user potentially sets it to be).

On Mon, Feb 13, 2023 at 10:46 PM Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Mon, Feb 13, 2023 at 12:59 PM Kalmár Gergely <kgregory89@gmail.com>
> wrote:
> >
> > Yes, I could do something similar as what zshi does, but that seems like
> a suboptimal solution, because it would require a duplication (and
> indefinite maintenance) of the startup file processing logic in an
> independent codebase.
>
> I don't follow that at all.  You're keeping the file for --init-file
> somewhere now (I presume), so why would pointing ZDOTDIR at it require
> a new codebase?
>
> Anyway "bash --init-file" replaces (both global and user) bashrc
> rather than run the file after them, which doesn't match what you
> originally asked for.  Does it  in fact do what you want?
>
> Does your file need to create internal variables or functions, or is
> it just populating the environment?
>

[-- Attachment #2: Type: text/html, Size: 2235 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-02-14 10:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-13 10:57 --init-file option Kalmár Gergely
2023-02-13 11:24 ` Roman Perepelitsa
2023-02-13 13:43   ` Kalmár Gergely
2023-02-13 20:28     ` Bart Schaefer
2023-02-13 20:34       ` Roman Perepelitsa
2023-02-13 20:59         ` Kalmár Gergely
2023-02-13 21:46           ` Bart Schaefer
2023-02-14 10:18             ` Kalmár Gergely

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).