From 0a2bad11f615f24bb40429bcc0f9231933c9210f Mon Sep 17 00:00:00 2001
From: Brian Picciano <me@mediocregopher.com>
Date: Fri, 20 Dec 2024 13:34:45 +0100
Subject: [PATCH] Change how release naming works, include build number

---
 default.nix                 | 15 ++++++++++-----
 dist/linux/arch/default.nix |  3 ++-
 dist/linux/deb/default.nix  |  7 ++++---
 docs/dev/releases.md        | 20 +++++++++++++++-----
 release.nix                 |  9 ++++-----
 release.sh                  | 14 +++++++++-----
 6 files changed, 44 insertions(+), 24 deletions(-)

diff --git a/default.nix b/default.nix
index 5064b2a..852f175 100644
--- a/default.nix
+++ b/default.nix
@@ -3,8 +3,13 @@
   hostSystem ? buildSystem,
   pkgsNix ? (import ./nix/pkgs.nix),
 
+  # We allow passing in the revision because nix flakes have their own mechanism
+  # for determining the git revision, and they don't have the git repo present
+  # to determine it off of the normal way.
   revision ? null,
+
   releaseName ? "dev",
+  buildNumber ? "1",
 }: let
 
   pkgs = pkgsNix.default {
@@ -35,7 +40,7 @@ in rec {
   version = pkgs.stdenv.mkDerivation {
     name = "isle-version";
 
-    inherit buildSystem hostSystem revisionFile releaseName;
+    inherit buildSystem hostSystem revisionFile;
 
     goVersion = pkgs.go.version;
     garageVersion = garageNix.version;
@@ -46,9 +51,8 @@ in rec {
 
       versionFile=version
 
-      echo "Release:         $releaseName"         >> "$versionFile"
+      echo "Version:         $(cat $revisionFile)" >> "$versionFile"
       echo "Platform:        $hostSystem"          >> "$versionFile"
-      echo "Git Revision:    $(cat $revisionFile)" >> "$versionFile"
       echo "Go Version:      $goVersion"           >> "$versionFile"
       echo "Garage Version:  $garageVersion"       >> "$versionFile"
       echo "NixPkgs Version: $nixpkgsVersion"      >> "$versionFile"
@@ -145,6 +149,7 @@ in rec {
   };
 
   build = rec {
+
     appImage = pkgs.stdenv.mkDerivation {
       name = "isle-AppImage";
       src = appDir {};
@@ -164,13 +169,13 @@ in rec {
     };
 
     archPkg = ((import ./dist/linux/arch) {
-      inherit hostSystem releaseName;
+      inherit hostSystem releaseName buildNumber;
       pkgs = pkgsNative;
       appDir = appDir { systemRoot = "/"; };
     });
 
     debPkg = ((import ./dist/linux/deb) {
-      inherit hostSystem releaseName;
+      inherit hostSystem releaseName buildNumber;
       pkgs = pkgsNative;
       appDir = appDir { systemRoot = "/"; };
     });
diff --git a/dist/linux/arch/default.nix b/dist/linux/arch/default.nix
index e3ad985..eab85f5 100644
--- a/dist/linux/arch/default.nix
+++ b/dist/linux/arch/default.nix
@@ -2,6 +2,7 @@
   pkgs,
   hostSystem,
   releaseName,
+  buildNumber ? "1",
   appDir,
 }: let
 
@@ -10,7 +11,7 @@
   pkgbuild = pkgs.writeText "isle-arch-PKGBUILD-${releaseName}-${cpuArch}" ''
     pkgname=isle
     pkgver=${builtins.replaceStrings ["-"] ["_"] releaseName}
-    pkgrel=0
+    pkgrel=${buildNumber}
     pkgdesc="The foundation for an autonomous community cloud infrastructure"
     arch=('${cpuArch}')
     url="https://code.betamike.com/micropelago/isle"
diff --git a/dist/linux/deb/default.nix b/dist/linux/deb/default.nix
index 78f7e45..c6912dd 100644
--- a/dist/linux/deb/default.nix
+++ b/dist/linux/deb/default.nix
@@ -2,6 +2,7 @@
   pkgs,
   hostSystem,
   releaseName,
+  buildNumber ? "1",
   appDir,
 }: let
 
@@ -14,7 +15,7 @@
 
   control = pkgs.writeTextDir "DEBIAN/control" ''
     Package: isle
-    Version: ${releaseName}
+    Version: ${releaseName}-${buildNumber}
     Section: net
     Priority: optional
     Architecture: ${debArch}
@@ -49,7 +50,7 @@
   };
 
 in pkgs.stdenv.mkDerivation {
-  name = "isle-deb-pkg-${releaseName}-${debArch}";
+  name = "isle-deb-pkg";
   nativeBuildInputs = [
     pkgs.zstd
     pkgs.dpkg
@@ -63,7 +64,7 @@ in pkgs.stdenv.mkDerivation {
     dpkg-deb \
       --root-owner-group \
       -Z zstd \
-      -b root isle_${releaseName}_${debArch}.deb
+      -b root isle_${releaseName}-${buildNumber}_${debArch}.deb
   '';
   installPhase = ''
     mkdir -p "$out"
diff --git a/docs/dev/releases.md b/docs/dev/releases.md
index 832f62e..816618b 100644
--- a/docs/dev/releases.md
+++ b/docs/dev/releases.md
@@ -8,12 +8,21 @@ A release consists of:
 - A file containing a signature of the hash file, created by whoever is building
   the release.
 
+Releases are named using semantic versioning: <major>.<minor>.<patch>. A
+preceding "v" is NOT a part of the canonical release name, though it use used in
+specific contexts.
+
+Release artifacts are also be labeled with a suffix indicating build number, eg
+"0.1.3-2" which indicates that this is of release "0.1.3". The first build of a
+release is always "1". Different build numbers of the same release indicate that
+no code is different, only the packaging.
+
 ## Building
 
 *NOTE: This has only been tested from an x86_64 linux machine*
 
 To create a release only a functional nix installation is required. Simply run
-the `./release.sh` script, and input a release name when prompted.
+the `./release.sh` script, providing the release name and build number.
 
 From here an `isle` binary will be cross-compiled for all supported
 platforms. This will take a long time the first time you perform it on your
@@ -25,12 +34,13 @@ create the signature.
 
 ## Releasing
 
-Release artifactes are hosted at `micropelago.net` under
-`/isle/releases/<release name>`. An `index.gmi` page should be created in that
+Release artifacts are hosted at `micropelago.net` under
+`/isle/releases/v<release name>`. An `index.gmi` page should be created in that
 directory which includes links to each artifact, as well as a changelog
 detailing all new features and fixes included since the previous release.
 
 A link to the new release should be included at `/isle/releases/index.gmi`.
 
-The release shoulld be tagged in the git repo using its release name as well,
-with the tag notes linking to the `micropelago.net` page.
+Each release should be tagged in the git repo using the release name and build
+number prefixed with a "v". If the build number of the release is "1" then the
+tag notes should link to the `micropelago.net` release page for the release.
diff --git a/release.nix b/release.nix
index 61dd8b3..d9ab0b2 100644
--- a/release.nix
+++ b/release.nix
@@ -1,11 +1,10 @@
 {
-  revision ? "dev",
   releaseName ? "dev",
+  buildNumber ? "1",
 
   buildSystem ? builtins.currentSystem,
   pkgsNix ? (import ./nix/pkgs.nix),
   supportedSystems ? pkgsNix.supportedSystems,
-
 }: let
 
   pkgs = pkgsNix.default { inherit buildSystem; };
@@ -13,12 +12,12 @@
   mkRelease = hostSystem: let
 
     build = ((import ./default.nix) {
-      inherit buildSystem hostSystem releaseName revision;
+      inherit buildSystem hostSystem releaseName buildNumber;
     }).build;
 
   in pkgs.stdenv.mkDerivation {
     name = "isle-release-${hostSystem}";
-    inherit releaseName hostSystem;
+    inherit releaseName buildNumber hostSystem;
 
     appImage = build.appImage;
     archPkg = build.archPkg;
@@ -28,7 +27,7 @@
       source $stdenv/setup
 
       mkdir -p "$out"/
-      cp "$appImage"/bin/isle "$out"/isle-$releaseName-$hostSystem.AppImage
+      cp "$appImage"/bin/isle "$out"/isle-$releaseName-$buildNumber-$hostSystem.AppImage
       cp "$archPkg"/* "$out"/
       cp "$debPkg"/* "$out"/
     '';
diff --git a/release.sh b/release.sh
index f95a8c8..91cdc88 100755
--- a/release.sh
+++ b/release.sh
@@ -7,21 +7,25 @@ cd "$scriptDir"
 
 releaseName="$1"
 if [ -z "$releaseName" ]; then
-    echo "USAGE: $0 \"v0.1.2\""
+    echo "USAGE: $0 \"0.1.2\" [build number]"
     exit 1
 fi
 
+buildNumber="$2"
+if [ -z "$buildNumber" ]; then
+    buildNumber="0"
+fi
+
 releasesDir="$(pwd)/releases"
 mkdir -p "$releasesDir"
 echo '*' > "$releasesDir"/.gitignore
 
-out="$releasesDir/$releaseName"
-
-revision=$(git rev-parse HEAD)
+out="$releasesDir/$releaseName-$buildNumber"
+rm -rf "$out"
 
 result=$(nix-build -v \
-            --argstr revision "$revision" \
             --argstr releaseName "$releaseName" \
+            --argstr buildNumber "$buildNumber" \
             --no-out-link \
             release.nix \
         )