Control order of lines automatically added to .bashrc

Hello, I’m struggling with something that feels like something I should be able to figure out but I’m feeling a bit dumb because I’ve been bashing my head against this issue all day and am no closer to figuring it out.

The context of the problem:
I’m trying to use zoxide and starship together, and zoxide is complaining about not being initialised last in my .bashrc. zoxide has a hook that runs before the prompt initialisation, and starship is overwriting that hook when it gets initialised. So the zoxide hook never runs and it doesn’t remember new directories while starship is enabled.

What I’m trying to do:
I want to make sure zoxide is initialised last, after starship. The problem is that I’m not explicitly adding either initialisation to my .bashrc - it’s being added automatically by enabling the packages in home-manager. If I try to add extra stuff to .bashrc manually using programs.bash.bashrcExtra, it puts any extra stuff at the very top of the file, so that isn’t helping me. I’d hoped that changing the order of the imports would have helped but it made no difference. I also verified that neither zoxide, nor starship are anywhere else in my config.

Here are my relevant parts of my config. I cut out all the unnecessary stuff to save time:

~/.bashrc

# Commands that should be applied only for interactive shells.
[[ $- == *i* ]] || return

HISTFILESIZE=100000
HISTSIZE=10000

shopt -s histappend
shopt -s checkwinsize
shopt -s extglob
shopt -s globstar
shopt -s checkjobs

alias blah=nvim
alias eza='eza --icons auto --group-directories-first --no-quotes --git-ignore '\''--icons=always'\'''
alias la='ls -alh'
alias ll='ls -lh'
alias lla='eza -la'
alias ls=eza
alias lt='eza --tree'
alias osr='sudo nixos-rebuild switch --flake ~/src/nixconf#bapm'
alias vim=nvim

if [[ ! -v BASH_COMPLETION_VERSINFO ]]; then
  . "/nix/store/r40zqmfxlvwbg6ap6dn96p3ycqn3yccn-bash-completion-2.16.0/etc/profile.d/bash_completion.sh"
fi

eval "$(/nix/store/7vl9dlly8cmndxys75kn3gzkzyd4i5jf-zoxide-0.9.7/bin/zoxide init bash )"

if [[ :$SHELLOPTS: =~ :(vi|emacs): ]]; then
  eval "$(/nix/store/y2fbihhpi1ml7m63c723qgh4izdvy1rr-fzf-0.60.2/bin/fzf --bash)"
fi

if [[ $TERM != "dumb" ]]; then
  eval "$(/etc/profiles/per-user/mike/bin/starship init bash --print-full-init)"
fi

home.nix (the missing parts are just other imports and home.packages list):

{ pkgs, inputs, config, lib, ... }: {
  imports = [
    ...
    ./programs/starship
    ./programs/zoxide
  ];
  ...
}

programs/starship/default.nix:

{ config, lib, ... }:
let
  accent = "#${config.lib.stylix.colors.base0D}";
  background-alt = "#${config.lib.stylix.colors.base01}";
in {
  programs.starship = {
    enable = true;
    settings = {
      add_newline = true;
      format = lib.concatStrings [
        "$hostname"
        "$directory"
        "$git_branch"
        "$git_state"
        "$git_status"
        "$character"
      ];
      directory = { style = accent; };

      character = {
        success_symbol = "[❯](${accent})";
        error_symbol = "[❯](red)";
        vimcmd_symbol = "[❮](cyan)";
      };

      git_branch = {
        symbol = "[](${background-alt}) ";
        style = "fg:${accent} bg:${background-alt}";
        format = "on [$symbol$branch]($style)[](${background-alt}) ";
      };

      git_status = {
        format =
          "[[(*$conflicted$untracked$modified$staged$renamed$deleted)](218)($ahead_behind$stashed)]($style)";
        style = "cyan";
        conflicted = "";
        renamed = "";
        deleted = "";
        stashed = "≡";
      };

      git_state = {
        format = "([$state( $progress_current/$progress_total)]($style)) ";
        style = "bright-black";
      };
    };
  };
}

programs/zoxide/default.nix:

{
  programs.zoxide = {
    enable = true;
    enableBashIntegration = true;
  };
}

So is it possible to get starship to write its initialisation to .bashrc before zoxide does? I can’t find anyone else mentioning the same issue so I’m hoping it’s an easy answer and I’m just bad at searching. Sorry if this is a dumb question, I’m quite new to nix and still very much learning the ropes.

A similar problem has been addressed here fzf: fix fzf bash-completion conflict by talhaHavadar · Pull Request #5955 · nix-community/home-manager · GitHub

1 Like

Thank you, this is exactly what I needed to solve the issue. For anyone else having the same problem, I just had to modify my zoxide configuration and .bashrc a little bit. I feel like I haven’t used the cleanest approach but it works for me for the time being.

Disable bash integration in zoxide conf:

{
  programs.zoxide = {
    enable = true;
    enableBashIntegration = false;
  };
}

Manually add the zoxide init to .bashrc (via my bash.nix config file) with order 2000 to push it to the end of the queue.

{ config, lib, pkgs, ... }:
let
  nixconf_dir = config.var.nixconf_dir;
  hostname = config.var.hostname;
in {
  programs.bash = {
    enable = true;

    shellAliases = {
      osr = "sudo nixos-rebuild switch --flake ${nixconf_dir}#${hostname}";

      ll = "ls -lh";
      la = "ls -alh";

      vim = "nvim";
      blah = "nvim";
    };

    initExtra = lib.mkOrder 2000
      ''
        eval "$(${lib.getExe pkgs.zoxide} init bash)"    
      '';
  };
}
1 Like

Might be worth opening a PR for if you feel so inclined.