SSH on newly installed NixOS VM

After installing NixOS on a VM, you can configure SSH like this:

On /etc/nixos/configuration.nix:

import [
  ./ssh.nix
  # ...
];
# Ensure flake is enabled

and /etc/nixos/ssh.nix:

{ ... }: {
  services.openssh = {
    enable = true;
    settings = {
      AllowUsers = [ "your-user" ];
      # Add
      PasswordAuthentication = true;
      PermitRootLogin = "yes";
    };
  };
}

then apply the change:

sudo nixos-rebuild switch --flake .

Then from home, you can log in as your user, and you should be able to SSH as your user.

It’s quite useful, especially if you want to copy/paste some stuff from host to VM.