My system configuration via Nix flakes
#!/usr/bin/env nu
# Learning nushell by writing a simple updater script
# Mostly just testing out all the different language features

use std assert;
use repo.nu len-unrecorded;

const required_paths = [".pijul" "flake.nix"];
const prompt_command = $"(ansi red)\(system config\)(ansi reset)";

# Helper function to make sure all necessary paths are present
def assert_path_exists [candidate] {
    assert ($candidate | path exists) $"(ansi red) Failed to find required file: (ansi red_bold)($candidate)(ansi reset)"
}

# If not root, need to elevate permissions
let script_location = $env.CURRENT_FILE;
# Get the nix flake dir in 3 steps
# 1: ~/config/src/update.nu
# 2: ~/config/src
# 3: ~/config
let parent_dir = $script_location | path dirname | path dirname;

echo $"(ansi purple)Attempting update in (ansi green_bold)($parent_dir)(ansi reset)";
cd $parent_dir;

# Check all the required paths are here
$required_paths | each {|required_path| assert_path_exists $required_path}

# Force version control to be clean before continuing
while (len-unrecorded) > 0 {
    echo $"(ansi yellow_bold)WARNING(ansi yellow): repository has unrecorded changes:(ansi reset)";
    # Drop into terminal to fix
    nu --interactive --execute $"enter ($parent_dir); use ($parent_dir | path join src/repo.nu) repl-prompt; $env.PROMPT_COMMAND = {|| repl-prompt }; pijul diff -us";
}

# Update all channels beforehand
sudo nix-channel --update;
nix flake update;

# Using `nh` (https://github.com/viperML/nh), which integrates:
#   - `nix-output-monitor` (https://github.com/maralorn/nix-output-monitor)
#   - `nvd` (https://gitlab.com/khumba/nvd)
nh os switch . -- --impure;

# Upgrade all the other package managers
nix-shell -p pkg-config openssl --run "topgrade --disable nix --disable system --yes --no-retry";