Allow ssh config hostnames (#49)

I prefer to keep my ssh configurations centralized, and refer to destinations by 'short names' every where else. This patch enables that. It might be a niche usecase so feel free to reject, but figured I'd make it public for anyone else that wants to use it.

Co-authored-by: Jimmy Reichley <jimmyqpublik@gmail.com>
Reviewed-on: #49
This commit is contained in:
Tristan D. 2023-12-11 19:35:56 +01:00 committed by Tristan Druyen
parent 7902ae845e
commit a2565bb9cc
Signed by: tristan
SSH key fingerprint: SHA256:U7y6eMb7CQDaTHv9XoX6/BaQnPqyxxKc+Xnfcefi6rY
2 changed files with 21 additions and 7 deletions

View file

@ -32,6 +32,10 @@ Below is a minimal example:
_module.args.nixinate = { _module.args.nixinate = {
host = "itchy.scratchy.com"; host = "itchy.scratchy.com";
sshUser = "matthew"; sshUser = "matthew";
# Or optionally pass a 'short' hostname that is defined in ssh config
sshConfigHost = "itchy-scratchy";
buildOn = "remote"; # valid args are "local" or "remote" buildOn = "remote"; # valid args are "local" or "remote"
substituteOnTarget = true; # if buildOn is "local" then it will substitute on the target, "-s" substituteOnTarget = true; # if buildOn is "local" then it will substitute on the target, "-s"
hermetic = false; hermetic = false;
@ -91,6 +95,11 @@ Connection to itchy.scratchy.com closed.
A string representing the username a machine to connect to via ssh. A string representing the username a machine to connect to via ssh.
- `sshConfigHost` *`string`*
A string representing an entry in ssh config. If provided, it takes precedence
over `host` and `sshUser`.
- `buildOn` *`"remote"`* or *`"local"`* - `buildOn` *`"remote"`* or *`"local"`*
- `"remote"` - `"remote"`

View file

@ -36,7 +36,9 @@
n = flake.nixosConfigurations.${machine}._module.args.nixinate; n = flake.nixosConfigurations.${machine}._module.args.nixinate;
hermetic = n.hermetic or true; hermetic = n.hermetic or true;
user = n.sshUser or "root"; user = n.sshUser or "root";
host = n.host; host = n.host or "";
sshConfigHost = n.sshConfigHost or "";
userHost = if sshConfigHost != "" then sshConfigHost else if host != "" then "${user}@${host}" else abort "_module.args.nixinate.host or _module.args.nixinate.sshConfigHost must be set";
where = n.buildOn or "remote"; where = n.buildOn or "remote";
remote = if where == "remote" then true else if where == "local" then false else abort "_module.args.nixinate.buildOn is not set to a valid value of 'local' or 'remote'"; remote = if where == "remote" then true else if where == "local" then false else abort "_module.args.nixinate.buildOn is not set to a valid value of 'local' or 'remote'";
substituteOnTarget = n.substituteOnTarget or false; substituteOnTarget = n.substituteOnTarget or false;
@ -47,22 +49,25 @@
'' ''
set -e set -e
echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}" echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}"
'' + (if sshConfigHost != "" then ''
echo "🌐 SSH Config Host: ${sshConfigHost}"
'' else ''
echo "👤 SSH User: ${user}" echo "👤 SSH User: ${user}"
echo "🌐 SSH Host: ${host}" echo "🌐 SSH Host: ${host}"
'' + (if remote then '' '') + (if remote then ''
echo "🚀 Sending flake to ${machine} via nix copy:" echo "🚀 Sending flake to ${machine} via nix copy:"
( set -x; ${nix} ${nixOptions} copy ${flake} --to ssh://${user}@${host} ) ( set -x; ${nix} ${nixOptions} copy ${flake} --to ssh://${userHost} )
'' + (if hermetic then '' '' + (if hermetic then ''
echo "🤞 Activating configuration hermetically on ${machine} via ssh:" echo "🤞 Activating configuration hermetically on ${machine} via ssh:"
( set -x; ${nix} ${nixOptions} copy --derivation ${nixos-rebuild} ${flock} --to ssh://${user}@${host} ) ( set -x; ${nix} ${nixOptions} copy --derivation ${nixos-rebuild} ${flock} --to ssh://${userHost} )
( set -x; ${openssh} -t ${user}@${host} "sudo nix-store --realise ${nixos-rebuild} ${flock} && sudo ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine}" ) ( set -x; ${openssh} -t ${userHost} "sudo nix-store --realise ${nixos-rebuild} ${flock} && sudo ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine}" )
'' else '' '' else ''
echo "🤞 Activating configuration non-hermetically on ${machine} via ssh:" echo "🤞 Activating configuration non-hermetically on ${machine} via ssh:"
( set -x; ${openssh} -t ${user}@${host} "sudo flock -w 60 /dev/shm/nixinate-${machine} nixos-rebuild ${switch} --flake ${flake}#${machine}" ) ( set -x; ${openssh} -t ${userHost} "sudo flock -w 60 /dev/shm/nixinate-${machine} nixos-rebuild ${switch} --flake ${flake}#${machine}" )
'') '')
else '' else ''
echo "🔨 Building system closure locally, copying it to remote store and activating it:" echo "🔨 Building system closure locally, copying it to remote store and activating it:"
( set -x; NIX_SSHOPTS="-t" ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine} --target-host ${user}@${host} --use-remote-sudo ${optionalString substituteOnTarget "-s"} ) ( set -x; NIX_SSHOPTS="-t" ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine} --target-host ${userHost} --use-remote-sudo ${optionalString substituteOnTarget "-s"} )
''); '');
in final.writeScript "deploy-${machine}.sh" script; in final.writeScript "deploy-${machine}.sh" script;