diff --git a/machines/massicot/secrets.yaml b/machines/massicot/secrets.yaml index fb88246..b5ca7fe 100644 --- a/machines/massicot/secrets.yaml +++ b/machines/massicot/secrets.yaml @@ -3,7 +3,7 @@ gts_env: ENC[AES256_GCM,data:CKFKHXCJvTD0HFkVrBWhabcl/cloCT03qcZIc5JymiIAu+o6wef hedgedoc_env: ENC[AES256_GCM,data:zwAA+zKSJT0tZyYArCaa1lfL0y8DNHDp/thS11DrVxNvjmk38o0ydsKArfZKzFYye+qNBzz1B4sPCdW4cFgQUNgbM+n9AvoMB8CssdmQ+sALKmozA5aEV23q+khZSGlHocP6WA==,iv:SgZruOS1nanK64Ex1dvgoD1HzbGbNa4DFSBuVoaNgEc=,tag:R+I8m1AloDCXs5PdpEpS0w==,type:str] grafana_oauth_secret: ENC[AES256_GCM,data:2dSgxeWXNtlvbrgW9whCVuM6tfzd4lVhynwQTSPbBJndhI8scpJle7LjI1+b14FS9boBsuYO+ym4Pf1I8/jJtKkj6X6I0BmXFBC/SfpCpo+ZGrxacg==,iv:N8iTPqMagKP3hWc7n0bjgYKvaFaw11ITvDn9lUkkAPY=,tag:Cz59fA2Zq3jVvhfxaFuGAA==,type:str] miniflux: - oauth2_secret: ENC[AES256_GCM,data:ktwQgPwcXTmMFhiTjXUGmPysfSg6X+EFBbfZMQ==,iv:vYF86NFW1EGf1TYLicTGiTIRKP/XC914zmVm42SyWPc=,tag:E1BdVp35362X373EE1HKvg==,type:str] + oauth2_secret: ENC[AES256_GCM,data:Q0JeT5VHGEDATXB9jf5+eU1Hoi9FsJrw6IK2T0bodvVgki+1oF+sWld5NGpoiXm/bQ==,iv:e8+84Zk5eXNIyIPhTG8jFhO+DCRorPFG0lDDNT4OxCs=,tag:IxlyFBcFaSy7Nz0aQCH3bw==,type:str] sops: kms: [] gcp_kms: [] @@ -28,8 +28,8 @@ sops: dnFBa0lDWWZtS1BHdzBoVzNTaGNkSEEKi/W1n7RT8NpTp00SBMwxsUJAPDhumJ/i V2VnaSNwouD3SswTcoBzqQpBP9XrqzjIYGke90ZODFQbMY9WDQ+O0g== -----END AGE ENCRYPTED FILE----- - lastmodified: "2024-08-05T07:07:04Z" - mac: ENC[AES256_GCM,data:1/PFLb8fgNgBz4YZDqNUxw5JMzt+ATD1wuFWNwJkVGVlXgLpSuOYZc5TYte3W8HTLDY8YOFYdcZtvDyjeFf7PAGxAVhqE1TgAfs0HxEHY3S8Ivcen86LTmcsiPy1frGHOLhzFfim3GRLyNA+MUFzCSLCbERJ0t0iK+L0EZ9SoyY=,iv:+DeQvoP3xBBEkv0IaQ5R0EFtQ/19bKSG/eRrDp4JPJs=,tag:Xz3sSbS07rcNaoImy10P5A==,type:str] + lastmodified: "2024-08-05T08:53:56Z" + mac: ENC[AES256_GCM,data:DtAL9k/t4pGV2UqCrb1R/1nT3gjJ8wced5yQOF5oneoncg/uuyX7IDZ0iZz0eGirj9Zadh9UQWNwxMzoiNu6pD1v04MkxT0NVDJ32vt5X+YDQJ60vRJjn9+zKvLk8Esx9sFsuBxjVXXmbtev7+djU+LbpPLfaobdheO2XlJXtdU=,iv:y2KI5ylgvuQ7ktYAr6XPEX3qyxnSP7BWC79mdsr4hgk=,tag:cvXvXeKvRwvttgQfmZRi2w==,type:str] pgp: [] unencrypted_suffix: _unencrypted version: 3.9.0 diff --git a/machines/massicot/services.nix b/machines/massicot/services.nix index 7989aeb..96ede16 100644 --- a/machines/massicot/services.nix +++ b/machines/massicot/services.nix @@ -29,6 +29,7 @@ in custom.prometheus = { enable = true; exporters.blackbox.enable = true; + exporters.miniflux.enable = true; }; systemd.mounts = map @@ -89,13 +90,14 @@ in custom.miniflux = { enable = true; environment = { + LOG_LEVEL = "debug"; LISTEN_ADDR = "127.0.0.1:58173"; + BASE_URL = "https://rss.xinyang.life/"; OAUTH2_PROVIDER = "oidc"; - OAUTH2_CLIEND_ID = "miniflux"; + OAUTH2_CLIENT_ID = "miniflux"; OAUTH2_REDIRECT_URL = "https://rss.xinyang.life/oauth2/oidc/callback"; OAUTH2_OIDC_DISCOVERY_ENDPOINT = "https://auth.xinyang.life/oauth2/openid/miniflux"; OAUTH2_USER_CREATION = 1; - CREATE_ADMIN = lib.mkForce ""; }; oauth2SecretFile = config.sops.secrets."miniflux/oauth2_secret".path; }; @@ -255,7 +257,7 @@ in ''; virtualHosts."https://rss.xinyang.life".extraConfig = '' - reverse_proxy ${config.services.miniflux.config.LISTEN_ADDR} + reverse_proxy ${config.custom.miniflux.environment.LISTEN_ADDR} ''; virtualHosts."https://ntfy.xinyang.life".extraConfig = '' diff --git a/modules/nixos/miniflux.nix b/modules/nixos/miniflux.nix index 9fcb8ad..2d539e0 100644 --- a/modules/nixos/miniflux.nix +++ b/modules/nixos/miniflux.nix @@ -1,36 +1,133 @@ { config, pkgs, lib, ... }: let - inherit (lib) mkEnableOption mkOption types; + inherit (lib) mkEnableOption mkPackageOption mkOption types literalExpression mkIf mkDefault; cfg = config.custom.miniflux; + + defaultAddress = "localhost:8080"; + + pgbin = "${config.services.postgresql.package}/bin"; + preStart = pkgs.writeScript "miniflux-pre-start" '' + #!${pkgs.runtimeShell} + ${pgbin}/psql "miniflux" -c "CREATE EXTENSION IF NOT EXISTS hstore" + ''; in { options = { custom.miniflux = { enable = mkEnableOption "miniflux"; + + package = mkPackageOption pkgs "miniflux" { }; + oauth2SecretFile = mkOption { type = types.path; }; - environmentFile = mkOption { - type = types.path; - default = "/dev/null"; - }; + environment = mkOption { type = with types; attrsOf (oneOf [ int str ]); }; + + createDatabaseLocally = mkOption { + type = types.bool; + default = true; + description = '' + Whether a PostgreSQL database should be automatically created and + configured on the local host. If set to `false`, you need provision a + database yourself and make sure to create the hstore extension in it. + ''; + }; }; }; config = lib.mkIf cfg.enable { - services.miniflux = { + services.miniflux.enable = false; + custom.miniflux.environment = { + LISTEN_ADDR = mkDefault defaultAddress; + RUN_MIGRATIONS = mkDefault 1; + DATABASE_URL = lib.mkIf cfg.createDatabaseLocally "user=miniflux host=/run/postgresql dbname=miniflux"; + OAUTH2_CLIENT_SECRET_FILE = "%d/oauth2_secret"; + WATCHDOG = mkDefault 1; + }; + + services.postgresql = lib.mkIf cfg.createDatabaseLocally { enable = true; - adminCredentialsFile = cfg.environmentFile; + ensureUsers = [{ + name = "miniflux"; + ensureDBOwnership = true; + }]; + ensureDatabases = [ "miniflux" ]; }; - systemd.services.miniflux = { + + systemd.services.miniflux-dbsetup = lib.mkIf cfg.createDatabaseLocally { + description = "Miniflux database setup"; + requires = [ "postgresql.service" ]; + after = [ "network.target" "postgresql.service" ]; serviceConfig = { - LoadCredential = [ "oauth2_secret:${cfg.oauth2SecretFile}" ]; - EnvironmentFile = [ "%d/oauth2_secret" ]; + Type = "oneshot"; + User = config.services.postgresql.superUser; + ExecStart = preStart; }; - environment = lib.mapAttrs (_: lib.mkForce) (lib.mapAttrs (_: toString) cfg.environment); }; + + systemd.services.miniflux = { + description = "Miniflux service"; + wantedBy = [ "multi-user.target" ]; + requires = lib.optional cfg.createDatabaseLocally "miniflux-dbsetup.service"; + after = [ "network.target" ] + ++ lib.optionals cfg.createDatabaseLocally [ "postgresql.service" "miniflux-dbsetup.service" ]; + + serviceConfig = { + Type = "notify"; + ExecStart = lib.getExe cfg.package; + User = "miniflux"; + DynamicUser = true; + LoadCredential = [ "oauth2_secret:${cfg.oauth2SecretFile}" ]; + RuntimeDirectory = "miniflux"; + RuntimeDirectoryMode = "0750"; + WatchdogSec = 60; + WatchdogSignal = "SIGKILL"; + Restart = "always"; + RestartSec = 5; + + # Hardening + CapabilityBoundingSet = [ "" ]; + DeviceAllow = [ "" ]; + LockPersonality = true; + MemoryDenyWriteExecute = true; + PrivateDevices = true; + PrivateUsers = true; + ProcSubset = "pid"; + ProtectClock = true; + ProtectControlGroups = true; + ProtectHome = true; + ProtectHostname = true; + ProtectKernelLogs = true; + ProtectKernelModules = true; + ProtectKernelTunables = true; + ProtectProc = "invisible"; + RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ]; + RestrictNamespaces = true; + RestrictRealtime = true; + RestrictSUIDSGID = true; + SystemCallArchitectures = "native"; + SystemCallFilter = [ "@system-service" "~@privileged" ]; + UMask = "0077"; + }; + + environment = lib.mapAttrs (_: toString) cfg.environment; + }; + environment.systemPackages = [ cfg.package ]; + + security.apparmor.policies."bin.miniflux".profile = '' + include + ${cfg.package}/bin/miniflux { + include + include + include + include "${pkgs.apparmorRulesFromClosure { name = "miniflux"; } cfg.package}" + r ${cfg.package}/bin/miniflux, + r @{sys}/kernel/mm/transparent_hugepage/hpage_pmd_size, + rw /run/miniflux/**, + } + ''; }; } diff --git a/modules/nixos/prometheus/default.nix b/modules/nixos/prometheus/default.nix index 8c43908..070e3d2 100644 --- a/modules/nixos/prometheus/default.nix +++ b/modules/nixos/prometheus/default.nix @@ -29,6 +29,7 @@ in ./caddy.nix ./gotosocial.nix ./immich.nix + ./miniflux.nix ./ntfy-sh.nix ./restic.nix ]; @@ -48,7 +49,8 @@ in caddy.enable = mkExporterOption config.services.caddy.enable; gotosocial.enable = mkExporterOption config.services.gotosocial.enable; immich.enable = mkExporterOption config.services.immich.enable; - ntfy-sh.enable = mkExporterOption config.services.gotosocial.enable; + miniflux.enable = mkExporterOption config.services.miniflux.enable; + ntfy-sh.enable = mkExporterOption config.services.ntfy-sh.enable; }; grafana = { enable = mkEnableOption "Grafana Cloud"; diff --git a/modules/nixos/prometheus/miniflux.nix b/modules/nixos/prometheus/miniflux.nix new file mode 100644 index 0000000..2a64f47 --- /dev/null +++ b/modules/nixos/prometheus/miniflux.nix @@ -0,0 +1,17 @@ +{ config, lib, ... }: +let + cfg = config.custom.prometheus; +in +{ + config = lib.mkIf (cfg.enable && cfg.exporters.miniflux.enable) { + systemd.services.miniflux.environment.METRICS_COLLECTOR = 1; + services.prometheus.scrapeConfigs = [ + { + job_name = "miniflux"; + static_configs = [ + { targets = [ config.systemd.services.miniflux.environment.LISTEN_ADDR ]; } + ]; + } + ]; + }; +}