aboutsummaryrefslogtreecommitdiff
path: root/lib/shell.nix
blob: a5b321da1cccef435cce85cbe3e9d5e1ad64ec58 (plain)
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
flake:
{ pkgs
}:

with pkgs.lib;

rec {
  mapAttrsToLines = mapping: attrs:
    concatStringsSep "\n" (mapAttrsToList mapping attrs);

  ifSomeAttrs = attrs: f: pkgs.lib.optionalString (attrs != {}) (f attrs);
  ifSomeList = list: f: pkgs.lib.optionalString (list != []) (f list);

  fmt = rec {
    codeBlock = code: text: ''"\e[${code}m"${escapeShellArg text}"\e[0m"'';
    keyword = codeBlock "1;36";
    section = codeBlock "4;35";
    printSectionTitle = title: ''echo -e "\n"${section title}'';
  };

  exportEnvVar = k: v: ''
    export ${escapeShellArg k}="${v}"
  '';

  exportEnvVars = mapAttrsToLines exportEnvVar;

  printEnvVar = k: v: ''
    echo -e ${fmt.keyword ("$" + k)}: ${escapeShellArg v}
  '';

  printEnvVars = envVars: ''
    ${fmt.printSectionTitle "ENVIRONMENT VARIABLES"}
    ${mapAttrsToLines printEnvVar envVars}
  '';

  printBins = packagePaths: ''
    ${fmt.printSectionTitle "ENVIRONMENT PROGRAMS"}
    ls "${pkgs.symlinkJoin { name = "env"; paths = packagePaths; }}/bin"
  '';

  # Use the default user shell instead of Bash
  startUserShell = ''
    $(${pkgs.finger_bsd}/bin/finger $USER \
      | ${pkgs.gnugrep}/bin/grep -oP 'Shell: \K.*')
  '';
}