summary refs log tree commit diff
path: root/nixos/modules/services/networking/zerobin.nix
blob: 16db25d623045ae2a7523c36953991fbe0741d78 (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
94
95
96
97
98
99
100
101
102
{ config, pkgs, lib, ... }:
with lib;
let
  cfg = config.services.zerobin;

  zerobin_config = pkgs.writeText "zerobin-config.py" ''
  PASTE_FILES_ROOT = "${cfg.dataDir}"
  ${cfg.extraConfig}
  '';

in
  {
    options = {
      services.zerobin = {
        enable = mkEnableOption "0bin";

        dataDir = mkOption {
          type = types.str;
          default = "/var/lib/zerobin";
          description = ''
          Path to the 0bin data directory
          '';
        };

        user = mkOption {
          type = types.str;
          default = "zerobin";
          description = ''
          The user 0bin should run as
          '';
        };

        group = mkOption {
          type = types.str;
          default = "zerobin";
          description = ''
          The group 0bin should run as
          '';
        };

        listenPort = mkOption {
          type = types.int;
          default = 8000;
          example = 1357;
          description = ''
          The port zerobin should listen on
          '';
        };

        listenAddress = mkOption {
          type = types.str;
          default = "localhost";
          example = "127.0.0.1";
          description = ''
          The address zerobin should listen to
          '';
        };

        extraConfig = mkOption {
          type = types.lines;
          default = "";
          example = ''
          MENU = (
          ('Home', '/'),
          )
          COMPRESSED_STATIC_FILE = True
          '';
          description = ''
          Extra configuration to be appended to the 0bin config file
          (see https://0bin.readthedocs.org/en/latest/en/options.html)
          '';
        };
      };
    };

    config = mkIf (cfg.enable) {
      users.users.${cfg.user} =
      if cfg.user == "zerobin" then {
        isSystemUser = true;
        group = cfg.group;
        home = cfg.dataDir;
        createHome = true;
      }
      else {};
      users.groups.${cfg.group} = {};

      systemd.services.zerobin = {
        enable = true;
        after = [ "network.target" ];
        wantedBy = [ "multi-user.target" ];
        serviceConfig.ExecStart = "${pkgs.zerobin}/bin/zerobin ${cfg.listenAddress} ${toString cfg.listenPort} false ${cfg.user} ${cfg.group} ${zerobin_config}";
        serviceConfig.PrivateTmp="yes";
        serviceConfig.User = cfg.user;
        serviceConfig.Group = cfg.group;
        preStart = ''
          mkdir -p ${cfg.dataDir}
          chown ${cfg.user} ${cfg.dataDir}
        '';
      };
    };
  }