Skip to content

Commit

Permalink
refactor: split further
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Jun 27, 2024
1 parent c79c3f6 commit 5be076d
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions activate/activate.nu
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ use nixos-flake.nu getData # This module is generated in Nix
let CURRENT_HOSTNAME = (hostname | str trim)
let data = getData

# Get all the data associated with a host
#
# Presently, this only deals with nixosConfigurations and darwinConfigurations.
# But we should also incorporate home-manager configurations.
def get_host_data [ host: string ] {
if $host not-in $data.nixos-flake-configs {
log error $"Host '($host)' not found in flake. Available hosts=($data.nixos-flake-configs | columns)"
exit 1
}
$data.nixos-flake-configs
| get $host
| insert "host" $host
| insert "flake" $"($data.cleanFlake)#($host)"
}

Expand Down Expand Up @@ -43,51 +48,57 @@ def main [
} else {
let host = if ($spec.host | is-empty) { $CURRENT_HOSTNAME } else { $spec.host }
let hostData = get_host_data $host
activate_system $host $hostData
activate_system $hostData
}
}

# TODO: https://github.com/srid/nixos-flake/issues/18
def activate_home [ user: string ] {
log error $"Cannot activate home environments yet; use .#activate-home instead"
exit 1
}

def activate_system [ host: string hostData: record ] {
log info $"(ansi grey)currentSystem=($data.system) currentHost=(ansi green_bold)($CURRENT_HOSTNAME)(ansi grey) targetHost=(ansi green_reverse)($host)(ansi reset)(ansi grey) hostData=($hostData)(ansi reset)"

let runtime = {
local: ($CURRENT_HOSTNAME == $host)
darwin: ($hostData.outputs.system in ["aarch64-darwin" "x86_64-darwin"])
}
def activate_system [ hostData: record ] {
log info $"(ansi grey)currentSystem=($data.system) currentHost=(ansi green_bold)($CURRENT_HOSTNAME)(ansi grey) targetHost=(ansi green_reverse)($hostData.host)(ansi reset)(ansi grey) hostData=($hostData)(ansi reset)"

if $runtime.local {
if ($CURRENT_HOSTNAME == $hostData.host) {
# Since the user asked to activate current host, do so.
log info $"Activating (ansi purple)locally(ansi reset)"
if $runtime.darwin {
log info $"(ansi blue_bold)>>>(ansi reset) darwin-rebuild switch --flake ($hostData.flake) ($hostData.outputs.nixArgs | str join)"
darwin-rebuild switch --flake $hostData.flake ...$hostData.outputs.nixArgs
} else {
log info $"(ansi blue_bold)>>>(ansi reset) nixos-rebuild switch --flake ($hostData.flake) ($hostData.outputs.nixArgs | str join) --use-remote-sudo "
nixos-rebuild switch --flake $hostData.flake ...$hostData.outputs.nixArgs --use-remote-sudo
}
activate_system_local $hostData
} else {
# Remote activation request, so copy the flake and the necessary inputs
# and then activate over SSH.
if $hostData.sshTarget == null {
log error $"sshTarget not found in host data for ($host). Add `nixos-flake.sshTarget = \"user@hostname\";` to your configuration."
log error $"sshTarget not found in host data for ($hostData.host). Add `nixos-flake.sshTarget = \"user@hostname\";` to your configuration."
exit 1
}
log info $"Activating (ansi purple_reverse)remotely(ansi reset) on ($hostData.sshTarget)"
nix_copy $data.cleanFlake $"ssh-ng://($hostData.sshTarget)"
activate_system_remote_ssh $hostData
}
}

$hostData.outputs.overrideInputs | transpose key value | each { |input|
nix_copy $input.value $"ssh-ng://($hostData.sshTarget)"
}
def activate_system_local [ hostData: record ] {
log info $"Activating (ansi purple)locally(ansi reset)"
let darwin = $hostData.outputs.system in ["aarch64-darwin" "x86_64-darwin"]
if $darwin {
log info $"(ansi blue_bold)>>>(ansi reset) darwin-rebuild switch --flake ($hostData.flake) ($hostData.outputs.nixArgs | str join)"
darwin-rebuild switch --flake $hostData.flake ...$hostData.outputs.nixArgs
} else {
log info $"(ansi blue_bold)>>>(ansi reset) nixos-rebuild switch --flake ($hostData.flake) ($hostData.outputs.nixArgs | str join) --use-remote-sudo "
nixos-rebuild switch --flake $hostData.flake ...$hostData.outputs.nixArgs --use-remote-sudo
}
}

def activate_system_remote_ssh [ hostData: record ] {
log info $"Activating (ansi purple_reverse)remotely(ansi reset) on ($hostData.sshTarget)"

# We re-run this script, but on the remote host.
log info $'(ansi blue_bold)>>>(ansi reset) ssh -t ($hostData.sshTarget) nix --extra-experimental-features '"nix-command flakes"' run ($hostData.outputs.nixArgs | str join) $"($data.cleanFlake)#activate" ($host)'
ssh -t $hostData.sshTarget nix --extra-experimental-features '"nix-command flakes"' run ...$hostData.outputs.nixArgs $"($data.cleanFlake)#activate ($host)"
# Copy the flake and the necessary inputs to the remote host.
nix_copy $data.cleanFlake $"ssh-ng://($hostData.sshTarget)"
$hostData.outputs.overrideInputs | transpose key value | each { |input|
nix_copy $input.value $"ssh-ng://($hostData.sshTarget)"
}

# We re-run this activation script, but on the remote host (where it will invoke activate_system_local).
log info $'(ansi blue_bold)>>>(ansi reset) ssh -t ($hostData.sshTarget) nix --extra-experimental-features '"nix-command flakes"' run ($hostData.outputs.nixArgs | str join) $"($data.cleanFlake)#activate" ($hostData.host)'
ssh -t $hostData.sshTarget nix --extra-experimental-features '"nix-command flakes"' run ...$hostData.outputs.nixArgs $"($data.cleanFlake)#activate ($hostData.host)"
}

def nix_copy [ src: string dst: string ] {
Expand Down

0 comments on commit 5be076d

Please sign in to comment.