From 4016009137056faa15bbc0c5604d750056a7d40a Mon Sep 17 00:00:00 2001 From: Moraxyc Date: Thu, 9 May 2024 14:41:25 +0800 Subject: [PATCH 1/2] artalk: init at 2.8.6 --- pkgs/by-name/ar/artalk/package.nix | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 pkgs/by-name/ar/artalk/package.nix diff --git a/pkgs/by-name/ar/artalk/package.nix b/pkgs/by-name/ar/artalk/package.nix new file mode 100644 index 000000000000..d2f71dac740f --- /dev/null +++ b/pkgs/by-name/ar/artalk/package.nix @@ -0,0 +1,50 @@ +{ + lib, + buildGoModule, + fetchFromGitHub, + artalk, + testers, + fetchurl, +}: +buildGoModule rec { + pname = "artalk"; + version = "2.8.6"; + + src = fetchFromGitHub { + owner = "ArtalkJS"; + repo = "artalk"; + rev = "v${version}"; + hash = "sha256-ya/by1PaWdYS/Fsbu6wDKuUcPy55/2F5hJEqko4K57o="; + }; + web = fetchurl { + url = "https://github.com/${src.owner}/${src.repo}/releases/download/v${version}/artalk_ui.tar.gz"; + hash = "sha256-3Rg5mCFigLkZ+X8Fxe6A16THd9j6hcTYMEAKU1SrLMw="; + }; + + CGO_ENABLED = 1; + + vendorHash = "sha256-R4/keVGCpCZfLrb2OrK9vdK+N+VKFLAvFXEOA1feqKo="; + + ldflags = [ + "-s" + "-w" + "-X github.com/ArtalkJS/Artalk/internal/config.Version=${version}" + "-X github.com/ArtalkJS/Artalk/internal/config.CommitHash=${version}" + ]; + preBuild = '' + tar -xzf ${web} + cp -r ./artalk_ui/* ./public + ''; + + passthru.tests = { + version = testers.testVersion { package = artalk; }; + }; + + meta = with lib; { + description = "A self-hosted comment system"; + homepage = "https://github.com/ArtalkJS/Artalk"; + license = licenses.mit; + maintainers = with maintainers; [ moraxyc ]; + mainProgram = "Artalk"; + }; +} From da28a5ff2a83b8da13a8e54486781ff72f161d4c Mon Sep 17 00:00:00 2001 From: Moraxyc Date: Thu, 9 May 2024 14:41:39 +0800 Subject: [PATCH 2/2] nixos/artalk: init module --- nixos/modules/module-list.nix | 1 + nixos/modules/services/web-apps/artalk.nix | 131 +++++++++++++++++++++ nixos/tests/all-tests.nix | 1 + nixos/tests/artalk.nix | 28 +++++ 4 files changed, 161 insertions(+) create mode 100644 nixos/modules/services/web-apps/artalk.nix create mode 100644 nixos/tests/artalk.nix diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 4ee71ac69239..a690d08decb1 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -1321,6 +1321,7 @@ ./services/web-apps/akkoma.nix ./services/web-apps/alps.nix ./services/web-apps/anuko-time-tracker.nix + ./services/web-apps/artalk.nix ./services/web-apps/atlassian/confluence.nix ./services/web-apps/atlassian/crowd.nix ./services/web-apps/atlassian/jira.nix diff --git a/nixos/modules/services/web-apps/artalk.nix b/nixos/modules/services/web-apps/artalk.nix new file mode 100644 index 000000000000..d3d06f1521b6 --- /dev/null +++ b/nixos/modules/services/web-apps/artalk.nix @@ -0,0 +1,131 @@ +{ + config, + lib, + pkgs, + utils, + ... +}: +let + cfg = config.services.artalk; + settingsFormat = pkgs.formats.json { }; +in +{ + + meta = { + maintainers = with lib.maintainers; [ moraxyc ]; + }; + + options = { + services.artalk = { + enable = lib.mkEnableOption "artalk, a comment system"; + configFile = lib.mkOption { + type = lib.types.str; + default = "/etc/artalk/config.yml"; + description = "Artalk config file path. If it is not exist, Artalk will generate one."; + }; + allowModify = lib.mkOption { + type = lib.types.bool; + default = true; + description = "allow Artalk store the settings to config file persistently"; + }; + workdir = lib.mkOption { + type = lib.types.str; + default = "/var/lib/artalk"; + description = "Artalk working directory"; + }; + user = lib.mkOption { + type = lib.types.str; + default = "artalk"; + description = "Artalk user name."; + }; + + group = lib.mkOption { + type = lib.types.str; + default = "artalk"; + description = "Artalk group name."; + }; + + package = lib.mkPackageOption pkgs "artalk" { }; + settings = lib.mkOption { + type = lib.types.submodule { + freeformType = settingsFormat.type; + options = { + host = lib.mkOption { + type = lib.types.str; + default = "0.0.0.0"; + description = '' + Artalk server listen host + ''; + }; + port = lib.mkOption { + type = lib.types.port; + default = 23366; + description = '' + Artalk server listen port + ''; + }; + }; + }; + default = { }; + description = '' + The artalk configuration. + + If you set allowModify to true, Artalk will be able to store the settings in the config file persistently. This section's content will update in the config file after the service restarts. + + Options containing secret data should be set to an attribute set + containing the attribute `_secret` - a string pointing to a file + containing the value the option should be set to. + ''; + }; + }; + }; + + config = lib.mkIf cfg.enable { + users.users.artalk = lib.optionalAttrs (cfg.user == "artalk") { + description = "artalk user"; + isSystemUser = true; + group = cfg.group; + }; + users.groups.artalk = lib.optionalAttrs (cfg.group == "artalk") { }; + + environment.systemPackages = [ cfg.package ]; + + systemd.services.artalk = { + after = [ "network.target" ]; + wantedBy = [ "multi-user.target" ]; + preStart = + '' + umask 0077 + ${utils.genJqSecretsReplacementSnippet cfg.settings "/run/artalk/new"} + '' + + ( + if cfg.allowModify then + '' + [ -e "${cfg.configFile}" ] || ${lib.getExe cfg.package} gen config "${cfg.configFile}" + cat "${cfg.configFile}" | ${lib.getExe pkgs.yj} > "/run/artalk/old" + ${lib.getExe pkgs.jq} -s '.[0] * .[1]' "/run/artalk/old" "/run/artalk/new" > "/run/artalk/result" + cat "/run/artalk/result" | ${lib.getExe pkgs.yj} -r > "${cfg.configFile}" + rm /run/artalk/{old,new,result} + '' + else + '' + cat /run/artalk/new | ${lib.getExe pkgs.yj} -r > "${cfg.configFile}" + rm /run/artalk/new + '' + ); + serviceConfig = { + User = cfg.user; + Group = cfg.group; + Type = "simple"; + ExecStart = "${lib.getExe cfg.package} server --config ${cfg.configFile} --workdir ${cfg.workdir} --host ${cfg.settings.host} --port ${builtins.toString cfg.settings.port}"; + Restart = "on-failure"; + RestartSec = "5s"; + ConfigurationDirectory = [ "artalk" ]; + StateDirectory = [ "artalk" ]; + RuntimeDirectory = [ "artalk" ]; + AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]; + ProtectHome = "yes"; + }; + }; + }; +} diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index ba876fe31fc0..89669f7fdf75 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -130,6 +130,7 @@ in { apparmor = handleTest ./apparmor.nix {}; archi = handleTest ./archi.nix {}; armagetronad = handleTest ./armagetronad.nix {}; + artalk = handleTest ./artalk.nix {}; atd = handleTest ./atd.nix {}; atop = handleTest ./atop.nix {}; atuin = handleTest ./atuin.nix {}; diff --git a/nixos/tests/artalk.nix b/nixos/tests/artalk.nix new file mode 100644 index 000000000000..1338e5cd380c --- /dev/null +++ b/nixos/tests/artalk.nix @@ -0,0 +1,28 @@ +import ./make-test-python.nix ( + { lib, pkgs, ... }: + { + + name = "artalk"; + + meta = { + maintainers = with lib.maintainers; [ moraxyc ]; + }; + + nodes.machine = + { pkgs, ... }: + { + environment.systemPackages = [ pkgs.curl ]; + services.artalk = { + enable = true; + }; + }; + + testScript = '' + machine.wait_for_unit("artalk.service") + + machine.wait_for_open_port(23366) + + machine.succeed("curl --fail --max-time 10 http://127.0.0.1:23366/") + ''; + } +)