aboutsummaryrefslogtreecommitdiff
path: root/lib/mk-dev-shell.nix
blob: c6b36b7cb6e27bad468a51228f5984ea397b41e8 (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
{ pkgs
, tools ? []
, envVars ? {}
, shell ? null
}:

with pkgs.lib;

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

  envVarsHook = envVars: ''
    echo -e "\nDEVSHELL ENVIRONMENT VARIABLES:"
    ${pkgs.lib.concatStringsSep "\n" (
      pkgs.lib.mapAttrsToList exportEnvVar envVars
    )}
  '';

  listToolsHook = tools: ''
    echo -e "\nDEVSHELL COMMANDS:"
    ls "${pkgs.symlinkJoin { name = "env"; paths = tools; }}/bin"
  '';

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

in pkgs.mkShell {
  buildInputs = tools;
  shellHook = ''
    ${optionalString (envVars != {}) (envVarsHook envVars)}
    ${optionalString (tools != []) (listToolsHook tools)}
    ${if (shell != null) then shell else startUserShellHook}
    exit $?
  '';
}