From 1e9c413afd1fabac373444a1e728153f6adce2ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lars=20Sj=C3=B6str=C3=B6m?= <lars@radicore.se>
Date: Fri, 13 Sep 2024 09:25:45 +0200
Subject: [PATCH] feat: initial update images

---
 flake.nix             | 18 ++++++++++++++++++
 modules/sysupdate.nix |  4 ++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/flake.nix b/flake.nix
index d62d9c3..6cdc6f5 100644
--- a/flake.nix
+++ b/flake.nix
@@ -7,6 +7,23 @@
 
   outputs = { self, nixpkgs }: {
     lib = {
+      # Prepare an update package for the system.
+      mkUpdate = nixos:
+        let
+          config = nixos.config;
+          pkgs = nixpkgs.legacyPackages.x86_64-linux.pkgs;
+        in
+        nixos.pkgs.runCommand "update-${config.system.image.version}"
+          {
+            nativeBuildInputs = with pkgs; [ xz ];
+          } ''
+          mkdir -p $out
+          xz -1 -cz ${config.system.build.uki}/${config.system.boot.loader.ukiFile} \
+            > $out/${config.system.boot.loader.ukiFile}.xz
+          xz -1 -cz ${config.system.build.image}/${config.boot.uki.name}_${config.system.image.version}.store.raw \
+            > $out/${config.boot.uki.name}_${config.system.image.version}.img.xz
+        '';
+
       # Prepare a ready-to-boot disk image.
       mkInstallImage = nixos:
         let
@@ -36,6 +53,7 @@
       default = self.packages.x86_64-linux.patos_image;
 
       patos_image = self.lib.mkInstallImage self.nixosConfigurations.patos;
+      patos_update = self.lib.mkUpdate self.nixosConfigurations.patos;
 
       # A helper script to run the disk images above.
       qemu-efi =
diff --git a/modules/sysupdate.nix b/modules/sysupdate.nix
index ed9cf21..949fbcb 100644
--- a/modules/sysupdate.nix
+++ b/modules/sysupdate.nix
@@ -36,7 +36,7 @@
       "20-store" = {
         Source = {
           MatchPattern = [
-            "store_@v.img.xz"
+            "${config.boot.uki.name}_@v.img.xz"
           ];
           # Path = "https://download.example.com/";
           # Type = "url-file";
@@ -52,7 +52,7 @@
           # Path = "auto";
           Path = "/dev/sda";
 
-          MatchPattern = "store_@v";
+          MatchPattern = "${config.boot.uki.name}_@v";
 
           Type = "partition";
           ReadOnly = "yes";