-
Notifications
You must be signed in to change notification settings - Fork 59
/
nix.py
314 lines (275 loc) · 8.71 KB
/
nix.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import json
import os
import shlex
import shutil
import subprocess
from dataclasses import dataclass, field
from pathlib import Path
from sys import platform
from tempfile import NamedTemporaryFile
from typing import Any, Dict, List, Optional, Set, Union
from .allow import AllowedFeatures
from .utils import ROOT, escape_attr, info, sh, warn
@dataclass
class Attr:
name: str
exists: bool
broken: bool
blacklisted: bool
path: Optional[str]
drv_path: Optional[str]
aliases: List[str] = field(default_factory=lambda: [])
_path_verified: Optional[bool] = field(init=False, default=None)
def was_build(self) -> bool:
if self.path is None:
return False
if self._path_verified is not None:
return self._path_verified
res = subprocess.run(
["nix-store", "--verify-path", self.path], stderr=subprocess.DEVNULL
)
self._path_verified = res.returncode == 0
return self._path_verified
def is_test(self) -> bool:
return self.name.startswith("nixosTests")
def nix_shell(
attrs: List[str],
cache_directory: Path,
system: str,
build_graph: str,
nixpkgs_config: Path,
run: Optional[str] = None,
sandbox: bool = False,
) -> None:
nix_shell = f"{shutil.which(build_graph)}-shell"
if not nix_shell:
raise RuntimeError(f"{build_graph} not found in PATH")
shell = cache_directory.joinpath("shell.nix")
write_shell_expression(shell, attrs, system, nixpkgs_config)
if sandbox:
args = _nix_shell_sandbox(nix_shell, shell, nixpkgs_config)
else:
args = [nix_shell, str(shell)]
if run:
args.extend(["--run", run])
sh(args, cwd=cache_directory, check=False)
def _nix_shell_sandbox(nix_shell: str, shell: Path, nixpkgs_config: Path) -> List[str]:
if platform != "linux":
raise RuntimeError("Sandbox mode is only available on Linux platforms.")
bwrap = shutil.which("bwrap")
if not bwrap:
raise RuntimeError(
"bwrap not found in PATH. Install it to use '--sandbox' flag."
)
warn("Using sandbox mode. Some things may break!")
def bind(
path: Union[Path, str],
ro: bool = True,
dev: bool = False,
try_: bool = False,
) -> List[str]:
if dev:
prefix = "--dev-"
elif ro:
prefix = "--ro-"
else:
prefix = "--"
suffix = "-try" if try_ else ""
return [prefix + "bind" + suffix, str(path), str(path)]
def tmpfs(path: Union[Path, str], dir: bool = True) -> List[str]:
dir_cmd = []
if dir:
dir_cmd = ["--dir", str(path)]
return [*dir_cmd, "--tmpfs", str(path)]
nixpkgs_review_pr = shell.parent
home = Path.home()
current_dir = Path().absolute()
xdg_config_home = Path(os.environ.get("XDG_CONFIG_HOME", home.joinpath(".config")))
nixpkgs_config_dir = xdg_config_home.joinpath("nixpkgs")
xauthority = Path(os.environ.get("XAUTHORITY", home.joinpath(".Xauthority")))
hub_config = xdg_config_home.joinpath("hub")
gh_config = xdg_config_home.joinpath("gh")
uid = os.environ.get("UID", "1000")
bwrap_args = [
"--die-with-parent",
"--unshare-cgroup",
"--unshare-ipc",
"--unshare-uts",
# / and cia.
*bind("/"),
*bind("/dev", dev=True),
*tmpfs("/tmp"),
# Required for evaluation
*bind(nixpkgs_config),
# /run (also cover sockets for wayland/pulseaudio and pipewires)
*bind(Path("/run/user").joinpath(uid), dev=True, try_=True),
# HOME
*tmpfs(home),
*bind(current_dir, ro=False),
*bind(nixpkgs_review_pr, ro=False),
*bind(nixpkgs_config_dir, try_=True),
# For X11 applications
*bind("/tmp/.X11-unix", try_=True),
*bind(xauthority, try_=True),
# GitHub
*bind(hub_config, try_=True),
*bind(gh_config, try_=True),
]
return [bwrap, *bwrap_args, "--", nix_shell, str(shell)]
def _nix_eval_filter(json: Dict[str, Any]) -> List[Attr]:
# workaround https://github.com/NixOS/ofborg/issues/269
blacklist = set(
[
"appimage-run-tests",
"darwin.builder",
"nixos-install-tools",
"tests.nixos-functions.nixos-test",
"tests.nixos-functions.nixosTest-test",
"tests.php.overrideAttrs-preserves-enabled-extensions",
"tests.php.withExtensions-enables-previously-disabled-extensions",
"tests.trivial",
"tests.writers",
]
)
attr_by_path: Dict[str, Attr] = {}
broken = []
for name, props in json.items():
attr = Attr(
name=name,
exists=props["exists"],
broken=props["broken"],
blacklisted=name in blacklist,
path=props["path"],
drv_path=props["drvPath"],
)
if attr.path is not None:
other = attr_by_path.get(attr.path, None)
if other is None:
attr_by_path[attr.path] = attr
else:
if len(other.name) > len(attr.name):
attr_by_path[attr.path] = attr
attr.aliases.append(other.name)
else:
other.aliases.append(attr.name)
else:
broken.append(attr)
return list(attr_by_path.values()) + broken
def nix_eval(
attrs: Set[str],
system: str,
allow: AllowedFeatures,
) -> List[Attr]:
attr_json = NamedTemporaryFile(mode="w+", delete=False)
delete = True
try:
json.dump(list(attrs), attr_json)
eval_script = str(ROOT.joinpath("nix/evalAttrs.nix"))
attr_json.flush()
cmd = [
"nix",
"--extra-experimental-features",
"nix-command" if allow.url_literals else "nix-command no-url-literals",
"--system",
system,
"eval",
"--json",
"--impure",
"--allow-import-from-derivation"
if allow.ifd
else "--no-allow-import-from-derivation",
"--expr",
f"(import {eval_script} {{ attr-json = {attr_json.name}; }})",
]
try:
nix_eval = subprocess.run(
cmd, check=True, stdout=subprocess.PIPE, text=True
)
except subprocess.CalledProcessError:
warn(
f"{' '.join(cmd)} failed to run, {attr_json.name} was stored inspection"
)
delete = False
raise
return _nix_eval_filter(json.loads(nix_eval.stdout))
finally:
attr_json.close()
if delete:
os.unlink(attr_json.name)
def nix_build(
attr_names: Set[str],
args: str,
cache_directory: Path,
system: str,
allow: AllowedFeatures,
build_graph: str,
nixpkgs_config: Path,
) -> List[Attr]:
if not attr_names:
info("Nothing to be built.")
return []
attrs = nix_eval(attr_names, system, allow)
filtered = []
for attr in attrs:
if not (attr.broken or attr.blacklisted):
filtered.append(attr.name)
if len(filtered) == 0:
return attrs
build = cache_directory.joinpath("build.nix")
write_shell_expression(build, filtered, system, nixpkgs_config)
command = [
build_graph,
"build",
"--extra-experimental-features",
"nix-command" if allow.url_literals else "nix-command no-url-literals",
"--no-link",
"--keep-going",
"--allow-import-from-derivation"
if allow.ifd
else "--no-allow-import-from-derivation",
]
if platform == "linux":
command += [
# only matters for single-user nix and trusted users
"--option",
"build-use-sandbox",
"relaxed",
]
command += [
"-f",
str(build),
] + shlex.split(args)
try:
sh(command)
except subprocess.CalledProcessError:
pass
return attrs
def write_shell_expression(
filename: Path, attrs: List[str], system: str, nixpkgs_config: Path
) -> None:
with open(filename, "w+") as f:
f.write(
f"""{{ pkgs ? import ./nixpkgs {{ system = \"{system}\"; config = import {nixpkgs_config}; }} }}:
with pkgs;
let
paths = [
"""
)
f.write("\n".join(f" {escape_attr(a)}" for a in attrs))
f.write(
"""
];
env = buildEnv {
name = "env";
inherit paths;
ignoreCollisions = true;
};
in (import ./nixpkgs { }).mkShell {
name = "review-shell";
preferLocalBuild = true;
allowSubstitutes = false;
dontWrapQtApps = true;
packages = if builtins.length paths > 50 then [ env ] else paths;
}
"""
)