From 9c7be9b75152b77df94005ace9fc6deb044ae67f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Tue, 22 Mar 2022 19:33:01 +0100 Subject: [PATCH] :rocket: add custom shell option --- modules/cli/default.nix | 51 ++++++++++++++++---- modules/cli/fish.nix | 90 ----------------------------------- modules/cli/shell/default.nix | 28 +++++++++++ modules/cli/shell/fish.nix | 68 ++++++++++++++++++++++++++ modules/cli/shell/zsh.nix | 35 ++++++++++++++ modules/cli/zsh.nix | 57 ---------------------- 6 files changed, 172 insertions(+), 157 deletions(-) delete mode 100644 modules/cli/fish.nix create mode 100644 modules/cli/shell/default.nix create mode 100644 modules/cli/shell/fish.nix create mode 100644 modules/cli/shell/zsh.nix delete mode 100644 modules/cli/zsh.nix diff --git a/modules/cli/default.nix b/modules/cli/default.nix index 2e0320b..3c21cf1 100644 --- a/modules/cli/default.nix +++ b/modules/cli/default.nix @@ -1,16 +1,47 @@ { config, lib, pkgs, ... }: { - imports = [ - ./adb.nix - ./bin - ./direnv.nix - ./fish.nix - ./git.nix - ./nix.nix - ./ssh.nix - # ./zsh.nix - ]; + imports = + [ ./adb.nix ./bin ./direnv.nix ./git.nix ./nix.nix ./ssh.nix ./shell ]; + modules.cli.shell = let + # HACK to fix 24bit color support with kitty + editor = "TERM=kitty-direct emacsclient -t -a 'emacs -t'"; + in { + name = "fish"; + abbreviations = { + us = "systemctl --user"; + rs = "doas systemctl"; + + ga = "git add"; + gb = "git branch"; + gc = "git commit"; + gco = "git checkout"; + gd = "git diff"; + gds = "git diff --staged"; + gp = "git push"; + gs = "git status"; + }; + aliases = { + du = "dust"; + ls = "exa -lh --icons --git"; + cat = "bat"; + feh = "feh --auto-zoom --scale-down"; + grep = "rg"; + rm = "rm -i"; + mv = "mv -i"; + + # HACK to fix kitty not being recongized + ssh = "TERM=xterm-color command ssh"; + + nix-switch = "doas nixos-rebuild switch --flake ~/.dotfiles"; + nix-boot = "doas nixos-rebuild boot --flake ~/.dotfiles"; + nix-lock = + "doas nixos-rebuild dry-activate --flake ~/.dotfiles --recreate-lock-file"; + + emacs = editor; + }; + variables = { EDITOR = editor; }; + }; home-manager.users.moritz = { home.packages = with pkgs; [ # archives diff --git a/modules/cli/fish.nix b/modules/cli/fish.nix deleted file mode 100644 index 04966e7..0000000 --- a/modules/cli/fish.nix +++ /dev/null @@ -1,90 +0,0 @@ -{ config, lib, pkgs, inputs, ... }: - -{ - users.users.moritz.shell = pkgs.fish; - # needed for nix completions - programs.fish.enable = true; - home-manager.users.moritz = { - programs = { - fish = let - # HACK to fix 24bit color support with kitty - editor = "TERM=kitty-direct emacsclient -t -a 'emacs -t'"; - in { - enable = true; - shellAbbrs = { - us = "systemctl --user"; - rs = "doas systemctl"; - - ga = "git add"; - gb = "git branch"; - gc = "git commit"; - gco = "git checkout"; - gd = "git diff"; - gds = "git diff --staged"; - gp = "git push"; - gs = "git status"; - }; - shellAliases = { - du = "dust"; - ls = "exa -lh --icons --git"; - cat = "bat"; - feh = "feh --auto-zoom --scale-down"; - grep = "rg"; - rm = "rm -i"; - mv = "mv -i"; - - # HACK to fix kitty not being recongized - ssh = "TERM=xterm-color command ssh"; - - nix-switch = "doas nixos-rebuild switch --flake ~/.dotfiles"; - nix-boot = "doas nixos-rebuild boot --flake ~/.dotfiles"; - nix-lock = - "doas nixos-rebuild dry-activate --flake ~/.dotfiles --recreate-lock-file"; - - emacs = editor; - }; - shellInit = '' - fzf_configure_bindings --git_log=\cg - - # Vi Mode - fish_vi_key_bindings - - # Emulates vim's cursor shape behavior - # Set the normal and visual mode cursors to a block - set fish_cursor_default block - # Set the insert mode cursor to a line - set fish_cursor_insert line - # Set the replace mode cursor to an underscore - set fish_cursor_replace_one underscore - # The following variable can be used to configure cursor shape in - # visual mode, but due to fish_cursor_default, is redundant here - set fish_cursor_visual block - - # Variables - set -x EDITOR ${editor} - ''; - functions = { - gi = '' - set url https://www.gitignore.io/api - if test (count $argv) -ne 1 - set list (curl -sL $url/list | string split ",") - set choice (string split " " $list | fzf -m --preview "curl -sL $url/{}" | string join ",") - else - set choice $argv[1] - end - curl -sL $url/$choice > .gitignore - ''; - fish_greeting = ""; - cheat = "cht.sh $argv | bat -p"; - }; - plugins = [{ - name = "dracula"; - src = inputs.dracula-fish; - }]; - }; - fzf.enableFishIntegration = true; - starship.enableFishIntegration = true; - }; - home.packages = with pkgs.fishPlugins; [ fzf-fish pisces ]; - }; -} diff --git a/modules/cli/shell/default.nix b/modules/cli/shell/default.nix new file mode 100644 index 0000000..fb82e05 --- /dev/null +++ b/modules/cli/shell/default.nix @@ -0,0 +1,28 @@ +{ config, lib, pkgs, ... }: + +with lib; +let cfg = config.module.cli.shell; +in { + options.modules.cli.shell = { + name = mkOption { + default = "fish"; + type = types.enum [ "fish" "zsh" ]; + example = "zsh"; + }; + abbreviations = mkOption { + default = { }; + type = with types; attrsOf str; + example = { gs = "git status"; }; + }; + aliases = mkOption { + default = { }; + type = with types; attrsOf (nullOr (either str path)); + }; + variables = mkOption { + default = { }; + type = with types; attrsOf str; + }; + }; + + imports = [ ./fish.nix ./zsh.nix ]; +} diff --git a/modules/cli/shell/fish.nix b/modules/cli/shell/fish.nix new file mode 100644 index 0000000..52a7c06 --- /dev/null +++ b/modules/cli/shell/fish.nix @@ -0,0 +1,68 @@ +{ config, lib, pkgs, inputs, ... }: + +let + cfg = config.modules.cli.shell; + exportedVariables = let + exportVariables = + lib.mapAttrsToList (n: v: ''set -x ${n} "${v}"'') cfg.variables; + in lib.concatStringsSep "\n" exportVariables; +in { + config = lib.mkIf (cfg.name == "fish") { + # set as default shell + users.users.moritz.shell = pkgs.fish; + # needed for nix completions + programs.fish.enable = true; + home-manager.users.moritz = { + programs = { + fish = { + enable = true; + shellAbbrs = cfg.abbreviations; + + shellAliases = cfg.aliases; + + shellInit = '' + fzf_configure_bindings --git_log=\cg + + # Vi Mode + fish_vi_key_bindings + + # Emulates vim's cursor shape behavior + # Set the normal and visual mode cursors to a block + set fish_cursor_default block + # Set the insert mode cursor to a line + set fish_cursor_insert line + # Set the replace mode cursor to an underscore + set fish_cursor_replace_one underscore + # The following variable can be used to configure cursor shape in + # visual mode, but due to fish_cursor_default, is redundant here + set fish_cursor_visual block + + # Variables + ${exportedVariables} + ''; + functions = { + gi = '' + set url https://www.gitignore.io/api + if test (count $argv) -ne 1 + set list (curl -sL $url/list | string split ",") + set choice (string split " " $list | fzf -m --preview "curl -sL $url/{}" | string join ",") + else + set choice $argv[1] + end + curl -sL $url/$choice > .gitignore + ''; + fish_greeting = ""; + cheat = "cht.sh $argv | bat -p"; + }; + plugins = [{ + name = "dracula"; + src = inputs.dracula-fish; + }]; + }; + fzf.enableFishIntegration = true; + starship.enableFishIntegration = true; + }; + home.packages = with pkgs.fishPlugins; [ fzf-fish pisces ]; + }; + }; +} diff --git a/modules/cli/shell/zsh.nix b/modules/cli/shell/zsh.nix new file mode 100644 index 0000000..0c5d305 --- /dev/null +++ b/modules/cli/shell/zsh.nix @@ -0,0 +1,35 @@ +{ config, lib, pkgs, inputs, ... }: + +let cfg = config.modules.cli.shell; +in { + config = lib.mkIf (cfg.name == "zsh") { + environment.pathsToLink = [ "/share/zsh" ]; + users.users.moritz.shell = pkgs.zsh; + home-manager.users.moritz = { + home.packages = with pkgs; [ du-dust ]; + programs = { + zsh = { + enable = true; + dotDir = ".config/zsh"; + history.expireDuplicatesFirst = true; + localVariables = cfg.variables; + shellAliases = lib.trivial.mergeAttrs cfg.aliases cfg.abbreviations; + enableSyntaxHighlighting = true; + enableAutosuggestions = true; + enableCompletion = true; + initExtra = '' + function nix-which() { + readlink -f $(which $1) + } + ''; + plugins = [{ + name = "forgit"; + src = inputs.forgit-git; + }]; + }; + fzf.enableZshIntegration = true; + starship.enableZshIntegration = true; + }; + }; + }; +} diff --git a/modules/cli/zsh.nix b/modules/cli/zsh.nix deleted file mode 100644 index a3a5bf8..0000000 --- a/modules/cli/zsh.nix +++ /dev/null @@ -1,57 +0,0 @@ -{ config, lib, pkgs, inputs, ... }: - -{ - environment.pathsToLink = [ "/share/zsh" ]; - users.users.moritz.shell = pkgs.zsh; - home-manager.users.moritz = { - home.packages = with pkgs; [ du-dust ]; - programs = { - zsh = { - enable = true; - dotDir = ".config/zsh"; - history.expireDuplicatesFirst = true; - localVariables = { EDITOR = "emacsclient -nw"; }; - shellAliases = { - du = "dust"; - ls = "exa -lh --icons --git"; - cat = "bat"; - feh = "feh --auto-zoom --scale-down"; - grep = "rg"; - - us = "systemctl --user"; - rs = "doas systemctl"; - - ga = "git add"; - gb = "git branch"; - gc = "git commit"; - gco = "git checkout"; - gd = "git diff"; - gds = "git diff --staged"; - gp = "git push"; - gs = "git status"; - - ssh = "TERM=xterm-color ssh"; - - nix-switch = "doas nixos-rebuild switch --flake ~/.dotfiles"; - nix-boot = "doas nixos-rebuild boot --flake ~/.dotfiles"; - nix-lock = - "doas nixos-rebuild dry-activate --flake ~/.dotfiles --recreate-lock-file"; - }; - enableSyntaxHighlighting = true; - enableAutosuggestions = true; - enableCompletion = true; - initExtra = '' - function nix-which() { - readlink -f $(which $1) - } - ''; - plugins = [{ - name = "forgit"; - src = inputs.forgit-git; - }]; - }; - fzfe.enableZshIntegration = true; - starship.enableZshIntegration = true; - }; - }; -}