1{ config, options, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.locate;
7in {
8 options.services.locate = {
9 enable = mkOption {
10 type = types.bool;
11 default = false;
12 description = ''
13 If enabled, NixOS will periodically update the database of
14 files used by the <command>locate</command> command.
15 '';
16 };
17
18 interval = mkOption {
19 type = types.str;
20 default = "02:15";
21 example = "hourly";
22 description = ''
23 Update the locate database at this interval. Updates by
24 default at 2:15 AM every day.
25
26 The format is described in
27 <citerefentry><refentrytitle>systemd.time</refentrytitle>
28 <manvolnum>7</manvolnum></citerefentry>.
29 '';
30 };
31
32 # This is no longer supported, but we keep it to give a better warning below
33 period = mkOption { visible = false; };
34
35 extraFlags = mkOption {
36 type = types.listOf types.str;
37 default = [ ];
38 description = ''
39 Extra flags to pass to <command>updatedb</command>.
40 '';
41 };
42
43 output = mkOption {
44 type = types.path;
45 default = "/var/cache/locatedb";
46 description = ''
47 The database file to build.
48 '';
49 };
50
51 localuser = mkOption {
52 type = types.str;
53 default = "nobody";
54 description = ''
55 The user to search non-network directories as, using
56 <command>su</command>.
57 '';
58 };
59
60 includeStore = mkOption {
61 type = types.bool;
62 default = false;
63 description = ''
64 Whether to include <filename>/nix/store</filename> in the locate database.
65 '';
66 };
67 };
68
69 config = {
70 warnings =
71 let opt = options.services.locate.period; in
72 optional opt.isDefined "The ‘services.locate.period’ option in ${showFiles opt.files} has been removed; please replace it with ‘services.locate.interval’, using the systemd.time(7) calendar event format.";
73
74 systemd.services.update-locatedb =
75 { description = "Update Locate Database";
76 path = [ pkgs.su ];
77 script =
78 ''
79 mkdir -m 0755 -p $(dirname ${toString cfg.output})
80 exec updatedb \
81 --localuser=${cfg.localuser} \
82 ${optionalString (!cfg.includeStore) "--prunepaths='/nix/store'"} \
83 --output=${toString cfg.output} ${concatStringsSep " " cfg.extraFlags}
84 '';
85 serviceConfig.Nice = 19;
86 serviceConfig.IOSchedulingClass = "idle";
87 serviceConfig.PrivateTmp = "yes";
88 serviceConfig.PrivateNetwork = "yes";
89 serviceConfig.NoNewPrivileges = "yes";
90 serviceConfig.ReadOnlyDirectories = "/";
91 serviceConfig.ReadWriteDirectories = cfg.output;
92 };
93
94 systemd.timers.update-locatedb = mkIf cfg.enable
95 { description = "Update timer for locate database";
96 partOf = [ "update-locatedb.service" ];
97 wantedBy = [ "timers.target" ];
98 timerConfig.OnCalendar = cfg.interval;
99 };
100 };
101}