dotfiles/modules/programs/fish.nix

121 lines
4.0 KiB
Nix
Raw Normal View History

2022-07-15 13:11:54 +02:00
{ config
, lib
, pkgs
, ...
}:
2022-03-22 19:33:01 +01:00
2022-07-15 13:11:54 +02:00
with lib;
2022-03-22 19:33:01 +01:00
let
2022-07-15 13:11:54 +02:00
cfg = config.my.programs.fish;
shellConfig = config.my.shell;
2023-09-10 15:07:55 +02:00
exportedVariables =
let
exportVariables =
lib.mapAttrsToList (n: v: ''set -x ${n} "${v}"'') shellConfig.variables;
in
lib.concatStringsSep "\n" exportVariables;
2022-07-15 13:11:54 +02:00
in
{
2022-10-15 20:00:09 +02:00
options.my.programs.fish.enable = mkEnableOption "fish";
2022-07-15 13:11:54 +02:00
config = lib.mkIf cfg.enable {
2022-03-22 19:33:01 +01:00
# set as default shell
users.users.moritz.shell = pkgs.fish;
2022-10-06 20:32:21 +02:00
environment.systemPackages = with pkgs.fishPlugins; [ fzf-fish pisces ];
2022-03-22 19:33:01 +01:00
# needed for nix completions
programs.fish.enable = true;
2022-07-15 13:11:54 +02:00
2022-03-22 19:33:01 +01:00
home-manager.users.moritz = {
programs = {
fish = {
enable = true;
2022-07-15 13:11:54 +02:00
shellAbbrs = shellConfig.abbreviations;
shellAliases = shellConfig.aliases;
2022-03-22 19:33:01 +01:00
shellInit = ''
# 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
2023-09-23 09:46:46 +02:00
# Complex abbreviations
function last_history_item
echo $history[1]
end
abbr -a !! --position anywhere --function last_history_item
function sed_history_item
echo $history[1] | perl -p -e (string trim -l --chars=! $argv)
end
abbr -a sed_history --position command --regex '^!s/.+/.*/g?' --function sed_history_item
function multicd
echo cd (string repeat -n (math (string length -- $argv[1]) - 1) ../)
end
abbr --add dotdot --regex '^\.\.+$' --function multicd
abbr -a P --position anywhere --set-cursor "% | $PAGER"
function editor_edit
echo $EDITOR $argv
end
abbr -a editor_edit_files --position command --regex ".+\.(txt|md|org)" --function editor_edit
function editor
echo $EDITOR
end
abbr -a e --function editor
# Completions
complete -c c -kfa '(zoxide query -l | sed "s|$HOME|~|")'
${optionalString config.virtualisation.podman.dockerCompat /* fish */ "complete -c docker -w podman"}
2023-09-23 09:46:46 +02:00
complete -c nom -fn "not __fish_seen_subcommand_from build shell develop" -a "build shell develop"
complete -c nom -fn "__fish_seen_subcommand_from build" -w "nix build"
complete -c nom -fn "__fish_seen_subcommand_from shell" -w "nix shell"
complete -c nom -fn "__fish_seen_subcommand_from develop" -w "nix develop"
complete -c timers \
-n "__fish_seen_subcommand_from toggle" \
-fa '(timers --json l | ${getExe pkgs.jq} -r .[][].name)'
complete -c timers \
-n "__fish_seen_subcommand_from remove" \
-fa '(timers --json l | ${getExe pkgs.jq} -r .[][].name)'
2022-03-22 19:33:01 +01:00
# Variables
${exportedVariables}
'';
functions = {
fish_greeting = "";
cheat = "cht.sh $argv | bat -p";
2023-09-19 09:50:51 +02:00
sourceenv = ''
set -f envfile "$argv"
if not test -f "$envfile"
echo "Unable to load $envfile"
return 1
end
printf "exported"
while read line
if not string match -qr '^#|^$' "$line"
set item (string split -m 1 '=' $line)
printf " $item[1]"
set -gx "$item[1]" "$item[2]"
end
end < "$envfile"
printf "\n"
'';
2022-03-22 19:33:01 +01:00
};
};
};
};
};
}