1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.powerdns-admin;
7
8 configText = ''
9 ${cfg.config}
10 ''
11 + optionalString (cfg.secretKeyFile != null) ''
12 with open('${cfg.secretKeyFile}') as file:
13 SECRET_KEY = file.read()
14 ''
15 + optionalString (cfg.saltFile != null) ''
16 with open('${cfg.saltFile}') as file:
17 SALT = file.read()
18 '';
19in
20{
21 options.services.powerdns-admin = {
22 enable = mkEnableOption (lib.mdDoc "the PowerDNS web interface");
23
24 extraArgs = mkOption {
25 type = types.listOf types.str;
26 default = [ ];
27 example = literalExpression ''
28 [ "-b" "127.0.0.1:8000" ]
29 '';
30 description = lib.mdDoc ''
31 Extra arguments passed to powerdns-admin.
32 '';
33 };
34
35 config = mkOption {
36 type = types.str;
37 default = "";
38 example = ''
39 BIND_ADDRESS = '127.0.0.1'
40 PORT = 8000
41 SQLALCHEMY_DATABASE_URI = 'postgresql://powerdnsadmin@/powerdnsadmin?host=/run/postgresql'
42 '';
43 description = lib.mdDoc ''
44 Configuration python file.
45 See [the example configuration](https://github.com/ngoduykhanh/PowerDNS-Admin/blob/v${pkgs.powerdns-admin.version}/configs/development.py)
46 for options.
47 '';
48 };
49
50 secretKeyFile = mkOption {
51 type = types.nullOr types.path;
52 example = "/etc/powerdns-admin/secret";
53 description = lib.mdDoc ''
54 The secret used to create cookies.
55 This needs to be set, otherwise the default is used and everyone can forge valid login cookies.
56 Set this to null to ignore this setting and configure it through another way.
57 '';
58 };
59
60 saltFile = mkOption {
61 type = types.nullOr types.path;
62 example = "/etc/powerdns-admin/salt";
63 description = lib.mdDoc ''
64 The salt used for serialization.
65 This should be set, otherwise the default is used.
66 Set this to null to ignore this setting and configure it through another way.
67 '';
68 };
69 };
70
71 config = mkIf cfg.enable {
72 systemd.services.powerdns-admin = {
73 description = "PowerDNS web interface";
74 wantedBy = [ "multi-user.target" ];
75 after = [ "networking.target" ];
76
77 environment.FLASK_CONF = builtins.toFile "powerdns-admin-config.py" configText;
78 environment.PYTHONPATH = pkgs.powerdns-admin.pythonPath;
79 serviceConfig = {
80 ExecStart = "${pkgs.powerdns-admin}/bin/powerdns-admin --pid /run/powerdns-admin/pid ${escapeShellArgs cfg.extraArgs}";
81 ExecStartPre = "${pkgs.coreutils}/bin/env FLASK_APP=${pkgs.powerdns-admin}/share/powerdnsadmin/__init__.py ${pkgs.python3Packages.flask}/bin/flask db upgrade -d ${pkgs.powerdns-admin}/share/migrations";
82 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
83 ExecStop = "${pkgs.coreutils}/bin/kill -TERM $MAINPID";
84 PIDFile = "/run/powerdns-admin/pid";
85 RuntimeDirectory = "powerdns-admin";
86 User = "powerdnsadmin";
87 Group = "powerdnsadmin";
88
89 AmbientCapabilities = "CAP_NET_BIND_SERVICE";
90 BindReadOnlyPaths = [
91 "/nix/store"
92 "-/etc/resolv.conf"
93 "-/etc/nsswitch.conf"
94 "-/etc/hosts"
95 "-/etc/localtime"
96 ]
97 ++ (optional (cfg.secretKeyFile != null) cfg.secretKeyFile)
98 ++ (optional (cfg.saltFile != null) cfg.saltFile);
99 CapabilityBoundingSet = "CAP_NET_BIND_SERVICE";
100 # ProtectClock= adds DeviceAllow=char-rtc r
101 DeviceAllow = "";
102 # Implies ProtectSystem=strict, which re-mounts all paths
103 #DynamicUser = true;
104 LockPersonality = true;
105 MemoryDenyWriteExecute = true;
106 NoNewPrivileges = true;
107 PrivateDevices = true;
108 PrivateMounts = true;
109 # Needs to start a server
110 #PrivateNetwork = true;
111 PrivateTmp = true;
112 PrivateUsers = true;
113 ProcSubset = "pid";
114 ProtectClock = true;
115 ProtectHome = true;
116 ProtectHostname = true;
117 # Would re-mount paths ignored by temporary root
118 #ProtectSystem = "strict";
119 ProtectControlGroups = true;
120 ProtectKernelLogs = true;
121 ProtectKernelModules = true;
122 ProtectKernelTunables = true;
123 ProtectProc = "invisible";
124 RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
125 RestrictNamespaces = true;
126 RestrictRealtime = true;
127 RestrictSUIDSGID = true;
128 SystemCallArchitectures = "native";
129 # gunicorn needs setuid
130 SystemCallFilter = [
131 "@system-service"
132 "~@privileged @resources @keyring"
133 # These got removed by the line above but are needed
134 "@setuid @chown"
135 ];
136 TemporaryFileSystem = "/:ro";
137 # Does not work well with the temporary root
138 #UMask = "0066";
139 };
140 };
141
142 users.groups.powerdnsadmin = { };
143 users.users.powerdnsadmin = {
144 description = "PowerDNS web interface user";
145 isSystemUser = true;
146 group = "powerdnsadmin";
147 };
148 };
149
150 # uses attributes of the linked package
151 meta.buildDocsInSandbox = false;
152}