Refactor pkgmgr self-install handling, add pip-enabled Python env to flake, and fix Nix/pip integration
- Rename requirements.txt to _requirements.txt to avoid unintended self-install via PythonInstaller. - Update flake.nix to provide Python with pip in both devShell and runtime closure. - Generate a wrapper for pkgmgr that uses the pip-enabled interpreter. - Adjust NixFlakeInstaller to correctly handle old profile removal and install outputs. - Update run_command to support string commands and allow_failure. - Improve integration test by adding nix profile cleanup and debugging output. Reference: https://chatgpt.com/share/69345df2-a960-800f-8395-92a7c3a6629f
This commit is contained in:
@@ -10,17 +10,22 @@ installer will try to install profile outputs from the flake.
|
||||
Behavior:
|
||||
- If flake.nix is present and `nix` exists on PATH:
|
||||
* First remove any existing `package-manager` profile entry (best-effort).
|
||||
* Then install the flake outputs (pkgmgr, default) via `nix profile install`.
|
||||
- Any failure in `nix profile install` is treated as fatal (SystemExit).
|
||||
* Then install the flake outputs (`pkgmgr`, `default`) via `nix profile install`.
|
||||
- Failure installing `pkgmgr` is treated as fatal.
|
||||
- Failure installing `default` is logged as an error/warning but does not abort.
|
||||
"""
|
||||
|
||||
import os
|
||||
import shutil
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from pkgmgr.context import RepoContext
|
||||
from pkgmgr.installers.base import BaseInstaller
|
||||
from pkgmgr.run_command import run_command
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pkgmgr.context import RepoContext
|
||||
from pkgmgr.install_repos import InstallContext
|
||||
|
||||
|
||||
class NixFlakeInstaller(BaseInstaller):
|
||||
"""Install Nix flake profiles for repositories that define flake.nix."""
|
||||
@@ -28,7 +33,7 @@ class NixFlakeInstaller(BaseInstaller):
|
||||
FLAKE_FILE = "flake.nix"
|
||||
PROFILE_NAME = "package-manager"
|
||||
|
||||
def supports(self, ctx: RepoContext) -> bool:
|
||||
def supports(self, ctx: "RepoContext") -> bool:
|
||||
"""
|
||||
Only support repositories that:
|
||||
- Have a flake.nix
|
||||
@@ -39,7 +44,7 @@ class NixFlakeInstaller(BaseInstaller):
|
||||
flake_path = os.path.join(ctx.repo_dir, self.FLAKE_FILE)
|
||||
return os.path.exists(flake_path)
|
||||
|
||||
def _ensure_old_profile_removed(self, ctx: RepoContext) -> None:
|
||||
def _ensure_old_profile_removed(self, ctx: "RepoContext") -> None:
|
||||
"""
|
||||
Best-effort removal of an existing profile entry.
|
||||
|
||||
@@ -53,45 +58,46 @@ class NixFlakeInstaller(BaseInstaller):
|
||||
if shutil.which("nix") is None:
|
||||
return
|
||||
|
||||
# We do NOT use run_command here, because we explicitly want to ignore
|
||||
# the failure of `nix profile remove` (e.g. entry not present).
|
||||
# Using `|| true` makes this idempotent.
|
||||
cmd = f"nix profile remove {self.PROFILE_NAME} || true"
|
||||
# This will still respect preview mode inside run_command.
|
||||
try:
|
||||
# NOTE: no allow_failure here → matches the existing unit tests
|
||||
run_command(cmd, cwd=ctx.repo_dir, preview=ctx.preview)
|
||||
except SystemExit:
|
||||
# Ignore any error here: if the profile entry does not exist,
|
||||
# that's fine and not a fatal condition.
|
||||
# Unit tests explicitly assert this is swallowed
|
||||
pass
|
||||
|
||||
def run(self, ctx: RepoContext) -> None:
|
||||
def run(self, ctx: "InstallContext") -> None:
|
||||
"""
|
||||
Install Nix flake profile outputs (pkgmgr, default).
|
||||
|
||||
Any failure in `nix profile install` is treated as fatal (SystemExit).
|
||||
The "already installed / file conflict" situation is avoided by
|
||||
removing the existing profile entry beforehand.
|
||||
Any failure installing `pkgmgr` is treated as fatal (SystemExit).
|
||||
A failure installing `default` is logged but does not abort.
|
||||
"""
|
||||
flake_path = os.path.join(ctx.repo_dir, self.FLAKE_FILE)
|
||||
if not os.path.exists(flake_path):
|
||||
return
|
||||
|
||||
if shutil.which("nix") is None:
|
||||
print("Warning: flake.nix found but 'nix' command not available. Skipping flake setup.")
|
||||
# Reuse supports() to keep logic in one place
|
||||
if not self.supports(ctx): # type: ignore[arg-type]
|
||||
return
|
||||
|
||||
print("Nix flake detected, attempting to install profile outputs...")
|
||||
|
||||
# Handle the "already installed" case up-front:
|
||||
self._ensure_old_profile_removed(ctx)
|
||||
self._ensure_old_profile_removed(ctx) # type: ignore[arg-type]
|
||||
|
||||
for output in ("pkgmgr", "default"):
|
||||
cmd = f"nix profile install {ctx.repo_dir}#{output}"
|
||||
|
||||
try:
|
||||
run_command(cmd, cwd=ctx.repo_dir, preview=ctx.preview)
|
||||
# For 'default' we don't want the process to exit on error
|
||||
allow_failure = output == "default"
|
||||
run_command(cmd, cwd=ctx.repo_dir, preview=ctx.preview, allow_failure=allow_failure)
|
||||
print(f"Nix flake output '{output}' successfully installed.")
|
||||
except SystemExit as e:
|
||||
print(f"[Error] Failed to install Nix flake output '{output}': {e}")
|
||||
# Hard fail: a broken flake is considered a fatal error.
|
||||
raise
|
||||
if output == "pkgmgr":
|
||||
# Broken main CLI install → fatal
|
||||
raise
|
||||
# For 'default' we log and continue
|
||||
print(
|
||||
"[Warning] Continuing despite failure to install 'default' "
|
||||
"because 'pkgmgr' is already installed."
|
||||
)
|
||||
break
|
||||
|
||||
@@ -1,18 +1,45 @@
|
||||
import sys
|
||||
# pkgmgr/run_command.py
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
from typing import List, Optional, Union
|
||||
|
||||
def run_command(command, cwd=None, preview=False):
|
||||
"""Run a shell command in a given directory, or print it in preview mode.
|
||||
|
||||
If the command fails, exit the program with the command's exit code.
|
||||
|
||||
CommandType = Union[str, List[str]]
|
||||
|
||||
|
||||
def run_command(
|
||||
cmd: CommandType,
|
||||
cwd: Optional[str] = None,
|
||||
preview: bool = False,
|
||||
allow_failure: bool = False,
|
||||
) -> subprocess.CompletedProcess:
|
||||
"""
|
||||
current_dir = cwd or os.getcwd()
|
||||
if preview:
|
||||
print(f"[Preview] In '{current_dir}': {command}")
|
||||
Run a command and optionally exit on error.
|
||||
|
||||
- If `cmd` is a string, it is executed with `shell=True`.
|
||||
- If `cmd` is a list of strings, it is executed without a shell.
|
||||
"""
|
||||
if isinstance(cmd, str):
|
||||
display = cmd
|
||||
else:
|
||||
print(f"Running in '{current_dir}': {command}")
|
||||
result = subprocess.run(command, cwd=cwd, shell=True, check=False)
|
||||
if result.returncode != 0:
|
||||
print(f"Command failed with exit code {result.returncode}. Exiting.")
|
||||
sys.exit(result.returncode)
|
||||
display = " ".join(cmd)
|
||||
|
||||
where = cwd or "."
|
||||
|
||||
if preview:
|
||||
print(f"[Preview] In '{where}': {display}")
|
||||
# Fake a successful result; most callers ignore the return value anyway
|
||||
return subprocess.CompletedProcess(cmd, 0) # type: ignore[arg-type]
|
||||
|
||||
print(f"Running in '{where}': {display}")
|
||||
|
||||
if isinstance(cmd, str):
|
||||
result = subprocess.run(cmd, cwd=cwd, shell=True)
|
||||
else:
|
||||
result = subprocess.run(cmd, cwd=cwd)
|
||||
|
||||
if result.returncode != 0 and not allow_failure:
|
||||
print(f"Command failed with exit code {result.returncode}. Exiting.")
|
||||
sys.exit(result.returncode)
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user