-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.nix
More file actions
96 lines (86 loc) · 2.38 KB
/
debug.nix
File metadata and controls
96 lines (86 loc) · 2.38 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
{ pkgs }: let
inherit (builtins) trace toString toJSON;
inherit (pkgs.lib.lists) head last isList length;
inherit (pkgs.lib.trivial) runCommand warn id;
inherit (pkgs.lib.strings) isString stringLength;
inherit (pkgs.lib.attrsets) mapAttrs' nameValuePair filterAttrs getAttrFromPath attrNames isAttrs;
inherit (import ./attrsets.nix { inherit pkgs; }) mapAttrVals without size;
fns = rec {
logFn = {
description = ''
Log a value after being passed through a function and yield its
original value.
'';
__functor = _: f: x: trace (f x) x;
short = "f";
};
log = {
description = ''
Log a value to stdout and yield it.
'';
__functor = _: logFn id;
short = "d";
};
logHead = {
description = ''
Log the head of a list and yield the list.
'';
__functor = _: logFn head;
short = "h";
};
logLast = {
description = ''
Log the last of a list and yield the list.
'';
__functor = _: logFn last;
short = "l";
};
logLength = {
description = ''
Log the length of a string, list, or attribute set and yield the input.
'';
__functor = x: let
f = if isList x then
length
else if isAttrs x then
size
else if isString x then
stringLength
else warn "logLength input is not a list, attrset, or string.";
in logFn f x;
short = "_";
};
logJSON = {
description = ''
Log the JSON representation of a value.
'';
__functor = _: logFn toJSON;
short = "j";
};
logAttr = {
description = ''
Log the value of an attribute and yield the attrset.
'';
__functor = _: a: s: trace s.${a} s;
short = "a";
};
logAttrPath = {
description = ''
Log the value of an attribute path and yield the attrset.
'';
__functor = _: as: s: trace (getAttrFromPath as s) s;
short = "p";
};
logNames = {
description = ''
Log the attribute set names and yield the attrset.
'';
__functor = _: logFn attrNames;
short = "n";
};
};
in {
_doc = ''
Helper functions related to debugging.
'';
} // mapAttrVals (without "short") fns // { short = mapAttrs' (n: v: nameValuePair v.short fns.${n}) (filterAttrs (_: v: v?short) fns); }