diff --git a/.github/scripts/update-nix-release.js b/.github/scripts/update-nix-release.js new file mode 100644 index 000000000..38ae944b5 --- /dev/null +++ b/.github/scripts/update-nix-release.js @@ -0,0 +1,76 @@ +import crypto from 'node:crypto'; +import fs from 'node:fs'; +import path from 'node:path'; + +function usage() { + console.error('Usage: node .github/scripts/update-nix-release.js --artifacts --version '); +} + +function parseArgs(argv) { + const args = {}; + for (let index = 2; index < argv.length; index += 1) { + const arg = argv[index]; + if (arg === '--artifacts' || arg === '--version') { + args[arg.slice(2)] = argv[index + 1]; + index += 1; + continue; + } + usage(); + process.exit(2); + } + if (!args.artifacts || !args.version) { + usage(); + process.exit(2); + } + return args; +} + +function sriSha256(filePath) { + const digest = crypto.createHash('sha256').update(fs.readFileSync(filePath)).digest('base64'); + return `sha256-${digest}`; +} + +function findArtifact(artifactsDir, fileName) { + const filePath = path.join(artifactsDir, fileName); + if (!fs.existsSync(filePath)) { + throw new Error(`Missing release artifact: ${filePath}`); + } + return filePath; +} + +function renderReleaseNix({ version, x64Hash, arm64Hash }) { + return `{ + version = "${version}"; + + sources = { + x86_64-linux = { + appImageArch = "x86_64"; + hash = "${x64Hash}"; + }; + aarch64-linux = { + appImageArch = "arm64"; + hash = "${arm64Hash}"; + }; + }; +} +`; +} + +const args = parseArgs(process.argv); +const version = args.version.replace(/^v/, ''); + +if (!/^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-.+)?$/.test(version)) { + throw new Error(`Expected semver version, got: ${args.version}`); +} + +const x64AppImage = findArtifact(args.artifacts, `Netcatty-${version}-linux-x86_64.AppImage`); +const arm64AppImage = findArtifact(args.artifacts, `Netcatty-${version}-linux-arm64.AppImage`); + +const releaseNix = renderReleaseNix({ + version, + x64Hash: sriSha256(x64AppImage), + arm64Hash: sriSha256(arm64AppImage), +}); + +fs.writeFileSync('nix/release.nix', releaseNix); +console.log(`Updated nix/release.nix for Netcatty ${version}`); diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8d7b4dc2e..5796c63a1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -687,6 +687,45 @@ jobs: fail_on_unmatched_files: false token: ${{ secrets.RELEASE_TOKEN }} + update-nix-release: + name: update Nix release metadata + runs-on: ubuntu-latest + needs: release + if: | + startsWith(github.ref, 'refs/tags/v') + && (github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.publish_release)) + permissions: + contents: write + actions: read + steps: + - name: Checkout default branch + uses: actions/checkout@v4 + with: + ref: ${{ github.event.repository.default_branch }} + token: ${{ secrets.RELEASE_TOKEN }} + + - name: Download Linux artifacts + uses: actions/download-artifact@v4 + with: + path: artifacts + merge-multiple: true + + - name: Update Nix release metadata + run: node .github/scripts/update-nix-release.js --artifacts artifacts --version "${GITHUB_REF_NAME#v}" + + - name: Commit Nix release metadata + shell: bash + run: | + if git diff --quiet -- nix/release.nix; then + echo "Nix release metadata is already current." + exit 0 + fi + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git add nix/release.nix + git commit -m "Update Nix release metadata for ${GITHUB_REF_NAME}" + git push origin HEAD:${{ github.event.repository.default_branch }} + homebrew-tap: name: bump homebrew tap runs-on: ubuntu-latest diff --git a/nix/package.nix b/nix/package.nix index 17cf8def6..9c7402f1a 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -2,23 +2,13 @@ appimageTools, fetchurl, lib, + release ? import ./release.nix, stdenv, }: let pname = "netcatty"; - version = "1.1.51"; - - sources = { - x86_64-linux = { - appImageArch = "x86_64"; - hash = "sha256-xMTXP3gjBuAzDXIYuhbQsCkwcf9lDMnRvqkL01PbGTA="; - }; - aarch64-linux = { - appImageArch = "arm64"; - hash = "sha256-UyWRFuc1yN2CkUl0/jyJ3+lJkrpvGHZB0u5itJ0CkG4="; - }; - }; + inherit (release) sources version; source = sources.${stdenv.hostPlatform.system} diff --git a/nix/release.nix b/nix/release.nix new file mode 100644 index 000000000..5180c7494 --- /dev/null +++ b/nix/release.nix @@ -0,0 +1,14 @@ +{ + version = "1.1.51"; + + sources = { + x86_64-linux = { + appImageArch = "x86_64"; + hash = "sha256-xMTXP3gjBuAzDXIYuhbQsCkwcf9lDMnRvqkL01PbGTA="; + }; + aarch64-linux = { + appImageArch = "arm64"; + hash = "sha256-UyWRFuc1yN2CkUl0/jyJ3+lJkrpvGHZB0u5itJ0CkG4="; + }; + }; +} diff --git a/scripts/update-nix-release.test.cjs b/scripts/update-nix-release.test.cjs new file mode 100644 index 000000000..fb1070d35 --- /dev/null +++ b/scripts/update-nix-release.test.cjs @@ -0,0 +1,34 @@ +const assert = require('node:assert/strict'); +const crypto = require('node:crypto'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const test = require('node:test'); +const { execFileSync } = require('node:child_process'); + +test('update-nix-release writes version and AppImage hashes', () => { + const root = path.join(__dirname, '..'); + const script = path.join(root, '.github', 'scripts', 'update-nix-release.js'); + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'netcatty-nix-release-')); + const artifacts = path.join(tmp, 'artifacts'); + fs.mkdirSync(path.join(tmp, 'nix')); + fs.mkdirSync(artifacts); + + const x64 = Buffer.from('x64 appimage'); + const arm64 = Buffer.from('arm64 appimage'); + fs.writeFileSync(path.join(artifacts, 'Netcatty-1.2.3-linux-x86_64.AppImage'), x64); + fs.writeFileSync(path.join(artifacts, 'Netcatty-1.2.3-linux-arm64.AppImage'), arm64); + + execFileSync(process.execPath, [script, '--artifacts', artifacts, '--version', 'v1.2.3'], { + cwd: tmp, + stdio: 'pipe', + }); + + const releaseNix = fs.readFileSync(path.join(tmp, 'nix', 'release.nix'), 'utf8'); + const x64Hash = `sha256-${crypto.createHash('sha256').update(x64).digest('base64')}`; + const arm64Hash = `sha256-${crypto.createHash('sha256').update(arm64).digest('base64')}`; + + assert.match(releaseNix, /version = "1\.2\.3";/); + assert.match(releaseNix, new RegExp(`hash = "${x64Hash.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}";`)); + assert.match(releaseNix, new RegExp(`hash = "${arm64Hash.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}";`)); +});