Abstract switch calling mechanic, add proper returncode checking
Catch different failure modes of subprocess.run Add default versions of NIXOS_CONFIGURATION and HOME_MANAGER_CONFIGURATION to compare to env vars, assume configuration is a flake if different and append "--flake" argument to process commands
This commit is contained in:
parent
ddee47ce40
commit
e170c2c50e
|
@ -8,7 +8,7 @@ pkgs.mkShellNoCC {
|
||||||
projectDir = ./.;
|
projectDir = ./.;
|
||||||
preferWheels = true;
|
preferWheels = true;
|
||||||
editablePackageSources = {nix_background_upgrade = ./src;};
|
editablePackageSources = {nix_background_upgrade = ./src;};
|
||||||
extraPackages = ps: with ps; [python-lsp-server];
|
extraPackages = ps: with ps; [mypy];
|
||||||
})
|
})
|
||||||
poetry
|
poetry
|
||||||
];
|
];
|
||||||
|
|
|
@ -5,7 +5,14 @@ from typing import Optional
|
||||||
|
|
||||||
from notifypy import Notify
|
from notifypy import Notify
|
||||||
|
|
||||||
NIXOS_CONFIGURATION: str = getenv("NIXOS_CONFIGURATION", "")
|
DEFAULT_NIXOS_CONFIGURATION: Path = Path("/etc/nixos")
|
||||||
|
DEFAULT_HOME_MANAGER_CONFIGURATION: Path = Path("~/.config/home-manager/").resolve()
|
||||||
|
NIXOS_CONFIGURATION: str = getenv(
|
||||||
|
"NIXOS_CONFIGURATION", str(DEFAULT_NIXOS_CONFIGURATION)
|
||||||
|
)
|
||||||
|
HOME_MANAGER_CONFIGURATION: str = getenv(
|
||||||
|
"HOME_MANAGER_CONFIGURATION", str(DEFAULT_HOME_MANAGER_CONFIGURATION)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def create_notification(source: Optional[str] = None) -> Notify:
|
def create_notification(source: Optional[str] = None) -> Notify:
|
||||||
|
@ -20,45 +27,75 @@ def create_notification(source: Optional[str] = None) -> Notify:
|
||||||
if source is None:
|
if source is None:
|
||||||
source = "Nix process"
|
source = "Nix process"
|
||||||
return Notify(
|
return Notify(
|
||||||
default_notification_title="Message from {}".format(source),
|
default_notification_title="{}".format(source),
|
||||||
default_notification_icon=Path(__file__).parent / "./nix-snowflake.png",
|
default_notification_icon=Path(__file__).parent / "./nix-snowflake.png",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _abstract_switch(
|
||||||
|
notification: Notify, command: str, flake: Optional[str] = None
|
||||||
|
) -> int:
|
||||||
|
if flake is not None:
|
||||||
|
command += " --flake {}".format(flake)
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
process = subprocess.run(command.split())
|
||||||
|
except FileNotFoundError as e:
|
||||||
|
notification.message = e
|
||||||
|
return -1
|
||||||
|
try:
|
||||||
|
process.check_returncode()
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
notification.message = e
|
||||||
|
return process.returncode
|
||||||
|
notification.message = "Success: {}".format(command)
|
||||||
|
return process.returncode
|
||||||
|
finally:
|
||||||
|
notification.send()
|
||||||
|
|
||||||
|
|
||||||
def nixos_switch() -> int:
|
def nixos_switch() -> int:
|
||||||
"""
|
"""
|
||||||
Switch NixOS system to new version
|
Switch NixOS system to new version
|
||||||
|
|
||||||
:returns: Error code of `sudo nixos-rebuild switch` call
|
:returns: Return code of `sudo nixos-rebuild switch` call
|
||||||
"""
|
"""
|
||||||
notification = create_notification(source="nixos_switch")
|
return _abstract_switch(
|
||||||
try:
|
notification=create_notification(source="nixos_switch"),
|
||||||
subprocess.run("which -a nixos-rebuild".split()).check_returncode()
|
command="which -a nixos-rebuild",
|
||||||
except subprocess.CalledProcessError as e:
|
flake=(
|
||||||
notification.message = e
|
NIXOS_CONFIGURATION
|
||||||
notification.send()
|
if NIXOS_CONFIGURATION != str(DEFAULT_NIXOS_CONFIGURATION)
|
||||||
return 0
|
else None
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def home_manager_switch() -> None:
|
def home_manager_switch() -> int:
|
||||||
"""
|
"""
|
||||||
Switch home-manager to new version
|
Switch home-manager to new version
|
||||||
|
|
||||||
:returns: None
|
:returns: Return code of `home-manager switch` call
|
||||||
"""
|
"""
|
||||||
subprocess.Popen("which -a home-manager".split())
|
return _abstract_switch(
|
||||||
return
|
notification=create_notification(source="home_manager_switch"),
|
||||||
|
command="which -a home-manager",
|
||||||
|
flake=(
|
||||||
|
HOME_MANAGER_CONFIGURATION
|
||||||
|
if HOME_MANAGER_CONFIGURATION != str(DEFAULT_HOME_MANAGER_CONFIGURATION)
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main(*args, **kwargs) -> None:
|
def main() -> None:
|
||||||
"""
|
"""
|
||||||
Sends a test notification to check everything is working
|
Sends a test notification to check everything is working
|
||||||
|
|
||||||
:returns: None
|
:returns: None
|
||||||
"""
|
"""
|
||||||
notification = create_notification()
|
print(nixos_switch())
|
||||||
notification.message = "This is a test notification"
|
print(home_manager_switch())
|
||||||
notification.send()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue