···107by other modules (typically the user’s
108<filename>configuration.nix</filename>):
109<option>services.locate.enable</option> (whether the database should
110-be updated) and <option>services.locate.period</option> (when the
111update should be done). It implements its functionality by defining
112two options declared by other modules:
113<option>systemd.services</option> (the set of all systemd services)
114-and <option>services.cron.systemCronJobs</option> (the list of
115-commands to be executed periodically by <command>cron</command>).</para>
116117<example xml:id='locate-example'><title>NixOS Module for the “locate” Service</title>
118<programlisting>
···120121with lib;
122123-let locatedb = "/var/cache/locatedb"; in
00000000000124125-{
126- options = {
127-128- services.locate = {
129-130- enable = mkOption {
131- type = types.bool;
132- default = false;
133- description = ''
134- If enabled, NixOS will periodically update the database of
135- files used by the <command>locate</command> command.
136- '';
137- };
138-139- period = mkOption {
140- type = types.str;
141- default = "15 02 * * *";
142- description = ''
143- This option defines (in the format used by cron) when the
144- locate database is updated. The default is to update at
145- 02:15 at night every day.
146- '';
147- };
1480000149 };
1500151 };
152153 config = {
154-155 systemd.services.update-locatedb =
156 { description = "Update Locate Database";
157 path = [ pkgs.su ];
158 script =
159 ''
160- mkdir -m 0755 -p $(dirname ${locatedb})
161- exec updatedb --localuser=nobody --output=${locatedb} --prunepaths='/tmp /var/tmp /run'
000162 '';
163 };
164165- services.cron.systemCronJobs = optional config.services.locate.enable
166- "${config.services.locate.period} root ${config.systemd.package}/bin/systemctl start update-locatedb.service";
167-000168 };
169-}</programlisting>
0170</example>
171172<xi:include href="option-declarations.xml" />
···107by other modules (typically the user’s
108<filename>configuration.nix</filename>):
109<option>services.locate.enable</option> (whether the database should
110+be updated) and <option>services.locate.interval</option> (when the
111update should be done). It implements its functionality by defining
112two options declared by other modules:
113<option>systemd.services</option> (the set of all systemd services)
114+and <option>systemd.timers</option> (the list of commands to be
115+executed periodically by <command>systemd</command>).</para>
116117<example xml:id='locate-example'><title>NixOS Module for the “locate” Service</title>
118<programlisting>
···120121with lib;
122123+let
124+ cfg = config.services.locate;
125+in {
126+ options.services.locate = {
127+ enable = mkOption {
128+ type = types.bool;
129+ default = false;
130+ description = ''
131+ If enabled, NixOS will periodically update the database of
132+ files used by the <command>locate</command> command.
133+ '';
134+ };
135136+ interval = mkOption {
137+ type = types.str;
138+ default = "02:15";
139+ example = "hourly";
140+ description = ''
141+ Update the locate database at this interval. Updates by
142+ default at 2:15 AM every day.
0000000000000000143144+ The format is described in
145+ <citerefentry><refentrytitle>systemd.time</refentrytitle>
146+ <manvolnum>7</manvolnum></citerefentry>.
147+ '';
148 };
149150+ # Other options omitted for documentation
151 };
152153 config = {
0154 systemd.services.update-locatedb =
155 { description = "Update Locate Database";
156 path = [ pkgs.su ];
157 script =
158 ''
159+ mkdir -m 0755 -p $(dirname ${toString cfg.output})
160+ exec updatedb \
161+ --localuser=${cfg.localuser} \
162+ ${optionalString (!cfg.includeStore) "--prunepaths='/nix/store'"} \
163+ --output=${toString cfg.output} ${concatStringsSep " " cfg.extraFlags}
164 '';
165 };
166167+ systemd.timers.update-locatedb = mkIf cfg.enable
168+ { description = "Update timer for locate database";
169+ partOf = [ "update-locatedb.service" ];
170+ wantedBy = [ "timers.target" ];
171+ timerConfig.OnCalendar = cfg.interval;
172+ };
173 };
174+}
175+</programlisting>
176</example>
177178<xi:include href="option-declarations.xml" />
+65-60
nixos/modules/misc/locate.nix
···1-{ config, lib, pkgs, ... }:
23with lib;
45let
6 cfg = config.services.locate;
7in {
00000000089- ###### interface
0000001011- options = {
00001213- services.locate = {
01415- enable = mkOption {
16- type = types.bool;
17- default = false;
18- description = ''
19- If enabled, NixOS will periodically update the database of
20- files used by the <command>locate</command> command.
21- '';
22- };
2324- period = mkOption {
25- type = types.str;
26- default = "15 02 * * *";
27- description = ''
28- This option defines (in the format used by cron) when the
29- locate database is updated.
30- The default is to update at 02:15 at night every day.
31- '';
32- };
3334- extraFlags = mkOption {
35- type = types.listOf types.str;
36- default = [ ];
37- description = ''
38- Extra flags to pass to <command>updatedb</command>.
39- '';
40- };
41-42- output = mkOption {
43- type = types.path;
44- default = "/var/cache/locatedb";
45- description = ''
46- The database file to build.
47- '';
48- };
49-50- localuser = mkOption {
51- type = types.str;
52- default = "nobody";
53- description = ''
54- The user to search non-network directories as, using
55- <command>su</command>.
56- '';
57- };
58-59- includeStore = mkOption {
60- type = types.bool;
61- default = false;
62- description = ''
63- Whether to include <filename>/nix/store</filename> in the locate database.
64- '';
65- };
66-67 };
68000000069 };
7071- ###### implementation
72-73 config = {
0074 systemd.services.update-locatedb =
75 { description = "Update Locate Database";
76 path = [ pkgs.su ];
···84 '';
85 serviceConfig.Nice = 19;
86 serviceConfig.IOSchedulingClass = "idle";
0000087 };
8889- services.cron.systemCronJobs = optional config.services.locate.enable
90- "${config.services.locate.period} root ${config.systemd.package}/bin/systemctl start update-locatedb.service";
91-00092 };
93-94}
···1+{ config, options, lib, pkgs, ... }:
23with lib;
45let
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+ };
1718+ 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.
2526+ The format is described in
27+ <citerefentry><refentrytitle>systemd.time</refentrytitle>
28+ <manvolnum>7</manvolnum></citerefentry>.
29+ '';
30+ };
3132+ # This is no longer supported, but we keep it to give a better warning below
33+ period = mkOption { visible = false; };
3435+ extraFlags = mkOption {
36+ type = types.listOf types.str;
37+ default = [ ];
38+ description = ''
39+ Extra flags to pass to <command>updatedb</command>.
40+ '';
41+ };
04243+ output = mkOption {
44+ type = types.path;
45+ default = "/var/cache/locatedb";
46+ description = ''
47+ The database file to build.
48+ '';
49+ };
005051+ 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+ '';
0000000000000000000000000058 };
5960+ 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 };
680069 config = {
70+ warnings = let opt = options.services.locate.period; in optional opt.isDefined "The `period` definition in ${showFiles opt.files} has been removed; please replace it with `interval`, using the new systemd.time interval specifier.";
71+72 systemd.services.update-locatedb =
73 { description = "Update Locate Database";
74 path = [ pkgs.su ];
···82 '';
83 serviceConfig.Nice = 19;
84 serviceConfig.IOSchedulingClass = "idle";
85+ serviceConfig.PrivateTmp = "yes";
86+ serviceConfig.PrivateNetwork = "yes";
87+ serviceConfig.NoNewPrivileges = "yes";
88+ serviceConfig.ReadOnlyDirectories = "/";
89+ serviceConfig.ReadWriteDirectories = cfg.output;
90 };
9192+ systemd.timers.update-locatedb = mkIf cfg.enable
93+ { description = "Update timer for locate database";
94+ partOf = [ "update-locatedb.service" ];
95+ wantedBy = [ "timers.target" ];
96+ timerConfig.OnCalendar = cfg.interval;
97+ };
98 };
099}