Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.#activate: Simplify CLI by obviating the "host" sub-command #59

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions activate/activate.nu
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
use std log
use std assert

use nixos-flake.nu getData # This module is generated in Nix

let CURRENT_HOSTNAME = (hostname | str trim)

# Activate system configuration of local machine
# Parse "[srid@]example" into { user: "srid", host: "example" }
#
# To activate a remote machine, use run with subcommands: `host <hostname>`
def main [] {
main host ($CURRENT_HOSTNAME)
# localhost hosts are ignored (null'ified)
def parseFlakeOutputRef [ spec: string ] {
let parts = $spec | split row "@"
let handleLocalhost = {|h| if $h == "localhost" { null } else { $h } }
if ($parts | length) == 1 {
{ user: null host: (do $handleLocalhost $parts.0) }
} else {
{ user: $parts.0 host: (do $handleLocalhost $parts.1) }
}
}

# Activate system configuration of the given host
def 'main host' [
host: string # Hostname to activate (must match flake.nix name)
# Activate system configuration of the given host
#
# The hostname should match the name of the corresponding nixosConfigurations or
# darwinConfigurations attrkey. "localhost" is an exception, which will use the
# current host.
def main [
ref: string = "localhost" # Hostname to activate
] {
let spec = parseFlakeOutputRef $ref
if $spec.user != null {
log error $"Cannot activate home environments yet; use .#activate-home instead"
exit 1
}
let host = if ($spec.host | is-empty) { $CURRENT_HOSTNAME } else { $spec.host }
let data = getData
if $host not-in $data.nixos-flake-configs {
log error $"Host '($host)' not found in flake. Available hosts=($data.nixos-flake-configs | columns)"
Expand Down Expand Up @@ -57,14 +75,7 @@ def 'main host' [
}

# 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 ($host)'
ssh -t $hostData.sshTarget nix --extra-experimental-features '"nix-command flakes"' run ...$hostData.outputs.nixArgs $"($data.cleanFlake)#activate host ($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)"
}
}

# TODO: Implement this, resolving https://github.com/srid/nixos-flake/issues/18
def 'main home' [] {
log error "Home activation not yet supported; use .#activate-home instead"
exit 1
}

}
2 changes: 1 addition & 1 deletion doc/activate.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Add the following to your configuration -- `nixosConfigurations.myhost` or `darw
Then, you will be able to run the following to deploy to `myhost` from any machine:

```sh
nix run .#activate host myhost
nix run .#activate myhost
```

### Non-goals
Expand Down