From e8459a4a34f2611a547b966b2c58867d99d0f40c Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 14 Nov 2022 00:39:47 +0100 Subject: mkDevShell: extract shell hooks --- lib/shell.nix | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/shell.nix (limited to 'lib/shell.nix') diff --git a/lib/shell.nix b/lib/shell.nix new file mode 100644 index 0000000..3f6defc --- /dev/null +++ b/lib/shell.nix @@ -0,0 +1,44 @@ +flake: +{ pkgs +}: + +with pkgs.lib; + +let + mapAttrsToLines = mapping: attrs: + pkgs.lib.concatStringsSep "\n" (pkgs.lib.mapAttrsToList mapping attrs); + + fmt = rec { + codeBlock = code: text: ''"\e[${code}m"${text}"\e[0m"''; + keyword = codeBlock "1;36"; + section = codeBlock "4;35"; + printSectionTitle = title: ''echo -e "\n\n"${section title}"\n"''; + }; + +in rec { + exportEnvVar = k: v: '' + export ${escapeShellArg k}="${v}" + ''; + + exportEnvVars = mapAttrsToLines exportEnvVar; + + printEnvVar = k: v: '' + echo -e ${fmt.keyword (escapeShellArg "$" + k)}: ${escapeShellArg v} + ''; + + printEnvVars = envVars: '' + ${fmt.printSectionTitle "ENVIRONMENT VARIABLES"} + ${mapAttrsToLines printEnvVar envVars} + ''; + + printBins = packagePaths: '' + ${fmt.printSectionTitle "TOOLS"} + 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.*') + ''; +} -- cgit v1.2.3