unbound: run in chroot

Changed files
+16 -16
nixos
modules
services
networking
+16 -16
nixos/modules/services/networking/unbound.nix
···
cfg = config.services.unbound;
-
username = "unbound";
-
stateDir = "/var/lib/unbound";
access = concatMapStrings (x: " access-control: ${x} allow\n") cfg.allowedAccess;
···
confFile = pkgs.writeText "unbound.conf" ''
server:
directory: "${stateDir}"
-
username: ${username}
-
# make sure unbound can access entropy from inside the chroot.
-
# e.g. on linux the use these commands (on BSD, devfs(8) is used):
-
# mount --bind -n /dev/random /etc/unbound/dev/random
-
# and mount --bind -n /dev/log /etc/unbound/dev/log
+
username: unbound
chroot: "${stateDir}"
-
# logfile: "${stateDir}/unbound.log" #uncomment to use logfile.
-
pidfile: "${stateDir}/unbound.pid"
-
verbosity: 1 # uncomment and increase to get more logging.
+
pidfile: ""
${interfaces}
${access}
-
-
${forward}
-
${cfg.extraConfig}
+
${forward}
'';
in
···
environment.systemPackages = [ pkgs.unbound ];
users.extraUsers = singleton {
-
name = username;
+
name = "unbound";
uid = config.ids.uids.unbound;
description = "unbound daemon user";
home = stateDir;
···
wants = [" nss-lookup.target" ];
wantedBy = [ "multi-user.target" ];
-
path = [ pkgs.unbound ];
-
serviceConfig.ExecStart = "${pkgs.unbound}/sbin/unbound -d -c ${confFile}";
+
preStart = ''
+
mkdir -m 0755 -p ${stateDir}/dev/
+
cp ${confFile} ${stateDir}/unbound.conf
+
chown unbound ${stateDir}
+
touch ${stateDir}/dev/random
+
${pkgs.utillinux}/bin/mount --bind -n /dev/random ${stateDir}/dev/random
+
'';
+
+
serviceConfig = {
+
ExecStart = "${pkgs.unbound}/sbin/unbound -d -c ${stateDir}/unbound.conf";
+
ExecStopPost="${pkgs.utillinux}/bin/umount ${stateDir}/dev/random";
+
};
};
};