1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 7 nssModulesPath = config.system.nssModules.path; 8 cfg = config.services.nscd; 9 10 inherit (lib) singleton; 11 12 cfgFile = pkgs.writeText "nscd.conf" cfg.config; 13 14in 15 16{ 17 18 ###### interface 19 20 options = { 21 22 services.nscd = { 23 24 enable = mkOption { 25 type = types.bool; 26 default = true; 27 description = "Whether to enable the Name Service Cache Daemon."; 28 }; 29 30 config = mkOption { 31 type = types.lines; 32 default = builtins.readFile ./nscd.conf; 33 description = "Configuration to use for Name Service Cache Daemon."; 34 }; 35 36 }; 37 38 }; 39 40 41 ###### implementation 42 43 config = mkIf cfg.enable { 44 45 users.extraUsers.nscd = 46 { isSystemUser = true; 47 description = "Name service cache daemon user"; 48 }; 49 50 systemd.services.nscd = 51 { description = "Name Service Cache Daemon"; 52 53 wantedBy = [ "nss-lookup.target" "nss-user-lookup.target" ]; 54 55 environment = { LD_LIBRARY_PATH = nssModulesPath; }; 56 57 preStart = 58 '' 59 mkdir -m 0755 -p /run/nscd 60 rm -f /run/nscd/nscd.pid 61 mkdir -m 0755 -p /var/db/nscd 62 ''; 63 64 restartTriggers = [ config.environment.etc.hosts.source config.environment.etc."nsswitch.conf".source ]; 65 66 serviceConfig = 67 { ExecStart = "@${pkgs.glibc}/sbin/nscd nscd -f ${cfgFile}"; 68 Type = "forking"; 69 PIDFile = "/run/nscd/nscd.pid"; 70 Restart = "always"; 71 ExecReload = 72 [ "${pkgs.glibc}/sbin/nscd --invalidate passwd" 73 "${pkgs.glibc}/sbin/nscd --invalidate group" 74 "${pkgs.glibc}/sbin/nscd --invalidate hosts" 75 ]; 76 }; 77 78 # Urgggggh... Nscd forks before opening its socket and writing 79 # its pid. So wait until it's ready. 80 postStart = 81 '' 82 while ! ${pkgs.glibc}/sbin/nscd -g -f ${cfgFile} > /dev/null; do 83 sleep 0.2 84 done 85 ''; 86 }; 87 88 }; 89}