From 9bfc63096a69689e34e3a7286e8129f10b5e6aec Mon Sep 17 00:00:00 2001 From: Moraxyc Date: Thu, 21 Nov 2024 12:50:14 +0800 Subject: [PATCH 1/2] artalk: build frontend from source --- pkgs/by-name/ar/artalk/package.nix | 71 +++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/pkgs/by-name/ar/artalk/package.nix b/pkgs/by-name/ar/artalk/package.nix index 96de91650e7b..4c1507a9f793 100644 --- a/pkgs/by-name/ar/artalk/package.nix +++ b/pkgs/by-name/ar/artalk/package.nix @@ -2,28 +2,68 @@ lib, buildGoModule, fetchFromGitHub, - artalk, - fetchurl, + nodejs, + pnpm_9, installShellFiles, versionCheckHook, stdenv, - testers, nixosTests, }: -buildGoModule rec { + +let pname = "artalk"; version = "2.9.1"; src = fetchFromGitHub { owner = "ArtalkJS"; repo = "artalk"; - rev = "refs/tags/v${version}"; + tag = "v${version}"; hash = "sha256-gzagE3muNpX/dwF45p11JAN9ElsGXNFQ3fCvF1QhvdU="; }; - web = fetchurl { - url = "https://github.com/${src.owner}/${src.repo}/releases/download/v${version}/artalk_ui.tar.gz"; - hash = "sha256-ckKC4lErKVdJuJ+pGysmMR96a9LkrCYnWB4j6VPP8OY="; - }; + + frontend = stdenv.mkDerivation (finalAttrs: { + pname = "${pname}-frontend"; + + inherit src version; + + nativeBuildInputs = [ + nodejs + pnpm_9.configHook + ]; + + pnpmDeps = pnpm_9.fetchDeps { + inherit (finalAttrs) pname version src; + hash = "sha256-QIfadS2gNPtH006O86EndY/Hx2ml2FoKfUXJF5qoluw="; + }; + + buildPhase = '' + runHook preBuild + + pnpm build:all + pnpm build:auth + + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + + mkdir -p $out/{dist/{i18n,plugins},sidebar} + + # dist + cp ./ui/artalk/dist/{Artalk,ArtalkLite}.{css,js} $out/dist + cp ./ui/artalk/dist/i18n/*.js $out/dist/i18n + cp ./ui/plugin-*/dist/*.js $out/dist/plugins + + # sidebar + cp -r ./ui/artalk-sidebar/dist/* $out/sidebar + + runHook postInstall + ''; + }); +in +buildGoModule { + inherit src pname version; vendorHash = "sha256-oAqYQzOUjly97H5L5PQ9I2SO2KqiUVxdJA+eoPrHD6Q="; @@ -33,8 +73,7 @@ buildGoModule rec { ]; preBuild = '' - tar -xzf ${web} - cp -r ./artalk_ui/* ./public + cp -r ${frontend}/* ./public ''; nativeBuildInputs = [ installShellFiles ]; @@ -47,13 +86,9 @@ buildGoModule rec { ''; doInstallCheck = true; - + nativeInstallCheckInputs = [ versionCheckHook ]; versionCheckProgramArg = "-v"; - nativeInstallCheckInputs = [ - versionCheckHook - ]; - passthru.tests = { inherit (nixosTests) artalk; }; @@ -63,10 +98,6 @@ buildGoModule rec { homepage = "https://github.com/ArtalkJS/Artalk"; changelog = "https://github.com/ArtalkJS/Artalk/releases/tag/v${version}"; license = lib.licenses.mit; - sourceProvenance = with lib.sourceTypes; [ - fromSource - binaryBytecode - ]; maintainers = with lib.maintainers; [ moraxyc ]; mainProgram = "artalk"; }; From 8a7f909947a836f93a068cb26754a3ff4e69f6a9 Mon Sep 17 00:00:00 2001 From: Moraxyc Date: Thu, 21 Nov 2024 12:52:14 +0800 Subject: [PATCH 2/2] nixosTests.artalk: add auth test --- nixos/tests/all-tests.nix | 2 +- nixos/tests/artalk.nix | 68 +++++++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 6ce214cc4ed6..3ff13868939b 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -135,7 +135,7 @@ in { archi = handleTest ./archi.nix {}; aria2 = handleTest ./aria2.nix {}; armagetronad = handleTest ./armagetronad.nix {}; - artalk = handleTest ./artalk.nix {}; + artalk = runTest ./artalk.nix; atd = handleTest ./atd.nix {}; atop = handleTest ./atop.nix {}; atticd = runTest ./atticd.nix; diff --git a/nixos/tests/artalk.nix b/nixos/tests/artalk.nix index 1338e5cd380c..336392ed67e3 100644 --- a/nixos/tests/artalk.nix +++ b/nixos/tests/artalk.nix @@ -1,28 +1,56 @@ -import ./make-test-python.nix ( - { lib, pkgs, ... }: - { +{ lib, pkgs, ... }: +{ - name = "artalk"; + name = "artalk"; - meta = { - maintainers = with lib.maintainers; [ moraxyc ]; - }; + meta = { + maintainers = with lib.maintainers; [ moraxyc ]; + }; - nodes.machine = - { pkgs, ... }: - { - environment.systemPackages = [ pkgs.curl ]; - services.artalk = { - enable = true; + nodes.machine = + { pkgs, ... }: + { + environment.systemPackages = [ + pkgs.curl + pkgs.artalk + pkgs.sudo + ]; + services.artalk = { + enable = true; + settings = { + cache.enabled = true; + admin_users = [ + { + name = "admin"; + email = "admin@example.org"; + # md5 for 'password' + password = "(md5)5F4DCC3B5AA765D61D8327DEB882CF99"; + } + ]; }; }; + }; - testScript = '' - machine.wait_for_unit("artalk.service") + testScript = '' + import json + machine.wait_for_unit("artalk.service") - machine.wait_for_open_port(23366) + machine.wait_for_open_port(23366) - machine.succeed("curl --fail --max-time 10 http://127.0.0.1:23366/") - ''; - } -) + assert '${pkgs.artalk.version}' in machine.succeed("curl --fail --max-time 10 http://127.0.0.1:23366/api/v2/version") + + # Get token + result = json.loads(machine.succeed(""" + curl --fail -X POST --json '{ + "email": "admin@example.org", + "password": "password" + }' 'http://127.0.0.1:23366/api/v2/auth/email/login' + """)) + token = result['token'] + + # Test admin + machine.succeed(f""" + curl --fail -X POST --header 'Authorization: {token}' 'http://127.0.0.1:23366/api/v2/cache/flush' + """) + ''; +}