From bb373ac40b81279d4fb43e0f02e998364bc2d952 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Mon, 30 Jun 2025 14:30:15 -0400 Subject: [PATCH] Initial pass on updater script --- flake.lock | 6 ++-- scripts/update | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 scripts/update diff --git a/flake.lock b/flake.lock index ff0319b..6e31a38 100644 --- a/flake.lock +++ b/flake.lock @@ -242,11 +242,11 @@ }, "nixpkgs_3": { "locked": { - "lastModified": 1750969886, - "narHash": "sha256-zW/OFnotiz/ndPFdebpo3X0CrbVNf22n4DjN2vxlb58=", + "lastModified": 1751211869, + "narHash": "sha256-1Cu92i1KSPbhPCKxoiVG5qnoRiKTgR5CcGSRyLpOd7Y=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "a676066377a2fe7457369dd37c31fd2263b662f4", + "rev": "b43c397f6c213918d6cfe6e3550abfe79b5d1c51", "type": "github" }, "original": { diff --git a/scripts/update b/scripts/update new file mode 100644 index 0000000..3db8216 --- /dev/null +++ b/scripts/update @@ -0,0 +1,80 @@ +#!/usr/bin/env raku + +# Find the flake path and make sure we are in the flake directory +my $flake-dir = $?FILE.IO.parent.parent.absolute; +my $flake-path = $flake-dir.IO.add('flake.nix'); +chdir $flake-dir.IO; + +# TODO: Parse `nix flake show --json` to figure out these targets instead of just +# yoloing it + +# Setup our exception handler +# We will display the error, then invoke git to undo any changes before exiting +CATCH { + default { + # Display backtrace + $*ERR.say: .message; + for .backtrace.reverse { + next if .file.starts-with('SETTING::'); + next unless .subname; + $*ERR.say: " in block {.subname} at {.file} line {.line}"; + } + # TODO: Revert any changes + run ; + } +} + +# Identify all the machines we will be building +sub systems() { + my $machine-dir = $flake-dir.IO.add('nixos').add('machines'); + dir($machine-dir).map: *.basename; +} + +# Identify all the home-manager configs we will be building +sub homes() { + my $homes-dir = $flake-dir.IO.add('home-manager').add('machines'); + dir($homes-dir).map: *.basename; +} + +# Build a system given its name +sub build-system($system-name) { + say "Building nixos system for $system-name"; + my $proc = run , "$flake-path#$system-name", + :out, :err, :merge; + my $output = $proc.out.slurp: :close; + unless $proc.exitcode == 0 { + die "Failed building nixos system named $system-name:" + , "nixos-rebuild output:" + , $output; + } +} + +# Build a home-manager config given its name +sub build-home($home-name) { + say "Building home-manager config for $home-name"; + my $proc = run , "$flake-path#$home-name", + :out, :err, :merge; + my $output = $proc.out.slurp: :close; + unless $proc.exitcode == 0 { + die "Failed building home-manger config named $home-name:" + , "home-manager build output:" + , $output; + } +} + +# Update the flake +my $nix-flake-update = run , :out, :err, :merge; +my $nfu-out = $nix-flake-update.out.slurp: :close; +die "Failed updating flake", $nfu-out unless $nix-flake-update; +# Validate each system +for systems() { + build-system $_; +} +# Validate each home-manager config +for homes() { + build-home $_; +} +# TODO: Validate each dev-shell +# TODO: Setup git and ssh +# TODO: Commit +# TODO: Push``