From 1af3a2817e7ab60d6f5e89e320a83e1e6af597f1 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Sun, 31 Dec 2023 18:29:59 +0300 Subject: [PATCH] feat: Recover the previous config on failure. --- .../migrations/migrate_to_flakes.py | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/selfprivacy_api/migrations/migrate_to_flakes.py b/selfprivacy_api/migrations/migrate_to_flakes.py index 78b0ec2..760be7b 100644 --- a/selfprivacy_api/migrations/migrate_to_flakes.py +++ b/selfprivacy_api/migrations/migrate_to_flakes.py @@ -84,7 +84,9 @@ class MigrateToFlakes(Migration): with tarfile.open(archive_path, "r:gz") as tar: tar.extractall(path=temp_dir) - extracted_folder = os.path.join(temp_dir, "selfprivacy-nixos-template") + extracted_folder = os.path.join( + temp_dir, "selfprivacy-nixos-template" + ) shutil.copytree(extracted_folder, "/etc/nixos") finally: @@ -99,7 +101,9 @@ class MigrateToFlakes(Migration): hardware_config_lines = hardware_config_file.readlines() # Check if the file contains the line with "./networking.nix" substring - hardware_config_lines = [line for line in hardware_config_lines if "./networking.nix" not in line] + hardware_config_lines = [ + line for line in hardware_config_lines if "./networking.nix" not in line + ] with open(new_hardware_config_path, "w") as new_hardware_config_file: new_hardware_config_file.writelines(hardware_config_lines) @@ -226,9 +230,11 @@ class MigrateToFlakes(Migration): "/etc/nixos#default", ] ) + print("================================") print( "NixOS built. You may reboot now! Creating a notification for the app" ) + print("================================") Jobs.add( name="NixOS upgrade to 23.11", @@ -250,3 +256,24 @@ class MigrateToFlakes(Migration): status_text=str(error), description="Upgrade to NixOS 23.11", ) + # Recover the old configuration if the nixos.pre-flakes exists + if os.path.exists("/etc/nixos.pre-flakes"): + print("Recovering the old configuration") + # Move the new configuration to /etc/nixos.new + subprocess.check_output( + [ + "mv", + "-v", + "/etc/nixos", + "/etc/nixos.failed", + ] + ) + subprocess.check_output( + [ + "mv", + "-v", + "/etc/nixos.pre-flakes", + "/etc/nixos", + ] + ) + print("Recovered the old configuration")