System/scripts/update

80 lines
2.4 KiB
Raku

#!/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 <echo git restore $flake-dir>;
}
}
# 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 <nixos-rebuild build -L --flake>, "$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 <home-manager build --flake>, "$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 <nix flake update>, :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``