1 { config, lib, pkgs, ... }:
7 nssModulesPath = config.system.nssModules.path;
8 cfg = config.services.nscd;
24 Whether to enable the Name Service Cache Daemon.
25 Disabling this is strongly discouraged, as this effectively disables NSS Lookups
26 from all non-glibc NSS modules, including the ones provided by systemd.
30 enableNsncd = mkOption {
34 Whether to use nsncd instead of nscd from glibc.
35 This is a nscd-compatible daemon, that proxies lookups, without any caching.
36 Using nscd from glibc is discouraged.
44 User account under which nscd runs.
52 User group under which nscd runs.
58 default = builtins.readFile ./nscd.conf;
60 Configuration to use for Name Service Cache Daemon.
61 Only used in case glibc-nscd is used.
68 if pkgs.stdenv.hostPlatform.libc == "glibc"
69 then pkgs.stdenv.cc.libc.bin
71 defaultText = lib.literalExpression ''
72 if pkgs.stdenv.hostPlatform.libc == "glibc"
73 then pkgs.stdenv.cc.libc.bin
77 package containing the nscd binary to be used by the service.
78 Ignored when enableNsncd is set to true.
89 config = mkIf cfg.enable {
90 environment.etc."nscd.conf".text = cfg.config;
92 users.users.${cfg.user} = {
97 users.groups.${cfg.group} = { };
99 systemd.services.nscd =
101 description = "Name Service Cache Daemon"
102 + lib.optionalString cfg.enableNsncd " (nsncd)";
104 before = [ "nss-lookup.target" "nss-user-lookup.target" ];
105 wants = [ "nss-lookup.target" "nss-user-lookup.target" ];
106 wantedBy = [ "multi-user.target" ];
107 requiredBy = [ "nss-lookup.target" "nss-user-lookup.target" ];
109 environment = { LD_LIBRARY_PATH = nssModulesPath; };
111 restartTriggers = lib.optionals (!cfg.enableNsncd) ([
112 config.environment.etc.hosts.source
113 config.environment.etc."nsswitch.conf".source
114 config.environment.etc."nscd.conf".source
115 ] ++ optionals config.users.mysql.enable [
116 config.environment.etc."libnss-mysql.cfg".source
117 config.environment.etc."libnss-mysql-root.cfg".source
120 # In some configurations, nscd needs to be started as root; it will
121 # drop privileges after all the NSS modules have read their
122 # configuration files. So prefix the ExecStart command with "!" to
123 # prevent systemd from dropping privileges early. See ExecStart in
124 # systemd.service(5). We use a static user, because some NSS modules
125 # sill want to read their configuration files after the privilege drop
126 # and so users can set the owner of those files to the nscd user.
130 if cfg.enableNsncd then "${pkgs.nsncd}/bin/nsncd"
131 else "!@${cfg.package}/bin/nscd nscd";
132 Type = if cfg.enableNsncd then "notify" else "forking";
137 NoNewPrivileges = true;
138 RestrictSUIDSGID = true;
139 ProtectSystem = "strict";
140 ProtectHome = "read-only";
141 RuntimeDirectory = "nscd";
142 PIDFile = "/run/nscd/nscd.pid";
145 lib.optionals (!cfg.enableNsncd) [
146 "${cfg.package}/bin/nscd --invalidate passwd"
147 "${cfg.package}/bin/nscd --invalidate group"
148 "${cfg.package}/bin/nscd --invalidate hosts"