nixos-anywhere-install.nu
· 2.1 KiB · Nu
Eredeti
def nixos-anywhere-install [
ip: string
hostname: string
port: int = 22
temp: string = ""
--rsa
--nix-args: list<string> = []
] {
if (which ssh-keygen | length) == 0 {
error make {msg: "ssh-keygen not found in PATH"}
}
if (which nix | length) == 0 {
error make {msg: "nix not found in PATH"}
}
let temp_dir = if $temp == "" {
let dir = mktemp -d | str trim
print $dir
$dir
} else {
$temp
}
let persist_dir = $temp_dir | path join "persist"
let etc_dir = $persist_dir | path join "etc"
let key_dir = $etc_dir | path join "ssh"
let target = $"root@($ip)"
let flake = $".#($hostname)"
let comment = $"root@($hostname)"
let ed25519_key = $temp_dir | path join "ssh_host_ed25519_key"
let rsa_key = $temp_dir | path join "ssh_host_rsa_key"
mkdir $key_dir
chmod 755 $persist_dir
chmod 755 $etc_dir
chmod 755 $key_dir
^ssh-keygen -q -t ed25519 -N "" -f $ed25519_key -C $comment
cp $ed25519_key ($key_dir | path join "ssh_host_ed25519_key")
cp $"($ed25519_key).pub" ($key_dir | path join "ssh_host_ed25519_key.pub")
chmod 600 ($key_dir | path join "ssh_host_ed25519_key")
chmod 644 ($key_dir | path join "ssh_host_ed25519_key.pub")
if $rsa {
^ssh-keygen -q -t rsa -b 4096 -N "" -f $rsa_key -C $comment
cp $rsa_key ($key_dir | path join "ssh_host_rsa_key")
cp $"($rsa_key).pub" ($key_dir | path join "ssh_host_rsa_key.pub")
chmod 600 ($key_dir | path join "ssh_host_rsa_key")
chmod 644 ($key_dir | path join "ssh_host_rsa_key.pub")
}
let nix_cmd = [
run
...$nix_args
github:nix-community/nixos-anywhere
--
--ssh-port
($port | into string)
--post-kexec-ssh-port
($port | into string)
--flake
$flake
--extra-files
$temp_dir
$target
]
print "Press (x) to start installation"
let key = (input --numchar 1)
if $key == "x" {
^nix ...$nix_cmd
} else {
print "Installation cancelled"
}
}
| 1 | def nixos-anywhere-install [ |
| 2 | ip: string |
| 3 | hostname: string |
| 4 | port: int = 22 |
| 5 | temp: string = "" |
| 6 | --rsa |
| 7 | --nix-args: list<string> = [] |
| 8 | ] { |
| 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 | } |
| 68 | } |
| 69 | |
| 70 |