-
Notifications
You must be signed in to change notification settings - Fork 106
/
flake.nix
173 lines (158 loc) · 5.99 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
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
{
description = "Create highly reproducible python environments";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
inputs.pypi-deps-db = {
url = "github:DavHau/pypi-deps-db";
flake = false;
};
outputs = { self, nixpkgs, flake-utils, ... }@inp:
with nixpkgs.lib;
let
dataLastModified = toInt (readFile "${inp.pypi-deps-db}/UNIX_TIMESTAMP");
dataOutdated =
if inp.nixpkgs.sourceInfo ? lastModified
&& dataLastModified < inp.nixpkgs.sourceInfo.lastModified then
true
else
false;
usageGen = "usage: nix (build|shell) mach-nix#gen.(python|docker).package1.package2...";
in
(flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
mach-nix-default = import ./default.nix {
inherit pkgs dataOutdated;
pypiData = inp.pypi-deps-db;
};
in rec
{
devShell = import ./shell.nix {
inherit pkgs;
pypiData = "${inp.pypi-deps-db}";
};
packages = rec {
inherit (mach-nix-default) mach-nix;
sdist = pkgs.runCommand "mach-nix-sdist"
{ buildInputs = mach-nix-default.pythonDeps; }
''
mkdir src
cp -r ${./.}/* src
cd src
python setup.py sdist -d $out
'';
# fake package which contains functions inside passthru
gen = pkgs.stdenv.mkDerivation {
name = usageGen;
src = throw usageGen;
passthru = {
python = mach-nix-default.pythonWith;
docker = mach-nix-default.dockerImageWith;
inherit (mach-nix-default)
pythonWith
dockerImageWith;
};
};
};
defaultPackage = packages.mach-nix;
apps.mach-nix = flake-utils.lib.mkApp { drv = packages.mach-nix.mach-nix; };
apps.extract-reqs =
{
type = "app";
program = toString (pkgs.writeScript "extract.sh" ''
export SRC=$1
nix-build -o reqs -E 'let
pkgs = import <nixpkgs> {};
srcEnv = builtins.getEnv "SRC";
src = pkgs.copyPathToStore srcEnv;
srcTar = pkgs.runCommand "src.tar.gz" {} "mkdir src && cp -r ''${src}/* src/ && pwd && ls -la && tar -c src | gzip -1 > $out";
in (import ./lib/extractor {}).extract_from_src {
py="python3";
src = srcTar;
name = "mach-nix";
}'
cat reqs/*
rm reqs
'');
};
apps.tests-unit = {
type = "app";
program = toString (pkgs.writeScript "tests-unit" ''
export PATH="${pkgs.lib.makeBinPath (with pkgs; [
(import ./mach_nix/nix/python.nix {
inherit pkgs;
dev = true;
})
]
++ pkgs.lib.optional (stdenv.isLinux) busybox
# This is not equivalent to "busybox", but is close enough for
# everything to work. The only quirk here is borrowing a trick
# from nixpkgs to provide a "/bin/sh" that's identical to the
# one nixpkgs exists (coreutils doesn't bundle /bin/sh but
# busybox does).
++ pkgs.lib.optionals (stdenv.isDarwin) [
coreutils
])}"
export PYPI_DATA=${inp.pypi-deps-db}
export CONDA_DATA=${(import ./mach_nix/nix/conda-channels.nix {
inherit pkgs;
providers = { _default = [ "conda/main" "conda/r" "conda/conda-forge"]; };
}).condaChannelsJson}
# Use "python -m pytest" to add current directory to python path.
# This ensures that mach_nix is importable.
echo "executing unit tests"
python -m pytest -n ''${WORKERS:-$(nproc)} -x ${./.}
'');
};
apps.tests-eval = {
type = "app";
program = toString (pkgs.writeScript "tests-eval" ''
export PATH="${pkgs.lib.makeBinPath (with pkgs; [
git
nixFlakes
parallel
bash
]
++ pkgs.lib.optional (stdenv.isLinux) busybox
++ pkgs.lib.optionals (stdenv.isDarwin) [
coreutils
])}"
cd tests
echo "executing evaluation tests"
./execute.sh
'');
};
apps.tests-all = {
type = "app";
program = toString (pkgs.writeScript "tests-eval" ''
set -e
${apps.tests-unit.program}
${apps.tests-eval.program}
CONDA_TESTS=y ${apps.tests-eval.program}
'');
};
defaultApp = { type = "app"; program = "${defaultPackage}/bin/mach-nix"; };
lib = {
inherit (mach-nix-default)
mkPython
mkPythonShell
mkDockerImage
mkOverlay
mkNixpkgs
mkPythonOverrides
buildPythonPackage
buildPythonApplication
fetchPypiSdist
fetchPypiWheel
;
};
}
))
// # deprecated usage
{
pythonWith = {} // throw "\n'pythonWith' is deprecated.\n${usageGen}";
"with" = {} // throw "\n'with' is deprecated.\n${usageGen}";
shellWith = {} // throw "\n'shellWith' is deprecated.\n${usageGen}";
dockerImageWith = {} // throw "\n'dockerImageWith' is deprecated.\n${usageGen}";
};
}