-
Notifications
You must be signed in to change notification settings - Fork 3
/
hax.nix
234 lines (215 loc) · 6.79 KB
/
hax.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# This overlay provides the `hax` library, which contains useful functions and other packages and configurations.
final: prev:
(x: { hax = x; }) (
with prev;
with lib;
lib // rec {
inherit (stdenv) isLinux isDarwin isAarch64;
inherit (pkgs) fetchFromGitHub;
isM1 = isDarwin && isAarch64;
isX86Mac = isDarwin && !isAarch64;
isArmLinux = isAarch64 && isLinux;
isNixDarwin = builtins.getEnv "NIXDARWIN_CONFIG" != "";
isWSL = builtins.pathExists "/proc/sys/fs/binfmt_misc/WSLInterop";
isNixOS = builtins.pathExists "/etc/NIXOS";
isNixOSWSL = isWSL && isNixOS;
nvidiaLdPath = if isWSL then "/usr/lib/wsl/lib" else if isNixOS then "/run/opengl-driver/lib" else "";
attrIf = check: name: if check then name else null;
# attrIf helpers
ifIsLinux = attrIf isLinux;
ifIsArmLinux = attrIf isArmLinux;
ifIsUbuntu = attrIf isUbuntu;
ifIsNixDarwin = attrIf isNixDarwin;
ifIsDarwin = attrIf isDarwin;
ifIsM1 = attrIf isM1;
chief_keef = flake.inputs.kwb.packages.${pkgs.system};
pythonPackageOverlay =
overlay: attr: self: super:
let
pyOverlay = a: {
${a} = self.lib.fix (py:
super.${a}.override (old: {
self = py;
packageOverrides = self.lib.composeExtensions
(old.packageOverrides or (_: _: { }))
overlay;
}));
};
in
if builtins.isList attr then
(builtins.zipAttrsWith (_: builtins.head) (map pyOverlay attr)) else pyOverlay attr;
ssh = rec {
github = ''
Host github.com
User git
Hostname github.com
PreferredAuthentications publickey
'';
mac_meme = ''
XAuthLocation /opt/X11/bin/xauth
'';
config = ''
Include config.d/*
Host *
User jacobi
PasswordAuthentication no
Compression yes
IdentitiesOnly yes
# secure stuff
KexAlgorithms curve25519-sha256,[email protected],diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256
HostKeyAlgorithms ssh-ed25519,[email protected],[email protected],[email protected],rsa-sha2-256,[email protected],rsa-sha2-512,[email protected]
${optionalString isDarwin mac_meme}
'';
};
comma = (pog {
name = ",";
description = "a quick and easy way to run software that you don't have!";
flags = [
{
name = "package";
description = "a specific package to use for this binary";
}
{
name = "unset";
description = "unset the saved package selection for this invocation";
bool = true;
}
{
name = "overlay";
description = "an additional overlay to include in comma";
}
{
name = "description";
description = "print out the description of the package";
bool = true;
}
];
arguments = [
{ name = "binary"; }
];
script = ''
exec ${chief_keef.better-comma}/bin/, \
''${overlay:+--overlay $overlay} \
''${unset:+-u} \
''${description:+-d} \
''${package:+-p $package} \
"$@"
'';
}).overrideAttrs (_: { name = "better-comma"; });
vanilla_comma = chief_keef.better-comma;
mapAttrValues = f: mapAttrs (_: f);
fakePlatform = x:
x.overrideAttrs (
attrs: {
meta = attrs.meta or { } // { platforms = lib.platforms.all; };
}
);
prefixIf = b: x: y: if b then x + y else y;
optList = conditional: list: if conditional then list else [ ];
mapLines = f: s:
concatMapStringsSep "\n" (l: if l != "" then f l else l)
(splitString "\n" s);
words = splitString " ";
alias = name: x:
writeShellScriptBin name
''exec ${if isDerivation x then exe x else x} "$@"'';
overridePackage = pkg:
let
path = head (splitString ":" pkg.meta.position);
in
final.callPackage path;
nix-direnv = prev.nix-direnv.override { inherit (prev) nix; };
excludeLines = f: text:
concatStringsSep "\n" (filter (x: !f x) (splitString "\n" text));
drvs = x:
if isDerivation x || isList x then
flatten x
else
flatten (mapAttrsToList (_: drvs) x);
writeBashBinChecked = name: text:
stdenv.mkDerivation {
inherit name text;
dontUnpack = true;
passAsFile = "text";
nativeBuildInputs = [ shellcheck ];
installPhase = ''
mkdir -p $out/bin
echo '#!/bin/bash' > $out/bin/${name}
cat $textPath >> $out/bin/${name}
chmod +x $out/bin/${name}
shellcheck $out/bin/${name}
'';
};
getJson = url: sha256:
let
text = fetchurl {
inherit url sha256;
};
in
fromJSON (readFile text);
drvsExcept = x: e:
with { excludeNames = concatMap attrNames (attrValues e); };
flatten (drvs (filterAttrsRecursive (n: _: !elem n excludeNames) x));
dmgOverride = name: pkg:
with rec {
src = sources."dmg-${name}";
msg = "${name}: src ${src.version} != pkg ${pkg.version}";
checkVersion = lib.assertMsg (pkg.version == src.version) msg;
};
if isDarwin then
assert checkVersion;
(mkDmgPackage name src) // {
originalPackage = pkg;
}
else
pkg;
qlScript = name: command:
(writeBashBinChecked name ''
${pkgs.up}/bin/up --unsafe-full-throttle -c '${command}'
'');
git-trim = writeBashBinChecked "git-trim" (readFile ../scripts/git-trim.sh);
docker_aliases = rec {
# docker
d = "docker";
da = "${d} ps -a";
daq = "${d} ps -aq";
di = "${d} images";
drma = "${d} stop $(${daq}) && ${d} rm -f $(${daq})";
};
kubernetes_aliases = {
# k8s
k = "kubectl";
kx = "kubectx";
ka = "kubectl get pods";
};
basePythonPackages = p: with p; [
# linting
# black
# common use case
gamble
httpx
# requests
cryptography
# text
# anybadge
# tabulate
beautifulsoup4
# data
numpy
pandas
# type annotations (from nixpkgs)
types-requests
types-tabulate
types-enum34
types-ipaddress
# my types (for nixpkgs)
boto3-stubs
botocore-stubs
]
++ (optList (!isM1) [ ])
++ (optList isLinux [ ])
;
}
)