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/mk-dev-shell.nix | 47 ++++++----------------------------------------- 1 file changed, 6 insertions(+), 41 deletions(-) (limited to 'lib/mk-dev-shell.nix') diff --git a/lib/mk-dev-shell.nix b/lib/mk-dev-shell.nix index 87b9dbb..4f46d69 100644 --- a/lib/mk-dev-shell.nix +++ b/lib/mk-dev-shell.nix @@ -1,58 +1,23 @@ +flake: { pkgs , tools ? [] , envVars ? {} , shell ? null }: -with pkgs.lib; - let - mapAttrsToLines = mapping: attrs: - pkgs.lib.concatStringsSep "\n" (pkgs.lib.mapAttrsToList mapping attrs); - - exportEnvVar = k: v: '' - export ${escapeShellArg k}="${v}" - ''; - - exportEnvVarsHook = mapAttrsToLines exportEnvVar; - - 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"''; - }; - - printEnvVar = k: v: '' - echo -e ${fmt.keyword (escapeShellArg "$" + k)}: ${escapeShellArg v} - ''; - - printEnvVarsHook = envVars: '' - ${fmt.printSectionTitle "DEVSHELL ENVIRONMENT VARIABLES"} - ${mapAttrsToLines printEnvVar envVars} - ''; - - printToolsHook = tools: '' - ${fmt.printSectionTitle "DEVSHELL TOOLS"} - 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.*') - ''; + shellLib = flake.lib.shell { inherit pkgs; }; in pkgs.mkShell { packages = tools; shellHook = '' - ${exportEnvVarsHook envVars} + ${shellLib.exportEnvVars envVars} - ${optionalString (envVars != {}) (printEnvVarsHook envVars)} - ${optionalString (tools != []) (printToolsHook tools)} + ${pkgs.lib.optionalString (envVars != {}) (shellLib.printEnvVars envVars)} + ${pkgs.lib.optionalString (tools != []) (shellLib.printBins tools)} echo - ${if (shell != null) then shell else startUserShellHook} + ${if (shell != null) then shell else shellLib.startUserShell} exit $? ''; } -- cgit v1.2.3