-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.nix
74 lines (67 loc) · 1.81 KB
/
lib.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
final: prev:
{
makePythonPth =
{ python, ... } @ pythonPackages: name: runtimeInputs:
final.runCommand "${name}-runtimeinputs"
{
passthru = {
pythonModule = python;
requiredPythonModules = pythonPackages.requiredPythonModules [ ];
};
}
''
d=$out/${python.sitePackages}
mkdir -p $d
printf '%s; ' \
'import os' \
'd=r"""${final.lib.makeBinPath runtimeInputs}"""' \
'os.environ["PATH"] += (os.pathsep+d) if d else ""' \
> $d/${name}-runtimeinputs.pth
'';
writePython3Application =
{ name
, text ? ""
, src ? ""
, libraries ? [ ]
, runtimeInputs ? [ ]
, pythonPackages ? final.python3Packages
, flakeIgnore ? [ ]
, flakeSelect ? [ "E9" "W2" ]
}:
let
guard = throw "exactly one of `text` or `src` must be given to writePython3Application.";
python = pythonPackages.python;
env =
if libraries != [ ] || runtimeInputs != [ ]
then python.withPackages (_: libraries ++ [ pth ])
else python;
pth = final.makePythonPth pythonPackages name runtimeInputs;
in
final.runCommand
name
{ src = if (src == "") == (text == "") then guard else src; }
''
mkdir -p $out/bin
py=$out/bin/${name}
echo '#!${env.interpreter}' > $py
chmod +x $py
if [[ -n "$src" ]]; then
cat $src >> $py
else
echo ${final.lib.escapeShellArgs [text]} >> $py
fi
'';
git-am-shim = final.writeShellScript "git-am-shim"
''
if [[ "$1" != am ]]; then
exit
fi
echo "$(basename $0)" "$@"
set -e
for f in "$@"; do
if [[ "$f" == *.patch ]]; then
( set -x ; cat $f | patch -p1 )
fi
done
'';
}