about summary refs log tree commit diff
path: root/modules/common/shell
diff options
context:
space:
mode:
Diffstat (limited to 'modules/common/shell')
-rw-r--r--modules/common/shell/default.nix208
-rw-r--r--modules/common/shell/functions.bash59
2 files changed, 267 insertions, 0 deletions
diff --git a/modules/common/shell/default.nix b/modules/common/shell/default.nix
new file mode 100644
index 0000000..437ce57
--- /dev/null
+++ b/modules/common/shell/default.nix
@@ -0,0 +1,208 @@
+{
+  config,
+  inputs,
+  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 = {
+      imports = [ inputs.nix-index-database.hmModules.nix-index ];
+
+      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/a1f8b505e691737db2f7f2b96275802c45f65c59/advcpmv-0.9-9.4.patch";
+                                  hash = "sha256-4fdqpkENPfra4nFQU4+xNrlfq6Dw/2JIZXUOMmdMtcM=";
+                                })
+                              ];
+                            }
+                          ))
+                        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;
+        nix-index-database.comma.enable = true;
+      };
+
+      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
+      ];
+    };
+  };
+}
diff --git a/modules/common/shell/functions.bash b/modules/common/shell/functions.bash
new file mode 100644
index 0000000..f354adb
--- /dev/null
+++ b/modules/common/shell/functions.bash
@@ -0,0 +1,59 @@
+function where() {
+  local s
+  s="$(type -P "$1")"
+  realpath "$s"
+}
+complete -F _command where
+
+function what() {
+  local s
+  s="$(where "$1")"
+  printf "%s\n" "${s%/*/*}"
+}
+complete -F _command what
+
+function cat() {
+  if (($# == 1)) && [[ -d $1 ]]; then
+    ll "$1"
+  else
+    command cat "$@"
+  fi
+}
+
+function cd() {
+  builtin cd "$@" &&
+    if ((${#FUNCNAME[@]} == 1)); then
+      ls
+    fi
+}
+
+function mkcd() {
+  mkdir -p "$1" && builtin cd "$1"
+}
+
+function mvcd() {
+  mv -i -- "$PWD" "$1" && builtin cd .
+}
+
+function bak() {
+  local f
+  for f; do
+    cp -ai -- "$f" "$f.bak"
+  done
+}
+
+function ubak() {
+  local f
+  for f; do
+    [[ $f == *.bak ]] || f="$f.bak"
+    mv -i -- "$f" "${f%.bak}"
+  done
+}
+
+function dec2hex() {
+  printf "0x%X\n" "$1"
+}
+
+function hex2dec() {
+  printf "%d\n" "0x$1"
+}

Consider giving Nix/NixOS a try! <3