summaryrefslogtreecommitdiffstats
path: root/modules/by-name/fi/fish/module.nix
blob: bb11d3467f8e999ce62b70fd8bd8ad902bc402c6 (plain) (blame)
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
# nixos-config - My current NixOS configuration
#
# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of my nixos-config.
#
# You should have received a copy of the License along with this program.
# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
{
  config,
  lib,
  pkgs,
  ...
}: let
  cfg = config.soispha.programs.fish;

  sourceFile = path: "source ${path}\n";

  extraFiles = builtins.concatStringsSep "\n" (
    builtins.map (value:
      if builtins.isPath value
      then (sourceFile value)
      else value) (
      builtins.attrValues cfg.integrations
    )
  );

  keymappings_raw = import ./keymappings.nix {inherit lib;};
  mkBinding = mode: base_key: binding: let
    sets_mode_to =
      if binding ? sets_mode_to
      then "--sets-mode ${binding.sets_mode_to} "
      else "";
  in
    if binding ? cmd
    then let
      mkCmd = cmd: "'${cmd}'";
      escaped_key =
        if base_key == ""
        then ''""''
        else
          lib.escape [
            "\""
            "("
            ")"
            "$"
            ";"
            "*"
            "&"
            "|"
            "~"
            "<"
            ">"
            "^"
          ]
          base_key;
      escaped_cmd =
        if builtins.isList binding.cmd
        then builtins.concatStringsSep " " (builtins.map mkCmd binding.cmd)
        else mkCmd binding.cmd;
    in ''
      bind --mode "${mode}" ${sets_mode_to}--preset ${escaped_key} ${escaped_cmd}
    ''
    else
      builtins.concatStringsSep "" (lib.mapAttrsToList
        (new_key: value: mkBinding mode "${base_key + "," + new_key}" value)
        binding);
  mkMode = mode: attr: builtins.concatStringsSep "" (lib.mapAttrsToList (mkBinding mode) attr);
  keymappings =
    ''
      function fish_my_key_bindings
        fish_vi_key_bindings
        bind --erase --all
    ''
    + builtins.concatStringsSep "\n" (lib.mapAttrsToList mkMode keymappings_raw)
    + ''
      end
      set -g fish_key_bindings fish_my_key_bindings
    '';
in {
  options.soispha.programs.fish = {
    enable = lib.mkEnableOption "fish";

    integrations = lib.mkOption {
      type = lib.types.attrsOf (lib.types.either lib.types.path lib.types.str);
      example = ''
        {
          atuin = ./integrations/atuin.fish;
        }
      '';
      default = {};
    };
  };

  config = lib.mkIf cfg.enable {
    # Ensure that the default shell of the user is actually enabled.
    programs.fish = {
      enable = true;
      package = pkgs.fish-patched;
    };

    users.users.soispha.shell = config.programs.fish.package;

    home-manager.users.soispha = {
      home.sessionPath = lib.mkForce [];

      programs.fish = {
        enable = true;

        package = config.programs.fish.package;

        interactiveShellInit = let
          start = lib.modules.mkBefore (
            sourceFile ./config/01_show-task.fish
          );
          end = lib.modules.mkAfter keymappings;
        in
          lib.modules.mkMerge
          [
            start

            extraFiles

            end
          ];
      };
    };
  };
}