-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-commands.nix
67 lines (61 loc) · 2.28 KB
/
shell-commands.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{ lib, symlinkJoin, writeShellScriptBin }:
name:
{ build ? { script = ''eval "$buildPhase"''; description = "Run the build."; }
, check ? { script = ''eval "$checkPhase"''; description = "Run the checks/test."; }
, run ? { script = "echo '🏃 \"run\" not supported'"; show = false; }
, format ? { script = "echo '📃 \"format\" not supported'"; show = false; }
, ...
}@commands:
let
shellCommandHelpText = descriptions: ''
esc=$(printf '\e[')
${builtins.concatStringsSep "\n" (lib.mapAttrsToList (name: desc:
''echo " ''${esc}32m${name}''${esc}0m ''${esc}33m${desc.args}''${esc}0m" ${if desc.description != "" then '';echo -e " ${builtins.replaceStrings ["\n"] ["\n "] desc.description}"'' else ""}'')
(descriptions // {
shellHelp = {
description = "Print this help text";
args = "";
};
}))}
'';
# need to wrangle a little bit to get the descriptions for the shell message
allCommands = commands // { inherit build check run format; };
inner = cmds: symlinkJoin {
name = "${name}-shell-commands";
paths = (lib.mapAttrsToList
(command: script: writeShellScriptBin command ''
envDir=$(mktemp -d -t shell-command-env.XXXXXX)
export 2>/dev/null >| "$envDir"/shell-env
# source setup to get functions like genericBuild for example
# as users might expect this to exist
NIX_BUILD_TOP=$envDir source $stdenv/setup > /dev/null
# reset the shell env
source "$envDir"/shell-env
# need to set this to be able to have local inputs
export NIX_ENFORCE_PURITY=0
rm -rf "$envDir"
set -euo pipefail
${if builtins.isAttrs script then script.script or "" else script}
'')
cmds) ++ [
(writeShellScriptBin "shellHelp" ''
${shellCommandHelpText (builtins.mapAttrs
(_: value:
{
description = if builtins.isAttrs value then value.description or "" else "";
args = if builtins.isAttrs value then value.args or "" else "";
}
)
(lib.filterAttrs (_: value:
if builtins.isAttrs value then
value.show or true
else
true)
cmds)
)
}
'')
];
};
in
lib.makeOverridable inner allCommands