diff options
-rw-r--r-- | flake.nix | 5 | ||||
-rw-r--r-- | lib/mk-dev-shell.nix | 47 | ||||
-rw-r--r-- | lib/mk-sandbox-system.nix | 1 | ||||
-rw-r--r-- | lib/shell.nix | 44 |
4 files changed, 54 insertions, 43 deletions
@@ -3,8 +3,9 @@ | |||
3 | 3 | ||
4 | outputs = { self }: { | 4 | outputs = { self }: { |
5 | lib = { | 5 | lib = { |
6 | mkDevShell = import ./lib/mk-dev-shell.nix; | 6 | shell = import ./lib/shell.nix self; |
7 | mkSandboxSystem = import ./lib/mk-sandbox-system.nix; | 7 | mkDevShell = import ./lib/mk-dev-shell.nix self; |
8 | mkSandboxSystem = import ./lib/mk-sandbox-system.nix self; | ||
8 | }; | 9 | }; |
9 | }; | 10 | }; |
10 | } | 11 | } |
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 | } |
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 @@ | |||
1 | flake: | ||
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 @@ | |||
1 | flake: | ||
2 | { pkgs | ||
3 | }: | ||
4 | |||
5 | with pkgs.lib; | ||
6 | |||
7 | let | ||
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 | |||
18 | in 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 | } | ||