feat(nix-monitor): add system stats

This commit is contained in:
avivbintangaringga
2026-07-14 15:26:15 +07:00
parent f350c4977d
commit a3e663990f
4 changed files with 121 additions and 3 deletions
+44 -1
View File
@@ -22,6 +22,7 @@ end
local branch = cfg("branch") local branch = cfg("branch")
local updateCheckInterval = cfg("check_interval") local updateCheckInterval = cfg("check_interval")
local systemStatsCheckInterval = cfg("system_stats_check_interval")
local completedCommands = 0 local completedCommands = 0
local localHashCmd = "nixos-version --hash 2> /dev/null | cut -c -7" local localHashCmd = "nixos-version --hash 2> /dev/null | cut -c -7"
@@ -30,7 +31,7 @@ local nixosGenerationsCmd = "nixos-rebuild list-generations | tail -n +2 | wc -l
local nixosCurrentGenCmd = "nixos-rebuild list-generations | grep True | awk '{print $1}'" local nixosCurrentGenCmd = "nixos-rebuild list-generations | grep True | awk '{print $1}'"
local hmGenerationsCmd = "home-manager generations | wc -l" local hmGenerationsCmd = "home-manager generations | wc -l"
local hmCurrentGenCmd = "home-manager generations | grep '(current)' | awk '{print $5}'" local hmCurrentGenCmd = "home-manager generations | grep '(current)' | awk '{print $5}'"
local nixStoreSizeCmd = `du -s --block-size=1GiB /nix/store | awk '\{print $1 " GiB"\}'` local storeSizeCmd = `du -s --block-size=1GiB /nix/store | awk '\{print $1 " GiB"\}'`
local closureSizeCmd = "nix path-info --closure-size --human-readable /run/current-system | awk '{print $2, $3}'" local closureSizeCmd = "nix path-info --closure-size --human-readable /run/current-system | awk '{print $2, $3}'"
setState("lastCheckTime", 0) setState("lastCheckTime", 0)
@@ -39,11 +40,23 @@ setState("localHash", "n/a")
setState("remoteHash", "n/a") setState("remoteHash", "n/a")
setState("isRunning", false) setState("isRunning", false)
setState("isUpdateAvailable", false) setState("isUpdateAvailable", false)
setState("lastSystemStatsCheckTime", 0)
setState("hasHomeManager", noctalia.commandExists("home-manager"))
setState("nixosGenerations", "•••")
setState("nixosCurrentGen", "•••")
setState("hmGenerations", "•••")
setState("hmCurrentGen", "•••")
setState("storeSize", "•••")
setState("closureSize", "•••")
function update() function update()
if (os.time() - getState("lastCheckTime")) >= 60 * updateCheckInterval then if (os.time() - getState("lastCheckTime")) >= 60 * updateCheckInterval then
checkUpdate() checkUpdate()
end end
if (os.time() - getState("lastSystemStatsCheckTime")) >= 60 * systemStatsCheckInterval then
checkSystemStats()
end
end end
function checkUpdate() function checkUpdate()
@@ -116,3 +129,33 @@ watchState("command", function(command)
completedCommands = 0 completedCommands = 0
end end
end) end)
function checkSystemStats()
setState("lastSystemStatsCheckTime", os.time())
noctalia.runAsync(nixosGenerationsCmd, function(result)
setState("nixosGenerations", result.stdout)
end)
noctalia.runAsync(nixosCurrentGenCmd, function(result)
setState("nixosCurrentGen", result.stdout)
end)
if getState("hasHomeManager") then
noctalia.runAsync(hmGenerationsCmd, function(result)
setState("hmGenerations", result.stdout)
end)
noctalia.runAsync(hmCurrentGenCmd, function(result)
setState("hmCurrentGen", result.stdout)
end)
end
noctalia.runStream(storeSizeCmd, function(line)
setState("storeSize", line)
end)
noctalia.runStream(closureSizeCmd, function(line)
setState("closureSize", line)
end)
end
+63 -1
View File
@@ -48,6 +48,27 @@ local function labelBox(subtext, text, glyph)
}) })
end end
function statLabel(text, glyph: string?)
local items = {}
if glyph ~= nil then
table.insert(items, ui.glyph({
name = glyph,
size = 20
}))
end
table.insert(items, ui.label({
text = text,
fontSize = 12,
fontWeight = "semibold"
}))
return ui.row({
gap = 8,
align = "center"
}, items)
end
local function render() local function render()
local firstBtn = btnCheck local firstBtn = btnCheck
@@ -80,10 +101,24 @@ local function render()
end end
local lastCheckTimeStr = getState("lastCheckTimeStr") local lastCheckTimeStr = getState("lastCheckTimeStr")
local generationItems = {
statLabel(`SYS: {getState("nixosGenerations")}`, "device-desktop"),
statLabel(`{tr("panel.current")}: {getState("nixosCurrentGen")}`, "device-desktop-check")
}
if getState("hasHomeManager") then
table.insert(generationItems,
statLabel(`HM: {getState("hmGenerations")}`, "home")
)
table.insert(generationItems,
statLabel(`{tr("panel.current")}: {getState("hmCurrentGen")}`, "home-check")
)
end
panel.render( panel.render(
ui.column({ ui.column({
gap = 8 gap = 8,
}, { }, {
ui.row({ ui.row({
justify = "space_between", justify = "space_between",
@@ -117,6 +152,33 @@ local function render()
orientation = "horizontal", orientation = "horizontal",
spacing = 4 spacing = 4
}), }),
ui.row({
fill = "surface/0.7",
align = "center",
justify = "space_between",
flexGrow = 1,
paddingV = 4,
paddingH = 8,
radius = 8,
}, generationItems),
ui.row({
fill = "surface/0.7",
align = "center",
justify = "center",
flexGrow = 1,
paddingV = 4,
paddingH = 8,
radius = 8,
}, {
statLabel(`{tr("panel.store_size")}: {getState("storeSize")}`, "database"),
ui.spacer({width = 16}),
statLabel(`{tr("panel.closure_size")}: {getState("closureSize")}`, "server"),
}),
ui.separator({
thickness = 2,
orientation = "horizontal",
spacing = 4
}),
ui.label({ ui.label({
text = `{tr("panel.last_checked")}: {lastCheckTimeStr}` text = `{tr("panel.last_checked")}: {lastCheckTimeStr}`
}), }),
+9 -1
View File
@@ -93,6 +93,14 @@ description_key = "setting.check_interval.description"
default = 60 default = 60
min = 10 min = 10
[[setting]]
key = "system_stats_check_interval"
type = "int"
label_key = "setting.system_stats_check_interval.label"
description_key = "setting.system_stats_check_interval.description"
default = 10
min = 5
[[setting]] [[setting]]
key = "show_checking_notification" key = "show_checking_notification"
type = "bool" type = "bool"
@@ -159,7 +167,7 @@ placement = "attached"
position = "auto" position = "auto"
open_near_click = true open_near_click = true
width = 420 width = 420
height = 290 height = 370
[[service]] [[service]]
id = "service" id = "service"
+5
View File
@@ -16,6 +16,9 @@
"panel.local": "Local", "panel.local": "Local",
"panel.remote": "Remote", "panel.remote": "Remote",
"panel.last_checked": "Last checked", "panel.last_checked": "Last checked",
"panel.current": "Current",
"panel.store_size": "Store size",
"panel.closure_size": "Closure size",
"terminal.press_enter_to_exit": "Press enter to exit...", "terminal.press_enter_to_exit": "Press enter to exit...",
"setting.widget.show_text.label": "Show text", "setting.widget.show_text.label": "Show text",
"setting.widget.colorize_text.label": "Colorize text", "setting.widget.colorize_text.label": "Colorize text",
@@ -31,6 +34,8 @@
"setting.widget.unknown_color.label": "Unknown status color", "setting.widget.unknown_color.label": "Unknown status color",
"setting.check_interval.label": "Update check interval", "setting.check_interval.label": "Update check interval",
"setting.check_interval.description": "How often to check for updates (in minutes)", "setting.check_interval.description": "How often to check for updates (in minutes)",
"setting.system_stats_check_interval.label": "System stats check interval",
"setting.system_stats_check_interval.description": "How often to check for system stats (in minutes)",
"setting.checking_notification.label": "Show checking update notification", "setting.checking_notification.label": "Show checking update notification",
"setting.checking_notification.description": "Wether to show a notification whenever checking is in progress", "setting.checking_notification.description": "Wether to show a notification whenever checking is in progress",
"setting.show_update_notification.label": "Show update notification", "setting.show_update_notification.label": "Show update notification",