diff --git a/nixos/doc/manual/release-notes/rl-2505.section.md b/nixos/doc/manual/release-notes/rl-2505.section.md index cb005a1e9b962..30c31cafea860 100644 --- a/nixos/doc/manual/release-notes/rl-2505.section.md +++ b/nixos/doc/manual/release-notes/rl-2505.section.md @@ -107,6 +107,8 @@ - [duckdns](https://www.duckdns.org), free dynamic DNS. Available with [services.duckdns](options.html#opt-services.duckdns.enable) +- [Linkwarden](https://linkwarden.app/), a self-hosted collaborative bookmark manager to collect, organize, and preserve webpages, articles, and more. Available as [services.linkwarden](#opt-services.linkwarden.enable) + - [nostr-rs-relay](https://git.sr.ht/~gheartsfield/nostr-rs-relay/), This is a nostr relay, written in Rust. Available as [services.nostr-rs-relay](options.html#opt-services.nostr-rs-relay.enable). - [Actual Budget](https://actualbudget.org/), a local-first personal finance app. Available as [services.actual](#opt-services.actual.enable). diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 3912bc43746d8..4f6f9773a6772 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -1517,6 +1517,7 @@ ./services/web-apps/lanraragi.nix ./services/web-apps/lemmy.nix ./services/web-apps/limesurvey.nix + ./services/web-apps/linkwarden.nix ./services/web-apps/mainsail.nix ./services/web-apps/mastodon.nix ./services/web-apps/matomo.nix diff --git a/nixos/modules/services/web-apps/linkwarden.nix b/nixos/modules/services/web-apps/linkwarden.nix new file mode 100644 index 0000000000000..0766bd16279e5 --- /dev/null +++ b/nixos/modules/services/web-apps/linkwarden.nix @@ -0,0 +1,224 @@ +{ + lib, + config, + pkgs, + ... +}: + +let + cfg = config.services.linkwarden; + isPostgresUnixSocket = lib.hasPrefix "/" cfg.database.host; + + inherit (lib) + types + mkIf + mkOption + mkEnableOption + ; +in +{ + options.services.linkwarden = { + enable = mkEnableOption "Linkwarden"; + package = lib.mkPackageOption pkgs "linkwarden" { }; + + storageLocation = mkOption { + type = types.path; + default = "/var/lib/linkwarden"; + description = "Directory used to store media files. If it is not the default, the directory has to be created manually such that the linkwarden user is able to read and write to it."; + }; + cacheLocation = mkOption { + type = types.path; + default = "/var/cache/linkwarden"; + description = "Directory used as cache. If it is not the default, the directory has to be created manually such that the linkwarden user is able to read and write to it."; + }; + + enableRegistration = mkEnableOption "registration for new users"; + + environment = mkOption { + type = types.attrsOf types.str; + default = { }; + example = { + PAGINATION_TAKE_COUNT = "50"; + }; + description = '' + Extra configuration environment variables. Refer to the [documentation](https://docs.linkwarden.app/self-hosting/environment-variables) for options. + ''; + }; + + secretsFile = mkOption { + type = types.str // { + # We don't want users to be able to pass a path literal here but + # it should look like a path. + check = it: lib.isString it && lib.types.path.check it; + }; + example = "/run/secrets/linkwarden"; + description = '' + Path of a file with extra environment variables to be loaded from disk. + This file is not added to the nix store, so it can be used to pass secrets to linkwarden. + Refer to the [documentation](https://docs.linkwarden.app/self-hosting/environment-variables) for options. + + Linkwarden needs at least a nextauth secret. To set a database password use POSTGRES_PASSWORD: + ``` + NEXTAUTH_SECRET= + POSTGRES_PASSWORD= + ``` + ''; + }; + + host = mkOption { + type = types.str; + default = "localhost"; + description = "The host that Linkwarden will listen on."; + }; + port = mkOption { + type = types.port; + default = 3000; + description = "The port that Linkwarden will listen on."; + }; + openFirewall = mkOption { + type = types.bool; + default = false; + description = "Whether to open the Linkwarden port in the firewall"; + }; + user = mkOption { + type = types.str; + default = "linkwarden"; + description = "The user Linkwarden should run as."; + }; + group = mkOption { + type = types.str; + default = "linkwarden"; + description = "The group Linkwarden should run as."; + }; + + database = { + enable = + mkEnableOption "the postgresql database for use with Linkwarden. See {option}`services.postgresql`" + // { + default = true; + }; + createDB = mkEnableOption "the automatic creation of the database for Linkwarden." // { + default = true; + }; + name = mkOption { + type = types.str; + default = "linkwarden"; + description = "The name of the Linkwarden database."; + }; + host = mkOption { + type = types.str; + default = "/run/postgresql"; + example = "127.0.0.1"; + description = "Hostname or address of the postgresql server. If an absolute path is given here, it will be interpreted as a unix socket path."; + }; + port = mkOption { + type = types.port; + default = 5432; + description = "Port of the postgresql server."; + }; + user = mkOption { + type = types.str; + default = "linkwarden"; + description = "The database user for Linkwarden."; + }; + }; + }; + + config = mkIf cfg.enable { + assertions = [ + { + assertion = cfg.database.createDB -> cfg.database.name == cfg.database.user; + message = "The postgres module requires the database name and the database user name to be the same."; + } + ]; + + services.postgresql = mkIf cfg.database.enable { + enable = true; + ensureDatabases = mkIf cfg.database.createDB [ cfg.database.name ]; + ensureUsers = mkIf cfg.database.createDB [ + { + name = cfg.database.user; + ensureDBOwnership = true; + ensureClauses.login = true; + } + ]; + }; + + networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ]; + + services.linkwarden.environment = { + LINKWARDEN_HOST = cfg.host; + LINKWARDEN_PORT = toString cfg.port; + LINKWARDEN_CACHE_DIR = cfg.cacheLocation; + STORAGE_FOLDER = cfg.storageLocation; + NEXT_PUBLIC_DISABLE_REGISTRATION = mkIf (!cfg.enableRegistration) "true"; + DATABASE_URL = mkIf isPostgresUnixSocket "postgresql://${lib.strings.escapeURL cfg.database.user}@localhost/${lib.strings.escapeURL cfg.database.name}?host=${cfg.database.host}"; + DATABASE_PORT = toString cfg.database.port; + DATABASE_HOST = mkIf (!isPostgresUnixSocket) cfg.database.host; + DATABASE_NAME = cfg.database.name; + DATABASE_USER = cfg.database.user; + }; + + systemd.services.linkwarden = { + description = "Linkwarden (Self-hosted collaborative bookmark manager to collect, organize, and preserve webpages, articles, and more...)"; + requires = [ + "network-online.target" + ] ++ lib.optionals cfg.database.enable [ "postgresql.service" ]; + after = [ + "network-online.target" + ] ++ lib.optionals cfg.database.enable [ "postgresql.service" ]; + wantedBy = [ "multi-user.target" ]; + environment = cfg.environment // { + # Required, otherwise chrome dumps core + CHROME_CONFIG_HOME = cfg.cacheLocation; + }; + + serviceConfig = { + Type = "simple"; + Restart = "on-failure"; + RestartSec = 3; + + ExecStart = lib.getExe cfg.package; + EnvironmentFile = cfg.secretsFile; + StateDirectory = "linkwarden"; + CacheDirectory = "linkwarden"; + User = cfg.user; + Group = cfg.group; + + # Hardening + CapabilityBoundingSet = ""; + NoNewPrivileges = true; + PrivateUsers = true; + PrivateTmp = true; + PrivateDevices = true; + PrivateMounts = true; + ProtectClock = true; + ProtectControlGroups = true; + ProtectHome = true; + ProtectHostname = true; + ProtectKernelLogs = true; + ProtectKernelModules = true; + ProtectKernelTunables = true; + RestrictAddressFamilies = [ + "AF_INET" + "AF_INET6" + "AF_UNIX" + ]; + RestrictNamespaces = true; + RestrictRealtime = true; + RestrictSUIDSGID = true; + }; + }; + + users.users = mkIf (cfg.user == "linkwarden") { + linkwarden = { + name = "linkwarden"; + group = cfg.group; + isSystemUser = true; + }; + }; + users.groups = mkIf (cfg.group == "linkwarden") { linkwarden = { }; }; + + meta.maintainers = with lib.maintainers; [ jvanbruegge ]; + }; +} diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index a8c334f62b84e..50f1e841c119d 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -546,6 +546,7 @@ in { lightdm = handleTest ./lightdm.nix {}; lighttpd = handleTest ./lighttpd.nix {}; limesurvey = handleTest ./limesurvey.nix {}; + linkwarden = handleTest ./web-apps/linkwarden.nix {}; listmonk = handleTestOn [ "x86_64-linux" "aarch64-linux" ] ./listmonk.nix {}; litestream = handleTest ./litestream.nix {}; lldap = handleTest ./lldap.nix {}; diff --git a/nixos/tests/web-apps/linkwarden.nix b/nixos/tests/web-apps/linkwarden.nix new file mode 100644 index 0000000000000..ec948b8871e48 --- /dev/null +++ b/nixos/tests/web-apps/linkwarden.nix @@ -0,0 +1,30 @@ +import ../make-test-python.nix ( + { ... }: + { + name = "linkwarden-nixos"; + + nodes.machine = + { pkgs, ... }: + let + secretsFile = pkgs.writeText "linkwarden-secret-env" '' + NEXTAUTH_SECRET=VERY_SENSITIVE_SECRET + ''; + in + { + services.linkwarden = { + enable = true; + enableRegistration = true; + secretsFile = toString secretsFile; + }; + }; + + testScript = '' + machine.wait_for_unit("linkwarden.service") + + machine.wait_for_open_port(3000) + machine.succeed("curl --fail -s http://localhost:3000/") + + machine.succeed("curl -L --fail -s --data '{\"name\":\"Admin\",\"username\":\"admin\",\"password\":\"adminadmin\"}' -H 'Content-Type: application/json' -X POST http://localhost:3000/api/v1/users") + ''; + } +) diff --git a/pkgs/by-name/li/linkwarden/package.nix b/pkgs/by-name/li/linkwarden/package.nix new file mode 100644 index 0000000000000..d25502df2a20f --- /dev/null +++ b/pkgs/by-name/li/linkwarden/package.nix @@ -0,0 +1,143 @@ +{ + lib, + stdenvNoCC, + buildNpmPackage, + fetchFromGitHub, + fetchYarnDeps, + makeWrapper, + nixosTests, + yarnBuildHook, + yarnConfigHook, + # dependencies + bash, + monolith, + nodejs, + openssl, + playwright-driver, + prisma, + prisma-engines, +}: + +let + bcrypt = buildNpmPackage rec { + pname = "bcrypt"; + version = "5.1.1"; + + src = fetchFromGitHub { + owner = "kelektiv"; + repo = "node.bcrypt.js"; + rev = "v${version}"; + hash = "sha256-mgfYEgvgC5JwgUhU8Kn/f1D7n9ljnIODkKotEcxQnDQ="; + }; + + npmDepsHash = "sha256-CPXZ/yLEjTBIyTPVrgCvb+UGZJ6yRZUJOvBSZpLSABY="; + + npmBuildScript = "install"; + + postInstall = '' + cp -r lib $out/lib/node_modules/bcrypt/ + ''; + }; +in +stdenvNoCC.mkDerivation rec { + pname = "linkwarden"; + version = "2.9.3"; + + src = fetchFromGitHub { + owner = "linkwarden"; + repo = "linkwarden"; + tag = "v${version}"; + hash = "sha256-7vSF2g7HZbo5jJ15JF4wTjFT7k+da9Hu7e4USw7+NuU="; + }; + + yarnOfflineCache = fetchYarnDeps { + yarnLock = src + "/yarn.lock"; + hash = "sha256-AugaWscW19VSWJWIj4IykuJp7aGBjuLSUt3Y48Kr3b4="; + }; + + nativeBuildInputs = [ + makeWrapper + nodejs + prisma + yarnBuildHook + yarnConfigHook + ]; + + buildInputs = [ + openssl + ]; + + NODE_ENV = "production"; + + postPatch = '' + substituteInPlace package.json \ + --replace-fail "yarn worker:prod" "ts-node --transpile-only --skip-project scripts/worker.ts" + + for f in lib/api/storage/*Folder.ts lib/api/storage/*File.ts; do + substituteInPlace $f \ + --replace-fail 'path.join(process.cwd(), storagePath + "/" + file' 'path.join(storagePath, file' + done + ''; + + preBuild = '' + export PRISMA_QUERY_ENGINE_LIBRARY="${prisma-engines}/lib/libquery_engine.node" + export PRISMA_QUERY_ENGINE_BINARY="${prisma-engines}/bin/query-engine" + export PRISMA_SCHEMA_ENGINE_BINARY="${prisma-engines}/bin/schema-engine" + prisma generate + ''; + + postBuild = '' + substituteInPlace node_modules/next/dist/server/image-optimizer.js \ + --replace-fail 'this.cacheDir = (0, _path.join)(distDir, "cache", "images");' 'this.cacheDir = (0, _path.join)(process.env.LINKWARDEN_CACHE_DIR, "cache", "images");' + ''; + + installPhase = '' + runHook preInstall + + rm -r node_modules/bcrypt node_modules/.prisma/client/libquery_engine.node node_modules/@next/swc-* + ln -s ${bcrypt}/lib/node_modules/bcrypt node_modules/ + mkdir -p $out/share/linkwarden/.next $out/bin + cp -r * .next $out/share/linkwarden/ + + echo "#!${lib.getExe bash} -e + export DATABASE_URL=\''${DATABASE_URL-"postgresql://\$DATABASE_USER:\$POSTGRES_PASSWORD@\$DATABASE_HOST:\$DATABASE_PORT/\$DATABASE_NAME"} + export npm_config_cache="\$LINKWARDEN_CACHE_DIR/npm" + ${lib.getExe prisma} migrate deploy --schema $out/share/linkwarden/prisma/schema.prisma \ + && ${lib.getExe' nodejs "npm"} start --prefix $out/share/linkwarden -- -H \$LINKWARDEN_HOST -p \$LINKWARDEN_PORT + " > $out/bin/start.sh + chmod +x $out/bin/start.sh + + makeWrapper $out/bin/start.sh $out/bin/linkwarden \ + --prefix PATH : "${ + lib.makeBinPath [ + bash + monolith + openssl + ] + }" \ + --set-default PRISMA_QUERY_ENGINE_LIBRARY "${prisma-engines}/lib/libquery_engine.node" \ + --set-default PRISMA_QUERY_ENGINE_BINARY "${prisma-engines}/bin/query-engine" \ + --set-default PRISMA_SCHEMA_ENGINE_BINARY "${prisma-engines}/bin/schema-engine" \ + --set-default PLAYWRIGHT_LAUNCH_OPTIONS_EXECUTABLE_PATH ${playwright-driver.browsers-chromium}/chromium-*/chrome-linux/chrome \ + --set-default LINKWARDEN_CACHE_DIR /var/cache/linkwarden \ + --set-default LINKWARDEN_HOST localhost \ + --set-default LINKWARDEN_PORT 3000 \ + --set-default STORAGE_FOLDER /var/lib/linkwarden + + runHook postInstall + ''; + + passthru.tests = { + inherit (nixosTests) linkwarden; + }; + + meta = { + description = "Self-hosted collaborative bookmark manager to collect, organize, and preserve webpages, articles, and more..."; + homepage = "https://linkwarden.app/"; + license = lib.licenses.agpl3Only; + maintainers = with lib.maintainers; [ jvanbruegge ]; + platforms = lib.platforms.linux; + mainProgram = "linkwarden"; + }; + +} diff --git a/pkgs/by-name/pr/prisma/package.nix b/pkgs/by-name/pr/prisma/package.nix index 6d1308c8c0ac9..24dde7268f4f7 100644 --- a/pkgs/by-name/pr/prisma/package.nix +++ b/pkgs/by-name/pr/prisma/package.nix @@ -96,6 +96,7 @@ stdenv.mkDerivation (finalAttrs: { homepage = "https://www.prisma.io/"; license = licenses.asl20; maintainers = with maintainers; [ aqrln ]; + mainProgram = "prisma"; platforms = platforms.unix; }; })