git.iko.soy/typeable/nixpkgs
on master help sign in
nsd service: add build time config validation
commitfd46f18cf6cb0edcc5a775e7be95a71297f0c54e authorChristoph Hrdinka <c.github@hrdinka.at> dateTue 19 Jan 2016, 21:53 (11 years ago) parent73630b8
1 file changed+58−28
nixos/modules/services/networking/nsd.nix+58−28
nixos/modules/services/networking/nsd.nix+58−28
@@ -9,6 +9,7 @@ let
99 stateDir = "/var/lib/nsd";
1010 pidFile = stateDir + "/var/nsd.pid";
1111
12+ # build nsd with the options needed for the given config
1213 nsdPkg = pkgs.nsd.override {
1314 bind8Stats = cfg.bind8Stats;
1415 ipv6 = cfg.ipv6;
@@ -17,21 +18,42 @@ let
1718 zoneStats = length (collect (x: (x.zoneStats or null) != null) cfg.zones) > 0;
1819 };
1920
20− zoneFiles = pkgs.stdenv.mkDerivation {
21− preferLocalBuild = true;
21+
22+ nsdEnv = pkgs.buildEnv {
2223 name = "nsd-env";
23− buildCommand = concatStringsSep "\n"
24− [ "mkdir -p $out"
25− (concatStrings (mapAttrsToList (zoneName: zoneOptions: ''
26− cat > "$out/${zoneName}" <<_EOF_
27− ${zoneOptions.data}
28− _EOF_
29− '') zoneConfigs))
30− ];
24+
25+ paths = [ configFile ]
26+ ++ mapAttrsToList (name: zone: writeZoneData name zone.data) zoneConfigs;
27+
28+ postBuild = ''
29+ echo "checking zone files"
30+ cd $out/zones
31+
32+ for zoneFile in *; do
33+ ${nsdPkg}/sbin/nsd-checkzone "$zoneFile" "$zoneFile" || {
34+ if grep -q \\\\\\$ "$zoneFile"; then
35+ echo zone "$zoneFile" contains escaped dollar signes \\\$
36+ echo Escaping them is not needed any more. Please make shure \
37+ to unescape them where they prefix a variable name
38+ fi
39+
40+ exit 1
41+ }
42+ done
43+
44+ echo "checking configuration file"
45+ ${nsdPkg}/sbin/nsd-checkconf $out/nsd.conf
46+ '';
47+ };
48+
49+ writeZoneData = name: text: pkgs.writeTextFile {
50+ inherit name text;
51+ destination = "/zones/${name}";
3152 };
3253
54+
3355 # options are ordered alphanumerically by the nixos option name
34− configFile = pkgs.writeText "nsd.conf" ''
56+ configFile = pkgs.writeTextDir "nsd.conf" ''
3557 server:
3658 chroot: "${stateDir}"
3759 username: ${username}
@@ -56,7 +78,7 @@ let
5678 ipv4-edns-size: ${toString cfg.ipv4EDNSSize}
5779 do-ip6: ${yesOrNo cfg.ipv6}
5880 ipv6-edns-size: ${toString cfg.ipv6EDNSSize}
59− log-time-asci: ${yesOrNo cfg.logTimeAscii}
81+ log-time-ascii: ${yesOrNo cfg.logTimeAscii}
6082 ${maybeString "nsid: " cfg.nsid}
6183 port: ${toString cfg.port}
6284 reuseport: ${yesOrNo cfg.reuseport}
@@ -89,7 +111,6 @@ let
89111 server-key-file: "${cfg.remoteControl.serverKeyFile}"
90112 server-cert-file: "${cfg.remoteControl.serverCertFile}"
91113
92− # zone files reside in "${zoneFiles}" linked to "${stateDir}/zones"
93114 ${concatStrings (mapAttrsToList zoneConfigFile zoneConfigs)}
94115
95116 ${cfg.extraConfig}
@@ -111,8 +132,8 @@ let
111132 secret=$(cat "${keyOptions.keyFile}")
112133 dest="${stateDir}/private/${keyName}"
113134 echo " secret: \"$secret\"" > "$dest"
114− ${pkgs.coreutils}/bin/chown ${username}:${username} "$dest"
115− ${pkgs.coreutils}/bin/chmod 0400 "$dest"
135+ chown ${username}:${username} "$dest"
136+ chmod 0400 "$dest"
116137 '') cfg.keys);
117138
118139
@@ -730,31 +751,40 @@ in
730751
731752 systemd.services.nsd = {
732753 description = "NSD authoritative only domain name service";
754+
755+ after = [ "keys.target" "network.target" ];
733756 wantedBy = [ "multi-user.target" ];
734− after = [ "network.target" ];
757+ wants = [ "keys.target" ];
735758
736759 serviceConfig = {
760+ ExecStart = "${nsdPkg}/sbin/nsd -d -c ${nsdEnv}/nsd.conf";
737761 PIDFile = pidFile;
738762 Restart = "always";
739− ExecStart = "${nsdPkg}/sbin/nsd -d -c ${configFile}";
763+ RestartSec = "4s";
764+ StartLimitBurst = 4;
765+ StartLimitInterval = "5min";
740766 };
741767
742768 preStart = ''
743− ${pkgs.coreutils}/bin/mkdir -m 0700 -p "${stateDir}/private"
744− ${pkgs.coreutils}/bin/mkdir -m 0700 -p "${stateDir}/tmp"
745− ${pkgs.coreutils}/bin/mkdir -m 0700 -p "${stateDir}/var"
769+ rm -Rf "${stateDir}/private/"
770+ rm -Rf "${stateDir}/tmp/"
746771
747− ${pkgs.coreutils}/bin/touch "${stateDir}/don't touch anything in here"
772+ mkdir -m 0700 -p "${stateDir}/private"
773+ mkdir -m 0700 -p "${stateDir}/tmp"
774+ mkdir -m 0700 -p "${stateDir}/var"
748775
749− ${pkgs.coreutils}/bin/rm -f "${stateDir}/private/"*
750− ${pkgs.coreutils}/bin/rm -f "${stateDir}/tmp/"*
776+ cat > "${stateDir}/don't touch anything in here" << EOF
777+ Everything in this directory except NSD's state in var is
778+ automatically generated and will be purged and redeployed
779+ by the nsd.service pre-start script.
780+ EOF
751781
752− ${pkgs.coreutils}/bin/chown nsd:nsd -R "${stateDir}/private"
753− ${pkgs.coreutils}/bin/chown nsd:nsd -R "${stateDir}/tmp"
754− ${pkgs.coreutils}/bin/chown nsd:nsd -R "${stateDir}/var"
782+ chown ${username}:${username} -R "${stateDir}/private"
783+ chown ${username}:${username} -R "${stateDir}/tmp"
784+ chown ${username}:${username} -R "${stateDir}/var"
755785
756− ${pkgs.coreutils}/bin/rm -rf "${stateDir}/zones"
757− ${pkgs.coreutils}/bin/cp -r "${zoneFiles}" "${stateDir}/zones"
786+ rm -rf "${stateDir}/zones"
787+ cp -rL "${nsdEnv}/zones" "${stateDir}/zones"
758788
759789 ${copyKeys}
760790 '';