(mirror) personal dotfiles
github.com/crescentrose/dotfiles
1#!/usr/bin/env nu
2
3use ../lib/vlog
4
5def "flake-name" [system_name?: string, flake_dir?: string ] {
6 let flake_name = match (uname).operating-system {
7 "Darwin" => "work"
8 "GNU/Linux" => "starlight"
9 _ => { vlog fatal $"Unsupported OS: ((uname).operating-system)" 2}
10 }
11 let default_dir = $"($env.HOME)/Code/dotfiles"
12
13 $"($flake_dir | default $default_dir)#($system_name | default $flake_name)"
14}
15
16# Update the Nix flake version.
17def "main update" [system_name?: string, flake_dir?: string] {
18 let flake_name = flake-name $system_name $flake_dir
19
20 vlog info $"🚚 Updating flake ($flake_name)..."
21 vlog timed "Update" {
22 nix flake update --flake $flake_name
23 }
24}
25
26# Build a new derivation and switch to it.
27def "main rebuild" [system_name?: string, flake_dir?: string] {
28 let flake_name = flake-name $system_name $flake_dir
29 vlog timed "Rebuild" {
30 vlog info $"🏠 Building flake ($flake_name)..."
31 match (uname).operating-system {
32 "Darwin" => { sudo darwin-rebuild --flake $flake_name --impure switch }
33 "GNU/Linux" => { sudo nixos-rebuild --flake $flake_name --impure switch}
34 _ => { vlog fatal $"Unsupported OS: ((uname).operating-system)" 2}
35 }
36 }
37}
38
39# Clean up garbage and optimise Nix store. (Most of the time this should happen automatically,
40# though.)
41def "main clean-up" [] {
42 vlog timed "Clean-up" {
43 vlog info "♻ Running garbage collection..."
44 sudo nix-collect-garbage --delete-old
45 }
46}
47
48# Show the latest logs for a (root) systemd unit in reverse chronological order.
49def "main inspect" [service: string, --root (-r)] {
50 vlog info $"🤨 Inspecting ($service)..."
51 vlog timed "Service inspection" {
52 if $root {
53 sudo journalctl -u $service --reverse --since="15m ago"
54 } else {
55 journalctl --user -u $service --reverse --since="15m ago"
56 }
57 }
58}
59
60def main [] {
61 help main
62}