From 6772fdbc2c7f51642579e21bee8cb6bc1c21dc0a Mon Sep 17 00:00:00 2001 From: Evie Litherland-Smith Date: Mon, 27 May 2024 08:36:42 +0100 Subject: [PATCH] Check for nixos-rebuild command triggers notification if not found Add source argument to default notification title --- src/nix_background_upgrade/nixos.py | 18 ++++++++++++++---- src/nix_background_upgrade/notification.py | 11 ++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/nix_background_upgrade/nixos.py b/src/nix_background_upgrade/nixos.py index 169e5b0..9f41be2 100644 --- a/src/nix_background_upgrade/nixos.py +++ b/src/nix_background_upgrade/nixos.py @@ -3,13 +3,23 @@ Control upgrading NixOS configuration """ import subprocess +from os import getenv + +from .notification import create_notification + +NIXOS_CONFIGURATION: str = getenv("NIXOS_CONFIGURATION", "") -def nixos_switch() -> None: +def nixos_switch() -> int: """ Switch NixOS system to new version - :returns: None + :returns: Error code of `sudo nixos-rebuild switch` call """ - subprocess.Popen("which -a nixos-rebuild".split()) - return + notification = create_notification(source="nixos_switch") + try: + subprocess.run("which -a nixos-rebuild".split()).check_returncode() + except subprocess.CalledProcessError as e: + notification.message = e + notification.send() + return 0 diff --git a/src/nix_background_upgrade/notification.py b/src/nix_background_upgrade/notification.py index 8c7f4ce..535b742 100644 --- a/src/nix_background_upgrade/notification.py +++ b/src/nix_background_upgrade/notification.py @@ -1,16 +1,21 @@ from pathlib import Path +from typing import Optional from notifypy import Notify -def create_notification() -> Notify: +def create_notification(source: Optional[str] = None) -> Notify: """ Create notification class + :param source: Name of originating file or function, used in default notification + title + :returns: Notify class with custom defaults """ + if source is None: + source = "Nix process" return Notify( - default_notification_title="Nix Background Update", - default_notification_message="Message from Nix process", + default_notification_title="Message from {}".format(source), default_notification_icon=Path(__file__).parent / "./nix-snowflake.png", )