From b5c428822468b9a2dfb00a673c39e809f4ac0651 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 19 Jun 2022 15:57:45 -0700 Subject: ref(home/shell): make it easier to share common things between shells I'm considering trying again fish, and there are a number of things that should be common between zsh and fish (aliases, environment variables, ...). Instead of duplicating these settings multiple time, I'm consolidating the shell configurations under `home/shell`, and I can set the shell I want to use with `my.home.shell.name`. The first step is to move the modules for fish and zsh under `home/shell`, add an interface to pick which one I want to use, and modify the `host/home.nix` configuration to keep using zsh with the new interface. Change-Id: Idb66b1a6fcc11a6eeaf5fd2d32dd3698d2d85bdf Reviewed-on: https://cl.fcuny.net/c/world/+/455 Tested-by: CI Reviewed-by: Franck Cuny --- home/default.nix | 3 +- home/direnv/default.nix | 2 +- home/fish/default.nix | 30 ------------------- home/shell/default.nix | 16 ++++++++++ home/shell/fish/default.nix | 29 +++++++++++++++++++ home/shell/zsh/completion-style.zsh | 34 ++++++++++++++++++++++ home/shell/zsh/default.nix | 54 ++++++++++++++++++++++++++++++++++ home/shell/zsh/gerrit.zsh | 5 ++++ home/shell/zsh/options.zsh | 27 +++++++++++++++++ home/shell/zsh/prompt.zsh | 11 +++++++ home/shell/zsh/sway.zsh | 3 ++ home/shell/zsh/tmux.zsh | 9 ++++++ home/zsh/completion-style.zsh | 34 ---------------------- home/zsh/default.nix | 58 ------------------------------------- home/zsh/gerrit.zsh | 5 ---- home/zsh/options.zsh | 27 ----------------- home/zsh/prompt.zsh | 11 ------- home/zsh/sway.zsh | 3 -- home/zsh/tmux.zsh | 9 ------ hosts/aptos/home.nix | 2 +- hosts/carmel/home.nix | 2 +- hosts/tahoe/home.nix | 4 +-- 22 files changed, 194 insertions(+), 184 deletions(-) delete mode 100644 home/fish/default.nix create mode 100644 home/shell/default.nix create mode 100644 home/shell/fish/default.nix create mode 100644 home/shell/zsh/completion-style.zsh create mode 100644 home/shell/zsh/default.nix create mode 100644 home/shell/zsh/gerrit.zsh create mode 100644 home/shell/zsh/options.zsh create mode 100644 home/shell/zsh/prompt.zsh create mode 100644 home/shell/zsh/sway.zsh create mode 100644 home/shell/zsh/tmux.zsh delete mode 100644 home/zsh/completion-style.zsh delete mode 100644 home/zsh/default.nix delete mode 100644 home/zsh/gerrit.zsh delete mode 100644 home/zsh/options.zsh delete mode 100644 home/zsh/prompt.zsh delete mode 100644 home/zsh/sway.zsh delete mode 100644 home/zsh/tmux.zsh diff --git a/home/default.nix b/home/default.nix index 035bdb6..f2111db 100644 --- a/home/default.nix +++ b/home/default.nix @@ -12,7 +12,6 @@ ./evince ./feh ./firefox - ./fish ./flac ./gcloud ./git @@ -27,6 +26,7 @@ ./python ./scanner ./seahorse + ./shell ./ssh ./sublime-music ./terminal @@ -36,6 +36,5 @@ ./wm ./xdg ./yt-dlp - ./zsh ]; } diff --git a/home/direnv/default.nix b/home/direnv/default.nix index f36a66c..9d8d419 100644 --- a/home/direnv/default.nix +++ b/home/direnv/default.nix @@ -1,7 +1,7 @@ { config, lib, pkgs, ... }: let cfg = config.my.home.direnv; - fishEnabled = config.my.home.fish.enable; + fishEnabled = config.my.home.shell.name == "fish"; in { options.my.home.direnv = with lib; { diff --git a/home/fish/default.nix b/home/fish/default.nix deleted file mode 100644 index 7ff4bbe..0000000 --- a/home/fish/default.nix +++ /dev/null @@ -1,30 +0,0 @@ -{ config, lib, pkgs, ... }: -let - cfg = config.my.home.fish; - swayEnabled = config.my.home.wm.windowManager == "sway"; -in -{ - options.my.home.fish = with lib; { - enable = mkEnableOption "fish configuration"; - }; - config.programs.fish = lib.mkIf cfg.enable { - enable = true; - interactiveShellInit = '' - set fish_greeting - - # Tmux on terminal start, unless we're in a SSH connection - if status is-interactive - if test -z "$SSH_CONNECTION" - if not tmux has-session 2>/dev/null; or test -z "$TMUX" - exec tmux new-session -A -s 0 - end - end - end - ''; - loginShellInit = lib.mkIf swayEnabled '' - if test -z "$DISPLAY"; and test (tty) = "/dev/tty1" - exec sway - end - ''; - }; -} diff --git a/home/shell/default.nix b/home/shell/default.nix new file mode 100644 index 0000000..35a6275 --- /dev/null +++ b/home/shell/default.nix @@ -0,0 +1,16 @@ +{ config, lib, pkgs, ... }: + +with lib; +let cfg = config.my.home.shell; +in +{ + options.my.home.shell = { + name = mkOption { + default = "zsh"; + type = types.enum [ "fish" "zsh" ]; + example = "zsh"; + }; + }; + + imports = [ ./fish ./zsh ]; +} diff --git a/home/shell/fish/default.nix b/home/shell/fish/default.nix new file mode 100644 index 0000000..3a21b24 --- /dev/null +++ b/home/shell/fish/default.nix @@ -0,0 +1,29 @@ +{ config, lib, pkgs, ... }: +let + cfg = config.my.home.shell; + swayEnabled = config.my.home.wm.windowManager == "sway"; +in +{ + config = lib.mkIf (cfg.name == "fish") { + programs.fish = { + enable = true; + interactiveShellInit = '' + set fish_greeting + + # Tmux on terminal start, unless we're in a SSH connection + if status is-interactive + if test -z "$SSH_CONNECTION" + if not tmux has-session 2>/dev/null; or test -z "$TMUX" + exec tmux new-session -A -s 0 + end + end + end + ''; + loginShellInit = lib.mkIf swayEnabled '' + if test -z "$DISPLAY"; and test (tty) = "/dev/tty1" + exec sway + end + ''; + }; + }; +} diff --git a/home/shell/zsh/completion-style.zsh b/home/shell/zsh/completion-style.zsh new file mode 100644 index 0000000..32bd6f4 --- /dev/null +++ b/home/shell/zsh/completion-style.zsh @@ -0,0 +1,34 @@ +# Style the completion a bit +zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} + +# Show a prompt on selection +zstyle ':completion:*' select-prompt '%SScrolling active: current selection at %p%s' + +# Use arrow keys in completion list +zstyle ':completion:*' menu select + +# Group results by category +zstyle ':completion:*' group-name '' + +# Keep directories and files separated +zstyle ':completion:*' list-dirs-first true + +# Add colors to processes for kill completion +zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31' + +# match uppercase from lowercase +zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' + +# Filename suffixes to ignore during completion (except after rm command) +zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.old' + +# command for process lists, the local web server details and host completion +# on processes completion complete all user processes +zstyle ':completion:*:processes' command 'ps -au$USER' + +# Completion formatting and messages +zstyle ':completion:*' verbose yes +zstyle ':completion:*:descriptions' format '%B%d%b' +zstyle ':completion:*:messages' format '%d' +zstyle ':completion:*:warnings' format 'No matches for: %d' +zstyle ':completion:*:corrections' format '%B%d (errors: %e)%b' diff --git a/home/shell/zsh/default.nix b/home/shell/zsh/default.nix new file mode 100644 index 0000000..14ddd6c --- /dev/null +++ b/home/shell/zsh/default.nix @@ -0,0 +1,54 @@ +{ config, pkgs, lib, ... }: +let cfg = config.my.home.shell; +in +{ + config = lib.mkIf (cfg.name == "zsh") { + home.packages = with pkgs; [ zsh-completions ]; + + programs.zsh = { + enable = true; + dotDir = ".config/zsh"; + enableCompletion = true; + + history = { + size = 500000; + save = 500000; + extended = false; + ignoreSpace = true; + ignoreDups = true; + share = false; + # see + # https://github.com/nix-community/home-manager/blob/32a7da69dc53c9eb5ad0675eb7fdc58f7fe35272/modules/programs/zsh.nix#L537 + path = ".local/share/zsh/zsh_history"; + }; + + localVariables = { + # Print timing statistics for everything which takes longer than 5 seconds of + # user + system time. + REPORTTIME = 5; + }; + + shellAliases = { + ll = "ls -l --color=auto"; + lt = "ls -ltrh --color=auto"; + }; + + defaultKeymap = "emacs"; + + # the order matters for sway.zsh: I want to load it first + # to start the session if I log on a machine where it's + # available. + # it can't be loaded with `initExtraFirst` because the session + # variables from home-manager are not set yet otherwise (and I + # need them for things like firefox or emacs). + initExtra = lib.concatMapStrings builtins.readFile [ + ./sway.zsh + ./completion-style.zsh + ./gerrit.zsh + ./options.zsh + ./prompt.zsh + ./tmux.zsh + ]; + }; + }; +} diff --git a/home/shell/zsh/gerrit.zsh b/home/shell/zsh/gerrit.zsh new file mode 100644 index 0000000..9766b2f --- /dev/null +++ b/home/shell/zsh/gerrit.zsh @@ -0,0 +1,5 @@ +function rgerrit() { + # gerrit runs on tahoe, and we ssh to it through its tailscale + # network interface. + ssh $(tailscale status --json | jq -r '.Peer | map(select(.HostName == "tahoe"))[0].TailscaleIPs[0]') -p 29418 gerrit $@ +} diff --git a/home/shell/zsh/options.zsh b/home/shell/zsh/options.zsh new file mode 100644 index 0000000..6d39bc1 --- /dev/null +++ b/home/shell/zsh/options.zsh @@ -0,0 +1,27 @@ +# Show an error when a globbing expansion doesn't find any match +setopt nomatch + +# List on ambiguous completion and Insert first match immediately +setopt autolist menucomplete + +# Use pushd when cd-ing around +setopt autopushd pushdminus pushdsilent + +# Use single quotes in string without the weird escape tricks +setopt rcquotes + +# Single word commands can resume an existing job +setopt autoresume + +# Append commands to history as they are exectuted +setopt inc_append_history_time + +# Remove useless whitespace from commands +setopt hist_reduce_blanks + +# Those options aren't wanted +unsetopt beep extendedglob notify + +# word select works like in bash +autoload -U select-word-style +select-word-style bash diff --git a/home/shell/zsh/prompt.zsh b/home/shell/zsh/prompt.zsh new file mode 100644 index 0000000..1c21d4b --- /dev/null +++ b/home/shell/zsh/prompt.zsh @@ -0,0 +1,11 @@ +setopt prompt_subst + +PROMPT='%K{cyan}%F{black}%m%k%f %~ %% ' + +# For tramp (emacs). +if [ "$TERM" = "dumb" ]; then + unset PROMPT + PS1='$ ' + unsetopt zle +fi + diff --git a/home/shell/zsh/sway.zsh b/home/shell/zsh/sway.zsh new file mode 100644 index 0000000..69d5103 --- /dev/null +++ b/home/shell/zsh/sway.zsh @@ -0,0 +1,3 @@ +if [ -z $DISPLAY ] && [ "$(tty)" = "/dev/tty1" ]; then + exec sway +fi diff --git a/home/shell/zsh/tmux.zsh b/home/shell/zsh/tmux.zsh new file mode 100644 index 0000000..97944f5 --- /dev/null +++ b/home/shell/zsh/tmux.zsh @@ -0,0 +1,9 @@ +# If we're not in an ssh connection, and tmux is installed, and we're +# not already in a tmux session, attach to the session named +# 'default', and if the session does not exist, start one named +# 'default' +if [ -z "$SSH_CONNECTION" ]; then + if command -v tmux &> /dev/null && [ -z "$TMUX" ]; then + tmux attach -t default || tmux new -s default + fi +fi diff --git a/home/zsh/completion-style.zsh b/home/zsh/completion-style.zsh deleted file mode 100644 index 32bd6f4..0000000 --- a/home/zsh/completion-style.zsh +++ /dev/null @@ -1,34 +0,0 @@ -# Style the completion a bit -zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} - -# Show a prompt on selection -zstyle ':completion:*' select-prompt '%SScrolling active: current selection at %p%s' - -# Use arrow keys in completion list -zstyle ':completion:*' menu select - -# Group results by category -zstyle ':completion:*' group-name '' - -# Keep directories and files separated -zstyle ':completion:*' list-dirs-first true - -# Add colors to processes for kill completion -zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31' - -# match uppercase from lowercase -zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' - -# Filename suffixes to ignore during completion (except after rm command) -zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.old' - -# command for process lists, the local web server details and host completion -# on processes completion complete all user processes -zstyle ':completion:*:processes' command 'ps -au$USER' - -# Completion formatting and messages -zstyle ':completion:*' verbose yes -zstyle ':completion:*:descriptions' format '%B%d%b' -zstyle ':completion:*:messages' format '%d' -zstyle ':completion:*:warnings' format 'No matches for: %d' -zstyle ':completion:*:corrections' format '%B%d (errors: %e)%b' diff --git a/home/zsh/default.nix b/home/zsh/default.nix deleted file mode 100644 index 21dbedd..0000000 --- a/home/zsh/default.nix +++ /dev/null @@ -1,58 +0,0 @@ -{ config, pkgs, lib, ... }: -let cfg = config.my.home.zsh; -in -{ - options.my.home.zsh = with lib; { - enable = mkEnableOption "zsh configuration"; - }; - - config = lib.mkIf cfg.enable { - home.packages = with pkgs; [ zsh-completions ]; - - programs.zsh = { - enable = true; - dotDir = ".config/zsh"; - enableCompletion = true; - - history = { - size = 500000; - save = 500000; - extended = false; - ignoreSpace = true; - ignoreDups = true; - share = false; - # see - # https://github.com/nix-community/home-manager/blob/32a7da69dc53c9eb5ad0675eb7fdc58f7fe35272/modules/programs/zsh.nix#L537 - path = ".local/share/zsh/zsh_history"; - }; - - localVariables = { - # Print timing statistics for everything which takes longer than 5 seconds of - # user + system time. - REPORTTIME = 5; - }; - - shellAliases = { - ll = "ls -l --color=auto"; - lt = "ls -ltrh --color=auto"; - }; - - defaultKeymap = "emacs"; - - # the order matters for sway.zsh: I want to load it first - # to start the session if I log on a machine where it's - # available. - # it can't be loaded with `initExtraFirst` because the session - # variables from home-manager are not set yet otherwise (and I - # need them for things like firefox or emacs). - initExtra = lib.concatMapStrings builtins.readFile [ - ./sway.zsh - ./completion-style.zsh - ./gerrit.zsh - ./options.zsh - ./prompt.zsh - ./tmux.zsh - ]; - }; - }; -} diff --git a/home/zsh/gerrit.zsh b/home/zsh/gerrit.zsh deleted file mode 100644 index 9766b2f..0000000 --- a/home/zsh/gerrit.zsh +++ /dev/null @@ -1,5 +0,0 @@ -function rgerrit() { - # gerrit runs on tahoe, and we ssh to it through its tailscale - # network interface. - ssh $(tailscale status --json | jq -r '.Peer | map(select(.HostName == "tahoe"))[0].TailscaleIPs[0]') -p 29418 gerrit $@ -} diff --git a/home/zsh/options.zsh b/home/zsh/options.zsh deleted file mode 100644 index 6d39bc1..0000000 --- a/home/zsh/options.zsh +++ /dev/null @@ -1,27 +0,0 @@ -# Show an error when a globbing expansion doesn't find any match -setopt nomatch - -# List on ambiguous completion and Insert first match immediately -setopt autolist menucomplete - -# Use pushd when cd-ing around -setopt autopushd pushdminus pushdsilent - -# Use single quotes in string without the weird escape tricks -setopt rcquotes - -# Single word commands can resume an existing job -setopt autoresume - -# Append commands to history as they are exectuted -setopt inc_append_history_time - -# Remove useless whitespace from commands -setopt hist_reduce_blanks - -# Those options aren't wanted -unsetopt beep extendedglob notify - -# word select works like in bash -autoload -U select-word-style -select-word-style bash diff --git a/home/zsh/prompt.zsh b/home/zsh/prompt.zsh deleted file mode 100644 index 1c21d4b..0000000 --- a/home/zsh/prompt.zsh +++ /dev/null @@ -1,11 +0,0 @@ -setopt prompt_subst - -PROMPT='%K{cyan}%F{black}%m%k%f %~ %% ' - -# For tramp (emacs). -if [ "$TERM" = "dumb" ]; then - unset PROMPT - PS1='$ ' - unsetopt zle -fi - diff --git a/home/zsh/sway.zsh b/home/zsh/sway.zsh deleted file mode 100644 index 69d5103..0000000 --- a/home/zsh/sway.zsh +++ /dev/null @@ -1,3 +0,0 @@ -if [ -z $DISPLAY ] && [ "$(tty)" = "/dev/tty1" ]; then - exec sway -fi diff --git a/home/zsh/tmux.zsh b/home/zsh/tmux.zsh deleted file mode 100644 index 97944f5..0000000 --- a/home/zsh/tmux.zsh +++ /dev/null @@ -1,9 +0,0 @@ -# If we're not in an ssh connection, and tmux is installed, and we're -# not already in a tmux session, attach to the session named -# 'default', and if the session does not exist, start one named -# 'default' -if [ -z "$SSH_CONNECTION" ]; then - if command -v tmux &> /dev/null && [ -z "$TMUX" ]; then - tmux attach -t default || tmux new -s default - fi -fi diff --git a/hosts/aptos/home.nix b/hosts/aptos/home.nix index e317788..106497d 100644 --- a/hosts/aptos/home.nix +++ b/hosts/aptos/home.nix @@ -23,11 +23,11 @@ # terminal direnv.enable = true; gcloud.enable = true; + shell.name = "zsh"; ssh.enable = true; terminal.program = "alacritty"; tmux.enable = true; xdg.enable = true; - zsh.enable = true; # software development emacs.enable = true; diff --git a/hosts/carmel/home.nix b/hosts/carmel/home.nix index e317788..106497d 100644 --- a/hosts/carmel/home.nix +++ b/hosts/carmel/home.nix @@ -23,11 +23,11 @@ # terminal direnv.enable = true; gcloud.enable = true; + shell.name = "zsh"; ssh.enable = true; terminal.program = "alacritty"; tmux.enable = true; xdg.enable = true; - zsh.enable = true; # software development emacs.enable = true; diff --git a/hosts/tahoe/home.nix b/hosts/tahoe/home.nix index dc9c0aa..c58d0d4 100644 --- a/hosts/tahoe/home.nix +++ b/hosts/tahoe/home.nix @@ -6,9 +6,9 @@ packages.enable = true; # terminal - tmux.enable = true; + shell.name = "zsh"; ssh.enable = true; - zsh.enable = true; + tmux.enable = true; # software development git.enable = true; -- cgit 1.4.1