Files
LogSeqDB/logseq/.recycle/pages_Tech_NixOS_Git status in shell prompt with Nix Home Manager.md
2025-12-11 06:26:12 -08:00

1.8 KiB
Raw Permalink Blame History

created:: 2024-01-25T16:49:34 (UTC -05:00) tags:: nixos source:: https://jeffkreeftmeijer.com/nix-home-manager-git-prompt/ author:: Jeff Kreeftmeijer

  • Git status in shell prompt with Nix Home Manager

    Excerpt

    By on 2023-03-19 (originally published on 2023-03-13)


    By on 2023-03-19 (originally published on 2023-03-13)

    Git comes with a script named git-prompt.sh that exposes information about the current directorys repository to use in your shells prompt. The script is in the contrib directory, but I used to extract it to not have to download the git source to set my prompt.

    Using Nix, the prompt is available in the ~/.nix-profile directory, meaning setting up the prompt doesnt require remembering to download an extra file. Instead, its three lines in programs.zsh.initExtra to source the script and set the prompt1:

    programs.zsh.initExtra =
    ''
    source ~/.nix-profile/share/git/contrib/completion/git-prompt.sh
    setopt PROMPT_SUBST
    export PS1='%~ $(__git_ps1 "(%s) ")%# '
    '';
    

    This produces prompts like ~/repository (main) % inside a git repository, and ~/not/a/repository % elsewhere.


    1. This example is for zsh, omit the setopt and alter the PS1 for bash:

      programs.bash.initExtra =
      ''
      source ~/.nix-profile/share/git/contrib/completion/git-prompt.sh
      export PS1='\w $(__git_ps1 "(%s) ")$ '
      '';
      

      ↩︎