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