Skip to content

Commit 7a9860d

Browse files
committed
Retry nix installs after restarting daemon
Signed-off-by: Nick Sweeting <git@sweeting.me>
1 parent 8af5550 commit 7a9860d

1 file changed

Lines changed: 120 additions & 9 deletions

File tree

abxpkg/binprovider_nix.py

Lines changed: 120 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
# ``ABXPKG_NIX_ROOT`` nor ``ABXPKG_LIB_DIR`` is set.
3535
DEFAULT_NIX_PROFILE = Path("~/.nix-profile").expanduser()
3636
DEFAULT_NIX_BIN_DIR = Path("/nix/var/nix/profiles/default/bin")
37-
# Use a stable per-user private store so ``nix profile`` does not
38-
# depend on a working system daemon on CI or single-user hosts.
39-
DEFAULT_PRIVATE_NIX_STORE = Path("~/.local/share/nix/root").expanduser()
4037

4138

4239
class NixProvider(BinProvider):
@@ -241,8 +238,6 @@ def default_install_handler(
241238
proc = self.exec(
242239
bin_name=installer_bin,
243240
cmd=[
244-
"--store",
245-
str(DEFAULT_PRIVATE_NIX_STORE),
246241
"--access-tokens",
247242
"",
248243
"profile",
@@ -258,6 +253,46 @@ def default_install_handler(
258253
env=env,
259254
timeout=timeout,
260255
)
256+
proc_output = format_subprocess_output(proc.stdout, proc.stderr)
257+
if (
258+
proc.returncode != 0
259+
and os.uname().sysname == "Linux"
260+
and Path("/run/systemd/system").is_dir()
261+
and (
262+
"cannot connect to socket at '/nix/var/nix/daemon-socket/socket'"
263+
in proc_output
264+
or "opening a connection to remote store 'daemon' previously failed"
265+
in proc_output
266+
)
267+
):
268+
self.exec(
269+
bin_name="sudo",
270+
cmd=["systemctl", "daemon-reload"],
271+
timeout=timeout,
272+
)
273+
self.exec(
274+
bin_name="sudo",
275+
cmd=["systemctl", "restart", "nix-daemon.socket"],
276+
timeout=timeout,
277+
)
278+
proc = self.exec(
279+
bin_name=installer_bin,
280+
cmd=[
281+
"--access-tokens",
282+
"",
283+
"profile",
284+
"add",
285+
"--extra-experimental-features",
286+
"nix-command",
287+
"--extra-experimental-features",
288+
"flakes",
289+
"--profile",
290+
str(self.install_root),
291+
*install_args,
292+
],
293+
env=env,
294+
timeout=timeout,
295+
)
261296
if proc.returncode != 0:
262297
self._raise_proc_error("install", install_args, proc)
263298

@@ -298,8 +333,6 @@ def default_update_handler(
298333
proc = self.exec(
299334
bin_name=installer_bin,
300335
cmd=[
301-
"--store",
302-
str(DEFAULT_PRIVATE_NIX_STORE),
303336
"--access-tokens",
304337
"",
305338
"profile",
@@ -315,6 +348,46 @@ def default_update_handler(
315348
env=env,
316349
timeout=timeout,
317350
)
351+
proc_output = format_subprocess_output(proc.stdout, proc.stderr)
352+
if (
353+
proc.returncode != 0
354+
and os.uname().sysname == "Linux"
355+
and Path("/run/systemd/system").is_dir()
356+
and (
357+
"cannot connect to socket at '/nix/var/nix/daemon-socket/socket'"
358+
in proc_output
359+
or "opening a connection to remote store 'daemon' previously failed"
360+
in proc_output
361+
)
362+
):
363+
self.exec(
364+
bin_name="sudo",
365+
cmd=["systemctl", "daemon-reload"],
366+
timeout=timeout,
367+
)
368+
self.exec(
369+
bin_name="sudo",
370+
cmd=["systemctl", "restart", "nix-daemon.socket"],
371+
timeout=timeout,
372+
)
373+
proc = self.exec(
374+
bin_name=installer_bin,
375+
cmd=[
376+
"--access-tokens",
377+
"",
378+
"profile",
379+
"upgrade",
380+
"--extra-experimental-features",
381+
"nix-command",
382+
"--extra-experimental-features",
383+
"flakes",
384+
"--profile",
385+
str(self.install_root),
386+
profile_element,
387+
],
388+
env=env,
389+
timeout=timeout,
390+
)
318391
if proc.returncode != 0:
319392
self._raise_proc_error("update", profile_element, proc)
320393

@@ -355,8 +428,6 @@ def default_uninstall_handler(
355428
proc = self.exec(
356429
bin_name=installer_bin,
357430
cmd=[
358-
"--store",
359-
str(DEFAULT_PRIVATE_NIX_STORE),
360431
"--access-tokens",
361432
"",
362433
"profile",
@@ -372,6 +443,46 @@ def default_uninstall_handler(
372443
env=env,
373444
timeout=timeout,
374445
)
446+
proc_output = format_subprocess_output(proc.stdout, proc.stderr)
447+
if (
448+
proc.returncode not in (0, 1)
449+
and os.uname().sysname == "Linux"
450+
and Path("/run/systemd/system").is_dir()
451+
and (
452+
"cannot connect to socket at '/nix/var/nix/daemon-socket/socket'"
453+
in proc_output
454+
or "opening a connection to remote store 'daemon' previously failed"
455+
in proc_output
456+
)
457+
):
458+
self.exec(
459+
bin_name="sudo",
460+
cmd=["systemctl", "daemon-reload"],
461+
timeout=timeout,
462+
)
463+
self.exec(
464+
bin_name="sudo",
465+
cmd=["systemctl", "restart", "nix-daemon.socket"],
466+
timeout=timeout,
467+
)
468+
proc = self.exec(
469+
bin_name=installer_bin,
470+
cmd=[
471+
"--access-tokens",
472+
"",
473+
"profile",
474+
"remove",
475+
"--extra-experimental-features",
476+
"nix-command",
477+
"--extra-experimental-features",
478+
"flakes",
479+
"--profile",
480+
str(self.install_root),
481+
profile_element,
482+
],
483+
env=env,
484+
timeout=timeout,
485+
)
375486
if proc.returncode not in (0, 1):
376487
self._raise_proc_error("uninstall", profile_element, proc)
377488

0 commit comments

Comments
 (0)