Autostart Zellij in Nushell

Cool kids multiplex with Zellij. Data engineers shred data with Nushell. Let’s make them work better together. Let’s autostart Zellij when our terminal and shell start.

The Zellij documentation provides instructions for making Zellij autostart in bash, zsh, and fish. In short, Zellij provides commands that inject some shell script into your shell’s startup script. It doesn’t offer this support for Nushell.

I started down a path of creating a pull request to add this support to Zellij. I discovered that Zellij determines your shell based on the Shell enum in clap:

match shell {
    Shell::Bash => {
        let _ = out.write_all(BASH_AUTO_START_SCRIPT);
    },
    Shell::Fish => {
        let _ = out.write_all(FISH_AUTO_START_SCRIPT);
    },
    Shell::Zsh => {
        let _ = out.write_all(ZSH_AUTO_START_SCRIPT);
    },
    _ => {},
}

https://github.com/zellij-org/zellij/blob/main/zellij-utils/src/setup.rs#L572

I also determined that clap’s Shell doesn’t support Nushell:

pub enum Shell {
    /// Bourne Again SHell (bash)
    Bash,
    /// Elvish shell
    Elvish,
    /// Friendly Interactive SHell (fish)
    Fish,
    /// PowerShell
    PowerShell,
    /// Z SHell (zsh)
    Zsh,
}

https://github.com/clap-rs/clap/blob/master/clap_complete/src/shells/shell.rs#L14

I wimped out. I figured I’d either have to pretend to add Nushell support to clap, which probably should amount to a lot more than adding another enum. Or I’d have to add logic to the unmatched case in Zellij, which would be kind of ugly. And ultimately, all we’re doing is adding some Nushell script to our startup. So I bypassed the middleman.

I’m a Nushell noob, so apologies if this script isn’t optimal. I translated the zsh version of the script that Zellij provides. My one hurdle was figuring out whether an environment variable exists in Nushell. The methods I found online didn’t seem to work, so I assume they were for older versions of Nushell. Ultimately, I settled on this:

# zellij
def start_zellij [] {
  if 'ZELLIJ' not-in ($env | columns) {
    if 'ZELLIJ_AUTO_ATTACH' in ($env | columns) and $env.ZELLIJ_AUTO_ATTACH == 'true' {
      zellij attach -c
    } else {
      zellij
    }

    if 'ZELLIJ_AUTO_EXIT' in ($env | columns) and $env.ZELLIJ_AUTO_EXIT == 'true' {
      exit
    }
  }
}

start_zellij

I put this in my config.nu configuration file. This function will start Zellij when Nushell starts, unless Zellij is already running in that terminal. It also supports two environment variables, like the zsh and bash versions of the script, that you can put in your env.nu:

  • ZELLIJ_AUTO_ATTACH: If set to “true”, attach to existing Zellij session instead of starting a new session.
  • ZELLIJ_AUTO_EXIT: If set to “true”, exit Nushell when Zellij exits.

Now I am shreddin’ cool.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.