diff options
author | pacien | 2022-11-14 00:39:47 +0100 |
---|---|---|
committer | pacien | 2022-11-14 00:39:47 +0100 |
commit | e8459a4a34f2611a547b966b2c58867d99d0f40c (patch) | |
tree | a97b7d60808e210df1743d00d10b393c21484a59 /lib/mk-dev-shell.nix | |
parent | 67ca8e5e889d289f9758f303b67dbcaf99fcd742 (diff) | |
download | flaky-utils-e8459a4a34f2611a547b966b2c58867d99d0f40c.tar.gz |
mkDevShell: extract shell hooks
Diffstat (limited to 'lib/mk-dev-shell.nix')
-rw-r--r-- | lib/mk-dev-shell.nix | 47 |
1 files changed, 6 insertions, 41 deletions
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 @@ | |||
1 | flake: | ||
1 | { pkgs | 2 | { pkgs |
2 | , tools ? [] | 3 | , tools ? [] |
3 | , envVars ? {} | 4 | , envVars ? {} |
4 | , shell ? null | 5 | , shell ? null |
5 | }: | 6 | }: |
6 | 7 | ||
7 | with pkgs.lib; | ||
8 | |||
9 | let | 8 | let |
10 | mapAttrsToLines = mapping: attrs: | 9 | shellLib = flake.lib.shell { inherit pkgs; }; |
11 | pkgs.lib.concatStringsSep "\n" (pkgs.lib.mapAttrsToList mapping attrs); | ||
12 | |||
13 | exportEnvVar = k: v: '' | ||
14 | export ${escapeShellArg k}="${v}" | ||
15 | ''; | ||
16 | |||
17 | exportEnvVarsHook = mapAttrsToLines exportEnvVar; | ||
18 | |||
19 | fmt = rec { | ||
20 | codeBlock = code: text: ''"\e[${code}m"${text}"\e[0m"''; | ||
21 | keyword = codeBlock "1;36"; | ||
22 | section = codeBlock "4;35"; | ||
23 | printSectionTitle = title: ''echo -e "\n\n"${section title}"\n"''; | ||
24 | }; | ||
25 | |||
26 | printEnvVar = k: v: '' | ||
27 | echo -e ${fmt.keyword (escapeShellArg "$" + k)}: ${escapeShellArg v} | ||
28 | ''; | ||
29 | |||
30 | printEnvVarsHook = envVars: '' | ||
31 | ${fmt.printSectionTitle "DEVSHELL ENVIRONMENT VARIABLES"} | ||
32 | ${mapAttrsToLines printEnvVar envVars} | ||
33 | ''; | ||
34 | |||
35 | printToolsHook = tools: '' | ||
36 | ${fmt.printSectionTitle "DEVSHELL TOOLS"} | ||
37 | ls "${pkgs.symlinkJoin { name = "env"; paths = tools; }}/bin" | ||
38 | ''; | ||
39 | |||
40 | # Use the default user shell instead of Bash | ||
41 | startUserShellHook = '' | ||
42 | $(${pkgs.finger_bsd}/bin/finger $USER \ | ||
43 | | ${pkgs.gnugrep}/bin/grep -oP 'Shell: \K.*') | ||
44 | ''; | ||
45 | 10 | ||
46 | in pkgs.mkShell { | 11 | in pkgs.mkShell { |
47 | packages = tools; | 12 | packages = tools; |
48 | shellHook = '' | 13 | shellHook = '' |
49 | ${exportEnvVarsHook envVars} | 14 | ${shellLib.exportEnvVars envVars} |
50 | 15 | ||
51 | ${optionalString (envVars != {}) (printEnvVarsHook envVars)} | 16 | ${pkgs.lib.optionalString (envVars != {}) (shellLib.printEnvVars envVars)} |
52 | ${optionalString (tools != []) (printToolsHook tools)} | 17 | ${pkgs.lib.optionalString (tools != []) (shellLib.printBins tools)} |
53 | echo | 18 | echo |
54 | 19 | ||
55 | ${if (shell != null) then shell else startUserShellHook} | 20 | ${if (shell != null) then shell else shellLib.startUserShell} |
56 | exit $? | 21 | exit $? |
57 | ''; | 22 | ''; |
58 | } | 23 | } |