diff --git a/nix-monitor/README.md b/nix-monitor/README.md index 1112ea9..e78d5ba 100644 --- a/nix-monitor/README.md +++ b/nix-monitor/README.md @@ -13,7 +13,7 @@ NixOS generations, store size, closure size, and update status from the bar. ## Requirements This plugin is intended for NixOS. It uses `nix`, `nixos-version`, -`nixos-rebuild`, and `git`, plus the standard commands `du`, `cat`, `awk`, +`nixos-rebuild`, `nix-store`, and `git`, plus the standard commands `du`, `cat`, `read`, `echo`, `awk`, `grep`, `tail`, `wc`, `kill`, and `pkill`. Home Manager generation information is shown when `home-manager` is available. @@ -29,8 +29,8 @@ Open the panel directly with: noctalia msg panel-toggle avivbintangaringga/nix-monitor:panel ``` -Set `update_command` before using **Update**. **Clean** runs the configured -cleanup command, which defaults to `nix-collect-garbage -d`. Both commands open +Set `update_command` before using **Update**. **Optimize** runs the configured command `nix-store --optimise -vv`. **Clean** runs the configured +cleanup command, which defaults to `nix-collect-garbage -d`. All commands open in a terminal so you can review their output. ## Settings @@ -47,7 +47,8 @@ Update behavior: | `show_update_available_notification` | `true` | Notifies when a newer revision is available. | | `branch` | `nixos-unstable` | Nixpkgs branch compared by the service. | | `update_command` | *(empty)* | Command launched by the panel's **Update** button. | -| `clean_command` | `nix-collect-garbage -d` | Command launched by **Clean**. | +| `optimize_command` | `nix-store --optimize -vv` | Command launched by the panel's **Optimize** button. | +| `clean_command` | `nix-collect-garbage -d` | Command launched by the panel's **Clean** button. | | `close_on_enter` | `true` | Keeps the command terminal open until Enter is pressed. | Widget appearance: diff --git a/nix-monitor/nix_monitor_service.luau b/nix-monitor/nix_monitor_service.luau index a808fb2..8252cc8 100644 --- a/nix-monitor/nix_monitor_service.luau +++ b/nix-monitor/nix_monitor_service.luau @@ -23,7 +23,8 @@ end function killWithPidFile(file) if noctalia.fileExists(file) then - local pid = noctalia.string.trim(noctalia.readFile(file)) + local content = noctalia.readFile(file) or "" + local pid = noctalia.string.trim(content) if type(pid) == "string" and pid ~= "" then noctalia.runAsync(`pkill -9 -P {pid} && kill -9 {pid}`) @@ -38,7 +39,7 @@ local updateCheckInterval = cfg("update_check_interval") local updateCheckDurationThreshold = cfg("update_check_duration_threshold") local systemStatsCheckInterval = cfg("system_stats_check_interval") local systemStatsCheckDurationThreshold = cfg("system_stats_check_duration_threshold") -local stateDir = "/tmp/nix-monitor" +local stateDir = noctalia.pluginDataDir() or "/tmp/nix-monitor" local remoteHashFile = stateDir .. "/remoteHash" local storeSizeFile = stateDir .. "/storeSize" local closureSizeFile = stateDir .. "/closureSize" @@ -95,7 +96,7 @@ end -- Currently it is writing to a file because when I use the runAsync with -- the callback, it is timed out function checkRemoteHash(delay: number?) - local delay = delay or 0 + local _delay = delay or 0 if getState("isRemoteCheckRunning") then return @@ -109,12 +110,13 @@ function checkRemoteHash(delay: number?) end noctalia.writeFile(remoteHashFile, "") - noctalia.runAsync(`sleep {delay} && {remoteHashCmd} > {remoteHashFile} & echo $! > {remotePidFile}`) + noctalia.runAsync(`sleep {_delay} && {remoteHashCmd} > {remoteHashFile} & echo $! > {remotePidFile}`) end -- Check if the remoteHash check command is finished by reading the file function checkRemoteStatus() - local remoteHash = noctalia.string.trim(noctalia.readFile(remoteHashFile)) + local remoteHashFileContent = noctalia.readFile(remoteHashFile) or "" + local remoteHash = noctalia.string.trim(remoteHashFileContent) if type(remoteHash) == "string" and remoteHash ~= "" then setState("isRemoteCheckRunning", false) @@ -179,8 +181,10 @@ function checkSystemStats() end function checkSystemStatsFile() - local storeSize = noctalia.string.trim(noctalia.readFile(storeSizeFile)) - local closureSize = noctalia.string.trim(noctalia.readFile(closureSizeFile)) + local storeSizeFileContent = noctalia.readFile(storeSizeFile) or "" + local storeSize = noctalia.string.trim(storeSizeFileContent) + local closureSizeFileContent = noctalia.readFile(closureSizeFile) or "" + local closureSize = noctalia.string.trim(closureSizeFileContent) if typeof(storeSize) == "string" and storeSize ~= "" then setState("storeSize", storeSize) diff --git a/nix-monitor/panel.luau b/nix-monitor/panel.luau index dde6834..2868b48 100644 --- a/nix-monitor/panel.luau +++ b/nix-monitor/panel.luau @@ -119,6 +119,19 @@ local function render() end end + if not cfg("hide_optimize_button") then + table.insert( + buttons, + ui.button({ + text = tr("panel.optimize"), + glyph = "database-cog", + flexGrow = 1, + variant = "primary", + onClick = "onOptimizeClicked", + }) + ) + end + if not cfg("hide_clean_button") then table.insert( buttons, @@ -273,3 +286,18 @@ function onCleanClicked() panel.close() noctalia.runInTerminal(command) end + +function onOptimizeClicked() + local command = cfg("optimize_command") + if command == "" then + noctalia.notifyError(tr("notification.optimize_command_is_empty")) + return + end + + if cfg("close_on_enter") then + command = `{command} && echo && read -p "{tr("terminal.press_enter_to_exit")}"` + end + + panel.close() + noctalia.runInTerminal(command) +end diff --git a/nix-monitor/plugin.toml b/nix-monitor/plugin.toml index 01b885b..d65b9d8 100644 --- a/nix-monitor/plugin.toml +++ b/nix-monitor/plugin.toml @@ -1,12 +1,12 @@ id = "avivbintangaringga/nix-monitor" name = "Nix Monitor" -version = "1.1.1" -plugin_api = 3 +version = "1.2.0" +plugin_api = 3 icon = "package" author = "avivbintangaringga" description = "Nixpkgs update monitor" tags = [ "nixos", "utility" ] -dependencies = [ "nix", "nixos-version", "nixos-rebuild", "git", "du", "cat", "awk", "grep", "tail", "wc", "kill", "pkill" ] +dependencies = [ "nix", "nixos-version", "nixos-rebuild", "nix-store", "git", "du", "cat", "echo", "read", "awk", "grep", "tail", "wc", "kill", "pkill" ] license = "MIT" [[widget]] @@ -180,6 +180,13 @@ label_key = "setting.update_command.label" description_key = "setting.update_command.description" default = "" +[[setting]] +key = "optimize_command" +type = "string" +label_key = "setting.optimize_command.label" +description_key = "setting.optimize_command.description" +default = "nix-store --optimise -vv" + [[setting]] key = "clean_command" type = "string" @@ -194,6 +201,13 @@ label_key = "setting.hide_update_button.label" description_key = "setting.hide_update_button.description" default = false +[[setting]] +key = "hide_optimize_button" +type = "bool" +label_key = "setting.hide_optimize_button.label" +description_key = "setting.hide_optimize_button.description" +default = false + [[setting]] key = "hide_clean_button" type = "bool" diff --git a/nix-monitor/thumbnail.webp b/nix-monitor/thumbnail.webp index 12b26c2..a3f894a 100644 Binary files a/nix-monitor/thumbnail.webp and b/nix-monitor/thumbnail.webp differ diff --git a/nix-monitor/translations/en.json b/nix-monitor/translations/en.json index 87cbe87..74140ec 100644 --- a/nix-monitor/translations/en.json +++ b/nix-monitor/translations/en.json @@ -8,6 +8,7 @@ "notification": { "checking_update": "Checking NixOS update...", "clean_command_is_empty": "Clean command is empty!", + "optimize_command_is_empty": "Optimize command is empty!", "update_check_completed": "NixOS update check completed!", "update_command_is_empty": "Update command is empty!", "update_is_available": "NixOS update is available!" @@ -15,6 +16,7 @@ "panel": { "cancel": "Cancel", "check": "Check", + "optimize": "Optimize", "clean": "Clean", "closure_size": "Closure size", "current": "Current", @@ -30,6 +32,14 @@ "description": "Which nixpkgs branch to use", "label": "Branch" }, + "update_command": { + "description": "Command to run when clicking Update button", + "label": "Update command" + }, + "optimize_command": { + "label": "Optimize command", + "description": "Command to run when clicking Optimize button" + }, "clean_command": { "description": "Command to run when clicking Clean button", "label": "Clean command" @@ -38,14 +48,18 @@ "description": "Add 'Press enter to exit' on the terminal window", "label": "Close on press Enter" }, - "hide_clean_button": { - "description": "Whether to hide the clean button on the panel", - "label": "Hide clean button" - }, "hide_update_button": { "description": "Whether to hide the update button on the panel", "label": "Hide update button" }, + "hide_optimize_button": { + "label": "Hide optimize button", + "description": "Whether to hide the optimize button on the panel" + }, + "hide_clean_button": { + "description": "Whether to hide the clean button on the panel", + "label": "Hide clean button" + }, "option": { "branch": { "master": "Master", @@ -91,10 +105,6 @@ "description": "How often to check for updates (in minutes)", "label": "Update check interval" }, - "update_command": { - "description": "Command to run when clicking Update button", - "label": "Update command" - }, "widget": { "checking_color": { "label": "Checking color"