Skip to content

Commit

Permalink
chore: .#activate: Simplify CLI by obviating the "host" sub-command (
Browse files Browse the repository at this point in the history
  • Loading branch information
srid authored Jun 26, 2024
1 parent dfd0be2 commit 8cefa1e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
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

0 comments on commit 8cefa1e

Please sign in to comment.