nixpkgs/nixos/modules/misc/locate.nix

162 lines
5.1 KiB
Nix
Raw Normal View History

{ config, options, lib, pkgs, ... }:
with lib;
let
cfg = config.services.locate;
2017-01-05 20:57:45 +03:00
isMLocate = hasPrefix "mlocate" cfg.locate.name;
isFindutils = hasPrefix "findutils" cfg.locate.name;
in {
2017-01-05 20:57:45 +03:00
options.services.locate = with types; {
enable = mkOption {
2017-01-05 20:57:45 +03:00
type = bool;
default = false;
description = ''
If enabled, NixOS will periodically update the database of
files used by the <command>locate</command> command.
'';
};
2016-10-25 14:58:49 +03:00
locate = mkOption {
2017-01-05 20:57:45 +03:00
type = package;
2016-10-25 14:58:49 +03:00
default = pkgs.findutils;
defaultText = "pkgs.findutils";
2016-10-25 14:58:49 +03:00
example = "pkgs.mlocate";
description = ''
The locate implementation to use
'';
};
interval = mkOption {
2017-01-05 20:57:45 +03:00
type = str;
default = "02:15";
example = "hourly";
description = ''
Update the locate database at this interval. Updates by
default at 2:15 AM every day.
The format is described in
<citerefentry><refentrytitle>systemd.time</refentrytitle>
<manvolnum>7</manvolnum></citerefentry>.
'';
};
extraFlags = mkOption {
2017-01-05 20:57:45 +03:00
type = listOf str;
default = [ ];
description = ''
Extra flags to pass to <command>updatedb</command>.
'';
};
output = mkOption {
2017-01-05 20:57:45 +03:00
type = path;
default = "/var/cache/locatedb";
description = ''
The database file to build.
'';
};
2015-05-28 13:18:31 +03:00
localuser = mkOption {
2017-01-05 20:57:45 +03:00
type = nullOr str;
default = "nobody";
description = ''
The user to search non-network directories as, using
<command>su</command>.
'';
};
2017-01-05 20:57:45 +03:00
pruneFS = mkOption {
type = listOf str;
default = ["afs" "anon_inodefs" "auto" "autofs" "bdev" "binfmt" "binfmt_misc" "cgroup" "cifs" "coda" "configfs" "cramfs" "cpuset" "debugfs" "devfs" "devpts" "devtmpfs" "ecryptfs" "eventpollfs" "exofs" "futexfs" "ftpfs" "fuse" "fusectl" "gfs" "gfs2" "hostfs" "hugetlbfs" "inotifyfs" "iso9660" "jffs2" "lustre" "misc" "mqueue" "ncpfs" "nnpfs" "ocfs" "ocfs2" "pipefs" "proc" "ramfs" "rpc_pipefs" "securityfs" "selinuxfs" "sfs" "shfs" "smbfs" "sockfs" "spufs" "nfs" "NFS" "nfs4" "nfsd" "sshfs" "subfs" "supermount" "sysfs" "tmpfs" "ubifs" "udf" "usbfs" "vboxsf" "vperfctrfs" ];
description = ''
Which filesystem types to exclude from indexing
'';
};
prunePaths = mkOption {
type = listOf path;
default = ["/tmp" "/var/tmp" "/var/cache" "/var/lock" "/var/run" "/var/spool" "/nix/store"];
description = ''
Which paths to exclude from indexing
'';
};
pruneNames = mkOption {
type = listOf str;
default = [];
description = ''
Directory components which should exclude paths containing them from indexing
'';
};
pruneBindMounts = mkOption {
type = bool;
default = false;
description = ''
2017-01-05 20:57:45 +03:00
Whether not to index bind mounts
'';
};
2017-01-05 20:57:45 +03:00
};
2017-01-05 20:57:45 +03:00
config = mkIf cfg.enable {
users.extraGroups = mkIf isMLocate { mlocate = {}; };
security.wrappers = mkIf isMLocate {
2017-03-01 17:16:23 +03:00
locate = {
group = "mlocate";
owner = "root";
permissions = "u+rx,g+x,o+x";
setgid = true;
setuid = false;
2017-03-01 17:16:23 +03:00
source = "${cfg.locate}/bin/locate";
};
};
2017-01-05 20:57:45 +03:00
2017-01-05 22:16:48 +03:00
nixpkgs.config = { locate.dbfile = cfg.output; };
2017-01-05 20:57:45 +03:00
environment.systemPackages = [ cfg.locate ];
2017-01-05 22:16:48 +03:00
environment.variables = mkIf (!isMLocate)
{ LOCATE_PATH = cfg.output;
};
2017-01-05 20:57:45 +03:00
warnings = optional (isMLocate && cfg.localuser != null) "mlocate does not support searching as user other than root"
++ optional (isFindutils && cfg.pruneNames != []) "findutils locate does not support pruning by directory component"
++ optional (isFindutils && cfg.pruneBindMounts) "findutils locate does not support skipping bind mounts";
systemd.services.update-locatedb =
{ description = "Update Locate Database";
2017-01-05 20:57:45 +03:00
path = mkIf (!isMLocate) [ pkgs.su ];
script =
''
mkdir -m 0755 -p ${dirOf cfg.output}
2016-10-25 14:58:49 +03:00
exec ${cfg.locate}/bin/updatedb \
${optionalString (cfg.localuser != null && ! isMLocate) ''--localuser=${cfg.localuser}''} \
2015-08-03 14:48:43 +03:00
--output=${toString cfg.output} ${concatStringsSep " " cfg.extraFlags}
'';
2017-01-05 20:57:45 +03:00
environment = {
PRUNEFS = concatStringsSep " " cfg.pruneFS;
PRUNEPATHS = concatStringsSep " " cfg.prunePaths;
PRUNENAMES = concatStringsSep " " cfg.pruneNames;
PRUNE_BIND_MOUNTS = if cfg.pruneBindMounts then "yes" else "no";
};
serviceConfig.Nice = 19;
serviceConfig.IOSchedulingClass = "idle";
serviceConfig.PrivateTmp = "yes";
serviceConfig.PrivateNetwork = "yes";
serviceConfig.NoNewPrivileges = "yes";
serviceConfig.ReadOnlyDirectories = "/";
serviceConfig.ReadWriteDirectories = dirOf cfg.output;
};
2017-01-05 20:57:45 +03:00
systemd.timers.update-locatedb =
{ description = "Update timer for locate database";
partOf = [ "update-locatedb.service" ];
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = cfg.interval;
};
};
}