1 { config, lib, pkgs, ... }:
4 cfg = config.services.locate;
5 isMLocate = lib.hasPrefix "mlocate" cfg.package.name;
6 isPLocate = lib.hasPrefix "plocate" cfg.package.name;
7 isMorPLocate = isMLocate || isPLocate;
8 isFindutils = lib.hasPrefix "findutils" cfg.package.name;
12 (lib.mkRenamedOptionModule [ "services" "locate" "period" ] [ "services" "locate" "interval" ])
13 (lib.mkRenamedOptionModule [ "services" "locate" "locate" ] [ "services" "locate" "package" ])
14 (lib.mkRemovedOptionModule [ "services" "locate" "includeStore" ] "Use services.locate.prunePaths")
17 options.services.locate = {
18 enable = lib.mkOption {
19 type = lib.types.bool;
22 If enabled, NixOS will periodically update the database of
23 files used by the {command}`locate` command.
27 package = lib.mkPackageOption pkgs [ "findutils" "locate" ] {
31 interval = lib.mkOption {
36 Update the locate database at this interval. Updates by
37 default at 2:15 AM every day.
39 The format is described in
40 {manpage}`systemd.time(7)`.
42 To disable automatic updates, set to `"never"`
43 and run {command}`updatedb` manually.
47 extraFlags = lib.mkOption {
48 type = lib.types.listOf lib.types.str;
51 Extra flags to pass to {command}`updatedb`.
55 output = lib.mkOption {
56 type = lib.types.path;
57 default = "/var/cache/locatedb";
59 The database file to build.
63 localuser = lib.mkOption {
64 type = lib.types.nullOr lib.types.str;
67 The user to search non-network directories as, using
72 pruneFS = lib.mkOption {
73 type = lib.types.listOf lib.types.str;
155 Which filesystem types to exclude from indexing
159 prunePaths = lib.mkOption {
160 type = lib.types.listOf lib.types.path;
172 Which paths to exclude from indexing
176 pruneNames = lib.mkOption {
177 type = lib.types.listOf lib.types.str;
178 default = lib.optionals (!isFindutils) [ ".bzr" ".cache" ".git" ".hg" ".svn" ];
179 defaultText = lib.literalMD ''
180 `[ ".bzr" ".cache" ".git" ".hg" ".svn" ]`, if
181 supported by the locate implementation (i.e. mlocate or plocate).
184 Directory components which should exclude paths containing them from indexing
188 pruneBindMounts = lib.mkOption {
189 type = lib.types.bool;
192 Whether not to index bind mounts
198 config = lib.mkIf cfg.enable {
199 users.groups = lib.mkMerge [
200 (lib.mkIf isMLocate { mlocate = { }; })
201 (lib.mkIf isPLocate { plocate = { }; })
208 permissions = "u+rx,g+x,o+x";
212 mlocate = lib.mkIf isMLocate {
214 source = "${cfg.package}/bin/locate";
216 plocate = lib.mkIf isPLocate {
218 source = "${cfg.package}/bin/plocate";
221 lib.mkIf isMorPLocate {
222 locate = lib.mkMerge [ common mlocate plocate ];
223 plocate = lib.mkIf isPLocate (lib.mkMerge [ common plocate ]);
227 # write /etc/updatedb.conf for manual calls to `updatedb`
228 etc."updatedb.conf".text = ''
229 PRUNEFS="${lib.concatStringsSep " " cfg.pruneFS}"
230 PRUNENAMES="${lib.concatStringsSep " " cfg.pruneNames}"
231 PRUNEPATHS="${lib.concatStringsSep " " cfg.prunePaths}"
232 PRUNE_BIND_MOUNTS="${if cfg.pruneBindMounts then "yes" else "no"}"
235 systemPackages = [ cfg.package ];
237 variables = lib.mkIf isFindutils {
238 LOCATE_PATH = cfg.output;
242 warnings = lib.optional (isMorPLocate && cfg.localuser != null)
243 "mlocate and plocate do not support the services.locate.localuser option. updatedb will run as root. Silence this warning by setting services.locate.localuser = null."
244 ++ lib.optional (isFindutils && cfg.pruneNames != [ ])
245 "findutils locate does not support pruning by directory component"
246 ++ lib.optional (isFindutils && cfg.pruneBindMounts)
247 "findutils locate does not support skipping bind mounts";
249 systemd.services.update-locatedb = {
250 description = "Update Locate Database";
251 path = lib.mkIf (!isMorPLocate) [ pkgs.su ];
253 # mlocate's updatedb takes flags via a configuration file or
254 # on the command line, but not by environment variable.
259 lib.optional (cfg.${x} != [ ])
260 "--${lib.toLower x} '${lib.concatStringsSep " " cfg.${x}}'";
261 args = lib.concatLists (map toFlags [ "pruneFS" "pruneNames" "prunePaths" ]);
264 exec ${cfg.package}/bin/updatedb \
265 --output ${toString cfg.output} ${lib.concatStringsSep " " args} \
266 --prune-bind-mounts ${if cfg.pruneBindMounts then "yes" else "no"} \
267 ${lib.concatStringsSep " " cfg.extraFlags}
270 exec ${cfg.package}/bin/updatedb \
271 ${lib.optionalString (cfg.localuser != null && !isMorPLocate) "--localuser=${cfg.localuser}"} \
272 --output=${toString cfg.output} ${lib.concatStringsSep " " cfg.extraFlags}
274 environment = lib.optionalAttrs (!isMorPLocate) {
275 PRUNEFS = lib.concatStringsSep " " cfg.pruneFS;
276 PRUNEPATHS = lib.concatStringsSep " " cfg.prunePaths;
277 PRUNENAMES = lib.concatStringsSep " " cfg.pruneNames;
278 PRUNE_BIND_MOUNTS = if cfg.pruneBindMounts then "yes" else "no";
282 IOSchedulingClass = "idle";
284 PrivateNetwork = "yes";
285 NoNewPrivileges = "yes";
287 # Use dirOf cfg.output because mlocate creates temporary files next to
288 # the actual database. We could specify and create them as well,
289 # but that would make this quite brittle when they change something.
290 # NOTE: If /var/cache does not exist, this leads to the misleading error message:
291 # update-locatedb.service: Failed at step NAMESPACE spawning …/update-locatedb-start: No such file or directory
292 ReadWritePaths = dirOf cfg.output;
296 systemd.timers.update-locatedb = lib.mkIf (cfg.interval != "never") {
297 description = "Update timer for locate database";
298 partOf = [ "update-locatedb.service" ];
299 wantedBy = [ "timers.target" ];
301 OnCalendar = cfg.interval;
307 meta.maintainers = with lib.maintainers; [ SuperSandro2000 ];