about summary refs log tree commit diff
path: root/application/module.nix
blob: 78ac54601febfa4d434a4d5645f0770bf8aaa401 (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
99
100
101
102
{
  lib,
  config,
  pkgs,
  self,
  ...
}:

let
  inherit (lib)
    mkIf
    mkOption
    mkEnableOption
    types
    ;
  inherit (pkgs) stdenv;
  inherit (stdenv.hostPlatform) system;

  cfg = config.services.specimen;
in
{
  options.services.specimen = {
    enable = mkEnableOption "specimen application";

    package = mkOption {
      type = types.package;
      description = "specimen package to use.";
      default = self.packages.${system}.default;
    };

    port = mkOption {
      type = types.port;
      default = 4444;
      description = "port that specimen will listen on.";
    };

    listenAddress = mkOption {
      type = types.str;
      default = "0.0.0.0";
      description = "address that specimen will listen on.";
    };

    openFirewall = mkOption {
      type = types.bool;
      default = false;
      description = "open specimen port in firewall for incoming connections.";
    };

    namePath = mkOption {
      type = types.path;
      description = "path from which specimen will get the content to reply with.";
    };
  };

  config = mkIf cfg.enable {
    networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];

    systemd.services.specimen = {
      description = "specimen application service";
      wantedBy = [ "multi-user.target" ];
      wants = [ "network.target" ];
      after = [ "network.target" ];

      serviceConfig = {
        DynamicUser = true;
        Type = "exec";
        Restart = "always";
        ExecStart = "${cfg.package}/bin/specimen -address ${cfg.listenAddress} -port ${toString cfg.port} -name ${cfg.namePath}";

        # a gigantic amount of hardening!!
        # realistically this much wouldn't be necessary.
        # mostly here to show off ;3
        # for documentation, see: https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html
        LockPersonality = true;
        CapabilityBoundingSet = [ "" ];
        DeviceAllow = [ "" ];
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateTmp = true;
        ProtectClock = true;
        ProtectProc = "invisible";
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectSystem = "strict";
        RemoveIPC = true;
        RestrictAddressFamilies = [
          "AF_INET"
          "AF_INET6"
        ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
      };
    };
  };
}