aboutsummaryrefslogtreecommitdiff
path: root/pkgs/yo/nixos.rb
diff options
context:
space:
mode:
authorCollin Williams <96917990+bluedragon1221@users.noreply.github.com>2026-03-03 16:45:26 -0600
committerCollin Williams <96917990+bluedragon1221@users.noreply.github.com>2026-03-03 16:45:26 -0600
commitc29a14135736c2516bb5d951e6e97dc8143598db (patch)
tree925628abfe934e3ea2cfd4a70357f8f9b5acb474 /pkgs/yo/nixos.rb
parentce2cc260710f801c10302847796b931f7ed19709 (diff)
changes
Diffstat (limited to 'pkgs/yo/nixos.rb')
-rw-r--r--pkgs/yo/nixos.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/pkgs/yo/nixos.rb b/pkgs/yo/nixos.rb
new file mode 100644
index 0000000..73a1872
--- /dev/null
+++ b/pkgs/yo/nixos.rb
@@ -0,0 +1,37 @@
+module Nix
+ def build_configuration(flake_path:, hostname:)
+ flake_target = "#{flake_path}#nixosConfigurations.'#{hostname}'.config.system.build.toplevel"
+ `nix build --no-link --print-out-paths #{flake_target}`.chomp.tap do
+ raise "Build command failed" unless $?.success?
+ end
+ end
+
+ def switch_to_configuration(store_path:, verb:, sudo_cmd: "sudo")
+ create_gen_cmd = "nix build --no-link --profile /nix/var/nix/profiles/system #{store_path}"
+ switch_cmd = "#{store_path}/bin/switch-to-configuration #{verb}"
+ system("#{sudo_cmd} bash -c '#{create_gen_cmd}; #{switch_cmd}'") or raise "Switch command failed"
+ end
+
+ def switch_to_configuration_remote(store_path:, ssh_host:, use_magic_rollback: false)
+ system("nix copy --to ssh://#{ssh_host} #{store_path}") or raise "Copy closure command failed"
+
+ create_gen_cmd = "nix build --no-link --profile /nix/var/nix/profiles/system #{store_path}"
+ switch_cmd = "#{store_path}/bin/switch-to-configuration switch"
+
+ if use_magic_rollback
+ test_cmd = "#{store_path}/bin/switch-to-configuration test"
+ system(%[ssh #{ssh_host} 'bash -c "#{test_cmd}; shutdown -r +5"']) or raise "Remote test command failed"
+
+ puts <<~MSG
+ Remote deployment complete.
+ If everything works, press ENTER to confirm and cancel rollback.
+ If SSH dies, it will restart (reversing changes) automatically in 5 minutes
+ MSG
+ $stdin.gets
+
+ system(%[ssh #{ssh_host} 'bash -c "shutdown -c; #{create_gen_cmd}; #{switch_cmd}"']) or raise "Remote switch command failed"
+ else
+ system(%[ssh #{ssh_host} 'bash -c "#{create_gen_cmd}; #{switch_cmd}"']) or raise "Remote switch command failed"
+ end
+ end
+end