at 24.11-pre 3.0 kB view raw
1{ pkgs, lib, config, ... }: 2 3let 4 cfg = config.services.lanraragi; 5in 6{ 7 meta.maintainers = with lib.maintainers; [ tomasajt ]; 8 9 options.services = { 10 lanraragi = { 11 enable = lib.mkEnableOption "LANraragi"; 12 package = lib.mkPackageOption pkgs "lanraragi" { }; 13 14 port = lib.mkOption { 15 type = lib.types.port; 16 default = 3000; 17 description = "Port for LANraragi's web interface."; 18 }; 19 20 passwordFile = lib.mkOption { 21 type = lib.types.nullOr lib.types.path; 22 default = null; 23 example = "/run/keys/lanraragi-password"; 24 description = '' 25 A file containing the password for LANraragi's admin interface. 26 ''; 27 }; 28 29 redis = { 30 port = lib.mkOption { 31 type = lib.types.port; 32 default = 6379; 33 description = "Port for LANraragi's Redis server."; 34 }; 35 passwordFile = lib.mkOption { 36 type = lib.types.nullOr lib.types.path; 37 default = null; 38 example = "/run/keys/redis-lanraragi-password"; 39 description = '' 40 A file containing the password for LANraragi's Redis server. 41 ''; 42 }; 43 }; 44 }; 45 }; 46 47 config = lib.mkIf cfg.enable { 48 services.redis.servers.lanraragi = { 49 enable = true; 50 port = cfg.redis.port; 51 requirePassFile = cfg.redis.passwordFile; 52 }; 53 54 systemd.services.lanraragi = { 55 description = "LANraragi main service"; 56 after = [ "network.target" "redis-lanraragi.service" ]; 57 requires = [ "redis-lanraragi.service" ]; 58 wantedBy = [ "multi-user.target" ]; 59 serviceConfig = { 60 ExecStart = lib.getExe cfg.package; 61 DynamicUser = true; 62 StateDirectory = "lanraragi"; 63 RuntimeDirectory = "lanraragi"; 64 LogsDirectory = "lanraragi"; 65 Restart = "on-failure"; 66 WorkingDirectory = "/var/lib/lanraragi"; 67 }; 68 environment = { 69 "LRR_TEMP_DIRECTORY" = "/run/lanraragi"; 70 "LRR_LOG_DIRECTORY" = "/var/log/lanraragi"; 71 "LRR_NETWORK" = "http://*:${toString cfg.port}"; 72 "HOME" = "/var/lib/lanraragi"; 73 }; 74 preStart = '' 75 cat > lrr.conf <<EOF 76 { 77 redis_address => "127.0.0.1:${toString cfg.redis.port}", 78 redis_password => "${lib.optionalString (cfg.redis.passwordFile != null) ''$(head -n1 ${cfg.redis.passwordFile})''}", 79 redis_database => "0", 80 redis_database_minion => "1", 81 redis_database_config => "2", 82 redis_database_search => "3", 83 } 84 EOF 85 '' + lib.optionalString (cfg.passwordFile != null) '' 86 ${lib.getExe pkgs.redis} -h 127.0.0.1 -p ${toString cfg.redis.port} ${lib.optionalString (cfg.redis.passwordFile != null) ''-a "$(head -n1 ${cfg.redis.passwordFile})"''}<<EOF 87 SELECT 2 88 HSET LRR_CONFIG password $(${cfg.package}/bin/helpers/lrr-make-password-hash $(head -n1 ${cfg.passwordFile})) 89 EOF 90 ''; 91 }; 92 }; 93}