nixos-rebuild-ng: make path part of Flake absolute

Fix: #433726.
This commit is contained in:
Thiago Kenji Okada 2025-08-14 18:41:08 +01:00
parent a9b58022f0
commit af8973050a
3 changed files with 21 additions and 9 deletions

View File

@ -86,7 +86,11 @@ class Flake:
@override @override
def __str__(self) -> str: def __str__(self) -> str:
return f"{self.path}#{self.attr}" if isinstance(self.path, Path):
# https://github.com/NixOS/nixpkgs/issues/433726
return f"{self.path.absolute()}#{self.attr}"
else:
return f"{self.path}#{self.attr}"
@classmethod @classmethod
def parse(cls, flake_str: str, target_host: Remote | None = None) -> Self: def parse(cls, flake_str: str, target_host: Remote | None = None) -> Self:

View File

@ -71,6 +71,14 @@ def test_flake_to_attr() -> None:
) )
def test_flake__str__(monkeypatch: MonkeyPatch, tmpdir: Path) -> None:
assert str(m.Flake("github:nixos/nixpkgs", "attr")) == "github:nixos/nixpkgs#attr"
assert str(m.Flake(Path("/etc/nixos"), "attr")) == "/etc/nixos#attr"
with monkeypatch.context() as patch_context:
patch_context.chdir(tmpdir)
assert str(m.Flake(Path("."), "attr")) == f"{tmpdir}#attr"
@patch("platform.node", autospec=True) @patch("platform.node", autospec=True)
def test_flake_from_arg( def test_flake_from_arg(
mock_node: Mock, monkeypatch: MonkeyPatch, tmpdir: Path mock_node: Mock, monkeypatch: MonkeyPatch, tmpdir: Path

View File

@ -54,7 +54,7 @@ def test_build(mock_run: Mock) -> None:
) )
def test_build_flake(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None: def test_build_flake(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None:
monkeypatch.chdir(tmpdir) monkeypatch.chdir(tmpdir)
flake = m.Flake.parse(".#hostname") flake = m.Flake.parse("/flake.nix#hostname")
assert n.build_flake( assert n.build_flake(
"config.system.build.toplevel", "config.system.build.toplevel",
@ -68,7 +68,7 @@ def test_build_flake(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) ->
"nix-command flakes", "nix-command flakes",
"build", "build",
"--print-out-paths", "--print-out-paths",
'.#nixosConfigurations."hostname".config.system.build.toplevel', '/flake.nix#nixosConfigurations."hostname".config.system.build.toplevel',
"--no-link", "--no-link",
"--nix-flag", "--nix-flag",
"foo", "foo",
@ -173,7 +173,7 @@ def test_build_remote_flake(
mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path
) -> None: ) -> None:
monkeypatch.chdir(tmpdir) monkeypatch.chdir(tmpdir)
flake = m.Flake.parse(".#hostname") flake = m.Flake.parse("/flake.nix#hostname")
build_host = m.Remote("user@host", [], None) build_host = m.Remote("user@host", [], None)
monkeypatch.setenv("NIX_SSHOPTS", "--ssh opts") monkeypatch.setenv("NIX_SSHOPTS", "--ssh opts")
@ -194,7 +194,7 @@ def test_build_remote_flake(
"nix-command flakes", "nix-command flakes",
"eval", "eval",
"--raw", "--raw",
'.#nixosConfigurations."hostname".config.system.build.toplevel.drvPath', '/flake.nix#nixosConfigurations."hostname".config.system.build.toplevel.drvPath',
"--flake", "--flake",
], ],
stdout=PIPE, stdout=PIPE,
@ -291,7 +291,7 @@ def test_edit(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None:
@patch(get_qualified_name(n.run_wrapper, n), autospec=True) @patch(get_qualified_name(n.run_wrapper, n), autospec=True)
def test_edit_flake(mock_run: Mock) -> None: def test_edit_flake(mock_run: Mock) -> None:
flake = m.Flake.parse(".#attr") flake = m.Flake.parse("/flake.nix#attr")
n.edit_flake(flake, {"commit_lock_file": True}) n.edit_flake(flake, {"commit_lock_file": True})
mock_run.assert_called_with( mock_run.assert_called_with(
[ [
@ -301,7 +301,7 @@ def test_edit_flake(mock_run: Mock) -> None:
"edit", "edit",
"--commit-lock-file", "--commit-lock-file",
"--", "--",
'.#nixosConfigurations."attr"', '/flake.nix#nixosConfigurations."attr"',
], ],
check=False, check=False,
) )
@ -385,7 +385,7 @@ def test_get_build_image_variants(mock_run: Mock, tmp_path: Path) -> None:
), ),
) )
def test_get_build_image_variants_flake(mock_run: Mock) -> None: def test_get_build_image_variants_flake(mock_run: Mock) -> None:
flake = m.Flake(Path("flake.nix"), "myAttr") flake = m.Flake(Path("/flake.nix"), "myAttr")
assert n.get_build_image_variants_flake(flake, {"eval_flag": True}) == { assert n.get_build_image_variants_flake(flake, {"eval_flag": True}) == {
"azure": "nixos-image-azure-25.05.20250102.6df2492-x86_64-linux.vhd", "azure": "nixos-image-azure-25.05.20250102.6df2492-x86_64-linux.vhd",
"vmware": "nixos-image-vmware-25.05.20250102.6df2492-x86_64-linux.vmdk", "vmware": "nixos-image-vmware-25.05.20250102.6df2492-x86_64-linux.vmdk",
@ -395,7 +395,7 @@ def test_get_build_image_variants_flake(mock_run: Mock) -> None:
"nix", "nix",
"eval", "eval",
"--json", "--json",
"flake.nix#myAttr.config.system.build.images", "/flake.nix#myAttr.config.system.build.images",
"--apply", "--apply",
"builtins.attrNames", "builtins.attrNames",
"--eval-flag", "--eval-flag",