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 ++++++----------------------------------------- lib/mk-sandbox-system.nix | 1 + lib/shell.nix | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 lib/shell.nix (limited to 'lib') 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 $? ''; } diff --git a/lib/mk-sandbox-system.nix b/lib/mk-sandbox-system.nix index b2d85e2..a56bdab 100644 --- a/lib/mk-sandbox-system.nix +++ b/lib/mk-sandbox-system.nix @@ -1,3 +1,4 @@ +flake: { pkgs , name ? "sandbox" , user ? "dummy" 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