git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/spacecookie: add service module and test
commit25503db8e845ccfa3db20ed1049837868d53775a authorsternenseemann <git@lukasepple.de> dateFri 24 May 2019, 21:17 (7 years ago) parent6eff44f
4 files changed+136
nixos/modules/module-list.nix+1 nixos/modules/services/networking/spacecookie.nix+83 nixos/tests/all-tests.nix+1 nixos/tests/spacecookie.nix+51
nixos/modules/module-list.nix+1
@@ -693,6 +693,7 @@
693693 ./services/networking/sniproxy.nix
694694 ./services/networking/smokeping.nix
695695 ./services/networking/softether.nix
696+ ./services/networking/spacecookie.nix
696697 ./services/networking/spiped.nix
697698 ./services/networking/squid.nix
698699 ./services/networking/sslh.nix
nixos/modules/services/networking/spacecookie.nixnew file+83
@@ -0,0 +1,83 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.spacecookie;
7+ configFile = pkgs.writeText "spacecookie.json" (lib.generators.toJSON {} {
8+ inherit (cfg) hostname port root;
9+ });
10+in {
11+
12+ options = {
13+
14+ services.spacecookie = {
15+
16+ enable = mkEnableOption "spacecookie";
17+
18+ hostname = mkOption {
19+ type = types.str;
20+ default = "localhost";
21+ description = "The hostname the service is reachable via. Clients will use this hostname for further requests after loading the initial gopher menu.";
22+ };
23+
24+ port = mkOption {
25+ type = types.port;
26+ default = 70;
27+ description = "Port the gopher service should be exposed on.";
28+ };
29+
30+ root = mkOption {
31+ type = types.path;
32+ default = "/srv/gopher";
33+ description = "The root directory spacecookie serves via gopher.";
34+ };
35+ };
36+ };
37+
38+ config = mkIf cfg.enable {
39+
40+ systemd.sockets.spacecookie = {
41+ description = "Socket for the Spacecookie Gopher Server";
42+ wantedBy = [ "sockets.target" ];
43+ listenStreams = [ "[::]:${toString cfg.port}" ];
44+ socketConfig = {
45+ BindIPv6Only = "both";
46+ };
47+ };
48+
49+ systemd.services.spacecookie = {
50+ description = "Spacecookie Gopher Server";
51+ wantedBy = [ "multi-user.target" ];
52+ requires = [ "spacecookie.socket" ];
53+
54+ serviceConfig = {
55+ Type = "notify";
56+ ExecStart = "${pkgs.haskellPackages.spacecookie}/bin/spacecookie ${configFile}";
57+ FileDescriptorStoreMax = 1;
58+
59+ DynamicUser = true;
60+
61+ ProtectSystem = "strict";
62+ ProtectHome = true;
63+ PrivateTmp = true;
64+ PrivateDevices = true;
65+ PrivateMounts = true;
66+ PrivateUsers = true;
67+
68+ ProtectKernelTunables = true;
69+ ProtectKernelModules = true;
70+ ProtectControlGroups = true;
71+
72+ CapabilityBoundingSet = "";
73+ NoNewPrivileges = true;
74+ LockPersonality = true;
75+ RestrictRealtime = true;
76+
77+ # AF_UNIX for communication with systemd
78+ # AF_INET replaced by BindIPv6Only=both
79+ RestrictAddressFamilies = "AF_UNIX AF_INET6";
80+ };
81+ };
82+ };
83+}
nixos/tests/all-tests.nix+1
@@ -255,6 +255,7 @@ in
255255 smokeping = handleTest ./smokeping.nix {};
256256 snapper = handleTest ./snapper.nix {};
257257 solr = handleTest ./solr.nix {};
258+ spacecookie = handleTest ./spacecookie.nix {};
258259 sonarr = handleTest ./sonarr.nix {};
259260 strongswan-swanctl = handleTest ./strongswan-swanctl.nix {};
260261 sudo = handleTest ./sudo.nix {};
nixos/tests/spacecookie.nixnew file+51
@@ -0,0 +1,51 @@
1+let
2+ gopherRoot = "/tmp/gopher";
3+ gopherHost = "gopherd";
4+ fileContent = "Hello Gopher!";
5+ fileName = "file.txt";
6+in
7+ import ./make-test-python.nix ({...}: {
8+ name = "spacecookie";
9+ nodes = {
10+ ${gopherHost} = {
11+ networking.firewall.allowedTCPPorts = [ 70 ];
12+ systemd.services.spacecookie = {
13+ preStart = ''
14+ mkdir -p ${gopherRoot}/directory
15+ echo "${fileContent}" > ${gopherRoot}/${fileName}
16+ '';
17+ };
18+
19+ services.spacecookie = {
20+ enable = true;
21+ root = gopherRoot;
22+ hostname = gopherHost;
23+ };
24+ };
25+
26+ client = {};
27+ };
28+
29+ testScript = ''
30+ start_all()
31+ ${gopherHost}.wait_for_open_port(70)
32+ ${gopherHost}.wait_for_unit("spacecookie.service")
33+ client.wait_for_unit("network.target")
34+
35+ fileResponse = client.succeed("curl -s gopher://${gopherHost}//${fileName}")
36+
37+ # the file response should return our created file exactly
38+ if not (fileResponse == "${fileContent}\n"):
39+ raise Exception("Unexpected file response")
40+
41+ # sanity check on the directory listing: we serve a directory and a file
42+ # via gopher, so the directory listing should have exactly two entries,
43+ # one with gopher file type 0 (file) and one with file type 1 (directory).
44+ dirResponse = client.succeed("curl -s gopher://${gopherHost}")
45+ dirEntries = [l[0] for l in dirResponse.split("\n") if len(l) > 0]
46+ dirEntries.sort()
47+
48+ if not (["0", "1"] == dirEntries):
49+ raise Exception("Unexpected directory response")
50+ '';
51+ })