nixos-rebuild-ng: quote hostname (#412341)

This commit is contained in:
Thiago Kenji Okada 2025-05-30 16:56:25 +01:00 committed by GitHub
commit 94d09d1046
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 26 deletions

View File

@ -122,7 +122,7 @@ class Flake:
m = cls._re.match(flake_str) m = cls._re.match(flake_str)
assert m is not None, f"got no matches for {flake_str}" assert m is not None, f"got no matches for {flake_str}"
attr = m.group("attr") attr = m.group("attr")
nixos_attr = f"nixosConfigurations.{attr or hostname_fn() or 'default'}" nixos_attr = f'nixosConfigurations."{attr or hostname_fn() or "default"}"'
path_str = m.group("path") path_str = m.group("path")
if ":" in path_str: if ":" in path_str:
return cls(path_str, nixos_attr) return cls(path_str, nixos_attr)

View File

@ -385,7 +385,7 @@ def test_execute_nix_build_image_flake(mock_run: Mock, tmp_path: Path) -> None:
"nix", "nix",
"eval", "eval",
"--json", "--json",
"/path/to/config#nixosConfigurations.hostname.config.system.build.images", '/path/to/config#nixosConfigurations."hostname".config.system.build.images',
"--apply", "--apply",
"builtins.attrNames", "builtins.attrNames",
], ],
@ -400,7 +400,7 @@ def test_execute_nix_build_image_flake(mock_run: Mock, tmp_path: Path) -> None:
"nix-command flakes", "nix-command flakes",
"build", "build",
"--print-out-paths", "--print-out-paths",
"/path/to/config#nixosConfigurations.hostname.config.system.build.images.azure", '/path/to/config#nixosConfigurations."hostname".config.system.build.images.azure',
], ],
check=True, check=True,
stdout=PIPE, stdout=PIPE,
@ -411,7 +411,7 @@ def test_execute_nix_build_image_flake(mock_run: Mock, tmp_path: Path) -> None:
"nix", "nix",
"eval", "eval",
"--json", "--json",
"/path/to/config#nixosConfigurations.hostname.config.system.build.images.azure.passthru.filePath", '/path/to/config#nixosConfigurations."hostname".config.system.build.images.azure.passthru.filePath',
], ],
check=True, check=True,
stdout=PIPE, stdout=PIPE,
@ -462,7 +462,7 @@ def test_execute_nix_switch_flake(mock_run: Mock, tmp_path: Path) -> None:
"nix-command flakes", "nix-command flakes",
"build", "build",
"--print-out-paths", "--print-out-paths",
"/path/to/config#nixosConfigurations.hostname.config.system.build.toplevel", '/path/to/config#nixosConfigurations."hostname".config.system.build.toplevel',
"-v", "-v",
"--option", "--option",
"narinfo-cache-negative-ttl", "narinfo-cache-negative-ttl",
@ -756,7 +756,7 @@ def test_execute_nix_switch_flake_target_host(
"nix-command flakes", "nix-command flakes",
"build", "build",
"--print-out-paths", "--print-out-paths",
"/path/to/config#nixosConfigurations.hostname.config.system.build.toplevel", '/path/to/config#nixosConfigurations."hostname".config.system.build.toplevel',
"--no-link", "--no-link",
], ],
check=True, check=True,
@ -860,7 +860,7 @@ def test_execute_nix_switch_flake_build_host(
"nix-command flakes", "nix-command flakes",
"eval", "eval",
"--raw", "--raw",
"/path/to/config#nixosConfigurations.hostname.config.system.build.toplevel.drvPath", '/path/to/config#nixosConfigurations."hostname".config.system.build.toplevel.drvPath',
], ],
check=True, check=True,
stdout=PIPE, stdout=PIPE,
@ -1063,7 +1063,7 @@ def test_execute_test_flake(mock_run: Mock, tmp_path: Path) -> None:
"nix-command flakes", "nix-command flakes",
"build", "build",
"--print-out-paths", "--print-out-paths",
"github:user/repo#nixosConfigurations.hostname.config.system.build.toplevel", 'github:user/repo#nixosConfigurations."hostname".config.system.build.toplevel',
], ],
check=True, check=True,
stdout=PIPE, stdout=PIPE,

View File

@ -32,38 +32,42 @@ def test_build_attr_to_attr() -> None:
def test_flake_parse(tmpdir: Path, monkeypatch: MonkeyPatch) -> None: def test_flake_parse(tmpdir: Path, monkeypatch: MonkeyPatch) -> None:
assert m.Flake.parse("/path/to/flake#attr") == m.Flake( assert m.Flake.parse("/path/to/flake#attr") == m.Flake(
Path("/path/to/flake"), "nixosConfigurations.attr" Path("/path/to/flake"), 'nixosConfigurations."attr"'
) )
assert m.Flake.parse("/path/ to /flake", lambda: "hostname") == m.Flake( assert m.Flake.parse("/path/ to /flake", lambda: "hostname") == m.Flake(
Path("/path/ to /flake"), "nixosConfigurations.hostname" Path("/path/ to /flake"), 'nixosConfigurations."hostname"'
) )
assert m.Flake.parse("/path/to/flake", lambda: "hostname") == m.Flake( assert m.Flake.parse("/path/to/flake", lambda: "hostname") == m.Flake(
Path("/path/to/flake"), "nixosConfigurations.hostname" Path("/path/to/flake"), 'nixosConfigurations."hostname"'
) )
# change directory to tmpdir # change directory to tmpdir
with monkeypatch.context() as patch_context: with monkeypatch.context() as patch_context:
patch_context.chdir(tmpdir) patch_context.chdir(tmpdir)
assert m.Flake.parse(".#attr") == m.Flake(Path("."), "nixosConfigurations.attr") assert m.Flake.parse(".#attr") == m.Flake(
assert m.Flake.parse("#attr") == m.Flake(Path("."), "nixosConfigurations.attr") Path("."), 'nixosConfigurations."attr"'
assert m.Flake.parse(".") == m.Flake(Path("."), "nixosConfigurations.default") )
assert m.Flake.parse("#attr") == m.Flake(
Path("."), 'nixosConfigurations."attr"'
)
assert m.Flake.parse(".") == m.Flake(Path("."), 'nixosConfigurations."default"')
assert m.Flake.parse("path:/to/flake#attr") == m.Flake( assert m.Flake.parse("path:/to/flake#attr") == m.Flake(
"path:/to/flake", "nixosConfigurations.attr" "path:/to/flake", 'nixosConfigurations."attr"'
) )
assert m.Flake.parse("github:user/repo/branch") == m.Flake( assert m.Flake.parse("github:user/repo/branch") == m.Flake(
"github:user/repo/branch", "nixosConfigurations.default" "github:user/repo/branch", 'nixosConfigurations."default"'
) )
git_root = tmpdir / "git_root" git_root = tmpdir / "git_root"
git_root.mkdir() git_root.mkdir()
(git_root / ".git").mkdir() (git_root / ".git").mkdir()
assert m.Flake.parse(str(git_root)) == m.Flake( assert m.Flake.parse(str(git_root)) == m.Flake(
f"git+file://{git_root}", "nixosConfigurations.default" f"git+file://{git_root}", 'nixosConfigurations."default"'
) )
work_tree = tmpdir / "work_tree" work_tree = tmpdir / "work_tree"
work_tree.mkdir() work_tree.mkdir()
(work_tree / ".git").write_text("gitdir: /path/to/git", "utf-8") (work_tree / ".git").write_text("gitdir: /path/to/git", "utf-8")
assert m.Flake.parse(str(work_tree)) == m.Flake( assert m.Flake.parse(str(work_tree)) == m.Flake(
"git+file:///path/to/git", "nixosConfigurations.default" "git+file:///path/to/git", 'nixosConfigurations."default"'
) )
@ -84,7 +88,7 @@ def test_flake_from_arg(
# Flake string # Flake string
assert m.Flake.from_arg("/path/to/flake#attr", None) == m.Flake( assert m.Flake.from_arg("/path/to/flake#attr", None) == m.Flake(
Path("/path/to/flake"), "nixosConfigurations.attr" Path("/path/to/flake"), 'nixosConfigurations."attr"'
) )
# False # False
@ -94,7 +98,7 @@ def test_flake_from_arg(
with monkeypatch.context() as patch_context: with monkeypatch.context() as patch_context:
patch_context.chdir(tmpdir) patch_context.chdir(tmpdir)
assert m.Flake.from_arg(True, None) == m.Flake( assert m.Flake.from_arg(True, None) == m.Flake(
Path("."), "nixosConfigurations.hostname" Path("."), 'nixosConfigurations."hostname"'
) )
# None when we do not have /etc/nixos/flake.nix # None when we do not have /etc/nixos/flake.nix
@ -124,7 +128,7 @@ def test_flake_from_arg(
), ),
): ):
assert m.Flake.from_arg(None, None) == m.Flake( assert m.Flake.from_arg(None, None) == m.Flake(
"git+file:///etc/nixos", "nixosConfigurations.hostname" "git+file:///etc/nixos", 'nixosConfigurations."hostname"'
) )
with ( with (
@ -145,7 +149,7 @@ def test_flake_from_arg(
), ),
): ):
assert m.Flake.from_arg(None, None) == m.Flake( assert m.Flake.from_arg(None, None) == m.Flake(
Path("/path/to"), "nixosConfigurations.hostname" Path("/path/to"), 'nixosConfigurations."hostname"'
) )
with ( with (
@ -156,7 +160,7 @@ def test_flake_from_arg(
), ),
): ):
assert m.Flake.from_arg("/path/to", m.Remote("user@host", [], None)) == m.Flake( assert m.Flake.from_arg("/path/to", m.Remote("user@host", [], None)) == m.Flake(
Path("/path/to"), "nixosConfigurations.remote-hostname" Path("/path/to"), 'nixosConfigurations."remote-hostname"'
) )

View File

@ -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", '.#nixosConfigurations."hostname".config.system.build.toplevel',
"--no-link", "--no-link",
"--nix-flag", "--nix-flag",
"foo", "foo",
@ -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", '.#nixosConfigurations."hostname".config.system.build.toplevel.drvPath',
"--flake", "--flake",
], ],
stdout=PIPE, stdout=PIPE,
@ -304,7 +304,7 @@ def test_edit(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None:
"edit", "edit",
"--commit-lock-file", "--commit-lock-file",
"--", "--",
f"{tmpdir}#nixosConfigurations.attr", f'{tmpdir}#nixosConfigurations."attr"',
], ],
check=False, check=False,
) )