Check for nixos-rebuild command triggers notification if not found

Add source argument to default notification title
This commit is contained in:
Evie Litherland-Smith 2024-05-27 08:36:42 +01:00
parent 0396bdb66a
commit 6772fdbc2c
2 changed files with 22 additions and 7 deletions

View file

@ -3,13 +3,23 @@ Control upgrading NixOS configuration
""" """
import subprocess 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 Switch NixOS system to new version
:returns: None :returns: Error code of `sudo nixos-rebuild switch` call
""" """
subprocess.Popen("which -a nixos-rebuild".split()) notification = create_notification(source="nixos_switch")
return try:
subprocess.run("which -a nixos-rebuild".split()).check_returncode()
except subprocess.CalledProcessError as e:
notification.message = e
notification.send()
return 0

View file

@ -1,16 +1,21 @@
from pathlib import Path from pathlib import Path
from typing import Optional
from notifypy import Notify from notifypy import Notify
def create_notification() -> Notify: def create_notification(source: Optional[str] = None) -> Notify:
""" """
Create notification class Create notification class
:param source: Name of originating file or function, used in default notification
title
:returns: Notify class with custom defaults :returns: Notify class with custom defaults
""" """
if source is None:
source = "Nix process"
return Notify( return Notify(
default_notification_title="Nix Background Update", default_notification_title="Message from {}".format(source),
default_notification_message="Message from Nix process",
default_notification_icon=Path(__file__).parent / "./nix-snowflake.png", default_notification_icon=Path(__file__).parent / "./nix-snowflake.png",
) )