[Grml] A scheme to manage several ZSH config file collections?

Frank Terbeck ft at grml.org
Thu Jul 24 22:39:30 CEST 2008


Frank Terbeck <ft at grml.org>:
> Richard Hartmann <richih.mailinglist at gmail.com>:
> > On Thu, Jul 24, 2008 at 14:58, Frank Terbeck <ft at grml.org> wrote:
> > >>  1. Don't touch the current zshrc at all. It works fine and should be
> > >>     left intact unmodified.
> > >
> > > We'll need to stop the normal zshrc from sourcing, so something like:
> > 
> > If there _is_ no normal zshrc, zsh-newuser-install will run. As we can
> > easily replace this function, ZSH does the handling of this for free.
> 
> As I said, I would not want to touch /etc/zsh/zshrc. It will always be
> there. Hence, the catch is needed.
> 
> I also do not want to touch ~/.zshrc. It should always be there. If we
> can set ZDOTDIR before zsh is run, we do have means to support the
> *full* range of zsh config files. No need to touch ~/.zshrc at all.
> 
> I really would prefer to keep grml's zsh config like it is now, and if
> you want to switch, you use a function, say 'zmode <whateverrc>' to
> spawn a differently set up shell.
> 
> You can even write the current mode into a file, so that subsequently
> opened shells automatically use that setup.
> 
> No need to fiddle with newuser-install, at all.
[...]
> > >>     Do NOT source ~/.zshrc in that case (set $ZDOTDIR to
> > >>     ~/.zshrc.$ZSHRC_ALT - that would make ~/.zshrc.$ZSHRC_ALT/.zshrc
> > >>     to be sourced; standard zsh mechanism.)
> > >
> > > We should touch a .zshrc in there if a given configuration does not
> > > provide one in order to avoid the newuser-module-dialog.
> > 
> > I would rather have a ~/.zshrc that simply uses $ZDOTDIR to
> > source everything. Solves two problems in one go.
> 
> That's not what $ZDOTDIR is meant for at all.
> It is meant for switching to alternate configurations.
> 
> If $ZDOTDIR is unset, the value of $HOME is used. Hence $HOME/.zshrc
> is used in normal environments.
> 
> Is $ZDOTDIR is /foo/bar/, /foo/bar/.zshrc is used. No matter what
> $HOME is set to.
[...]

So, maybe a bit of code makes things clearer:

[snip]
diff -r f344eeb49c9f etc/zsh/zshenv
--- a/etc/zsh/zshenv	Fri May 30 14:59:34 2008 +0200
+++ b/etc/zsh/zshenv	Thu Jul 24 22:32:19 2008 +0200
@@ -103,5 +103,51 @@
 # MAKEDEV should be usable on udev as well by default:
 export WRITE_ON_UDEV=yes
 
+if [[ -o interactive ]] ; then
+
+    function zmode() {
+        mode=$1 ; shift
+
+        typeset -g ZSHRC_ALT="${mode}"
+        print ${mode} > /var/run/zsh.${USER}.mode
+        if [[ -o login ]] ; then
+            exec zsh -l
+        else
+            exec zsh
+        fi
+    }
+
+    [[ -z ${ZSHRC_ALT} ]] && [[ -e /var/run/zsh.${USER}.mode ]] && ZSHRC_ALT=$(</var/run/zsh.${USER}.mode)
+
+    if [[ -n ${ZSHRC_ALT} ]] ; then
+        if [[ -e /etc/zsh/rcs/zshrc.${ZSHRC_ALT} ]] \
+           && [[ -d /etc/zsh/rcs/${ZSHRC_ALT}.d ]]; then
+
+            setopt extendedglob
+
+            fpath=(
+                /etc/zsh/rcs/${ZSHRC_ALT}.d
+                /etc/zsh/functions.d
+                /etc/zsh/completion.d
+                $fpath
+            )
+
+            for func in /etc/zsh/functions.d/[^_]*[^~] ; do
+                autoload -Uz ${func:t}
+            done
+
+            ZDOTDIR="$HOME/.zshrc.${ZSHRC_ALT}"
+            [[ ! -d ${ZDOTDIR} ]] && mkdir ${ZDOTDIR}
+            [[ ! -e ${ZDOTDIR}/.zshrc ]] && touch ${ZDOTDIR}/.zshrc
+
+            setopt noextendedglob
+
+        else
+            unset ZSHRC_ALT
+        fi
+    fi
+
+fi
+
 ## END OF FILE #################################################################
 # vim:filetype=zsh foldmethod=marker autoindent expandtab shiftwidth=4
diff -r f344eeb49c9f etc/zsh/zshrc
--- a/etc/zsh/zshrc	Fri May 30 14:59:34 2008 +0200
+++ b/etc/zsh/zshrc	Thu Jul 24 22:32:19 2008 +0200
@@ -84,6 +84,11 @@
 #   exactly the same as @@INSERT-aliases-default@@. If you want a list of
 #   *all* aliases, for example, use @@INSERT-aliases-all@@.
 #}}}
+
+if [[ -n ${ZSHRC_ALT} ]] ; then
+    source /etc/zsh/rcs/zshrc.${ZSHRC_ALT}
+    return
+fi
 
 # zsh profiling {{{
 # just execute 'ZSH_PROFILE_RC=1 zsh' and run 'zprof' to get the details
[snap]

I didn't test this at all.
But I think it implements most of the needed mechanisms.

I do not think a releasable version would be a lot more complex. And
IMO this is quite simple.

Regards, Frank



More information about the Grml mailing list