js0ny revisou este gist 1 week ago. Ir para a revisão
1 file changed, 66 insertions, 59 deletions
nixos-anywhere-install.nu
| @@ -1,62 +1,69 @@ | |||
| 1 | 1 | def nixos-anywhere-install [ | |
| 2 | - | ip: string, | |
| 3 | - | hostname: string, | |
| 4 | - | --rsa, | |
| 5 | - | --nix-args: list<string> = [] | |
| 2 | + | ip: string | |
| 3 | + | hostname: string | |
| 4 | + | port: int = 22 | |
| 5 | + | temp: string = "" | |
| 6 | + | --rsa | |
| 7 | + | --nix-args: list<string> = [] | |
| 6 | 8 | ] { | |
| 7 | - | if ((which ssh-keygen | length) == 0) { | |
| 8 | - | error make { msg: "ssh-keygen not found in PATH" } | |
| 9 | - | } | |
| 10 | - | ||
| 11 | - | if ((which nix | length) == 0) { | |
| 12 | - | error make { msg: "nix not found in PATH" } | |
| 13 | - | } | |
| 14 | - | ||
| 15 | - | let temp = (mktemp -d | str trim) | |
| 16 | - | let persist_dir = ($temp | path join "persist") | |
| 17 | - | let etc_dir = ($persist_dir | path join "etc") | |
| 18 | - | let key_dir = ($etc_dir | path join "ssh") | |
| 19 | - | ||
| 20 | - | let target = $"root@($ip)" | |
| 21 | - | let flake = $".#($hostname)" | |
| 22 | - | let comment = $"root@($hostname)" | |
| 23 | - | ||
| 24 | - | let ed25519_key = ($temp | path join "ssh_host_ed25519_key") | |
| 25 | - | let rsa_key = ($temp | path join "ssh_host_rsa_key") | |
| 26 | - | ||
| 27 | - | mkdir $key_dir | |
| 28 | - | ||
| 29 | - | chmod 755 $persist_dir | |
| 30 | - | chmod 755 $etc_dir | |
| 31 | - | chmod 755 $key_dir | |
| 32 | - | ||
| 33 | - | ^ssh-keygen -q -t ed25519 -N "" -f $ed25519_key -C $comment | |
| 34 | - | ||
| 35 | - | cp $ed25519_key ($key_dir | path join "ssh_host_ed25519_key") | |
| 36 | - | cp $"($ed25519_key).pub" ($key_dir | path join "ssh_host_ed25519_key.pub") | |
| 37 | - | ||
| 38 | - | chmod 600 ($key_dir | path join "ssh_host_ed25519_key") | |
| 39 | - | chmod 644 ($key_dir | path join "ssh_host_ed25519_key.pub") | |
| 40 | - | ||
| 41 | - | if $rsa { | |
| 42 | - | ^ssh-keygen -q -t rsa -b 4096 -N "" -f $rsa_key -C $comment | |
| 43 | - | ||
| 44 | - | cp $rsa_key ($key_dir | path join "ssh_host_rsa_key") | |
| 45 | - | cp $"($rsa_key).pub" ($key_dir | path join "ssh_host_rsa_key.pub") | |
| 46 | - | ||
| 47 | - | chmod 600 ($key_dir | path join "ssh_host_rsa_key") | |
| 48 | - | chmod 644 ($key_dir | path join "ssh_host_rsa_key.pub") | |
| 49 | - | } | |
| 50 | - | ||
| 51 | - | let nix_cmd = [ | |
| 52 | - | run | |
| 53 | - | ...$nix_args | |
| 54 | - | github:nix-community/nixos-anywhere | |
| 55 | - | -- | |
| 56 | - | --flake $flake | |
| 57 | - | --extra-files $temp | |
| 58 | - | $target | |
| 59 | - | ] | |
| 60 | - | ||
| 61 | - | ^nix ...$nix_cmd | |
| 9 | + | if (which ssh-keygen | length) == 0 { | |
| 10 | + | error make {msg: "ssh-keygen not found in PATH"} | |
| 11 | + | } | |
| 12 | + | if (which nix | length) == 0 { | |
| 13 | + | error make {msg: "nix not found in PATH"} | |
| 14 | + | } | |
| 15 | + | let temp_dir = if $temp == "" { | |
| 16 | + | let dir = mktemp -d | str trim | |
| 17 | + | print $dir | |
| 18 | + | $dir | |
| 19 | + | } else { | |
| 20 | + | $temp | |
| 21 | + | } | |
| 22 | + | let persist_dir = $temp_dir | path join "persist" | |
| 23 | + | let etc_dir = $persist_dir | path join "etc" | |
| 24 | + | let key_dir = $etc_dir | path join "ssh" | |
| 25 | + | let target = $"root@($ip)" | |
| 26 | + | let flake = $".#($hostname)" | |
| 27 | + | let comment = $"root@($hostname)" | |
| 28 | + | let ed25519_key = $temp_dir | path join "ssh_host_ed25519_key" | |
| 29 | + | let rsa_key = $temp_dir | path join "ssh_host_rsa_key" | |
| 30 | + | mkdir $key_dir | |
| 31 | + | chmod 755 $persist_dir | |
| 32 | + | chmod 755 $etc_dir | |
| 33 | + | chmod 755 $key_dir | |
| 34 | + | ^ssh-keygen -q -t ed25519 -N "" -f $ed25519_key -C $comment | |
| 35 | + | cp $ed25519_key ($key_dir | path join "ssh_host_ed25519_key") | |
| 36 | + | cp $"($ed25519_key).pub" ($key_dir | path join "ssh_host_ed25519_key.pub") | |
| 37 | + | chmod 600 ($key_dir | path join "ssh_host_ed25519_key") | |
| 38 | + | chmod 644 ($key_dir | path join "ssh_host_ed25519_key.pub") | |
| 39 | + | if $rsa { | |
| 40 | + | ^ssh-keygen -q -t rsa -b 4096 -N "" -f $rsa_key -C $comment | |
| 41 | + | cp $rsa_key ($key_dir | path join "ssh_host_rsa_key") | |
| 42 | + | cp $"($rsa_key).pub" ($key_dir | path join "ssh_host_rsa_key.pub") | |
| 43 | + | chmod 600 ($key_dir | path join "ssh_host_rsa_key") | |
| 44 | + | chmod 644 ($key_dir | path join "ssh_host_rsa_key.pub") | |
| 45 | + | } | |
| 46 | + | let nix_cmd = [ | |
| 47 | + | run | |
| 48 | + | ...$nix_args | |
| 49 | + | github:nix-community/nixos-anywhere | |
| 50 | + | -- | |
| 51 | + | --ssh-port | |
| 52 | + | ($port | into string) | |
| 53 | + | --post-kexec-ssh-port | |
| 54 | + | ($port | into string) | |
| 55 | + | --flake | |
| 56 | + | $flake | |
| 57 | + | --extra-files | |
| 58 | + | $temp_dir | |
| 59 | + | $target | |
| 60 | + | ] | |
| 61 | + | print "Press (x) to start installation" | |
| 62 | + | let key = (input --numchar 1) | |
| 63 | + | if $key == "x" { | |
| 64 | + | ^nix ...$nix_cmd | |
| 65 | + | } else { | |
| 66 | + | print "Installation cancelled" | |
| 67 | + | } | |
| 62 | 68 | } | |
| 69 | + | ||
js0ny revisou este gist 3 months ago. Ir para a revisão
1 file changed, 62 insertions
nixos-anywhere-install.nu(arquivo criado)
| @@ -0,0 +1,62 @@ | |||
| 1 | + | def nixos-anywhere-install [ | |
| 2 | + | ip: string, | |
| 3 | + | hostname: string, | |
| 4 | + | --rsa, | |
| 5 | + | --nix-args: list<string> = [] | |
| 6 | + | ] { | |
| 7 | + | if ((which ssh-keygen | length) == 0) { | |
| 8 | + | error make { msg: "ssh-keygen not found in PATH" } | |
| 9 | + | } | |
| 10 | + | ||
| 11 | + | if ((which nix | length) == 0) { | |
| 12 | + | error make { msg: "nix not found in PATH" } | |
| 13 | + | } | |
| 14 | + | ||
| 15 | + | let temp = (mktemp -d | str trim) | |
| 16 | + | let persist_dir = ($temp | path join "persist") | |
| 17 | + | let etc_dir = ($persist_dir | path join "etc") | |
| 18 | + | let key_dir = ($etc_dir | path join "ssh") | |
| 19 | + | ||
| 20 | + | let target = $"root@($ip)" | |
| 21 | + | let flake = $".#($hostname)" | |
| 22 | + | let comment = $"root@($hostname)" | |
| 23 | + | ||
| 24 | + | let ed25519_key = ($temp | path join "ssh_host_ed25519_key") | |
| 25 | + | let rsa_key = ($temp | path join "ssh_host_rsa_key") | |
| 26 | + | ||
| 27 | + | mkdir $key_dir | |
| 28 | + | ||
| 29 | + | chmod 755 $persist_dir | |
| 30 | + | chmod 755 $etc_dir | |
| 31 | + | chmod 755 $key_dir | |
| 32 | + | ||
| 33 | + | ^ssh-keygen -q -t ed25519 -N "" -f $ed25519_key -C $comment | |
| 34 | + | ||
| 35 | + | cp $ed25519_key ($key_dir | path join "ssh_host_ed25519_key") | |
| 36 | + | cp $"($ed25519_key).pub" ($key_dir | path join "ssh_host_ed25519_key.pub") | |
| 37 | + | ||
| 38 | + | chmod 600 ($key_dir | path join "ssh_host_ed25519_key") | |
| 39 | + | chmod 644 ($key_dir | path join "ssh_host_ed25519_key.pub") | |
| 40 | + | ||
| 41 | + | if $rsa { | |
| 42 | + | ^ssh-keygen -q -t rsa -b 4096 -N "" -f $rsa_key -C $comment | |
| 43 | + | ||
| 44 | + | cp $rsa_key ($key_dir | path join "ssh_host_rsa_key") | |
| 45 | + | cp $"($rsa_key).pub" ($key_dir | path join "ssh_host_rsa_key.pub") | |
| 46 | + | ||
| 47 | + | chmod 600 ($key_dir | path join "ssh_host_rsa_key") | |
| 48 | + | chmod 644 ($key_dir | path join "ssh_host_rsa_key.pub") | |
| 49 | + | } | |
| 50 | + | ||
| 51 | + | let nix_cmd = [ | |
| 52 | + | run | |
| 53 | + | ...$nix_args | |
| 54 | + | github:nix-community/nixos-anywhere | |
| 55 | + | -- | |
| 56 | + | --flake $flake | |
| 57 | + | --extra-files $temp | |
| 58 | + | $target | |
| 59 | + | ] | |
| 60 | + | ||
| 61 | + | ^nix ...$nix_cmd | |
| 62 | + | } | |