summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/networking/go-camo.nix
blob: d360b0cc6eed9a7be3911e6e82fa6f6b2d3b7639 (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
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
94
95
96
97
98
{
  lib,
  pkgs,
  config,
  ...
}:

let
  cfg = config.services.go-camo;
  inherit (lib)
    mkOption
    mkEnableOption
    mkIf
    types
    optionalString
    ;
in
{
  options.services.go-camo = {
    enable = mkEnableOption "go-camo service";
    listen = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = "Address:Port to bind to for HTTP (default: 0.0.0.0:8080).";
      apply = v: optionalString (v != null) "--listen=${v}";
    };
    sslListen = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = "Address:Port to bind to for HTTPS.";
      apply = v: optionalString (v != null) "--ssl-listen=${v}";
    };
    sslKey = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = "Path to TLS private key.";
      apply = v: optionalString (v != null) "--ssl-key=${v}";
    };
    sslCert = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = "Path to TLS certificate.";
      apply = v: optionalString (v != null) "--ssl-cert=${v}";
    };
    keyFile = mkOption {
      type = types.path;
      default = null;
      description = ''
        A file containing the HMAC key to use for signing URLs.
        The file can contain any string. Can be generated using "openssl rand -base64 18 > the_file".
      '';
    };
    extraOptions = mkOption {
      type = with types; listOf str;
      default = [ ];
      description = "Extra options passed to the go-camo command.";
    };
  };

  config = mkIf cfg.enable {
    systemd.services.go-camo = {
      description = "go-camo service";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      environment = {
        GOCAMO_HMAC_FILE = "%d/hmac";
      };
      script = ''
        GOCAMO_HMAC="$(cat "$GOCAMO_HMAC_FILE")"
        export GOCAMO_HMAC
        exec ${
          lib.escapeShellArgs (
            lib.lists.remove "" (
              [
                "${pkgs.go-camo}/bin/go-camo"
                cfg.listen
                cfg.sslListen
                cfg.sslKey
                cfg.sslCert
              ]
              ++ cfg.extraOptions
            )
          )
        }
      '';
      serviceConfig = {
        NoNewPrivileges = true;
        ProtectSystem = "strict";
        DynamicUser = true;
        User = "gocamo";
        Group = "gocamo";
        LoadCredential = [
          "hmac:${cfg.keyFile}"
        ];
      };
    };
  };
}