From 3e03999b9e5574f5139339f9baba21919e59468f Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Fri, 18 Sep 2026 14:16:32 -0400 Subject: [PATCH] speak-selection v2 + hermenux home --- bin/lib/cmds.nu | 73 ++++++++-------- bin/lib/profile.nu | 2 +- bin/lib/types.nu | 12 ++- configs/extra.nu | 23 +++-- configs/fwbook/hypr/speak-selection.nu | 111 +++++++++++++++---------- hosts/scripts/fwbook/functions.nu | 15 ++-- 6 files changed, 132 insertions(+), 104 deletions(-) diff --git a/bin/lib/cmds.nu b/bin/lib/cmds.nu index a2d0798..33d3faf 100644 --- a/bin/lib/cmds.nu +++ b/bin/lib/cmds.nu @@ -7,7 +7,7 @@ export def skyg [] { print $REPO_ROOT let subcmds = (help commands | where name starts-with "skyg " | - each { |c| $c.name | split row " " | get 1 } | + each {|c| $c.name | split row " " | get 1 } | uniq | str join ', ') print $"Subcommands: ($subcmds)" print "Run: skyg --help" @@ -22,7 +22,7 @@ export def "skyg profiles" [only_remote: bool = false] { } export def "skyg check" [] { - nix flake check --no-build + nix flake check --no-build } # Validate that all versioned flake inputs use the same NixOS release @@ -40,7 +40,7 @@ export def "skyg check-flake-versions" [] { }) let versions = ($url_lines - | each { |row| $row.item | parse --regex '.*release-(?P\d+\.\d+).*' | get version } + | each {|row| $row.item | parse --regex '.*release-(?P\d+\.\d+).*' | get version } | flatten | uniq) @@ -53,13 +53,13 @@ export def "skyg check-flake-versions" [] { print "" for version in $versions { print $" release-($version):" - $url_lines | where { |row| $row.item | str contains $"release-($version)" } | each { |row| + $url_lines | where {|row| $row.item | str contains $"release-($version)" } | each { |row| print $" line ($row.index + 1): ($row.item | str trim)" } } print "" print "All versioned inputs (home-manager, stylix, etc.) must use the same NixOS release." - error make { msg: "Mismatched NixOS release versions" } + error make {msg: "Mismatched NixOS release versions"} } # rollback configuration @@ -80,12 +80,8 @@ export def "skyg update" [input_name: string = ""] { git diff flake.lock | print } - # Build local os -export def "skyg os" [ - cmd: string@remote-cmds, - --build-host: string@remote_targets = "", -] { +export def "skyg os" [cmd: string, --build-host: string = ""] { cd $REPO_ROOT mut runcmd = $"nh os ($cmd)" if $build_host != "" { @@ -134,7 +130,7 @@ export def "skyg remote install" [ } let disko_file = $"hosts/($profile).disko.nix" if not ($disko_file | path exists) { - error make { msg: $"Missing disko config: ($disko_file). Refusing to install ($profile) without a disko layout." } + error make {msg: $"Missing disko config: ($disko_file). Refusing to install ($profile) without a disko layout."} } print "" @@ -145,8 +141,8 @@ export def "skyg remote install" [ let disks = ( open $disko_file | lines - | where { |l| ($l | str contains "device") and ($l | str contains "=") } - | each { |l| $l | str trim } + | where {|l| ($l | str contains "device") and ($l | str contains "=") } + | each {|l| $l | str trim } ) for line in $disks { print $" ($line)" @@ -165,7 +161,7 @@ export def "skyg remote install" [ let installer_fs = ["overlay" "tmpfs" "squashfs"] if not ($root_fstype in $installer_fs) { if not $force_wipe { - error make { msg: $"($target_host) already boots from disk (root fstype ($root_fstype)). Refusing to wipe. Use `skyg remote switch ($target)` to update, or pass --force-wipe to reinstall." } + error make {msg: $"($target_host) already boots from disk (root fstype ($root_fstype)). Refusing to wipe. Use `skyg remote switch ($target)` to update, or pass --force-wipe to reinstall."} } print $"(ansi yellow)--force-wipe set: proceeding against a live disk OS.(ansi reset)" } else { @@ -173,15 +169,15 @@ export def "skyg remote install" [ } } - let confirm = (input $"Type ($target) to continue: " | str trim) + let confirm = input $"Type ($target) to continue: " | str trim if $confirm != $target { - error make { msg: $"Aborted: typed '($confirm)', expected '($target)'." } + error make {msg: $"Aborted: typed '($confirm)', expected '($target)'."} } $"Installing [(ansi green_bold)($profile)(ansi reset)] on ($target_host) via nixos-anywhere, using ($disko_file)" | print let runcmd = $"nix run github:nix-community/nixos-anywhere -- --flake .#($profile) ($target_host)" $runcmd | print - let bash_path = (which bash | get 0.path) + let bash_path = which bash | get 0.path with-env { SHELL: $bash_path } { bash -c $runcmd } @@ -200,7 +196,7 @@ export def "skyg remote boot-all" [ let report_file = ".tmp/boot-report.txt" # Discover remote targets from the flake (same logic as skyg remote) - let all_hosts = (remote_targets | where { |h| $h != $current_host }) + let all_hosts = remote_targets | where {|h| $h != $current_host } print $"Found ($all_hosts | length) candidate hosts excluding ($current_host)" @@ -257,7 +253,7 @@ Starting parallel boots (concurrency: 2)..." } # Write report - let timestamp = (date now | format date "%Y-%m-%d %H:%M:%S") + let timestamp = date now | format date "%Y-%m-%d %H:%M:%S" let report = $"Boot Report - ($timestamp) Total hosts considered: ($all_hosts | length) @@ -284,7 +280,7 @@ export def "skyg remote status" [] { let current_host = "fwbook" let report_file = ".tmp/status-report.txt" - let all_hosts = (remote_targets | where { |h| $h != $current_host }) + let all_hosts = remote_targets | where {|h| $h != $current_host } print $"Checking status for ($all_hosts | length) remotes..." let results = ($all_hosts | each { |host| @@ -302,8 +298,8 @@ export def "skyg remote status" [] { } let info = (try { - let booted = (bash -c $"ssh ($target) 'readlink /run/booted-system' 2>/dev/null" | str trim) - let current = (bash -c $"ssh ($target) 'readlink /run/current-system' 2>/dev/null" | str trim) + let booted = bash -c $"ssh ($target) 'readlink /run/booted-system' 2>/dev/null" | str trim + let current = bash -c $"ssh ($target) 'readlink /run/current-system' 2>/dev/null" | str trim let reboot_pending = ($booted != $current) # Compare what the local flake currently evaluates to vs remote's active system @@ -331,7 +327,7 @@ export def "skyg remote status" [] { $info }) - let timestamp = (date now | format date "%Y-%m-%d %H:%M:%S") + let timestamp = date now | format date "%Y-%m-%d %H:%M:%S" let lines = ($results | each { |r| if $r.status == "offline" { $"- ($r.host): (ansi yellow)OFFLINE(ansi reset)" @@ -370,9 +366,7 @@ export def "skyg openwrt" [ } # Build a bootable image: ISO, or Pi SD card image (fully-baked, no on-device rebuild) -export def "skyg build-image" [ - target: string@image-targets -] { +export def "skyg build-image" [target: string] { cd $REPO_ROOT let spec = (match $target { "iso" => { config: "iso", out: "iso", attr: "isoImage" } @@ -380,17 +374,17 @@ export def "skyg build-image" [ "pi2" => { config: "hl-pi2", out: "sd-image", attr: "sdImage" } _ => { error make { msg: $"Unknown image target '($target)'. Expected: iso, pi1, pi2" } } }) - let imageBuildTime = date now | format date "%Y.%m.%d_%H_%M_%S"; - let rootOutputFolder = $REPO_ROOT | path join "build" $spec.config; - let outPutFolder = $rootOutputFolder | path join $imageBuildTime; - rm -rf $rootOutputFolder; - mkdir $rootOutputFolder; + let imageBuildTime = date now | format date "%Y.%m.%d_%H_%M_%S" + let rootOutputFolder = $REPO_ROOT | path join "build" $spec.config + let outPutFolder = $rootOutputFolder | path join $imageBuildTime + rm -rf $rootOutputFolder + mkdir $rootOutputFolder nix build $"#nixosConfigurations.($spec.config).config.system.build.($spec.attr)" -o $outPutFolder printf $outPutFolder } # Home Manager operations -export def "skyg hm" [action: string@hm-actions, profile: string = "ari"] { +export def "skyg hm" [action: string, profile: string = "ari"] { cd $REPO_ROOT $"Running home-manager ($action) for profile ($profile)" | print home-manager $action --flake $".#($profile)" @@ -402,11 +396,11 @@ export def "skyg secrets" [] { } # Decrypt a secret from secrets/ folder -export def "skyg decrypt" [secret: string@secret-names] { +export def "skyg decrypt" [secret: string] { cd $REPO_ROOT let src = $"secrets/($secret).age" if not ($src | path exists) { - error make { msg: $"Secret not found: ($src)" } + error make {msg: $"Secret not found: ($src)"} } mkdir .tmp let dest = $".tmp/unencrypted-($secret)" @@ -435,8 +429,13 @@ export def "skyg encrypt" [ vi $src if $yaml { + # `complete` captures stdout/stderr/exit_code; `try/catch` guards a missing yq - let lint = (try { yq e '.' $src | complete } catch { null }) + let lint = ( + try { + yq e '.' $src | complete + } catch { null } + ) if $lint == null { print $"❌ Could not run yq — is it on PATH? Add 'yq' to the devShell in flake.nix." return @@ -451,13 +450,13 @@ export def "skyg encrypt" [ } let dest = $"secrets/($secret).age" - cat $src | EDITOR="cp /dev/stdin" agenix -e $dest + cat $src | agenix -e $dest print $"Encrypted ($dest)" return $dest } # Compare unencrypted local version with encrypted version of a secret -export def "skyg compare-secret" [secret: string@secret-names] { +export def "skyg compare-secret" [secret: string] { cd $REPO_ROOT let unencrypted = $".tmp/unencrypted-($secret)" let encrypted = $"secrets/($secret).age" diff --git a/bin/lib/profile.nu b/bin/lib/profile.nu index 1893bbd..1539ebf 100644 --- a/bin/lib/profile.nu +++ b/bin/lib/profile.nu @@ -3,7 +3,7 @@ const REPO_ROOT = path self | path dirname | path dirname | path dirname export def valid_profiles [] { cd $REPO_ROOT nix eval .#nixosConfigurations --apply 'builtins.attrNames' --json err> /dev/null - | from json + | from json } export def valid_remote_profiles [] { diff --git a/bin/lib/types.nu b/bin/lib/types.nu index b1fcdc5..cbd05f3 100644 --- a/bin/lib/types.nu +++ b/bin/lib/types.nu @@ -5,15 +5,21 @@ export def hm-actions [] { } export def remote-cmds [] { - ["test", "dry-activate", "run", "switch", "build", "boot"] + [ + "test" + "dry-activate" + "run" + "switch" + "build" + "boot" + ] } export def image-targets [] { ["iso", "pi1", "pi2"] } - export def secret-names [] { cd $REPO_ROOT - glob secrets/*.age | each { |f| $f | path basename | str replace ".age" "" } + glob secrets/*.age | each {|f| $f | path basename | str replace ".age" "" } } diff --git a/configs/extra.nu b/configs/extra.nu index 8215368..1fd9b1f 100644 --- a/configs/extra.nu +++ b/configs/extra.nu @@ -1,14 +1,11 @@ -def rocm-monitor [ - cacheFile: string = "/tmp/rocm-monitor.jsonl", - timeout: duration = 1sec -] { - if ($cacheFile | path exists) { - rm $cacheFile - } - loop { - clear - rocm-smi --showmemuse --showuse --json | save --append $cacheFile - tail -n1 $cacheFile | from json --objects | flatten | print - sleep $timeout - } +def rocm-monitor [cacheFile: string = "/tmp/rocm-monitor.jsonl", timeout: duration = 1sec] { + if ($cacheFile | path exists) { + rm $cacheFile + } + loop { + clear + rocm-smi --showmemuse --showuse --json | save --append $cacheFile + tail -n1 $cacheFile | from json --objects | flatten | print + sleep $timeout + } } diff --git a/configs/fwbook/hypr/speak-selection.nu b/configs/fwbook/hypr/speak-selection.nu index 3b14323..2ecae23 100755 --- a/configs/fwbook/hypr/speak-selection.nu +++ b/configs/fwbook/hypr/speak-selection.nu @@ -16,7 +16,7 @@ def kill-tree [pid: int] { let kids = (^pgrep -P $pid | complete) if $kids.exit_code == 0 { for kid in ($kids.stdout | lines) { - let kid = ($kid | str trim) + let kid = $kid | str trim if not ($kid | is-empty) { kill-tree ($kid | into int) } @@ -26,8 +26,12 @@ def kill-tree [pid: int] { } def terminate-last [file: string] { - if ($file | path exists) { - let old = (try { open --raw $file | str trim | into int } catch { null }) + if ($file | path exists) { + let old = ( + try { + open --raw $file | str trim | into int + } catch { null } + ) rm --force $file if $old != null and (pid-alive $old) { kill-tree $old @@ -40,9 +44,9 @@ def terminate-last [file: string] { # Show a failure notification. Works with `complete` results # ({exit_code, stderr, ...}) and `catch` error records. def notify-error [context: string, err: record] { - let exit = ($err | get -o exit_code) + let exit = $err | get -o exit_code let detail = if $exit != null { - let stderr = ($err | get -o stderr | default "") + let stderr = $err | get -o stderr | default "" if ($stderr | is-empty) { $"exit ($exit)" } else { $"exit ($exit): ($stderr | str trim | lines | last)" } } else { $err.msg @@ -53,62 +57,85 @@ def notify-error [context: string, err: record] { } } -def main [--cache] { - let file = (pidfile) - if (terminate-last $file) { - notify-send -i dialog-information "Piper" "Stopped" - return - } - +def get-text [] { let paste = (^wl-paste --primary | complete) if $paste.exit_code != 0 { - notify-send -i dialog-warning "Piper" "No text, make sure to copy something to clipboard" + notify-send -i dialog-warning "Piper" "No text, make sure to copy something to clipboard" return } - let text = ($paste.stdout - | str replace --all --regex '[\n\t]' ' ' - | str replace --all --regex "[^0-9A-Za-z .,!?'-]" '' - | str replace --all --regex ' +' ' ' - | str trim) + return ($paste.stdout + | str replace --all --regex '[\n\t]' ' ' + | str replace --all --regex "[^0-9A-Za-z .,!?'-]" '' + | str replace --all --regex ' +' ' ' + | str trim) +} - if ($text | is-empty) { - notify-send -i dialog-warning "Piper" "No text, make sure to copy something to clipboard" - return +# returns true if failed +def run-tts [text: string, cache_file: string] { + ^mkdir -p $piperSoundCacheFolder + let res = ( + $text + | ^piper -m $modelName --data-dir $piperFolder --output-file $cache_file + | complete + ) + if $res.exit_code != 0 { + notify-error "piper" $res + rm --force $cache_file + return true } + return false +} - # cache key includes the model so different voices don't collide - let text_sha = ($"($modelName)|($text)" | hash sha256) - let cache_file = $piperSoundCacheFolder | path join $"($text_sha).wav" - - $"($nu.pid)" | save --force $file - +def run-tts-pipeline [use_cache: bool, cache_file: string, text: string] { try { notify-send -i dialog-information $"Piper `($modelName)`" $"Text: ($text)" # skip piper when the audio is already cached - if not ($cache and ($cache_file | path exists)) { - ^mkdir -p $piperSoundCacheFolder - let res = ($text | ^piper -m $modelName --data-dir $piperFolder --output-file $cache_file | complete) - if $res.exit_code != 0 { - notify-error "piper" $res - rm --force $cache_file + if not ($use_cache and ($cache_file | path exists)) { + if (run-tts $text $cache_file) { return } } - - let res = (^ffplay -nodisp -autoexit $cache_file | complete) - if $res.exit_code != 0 { - notify-error "ffplay" $res + if (play-file $cache_file) { return } - } catch { |err| - notify-error "unexpected" $err - } - rm --force $file + } catch {|err| notify-error "unexpected" $err } # no caching → don't leave the audio file behind - if not $cache { + if not $use_cache { rm --force $cache_file } } + +# returns true if failed +def play-file [cache_file: string] { + let res = (^ffplay -nodisp -autoexit $cache_file | complete) + if $res.exit_code != 0 { + notify-error "ffplay" $res + return true + } + return false +} + +def main [--cache] { + let file = (pidfile) + if (terminate-last $file) { + notify-send -i dialog-information "Piper" "Stopped" + return + } + let text = get-text + + if ($text | is-empty) { + notify-send -i dialog-warning "Piper" "No text, make sure to copy something to clipboard" + return + } + + # cache key includes the model so different voices don't collide + let text_sha = $"($modelName)|($text)" | hash sha256 + let cache_file = $piperSoundCacheFolder | path join $"($text_sha).wav" + + $"($nu.pid)" | save --force $file + run-tts-pipeline $cache $cache_file $text + rm --force $file +} diff --git a/hosts/scripts/fwbook/functions.nu b/hosts/scripts/fwbook/functions.nu index 9c760a6..75007f3 100644 --- a/hosts/scripts/fwbook/functions.nu +++ b/hosts/scripts/fwbook/functions.nu @@ -1,18 +1,17 @@ - def k8s-namespaces [] { kubectl get namespaces -o jsonpath='{.items}' | from json | select metadata.name | rename value } -def k8s-secrets [namespace: string@k8s-namespaces = ""] { +def k8s-secrets [namespace: string@"k8s-namespaces" = ""] { if not ($namespace | is-empty) { kubectl -n $namespace get secrets -o jsonpath='{.items}' | from json | select metadata.name | rename value } else { kubectl get secrets -o jsonpath='{.items}' | from json | select metadata.name | rename value } } -def decode-secret [name: string@k8s-secrets] { - kubectl get secret $name -o jsonpath='{.data}' | - from json | - transpose key value | - update value { |r| $r.value | decode base64 | decode} -} \ No newline at end of file +def decode-secret [name: string@"k8s-secrets"] { + kubectl get secret $name -o jsonpath='{.data}' + | from json + | transpose key value + | update value {|r| $r.value | decode base64 | decode} +}