summaryrefslogtreecommitdiffstats
path: root/makefu/2configs/zsh-user.nix
blob: 23ae572da6d08c1c5105c36544fc4d189afa41f0 (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
{ config, lib, pkgs, ... }:
##
with import <stockholm/lib>;
let
  mainUser = config.krebs.build.user.name;
in
{
  users.extraUsers.${mainUser}.shell = "/run/current-system/sw/bin/zsh";
  programs.zsh= {
    enable = true;
    enableCompletion = true ; #manually at the end
    interactiveShellInit = ''
      HISTSIZE=900001
      HISTFILESIZE=$HISTSIZE
      SAVEHIST=$HISTSIZE
      HISTFILE=$HOME/.zsh_history

      setopt HIST_IGNORE_ALL_DUPS
      setopt HIST_IGNORE_SPACE
      setopt HIST_FIND_NO_DUPS
      bindkey -e
      # shift-tab
      bindkey '^[[Z' reverse-menu-complete
      bindkey "\e[3~" delete-char
      zstyle ':completion:*' menu select

      ${pkgs.gnupg}/bin/gpg-connect-agent updatestartuptty /bye >/dev/null
      GPG_TTY=$(tty)
      export GPG_TTY
      LS_COLORS=$LS_COLORS:'di=1;31:' ; export LS_COLORS

      unset SSH_AGENT_PID
      export SSH_AUTH_SOCK="/run/user/$UID/gnupg/S.gpg-agent.ssh"

      # fzf
      __fsel_fzf() {
        local cmd="''${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
          -o -type f -print \
          -o -type d -print \
          -o -type l -print 2> /dev/null | cut -b3-"}"
        setopt localoptions pipefail 2> /dev/null
        eval "$cmd" | FZF_DEFAULT_OPTS="--height ''${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read item; do
          echo -n "''${(q)item} "
        done
        local ret=$?
        echo
        return $ret
      }

      __fzf_use_tmux__() {
        [ -n "$TMUX_PANE" ] && [ "''${FZF_TMUX:-0}" != 0 ] && [ ''${LINES:-40} -gt 15 ]
      }

      __fzfcmd() {
        __fzf_use_tmux__ &&
          echo "fzf-tmux -d''${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"
      }

      fzf-file-widget() {
        LBUFFER="''${LBUFFER}$(__fsel_fzf)"
        local ret=$?
        zle redisplay
        typeset -f zle-line-init >/dev/null && zle zle-line-init
        return $ret
      }
      zle     -N   fzf-file-widget
      bindkey '^T' fzf-file-widget

      compdef _pass brain
      zstyle ':completion::complete:brain::' prefix "$HOME/brain"
      compdef _pass secrets
      zstyle ':completion::complete:secrets::' prefix "$HOME/.secrets-pass/"

      # ctrl-x ctrl-e
      autoload -U edit-command-line
      zle -N edit-command-line
      bindkey '^xe' edit-command-line
      bindkey '^x^e' edit-command-line

    '';

    promptInit = ''
      RPROMPT=""
      autoload colors && colors
      case $UID in
         0) PROMPT="%{$fg[red]%}%~%{$reset_color%} " ;;
      9001) PROMPT="%{$fg[green]%}%~%{$reset_color%} " ;;
         *) PROMPT="%{$fg[yellow]%}%n %{$fg[green]%}%~%{$reset_color%} " ;;
      esac
      if test -n "$SSH_CLIENT"; then
        PROMPT="%{$fg[magenta]%}%m $PROMPT"
      fi
      '';
  };

  users.users.${mainUser}.packages = [
    pkgs.nix-zsh-completions
    pkgs.fzf
  ];
}