-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
85 lines (70 loc) · 2.34 KB
/
flake.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
description = "nix-command-utils";
inputs = {
nixpkgs.url = "nixpkgs/nixos-23.11";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
cmd = description: script: { inherit description script; };
command = { name, script, description ? "<No description given>" }:
{
inherit name description;
package =
pkgs.writeScriptBin name ''
#!${pkgs.bash}/bin/bash
echo "⚙️ Running ${name}..."
${script}
'';
};
commands = defs:
let
names =
builtins.attrNames defs;
helper =
let
lengths = map builtins.stringLength names;
maxLen = builtins.foldl' (acc: x: if x > acc then x else acc) 0 lengths;
maxPad =
let
go = acc:
if builtins.stringLength acc >= maxLen
then acc
else go (" " + acc);
in
go "";
folder = acc: name:
let
nameLen = builtins.stringLength name;
padLen = maxLen - nameLen;
padding = builtins.substring 0 padLen maxPad;
in
acc + " && echo '${name} ${padding}| ${(builtins.getAttr name defs).description}'";
lines =
builtins.foldl' folder "echo ''" names;
in
pkgs.writeScriptBin "menu" ''
#!${pkgs.stdenv.shell}
${pkgs.figlet}/bin/figlet "Commands" | ${pkgs.lolcat}/bin/lolcat
${toString lines}
'';
mapper = name:
let
element =
builtins.getAttr name defs;
task = command {
inherit name;
description = element.description;
script = element.script;
};
in
task.package;
packages =
map mapper names;
in
[ helper ] ++ packages;
in
{ inherit cmd command commands; }
);
}