about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/web-apps/lanraragi.nix
blob: 6703da005ab097b71b16948508687fac254422de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{ pkgs, lib, config, ... }:

let
  cfg = config.services.lanraragi;
in
{
  meta.maintainers = with lib.maintainers; [ tomasajt ];

  options.services = {
    lanraragi = {
      enable = lib.mkEnableOption (lib.mdDoc "LANraragi");
      package = lib.mkPackageOption pkgs "lanraragi" { };

      port = lib.mkOption {
        type = lib.types.port;
        default = 3000;
        description = lib.mdDoc "Port for LANraragi's web interface.";
      };

      passwordFile = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        default = null;
        example = "/run/keys/lanraragi-password";
        description = lib.mdDoc ''
          A file containing the password for LANraragi's admin interface.
        '';
      };

      redis = {
        port = lib.mkOption {
          type = lib.types.port;
          default = 6379;
          description = lib.mdDoc "Port for LANraragi's Redis server.";
        };
        passwordFile = lib.mkOption {
          type = lib.types.nullOr lib.types.path;
          default = null;
          example = "/run/keys/redis-lanraragi-password";
          description = lib.mdDoc ''
            A file containing the password for LANraragi's Redis server.
          '';
        };
      };
    };
  };

  config = lib.mkIf cfg.enable {
    services.redis.servers.lanraragi = {
      enable = true;
      port = cfg.redis.port;
      requirePassFile = cfg.redis.passwordFile;
    };

    systemd.services.lanraragi = {
      description = "LANraragi main service";
      after = [ "network.target" "redis-lanraragi.service" ];
      requires = [ "redis-lanraragi.service" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        ExecStart = lib.getExe cfg.package;
        DynamicUser = true;
        StateDirectory = "lanraragi";
        RuntimeDirectory = "lanraragi";
        LogsDirectory = "lanraragi";
        Restart = "on-failure";
        WorkingDirectory = "/var/lib/lanraragi";
      };
      environment = {
        "LRR_TEMP_DIRECTORY" = "/run/lanraragi";
        "LRR_LOG_DIRECTORY" = "/var/log/lanraragi";
        "LRR_NETWORK" = "http://*:${toString cfg.port}";
        "HOME" = "/var/lib/lanraragi";
      };
      preStart = ''
        cat > lrr.conf <<EOF
        {
          redis_address => "127.0.0.1:${toString cfg.redis.port}",
          redis_password => "${lib.optionalString (cfg.redis.passwordFile != null) ''$(head -n1 ${cfg.redis.passwordFile})''}",
          redis_database => "0",
          redis_database_minion => "1",
          redis_database_config => "2",
          redis_database_search => "3",
        }
        EOF
      '' + lib.optionalString (cfg.passwordFile != null) ''
        ${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
          SELECT 2
          HSET LRR_CONFIG password $(${cfg.package}/bin/helpers/lrr-make-password-hash $(head -n1 ${cfg.passwordFile}))
        EOF
      '';
    };
  };
}