about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/web-apps/microbin.nix
blob: 233bfac6e6995f40c25e2ddea58167672d160604 (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
{ config, lib, pkgs, ... }:

let
  cfg = config.services.microbin;
in
{
  options.services.microbin = {
    enable = lib.mkEnableOption (lib.mdDoc "MicroBin is a super tiny, feature rich, configurable paste bin web application");

    package = lib.mkPackageOption pkgs "microbin" { };

    settings = lib.mkOption {
      type = lib.types.submodule { freeformType = with lib.types; attrsOf (oneOf [ bool int str ]); };
      default = { };
      example = {
        MICROBIN_PORT = 8080;
        MICROBIN_HIDE_LOGO = false;
      };
      description = lib.mdDoc ''
        Additional configuration for MicroBin, see
        <https://microbin.eu/docs/installation-and-configuration/configuration/>
        for supported values.

        For secrets use passwordFile option instead.
      '';
    };

    dataDir = lib.mkOption {
      type = lib.types.str;
      default = "/var/lib/microbin";
      description = lib.mdDoc "Default data folder for MicroBin.";
    };

    passwordFile = lib.mkOption {
      type = lib.types.nullOr lib.types.path;
      default = null;
      example = "/run/secrets/microbin.env";
      description = lib.mdDoc ''
        Path to file containing environment variables.
        Useful for passing down secrets.
        Variables that can be considered secrets are:
         - MICROBIN_BASIC_AUTH_USERNAME
         - MICROBIN_BASIC_AUTH_PASSWORD
         - MICROBIN_ADMIN_USERNAME
         - MICROBIN_ADMIN_PASSWORD
         - MICROBIN_UPLOADER_PASSWORD
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    services.microbin.settings = with lib; {
      MICROBIN_BIND = mkDefault "0.0.0.0";
      MICROBIN_DISABLE_TELEMETRY = mkDefault true;
      MICROBIN_LIST_SERVER = mkDefault false;
      MICROBIN_PORT = mkDefault "8080";
    };

    systemd.services.microbin = {
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      environment = lib.mapAttrs (_: v: if lib.isBool v then lib.boolToString v else toString v) cfg.settings;
      serviceConfig = {
        CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
        DevicePolicy = "closed";
        DynamicUser = true;
        EnvironmentFile = lib.optional (cfg.passwordFile != null) cfg.passwordFile;
        ExecStart = "${cfg.package}/bin/microbin";
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        PrivateDevices = true;
        PrivateUsers = true;
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ReadWritePaths = cfg.dataDir;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        StateDirectory = "microbin";
        SystemCallArchitectures = [ "native" ];
        SystemCallFilter = [ "@system-service" ];
        WorkingDirectory = cfg.dataDir;
      };
    };
  };

  meta.maintainers = with lib.maintainers; [ surfaceflinger ];
}