From c4902dad75f98479b5ca13be0d6a5abfc6d3f22a Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sun, 1 Dec 2024 12:15:54 +0000 Subject: [PATCH] nixos-rebuild-ng: use raw NIX_SSHOPTS in copy_closure --- .../nixos-rebuild-ng/src/nixos_rebuild/nix.py | 14 +++++++------- .../ni/nixos-rebuild-ng/src/tests/test_nix.py | 18 +++++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py index e4e1c5663fcb..7c9338b3ef92 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py @@ -1,6 +1,5 @@ import logging import os -import shlex from datetime import datetime from importlib.resources import files from pathlib import Path @@ -145,6 +144,7 @@ def copy_closure( if not host: return + sshopts = os.getenv("NIX_SSHOPTS", "") run_wrapper( [ "nix-copy-closure", @@ -154,13 +154,13 @@ def copy_closure( closure, ], extra_env={ - # for the remote to remote case, we can't add SSH_DEFAULT_OPTS to - # host.opts because it includes the ControlPane opts that will not - # work in the remote, because the temporary directory that we - # created will not exist - "NIX_SSHOPTS": shlex.join(host.opts) + # Using raw NIX_SSHOPTS here to avoid messing up with the passed + # parameters, and we do not add the SSH_DEFAULT_OPTS in the remote + # to remote case, otherwise it will fail because of ControlPath + # will not exist in remote + "NIX_SSHOPTS": sshopts if from_host and to_host - else shlex.join(SSH_DEFAULT_OPTS + host.opts) + else " ".join(filter(lambda x: x, [*SSH_DEFAULT_OPTS, sshopts])) }, remote=from_host if to_host else None, ) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py index 0a98270490f9..6a34f98bd066 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py @@ -79,7 +79,8 @@ def test_build_flake(mock_run: Any) -> None: return_value=CompletedProcess([], 0, stdout=" \n/path/to/file\n "), ) def test_remote_build(mock_run: Any, monkeypatch: Any) -> None: - build_host = m.Remote("user@host", ["--ssh", "opts"], None) + build_host = m.Remote("user@host", [], None) + monkeypatch.setenv("NIX_SSHOPTS", "--ssh opts") assert n.remote_build( "config.system.build.toplevel", m.BuildAttr("", "preAttr"), @@ -128,9 +129,10 @@ def test_remote_build(mock_run: Any, monkeypatch: Any) -> None: autospec=True, return_value=CompletedProcess([], 0, stdout=" \n/path/to/file\n "), ) -def test_remote_build_flake(mock_run: Any) -> None: +def test_remote_build_flake(mock_run: Any, monkeypatch: Any) -> None: flake = m.Flake.parse(".#hostname") - build_host = m.Remote("user@host", ["--ssh", "opts"], None) + build_host = m.Remote("user@host", [], None) + monkeypatch.setenv("NIX_SSHOPTS", "--ssh opts") assert n.remote_build_flake( "config.system.build.toplevel", @@ -190,16 +192,17 @@ def test_copy_closure(mock_run: Any, monkeypatch: Any) -> None: n.copy_closure(closure, None) mock_run.assert_not_called() - target_host = m.Remote("user@target.host", ["--ssh", "target-opt"], None) - build_host = m.Remote("user@build.host", ["--ssh", "build-opt"], None) + target_host = m.Remote("user@target.host", [], None) + build_host = m.Remote("user@build.host", [], None) n.copy_closure(closure, target_host) mock_run.assert_called_with( ["nix-copy-closure", "--to", "user@target.host", closure], - extra_env={"NIX_SSHOPTS": " ".join(p.SSH_DEFAULT_OPTS + ["--ssh target-opt"])}, + extra_env={"NIX_SSHOPTS": " ".join(p.SSH_DEFAULT_OPTS)}, remote=None, ) + monkeypatch.setenv("NIX_SSHOPTS", "--ssh build-opt") n.copy_closure(closure, None, build_host) mock_run.assert_called_with( ["nix-copy-closure", "--from", "user@build.host", closure], @@ -207,11 +210,12 @@ def test_copy_closure(mock_run: Any, monkeypatch: Any) -> None: remote=None, ) + monkeypatch.setenv("NIX_SSHOPTS", "--ssh build-target-opt") n.copy_closure(closure, target_host, build_host) mock_run.assert_called_with( ["nix-copy-closure", "--to", "user@target.host", closure], remote=build_host, - extra_env={"NIX_SSHOPTS": "--ssh target-opt"}, + extra_env={"NIX_SSHOPTS": "--ssh build-target-opt"}, )