Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add var.ssh_options to pass additional SSH options to nixos-rebuild #427

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

threddast
Copy link

This PR adds a Terraform input variable named ssh_options to nixos-rebuild (and to the rebuild part of nixos-anywhere)

The problem I'm trying to solve is being able to use gcloud compute ssh to rebuild NixOS. gcloud compute ssh is a wrapper around ssh that sets a bunch of ssh options to connect to a GCP VM, even if there is no direct connection to it.

Example usage:

module "deploy" {
  source       = "github.com/nix-community/nixos-anywhere//terraform/nixos-rebuild"
  nixos_system = module.system-build.result.out
  target_host = "target"
  ssh_options = {
    IdentityFile          = "/home/user/.ssh/google_compute_engine"
    CheckHostIP           = "no"
    HashKnownHosts        = "no"
    HostKeyAlias          = "compute.123456789"
    IdentitiesOnly        = "yes"
    StrictHostKeyChecking = "yes"
    UserKnownHostsFile    = "/home/user/.ssh/google_compute_known_hosts"
    ProxyCommand          = "/nix/store/nrzkzg0if8p9ak18070x8mj6clbcvdm7-python3-3.11.9-env/bin/python -S /nix/store/lann7mq6gpyl7q3d3hq6d93jr69pgm0v-google-cloud-sdk-475.0.0/google-cloud-sdk/lib/gcloud.py compute start-iap-tunnel test-vm %p --listen-on-stdin --project=my-project --zone=europe-west4-a --verbosity=warning"
    ProxyUseFdpass        = "no"
  }
}

I'm using a file to pass the SSH options because of this bug in Nix: NixOS/nix#5181. The content of NIX_SSHOPTS is passed to this tokenizer which splits by spaces and doesn't take quoted substrings into account.
This means that something like NIX_SSHOPTS='-o ProxyCommand="my-ssh-command foo bar"' wont't work unless the tokenizer is changed upstream.
The disadvantage of passing options as a file is that it will break the terraform module for people relying on their .ssh/config, unless they copy the ssh options from .ssh/config to the terraform module. I understand this might be too much of a downside and we might want to change the tokenizer instead.

sshOpts+=(-o StrictHostKeyChecking=no)
sshConfigFile="$workDir/ssh_config"
cat >"$sshConfigFile" <<EOF
Host $TARGET_HOST
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. As you have rightfully said, this will be an issue for users configuring private ssh keys and jump hosts in their ssh_config.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a workaround for you specifically, could you not pass in -F in your case instead? terraform also allows to write files, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add support for passing in an ssh config file?

Copy link
Author

@threddast threddast Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a workaround for you specifically, could you not pass in -F in your case instead? terraform also allows to write files, no?

Yes this would work too. I changed the PR so that additional options are passed as a string to NIX_SSHOPTS

Maybe we can add support for passing in an ssh config file?

I think passing flags is more flexible. As you suggested we can pass -F if a config file is needed

Comment on lines -23 to -24
sshOpts+=(-o UserKnownHostsFile=/dev/null)
sshOpts+=(-o StrictHostKeyChecking=no)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the current behavior in nixos-anywhere, if you are not using terraform.

@Mic92
Copy link
Member

Mic92 commented Dec 11, 2024

I think this one will solve your quoting issues btw: NixOS/nix#12020

@eliecharra
Copy link

I'm also interested by this change since it can also cover another usecase 👇🏻

I'm using passwordless sudo with security.pam.sshAgentAuth.enable = true; in order to avoid having NOPASSWD directives in my sudoers. This require me to add the -A flag to ssh to forward my agent.
I can use a workaround by setting a ssh config like below, but It would be simpler to just pass an additional -A flag there 😃

 Host target_host
   ForwardAgent yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants