about summary refs log tree commit diff
path: root/modules/common/shell/default.nix
blob: 4ae41d832f7d46302da9c7e3152782032ec3a67b (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{
  config,
  lib,
  pkgs,
  this,
  ...
}:
with lib;
let
  cfg = config.nixfiles.modules.common.shell;
in
{
  options.nixfiles.modules.common.shell.aliases = mkOption {
    description = "An attribute set of shell aliases.";
    type = with types; attrsOf str;
    default = { };
  };

  config = {
    hm = {
      programs = {
        bash = {
          enable = true;

          initExtra =
            let
              aliasCompletions = concatStringsSep "\n" (
                mapAttrsToList (name: _: "complete -F _complete_alias ${name}") cfg.aliases
              );
            in
            ''
              # Apropriated from the default NixOS prompt settings.
              if [ "$TERM" != "dumb" ] || [ -n "$INSIDE_EMACS" ]; then
                  PROMPT_COLOR="1;31m"
                  ((UID)) && PROMPT_COLOR="1;32m"
                  if [ -n "$INSIDE_EMACS" ] || [ "$TERM" = "eterm" ] || [ "$TERM" = "eterm-color" ]; then
                      PS1="\n\[\033[$PROMPT_COLOR\][\u@\h:\w]\\$\[\033[0m\] "
                  else
                      PS1="\n\[\033[$PROMPT_COLOR\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\\$\[\033[0m\] "
                  fi
                  if test "$TERM" = "xterm"; then
                      PS1="\[\033]2;\h:\u:\w\007\]$PS1"
                  fi
              fi

              source "${./functions.bash}"

              source "${getExe' pkgs.complete-alias "complete_alias"}"
              ${aliasCompletions}

              # https://github.com/garabik/grc?tab=readme-ov-file#bash
              GRC_ALIASES=true
              source ${pkgs.grc}/etc/profile.d/grc.sh
            '';

          shellOptions = [
            "autocd"
            "cdspell"
            "checkjobs"
            "checkwinsize"
            "dirspell"
            "extglob"
            "globstar"
            "histappend"
            "histreedit"
            "histverify"
          ];

          historyControl = [
            "erasedups"
            "ignoredups"
            "ignorespace"
          ];

          shellAliases =
            listToAttrs (
              map
                (
                  { name, value }:
                  nameValuePair name (
                    with pkgs;
                    let
                      pkg =
                        if this.isHeadful then
                          (pkgs.coreutils.overrideAttrs (
                            _: super: {
                              patches = (super.patches or [ ]) ++ [
                                (fetchpatch {
                                  url = "https://raw.githubusercontent.com/jarun/advcpmv/1e2b1c6b74fa0974896bf94604279a3f74b37a63/advcpmv-0.9-9.5.patch";
                                  hash = "sha256-LRfb4heZlAUKiXl/hC/HgoqeGMxCt8ruBYZUrbzSH+Y=";
                                })
                              ];
                            }
                          ))
                        else
                          coreutils;
                    in
                    "${getExe' pkg "coreutils"} --coreutils-prog=${value}"
                  )
                )
                (
                  let
                    mkAlias =
                      {
                        name ? head command,
                        command,
                      }:
                      {
                        inherit name;
                        value = concatStringsSep " " command;
                      };

                    progressBar = optionalString this.isHeadful "--progress-bar";
                  in
                  [
                    (mkAlias {
                      command = [
                        "cp"
                        "--interactive"
                        "--recursive"
                        progressBar
                      ];
                    })
                    (mkAlias {
                      command = [
                        "mv"
                        "--interactive"
                        progressBar
                      ];
                    })
                    (mkAlias {
                      command = [
                        "rm"
                        "--interactive=once"
                      ];
                    })
                    (mkAlias {
                      command = [
                        "ln"
                        "--interactive"
                      ];
                    })
                    (mkAlias {
                      command = [
                        "mkdir"
                        "--parents"
                      ];
                    })
                    (mkAlias {
                      command = [
                        "rmdir"
                        "--parents"
                      ];
                    })
                    (mkAlias {
                      name = "lower";
                      command = [
                        "tr"
                        "'[:upper:]'"
                        "'[:lower:]'"
                      ];
                    })
                    (mkAlias {
                      name = "upper";
                      command = [
                        "tr"
                        "'[:lower:]'"
                        "'[:upper:]'"
                      ];
                    })
                  ]
                )
            )
            // (genAttrs [
              "grep"
              "egrep"
              "fgrep"
            ] (name: "${pkgs.gnugrep}/bin/${name} --color=always"))
            // cfg.aliases;
        };

        dircolors.enable = true;

        command-not-found.enable = false;
      };

      home.packages = with pkgs; [ grc ];
    };

    programs.command-not-found.enable = false;

    environment = {
      etc."grc.conf".source = "${pkgs.grc}/etc/grc.conf";

      systemPackages = with pkgs; [
        bc
        gawk
        hr
        moreutils
        pv
      ];
    };
  };
}

Consider giving Nix/NixOS a try! <3