diff --git a/apps/update-sources/src/app.ts b/apps/update-sources/src/app.ts index e93cd920..843a3435 100644 --- a/apps/update-sources/src/app.ts +++ b/apps/update-sources/src/app.ts @@ -3,6 +3,7 @@ import { writeFileSync } from 'node:fs'; import { parseArgs } from './lib'; +import { updateCaddyPlugins } from './caddy'; import { updateDocker } from './docker'; import { updateFirefoxAddons } from '././firefox'; import { updateFlakeInputs } from './flake'; @@ -10,7 +11,6 @@ import updateNodeModules from './node-modules'; import { runNixUpdate, - updateCaddyPlugins, updateVuetorrent, } from './misc'; diff --git a/apps/update-sources/src/caddy.ts b/apps/update-sources/src/caddy.ts new file mode 100644 index 00000000..1bf948f7 --- /dev/null +++ b/apps/update-sources/src/caddy.ts @@ -0,0 +1,89 @@ +import { writeFileSync } from 'node:fs'; +import { spawnSync } from 'node:child_process'; + +import { replaceInFile } from './lib'; + +/* Types */ +interface Plugin { + url: string + version: string + type: 'version' | 'git' +} + + +/* Constants */ +const FLAKE = process.env.FLAKE; + +const genPluginsText = ( + plugins: Record<string, Plugin>, +) => `{ + plugins = { +${Object.entries(plugins) + .map(([key, value]) => ` + ${key} = { + url = "${value.url}"; + version = "${value.version}"; + type = "${value.type}"; + }; + `) + .join('')} + }; + + hash = ""; +} +`; + +export const updateCaddyPlugins = () => { + let updates = ''; + const dir = `${FLAKE}/configurations/cluster/modules/caddy`; + + // Setup workspace + spawnSync( + [ + 'rm -rf /tmp/update-caddy', + 'mkdir -p /tmp/update-caddy', + 'cd /tmp/update-caddy || exit 1', + 'go mod init temp', + ].join('; '), + [], + { shell: true, cwd: '/tmp' }, + ); + + const plugins = JSON.parse(spawnSync('nix', + ['eval', '-f', `${dir}/plugins.nix`, '--json'], + { shell: true }).stdout.toString()).plugins as Record<string, Plugin>; + + // Get most recent versions of plugins + Object.entries(plugins).forEach(([key, value]) => { + const NEW_VERSION = spawnSync([ + 'go mod init temp > /dev/null', + `go get ${value.url}${value.type === 'git' ? '@HEAD' : ''} > /dev/null`, + `grep '${value.url}' go.mod`, + ].join('; '), [], { shell: true, cwd: '/tmp/update-caddy' }) + .stdout + .toString() + .trim() + .replace(' // indirect', '') + .split(' ')[1]; + + if (plugins[key].version !== NEW_VERSION) { + updates += `${key}: ${plugins[key].version} -> ${NEW_VERSION}\n`; + plugins[key].version = NEW_VERSION; + } + }); + + writeFileSync(`${dir}/plugins.nix`, genPluginsText(plugins)); + + // Get new hash + const caddyPkgAttr = 'nixosConfigurations.thingone.config.services.caddy.package'; + + const NEW_HASH = spawnSync( + `nix build "$FLAKE#${caddyPkgAttr}" |& sed -n 's/.*got: *//p'`, + [], + { shell: true }, + ).stdout.toString().trim(); + + replaceInFile(/hash = ".*";/, `hash = "${NEW_HASH}";`, `${dir}/plugins.nix`); + + return updates; +}; diff --git a/apps/update-sources/src/misc.ts b/apps/update-sources/src/misc.ts index 39ce95b8..40b5e4d4 100644 --- a/apps/update-sources/src/misc.ts +++ b/apps/update-sources/src/misc.ts @@ -1,7 +1,7 @@ import { writeFileSync } from 'node:fs'; import { spawnSync } from 'node:child_process'; -import { parseFetchurl, replaceInFile } from './lib'; +import { parseFetchurl } from './lib'; /* Constants */ @@ -84,78 +84,3 @@ export const runNixUpdate = ( stderr: execution.stderr.toString(), }; }; - - -const genPluginsText = ( - plugins: Record<string, { url: string, version: string }>, -) => `# This file was autogenerated. DO NOT EDIT! -{ - plugins = { -${Object.entries(plugins) - .map(([key, value]) => ` - ${key} = { - url = "${value.url}"; - version = "${value.version}"; - }; - `) - .join('')} - }; - - hash = ""; -} -`; - -export const updateCaddyPlugins = () => { - let updates = ''; - const dir = `${FLAKE}/configurations/cluster/modules/caddy`; - - // Setup workspace - spawnSync( - [ - 'rm -rf /tmp/update-caddy', - 'mkdir -p /tmp/update-caddy', - 'cd /tmp/update-caddy || exit 1', - 'go mod init temp', - ].join('; '), - [], - { shell: true, cwd: '/tmp' }, - ); - - const plugins = JSON.parse(spawnSync('nix', - ['eval', '-f', `${dir}/plugins.nix`, '--json'], - { shell: true }).stdout.toString()).plugins as Record<string, { url: string, version: string }>; - - // Get most recent versions of plugins - Object.entries(plugins).forEach(([key, value]) => { - const NEW_VERSION = spawnSync([ - 'go mod init temp > /dev/null', - `go get ${value.url} > /dev/null`, - `grep '${value.url}' go.mod`, - ].join('; '), [], { shell: true, cwd: '/tmp/update-caddy' }) - .stdout - .toString() - .trim() - .replace(' // indirect', '') - .split(' ')[1]; - - if (plugins[key].version !== NEW_VERSION) { - updates += `${key}: ${plugins[key].version} -> ${NEW_VERSION}`; - plugins[key].version = NEW_VERSION; - } - }); - - writeFileSync(`${dir}/plugins.nix`, genPluginsText(plugins)); - - // Get new hash - const caddyPkgAttr = 'nixosConfigurations.thingone.config.services.caddy.package'; - - const NEW_HASH = spawnSync( - `nix build "$FLAKE#${caddyPkgAttr}" |& sed -n 's/.*got: *//p'`, - [], - { shell: true }, - ).stdout.toString().trim(); - - replaceInFile(/hash = ".*";/, `hash = "${NEW_HASH}";`, `${dir}/plugins.nix`); - - return updates; -}; diff --git a/configurations/cluster/modules/caddy/plugins.nix b/configurations/cluster/modules/caddy/plugins.nix index b4bc47cd..a38dcc25 100644 --- a/configurations/cluster/modules/caddy/plugins.nix +++ b/configurations/cluster/modules/caddy/plugins.nix @@ -1,11 +1,18 @@ # This file was autogenerated. DO NOT EDIT! { plugins = { + certmagic = { + url = "github.com/caddyserver/certmagic"; + version = "v0.22.2"; + type = "version"; + }; + cloudflare = { url = "github.com/caddy-dns/cloudflare"; version = "v0.0.0-20250228175314-1fb64108d4de"; + type = "version"; }; }; - hash = "sha256-YYpsf8HMONR1teMiSymo2y+HrKoxuJMKIea5/NEykGc="; + hash = "sha256-rT+igzVTqHXUHqYFENH9GJYNTgY6oB4XlEzUU/vihdk="; }