aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorpacien2022-11-14 00:39:47 +0100
committerpacien2022-11-14 00:39:47 +0100
commite8459a4a34f2611a547b966b2c58867d99d0f40c (patch)
treea97b7d60808e210df1743d00d10b393c21484a59 /lib
parent67ca8e5e889d289f9758f303b67dbcaf99fcd742 (diff)
downloadflaky-utils-e8459a4a34f2611a547b966b2c58867d99d0f40c.tar.gz
mkDevShell: extract shell hooks
Diffstat (limited to 'lib')
-rw-r--r--lib/mk-dev-shell.nix47
-rw-r--r--lib/mk-sandbox-system.nix1
-rw-r--r--lib/shell.nix44
3 files changed, 51 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 @@
1flake:
1{ pkgs 2{ pkgs
2, tools ? [] 3, tools ? []
3, envVars ? {} 4, envVars ? {}
4, shell ? null 5, shell ? null
5}: 6}:
6 7
7with pkgs.lib;
8
9let 8let
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
46in pkgs.mkShell { 11in 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}
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 @@
1flake:
1{ pkgs 2{ pkgs
2, name ? "sandbox" 3, name ? "sandbox"
3, user ? "dummy" 4, 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 @@
1flake:
2{ pkgs
3}:
4
5with pkgs.lib;
6
7let
8 mapAttrsToLines = mapping: attrs:
9 pkgs.lib.concatStringsSep "\n" (pkgs.lib.mapAttrsToList mapping attrs);
10
11 fmt = rec {
12 codeBlock = code: text: ''"\e[${code}m"${text}"\e[0m"'';
13 keyword = codeBlock "1;36";
14 section = codeBlock "4;35";
15 printSectionTitle = title: ''echo -e "\n\n"${section title}"\n"'';
16 };
17
18in rec {
19 exportEnvVar = k: v: ''
20 export ${escapeShellArg k}="${v}"
21 '';
22
23 exportEnvVars = mapAttrsToLines exportEnvVar;
24
25 printEnvVar = k: v: ''
26 echo -e ${fmt.keyword (escapeShellArg "$" + k)}: ${escapeShellArg v}
27 '';
28
29 printEnvVars = envVars: ''
30 ${fmt.printSectionTitle "ENVIRONMENT VARIABLES"}
31 ${mapAttrsToLines printEnvVar envVars}
32 '';
33
34 printBins = packagePaths: ''
35 ${fmt.printSectionTitle "TOOLS"}
36 ls "${pkgs.symlinkJoin { name = "env"; paths = packagePaths; }}/bin"
37 '';
38
39 # Use the default user shell instead of Bash
40 startUserShell = ''
41 $(${pkgs.finger_bsd}/bin/finger $USER \
42 | ${pkgs.gnugrep}/bin/grep -oP 'Shell: \K.*')
43 '';
44}