summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/go.nix
blob: 218ac9221bb5463cf28c85dfc34deb4cff5c4ee7 (plain)
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
{ config, lib, pkgs, ... }:

with import <stockholm/lib>;

let
  cfg = config.krebs.go;

  out = {
    options.krebs.go = api;
    config = lib.mkIf cfg.enable imp;
  };

  api = {
    enable = mkEnableOption "Enable go url shortener";
    port = mkOption {
      type = types.str;
      default = "1337";
      description = "on which port go should run on";
    };
    redisKeyPrefix = mkOption {
      type = types.str;
      default = "go:";
      description = "change the Redis key prefix which defaults to `go:`";
    };
  };

  imp = {
    services.redis = {
      enable = mkDefault true;
      bind = mkDefault "127.0.0.1";
    };

    users.extraUsers.go = rec {
      name = "go";
      uid = genid name;
      description = "go url shortener user";
      home = "/var/lib/go";
      createHome = true;
    };

    systemd.services.go = {
      description = "go url shortener";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      path = with pkgs; [
        go-shortener
      ];

      environment = {
        PORT = cfg.port;
        REDIS_KEY_PREFIX = cfg.redisKeyPrefix;
      };

      restartIfChanged = true;

      serviceConfig = {
        User = "go";
        Restart = "always";
        ExecStart = "${pkgs.go-shortener}/bin/go";
      };
    };
  };

in out