summaryrefslogtreecommitdiffstats
path: root/lass/2configs/zsh.nix
blob: a7b0c372c4baf195d850c0eb5537ceae396135f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{ config, lib, pkgs, ... }:
{
  environment.systemPackages = with pkgs; [
    atuin
    direnv
    fzf
  ];
  environment.variables.ATUIN_CONFIG_DIR = toString (pkgs.writeTextDir "/config.toml" ''
    auto_sync = true
    update_check = false
    sync_address = "http://green.r:8888"
    sync_frequency = 0
    style = "compact"
  '');
  programs.zsh = {
    enable = true;
    shellInit = ''
      #disable config wizard
      zsh-newuser-install() { :; }
    '';
    interactiveShellInit = ''
      unsetopt nomatch # no matches found urls
      setopt autocd extendedglob
      bindkey -e


      # # setopt inc_append_history
      # bindkey '^R' history-incremental-search-backward

      #C-x C-e open line in editor
      autoload -z edit-command-line
      zle -N edit-command-line
      bindkey "^X^E" edit-command-line

      #fzf inclusion
      source ${pkgs.fzf}/share/fzf/completion.zsh
      source ${pkgs.fzf}/share/fzf/key-bindings.zsh

      # atuin distributed shell history
      export ATUIN_NOBIND="true" # disable all keybdinings of atuin
      eval "$(atuin init zsh)"
      bindkey '^r' _atuin_search_widget # bind ctrl+r to atuin
      # use zsh only session history
      fc -p

      #completion magic
      autoload -Uz compinit
      compinit
      zstyle ':completion:*' menu select

      #enable automatic rehashing of $PATH
      zstyle ':completion:*' rehash true

      #beautiful colors
      eval $(dircolors -b ${pkgs.fetchFromGitHub {
        owner = "trapd00r";
        repo = "LS_COLORS";
        rev = "a75fca8545f91abb8a5f802981033ef54bf1eac0";
        sha256="1lzj0qnj89mzh76ha137mnz2hf86k278rh0y9x124ghxj9yqsnb4";
      }}/LS_COLORS)
      zstyle ':completion:*:default' list-colors ''${(s.:.)LS_COLORS}

      #emacs bindings
      bindkey "[7~" beginning-of-line
      bindkey "[8~" end-of-line
      bindkey "Oc" emacs-forward-word
      bindkey "Od" emacs-backward-word

      # direnv integration
      eval "$(${pkgs.direnv}/bin/direnv hook zsh)"
    '';
    promptInit = ''
      autoload -U promptinit
      promptinit

      p_error='%(?..%F{red}%?%f )'
      t_error='%(?..%? )'

      case $UID in
        0)
          p_username='%F{red}root%f'
          t_username='root'
          ;;
        1337)
          p_username=""
          t_username=""
          ;;
        *)
          p_username='%F{blue}%n%f'
          t_username='%n'
          ;;
      esac

      if test -n "$SSH_CLIENT"; then
        p_hostname='@%F{magenta}%M%f '
        t_hostname='@%M '
      else
        p_hostname=""
        t_hostname=""
      fi

      #check if in nix shell
      if test -n "$IN_NIX_SHELL"; then
        p_nixshell='%F{green}[s]%f '
        t_nixshell='[s] '
      else
        p_nixshell=""
        t_nixshell=""
      fi

      PROMPT="$p_error$p_username$p_hostname$p_nixshell%~ "
      TITLE="$t_error$t_username$t_hostname$t_nixshell%~"
      case $TERM in
        (*xterm* | *rxvt*)
          function precmd {
            PROMPT_EVALED=$(print -P "$TITLE")
            echo -ne "\033]0;$$ $PROMPT_EVALED\007"
          }
          # This seems broken for some reason
          # # This is seen while the shell waits for a command to complete.
          # function preexec {
          #   PROMPT_EVALED=$(print -P "$TITLE")
          #   echo -ne "\033]0;$$ $PROMPT_EVALED $1\007"
          # }
        ;;
      esac
    '';
  };
  environment.shellAliases.ns = "nix-shell --command zsh";

  users.defaultUserShell = "/run/current-system/sw/bin/zsh";
}