···107107by other modules (typically the user’s
108108<filename>configuration.nix</filename>):
109109<option>services.locate.enable</option> (whether the database should
110110-be updated) and <option>services.locate.period</option> (when the
110110+be updated) and <option>services.locate.interval</option> (when the
111111update should be done). It implements its functionality by defining
112112two options declared by other modules:
113113<option>systemd.services</option> (the set of all systemd services)
114114-and <option>services.cron.systemCronJobs</option> (the list of
115115-commands to be executed periodically by <command>cron</command>).</para>
114114+and <option>systemd.timers</option> (the list of commands to be
115115+executed periodically by <command>systemd</command>).</para>
116116117117<example xml:id='locate-example'><title>NixOS Module for the “locate” Service</title>
118118<programlisting>
···120120121121with lib;
122122123123-let locatedb = "/var/cache/locatedb"; in
123123+let
124124+ cfg = config.services.locate;
125125+in {
126126+ options.services.locate = {
127127+ enable = mkOption {
128128+ type = types.bool;
129129+ default = false;
130130+ description = ''
131131+ If enabled, NixOS will periodically update the database of
132132+ files used by the <command>locate</command> command.
133133+ '';
134134+ };
124135125125-{
126126- options = {
127127-128128- services.locate = {
129129-130130- enable = mkOption {
131131- type = types.bool;
132132- default = false;
133133- description = ''
134134- If enabled, NixOS will periodically update the database of
135135- files used by the <command>locate</command> command.
136136- '';
137137- };
138138-139139- period = mkOption {
140140- type = types.str;
141141- default = "15 02 * * *";
142142- description = ''
143143- This option defines (in the format used by cron) when the
144144- locate database is updated. The default is to update at
145145- 02:15 at night every day.
146146- '';
147147- };
136136+ interval = mkOption {
137137+ type = types.str;
138138+ default = "02:15";
139139+ example = "hourly";
140140+ description = ''
141141+ Update the locate database at this interval. Updates by
142142+ default at 2:15 AM every day.
148143144144+ The format is described in
145145+ <citerefentry><refentrytitle>systemd.time</refentrytitle>
146146+ <manvolnum>7</manvolnum></citerefentry>.
147147+ '';
149148 };
150149150150+ # Other options omitted for documentation
151151 };
152152153153 config = {
154154-155154 systemd.services.update-locatedb =
156155 { description = "Update Locate Database";
157156 path = [ pkgs.su ];
158157 script =
159158 ''
160160- mkdir -m 0755 -p $(dirname ${locatedb})
161161- exec updatedb --localuser=nobody --output=${locatedb} --prunepaths='/tmp /var/tmp /run'
159159+ mkdir -m 0755 -p $(dirname ${toString cfg.output})
160160+ exec updatedb \
161161+ --localuser=${cfg.localuser} \
162162+ ${optionalString (!cfg.includeStore) "--prunepaths='/nix/store'"} \
163163+ --output=${toString cfg.output} ${concatStringsSep " " cfg.extraFlags}
162164 '';
163165 };
164166165165- services.cron.systemCronJobs = optional config.services.locate.enable
166166- "${config.services.locate.period} root ${config.systemd.package}/bin/systemctl start update-locatedb.service";
167167-167167+ systemd.timers.update-locatedb = mkIf cfg.enable
168168+ { description = "Update timer for locate database";
169169+ partOf = [ "update-locatedb.service" ];
170170+ wantedBy = [ "timers.target" ];
171171+ timerConfig.OnCalendar = cfg.interval;
172172+ };
168173 };
169169-}</programlisting>
174174+}
175175+</programlisting>
170176</example>
171177172178<xi:include href="option-declarations.xml" />
+65-60
nixos/modules/misc/locate.nix
···11-{ config, lib, pkgs, ... }:
11+{ config, options, lib, pkgs, ... }:
2233with lib;
4455let
66 cfg = config.services.locate;
77in {
88+ options.services.locate = {
99+ enable = mkOption {
1010+ type = types.bool;
1111+ default = false;
1212+ description = ''
1313+ If enabled, NixOS will periodically update the database of
1414+ files used by the <command>locate</command> command.
1515+ '';
1616+ };
81799- ###### interface
1818+ interval = mkOption {
1919+ type = types.str;
2020+ default = "02:15";
2121+ example = "hourly";
2222+ description = ''
2323+ Update the locate database at this interval. Updates by
2424+ default at 2:15 AM every day.
10251111- options = {
2626+ The format is described in
2727+ <citerefentry><refentrytitle>systemd.time</refentrytitle>
2828+ <manvolnum>7</manvolnum></citerefentry>.
2929+ '';
3030+ };
12311313- services.locate = {
3232+ # This is no longer supported, but we keep it to give a better warning below
3333+ period = mkOption { visible = false; };
14341515- enable = mkOption {
1616- type = types.bool;
1717- default = false;
1818- description = ''
1919- If enabled, NixOS will periodically update the database of
2020- files used by the <command>locate</command> command.
2121- '';
2222- };
3535+ extraFlags = mkOption {
3636+ type = types.listOf types.str;
3737+ default = [ ];
3838+ description = ''
3939+ Extra flags to pass to <command>updatedb</command>.
4040+ '';
4141+ };
23422424- period = mkOption {
2525- type = types.str;
2626- default = "15 02 * * *";
2727- description = ''
2828- This option defines (in the format used by cron) when the
2929- locate database is updated.
3030- The default is to update at 02:15 at night every day.
3131- '';
3232- };
4343+ output = mkOption {
4444+ type = types.path;
4545+ default = "/var/cache/locatedb";
4646+ description = ''
4747+ The database file to build.
4848+ '';
4949+ };
33503434- extraFlags = mkOption {
3535- type = types.listOf types.str;
3636- default = [ ];
3737- description = ''
3838- Extra flags to pass to <command>updatedb</command>.
3939- '';
4040- };
4141-4242- output = mkOption {
4343- type = types.path;
4444- default = "/var/cache/locatedb";
4545- description = ''
4646- The database file to build.
4747- '';
4848- };
4949-5050- localuser = mkOption {
5151- type = types.str;
5252- default = "nobody";
5353- description = ''
5454- The user to search non-network directories as, using
5555- <command>su</command>.
5656- '';
5757- };
5858-5959- includeStore = mkOption {
6060- type = types.bool;
6161- default = false;
6262- description = ''
6363- Whether to include <filename>/nix/store</filename> in the locate database.
6464- '';
6565- };
6666-5151+ localuser = mkOption {
5252+ type = types.str;
5353+ default = "nobody";
5454+ description = ''
5555+ The user to search non-network directories as, using
5656+ <command>su</command>.
5757+ '';
6758 };
68596060+ includeStore = mkOption {
6161+ type = types.bool;
6262+ default = false;
6363+ description = ''
6464+ Whether to include <filename>/nix/store</filename> in the locate database.
6565+ '';
6666+ };
6967 };
70687171- ###### implementation
7272-7369 config = {
7070+ 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.";
7171+7472 systemd.services.update-locatedb =
7573 { description = "Update Locate Database";
7674 path = [ pkgs.su ];
···8482 '';
8583 serviceConfig.Nice = 19;
8684 serviceConfig.IOSchedulingClass = "idle";
8585+ serviceConfig.PrivateTmp = "yes";
8686+ serviceConfig.PrivateNetwork = "yes";
8787+ serviceConfig.NoNewPrivileges = "yes";
8888+ serviceConfig.ReadOnlyDirectories = "/";
8989+ serviceConfig.ReadWriteDirectories = cfg.output;
8790 };
88918989- services.cron.systemCronJobs = optional config.services.locate.enable
9090- "${config.services.locate.period} root ${config.systemd.package}/bin/systemctl start update-locatedb.service";
9191-9292+ systemd.timers.update-locatedb = mkIf cfg.enable
9393+ { description = "Update timer for locate database";
9494+ partOf = [ "update-locatedb.service" ];
9595+ wantedBy = [ "timers.target" ];
9696+ timerConfig.OnCalendar = cfg.interval;
9797+ };
9298 };
9393-9499}