Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

184 lignes
6.4 KiB

  1. { config, lib, pkgs, ... }:
  2. with lib;
  3. let
  4. cfg = config.services.locate;
  5. isMLocate = hasPrefix "mlocate" cfg.locate.name;
  6. isFindutils = hasPrefix "findutils" cfg.locate.name;
  7. in {
  8. imports = [
  9. (mkRenamedOptionModule [ "services" "locate" "period" ] [ "services" "locate" "interval" ])
  10. (mkRemovedOptionModule [ "services" "locate" "includeStore" ] "Use services.locate.prunePaths" )
  11. ];
  12. options.services.locate = with types; {
  13. enable = mkOption {
  14. type = bool;
  15. default = false;
  16. description = ''
  17. If enabled, NixOS will periodically update the database of
  18. files used by the <command>locate</command> command.
  19. '';
  20. };
  21. locate = mkOption {
  22. type = package;
  23. default = pkgs.findutils;
  24. defaultText = "pkgs.findutils";
  25. example = "pkgs.mlocate";
  26. description = ''
  27. The locate implementation to use
  28. '';
  29. };
  30. interval = mkOption {
  31. type = str;
  32. default = "02:15";
  33. example = "hourly";
  34. description = ''
  35. Update the locate database at this interval. Updates by
  36. default at 2:15 AM every day.
  37. The format is described in
  38. <citerefentry><refentrytitle>systemd.time</refentrytitle>
  39. <manvolnum>7</manvolnum></citerefentry>.
  40. '';
  41. };
  42. extraFlags = mkOption {
  43. type = listOf str;
  44. default = [ ];
  45. description = ''
  46. Extra flags to pass to <command>updatedb</command>.
  47. '';
  48. };
  49. output = mkOption {
  50. type = path;
  51. default = "/var/cache/locatedb";
  52. description = ''
  53. The database file to build.
  54. '';
  55. };
  56. localuser = mkOption {
  57. type = nullOr str;
  58. default = "nobody";
  59. description = ''
  60. The user to search non-network directories as, using
  61. <command>su</command>.
  62. '';
  63. };
  64. pruneFS = mkOption {
  65. type = listOf str;
  66. 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" ];
  67. description = ''
  68. Which filesystem types to exclude from indexing
  69. '';
  70. };
  71. prunePaths = mkOption {
  72. type = listOf path;
  73. default = ["/tmp" "/var/tmp" "/var/cache" "/var/lock" "/var/run" "/var/spool" "/nix/store"];
  74. description = ''
  75. Which paths to exclude from indexing
  76. '';
  77. };
  78. pruneNames = mkOption {
  79. type = listOf str;
  80. default = [];
  81. description = ''
  82. Directory components which should exclude paths containing them from indexing
  83. '';
  84. };
  85. pruneBindMounts = mkOption {
  86. type = bool;
  87. default = false;
  88. description = ''
  89. Whether not to index bind mounts
  90. '';
  91. };
  92. };
  93. config = mkIf cfg.enable {
  94. users.groups = mkIf isMLocate { mlocate = {}; };
  95. security.wrappers = mkIf isMLocate {
  96. locate = {
  97. group = "mlocate";
  98. owner = "root";
  99. permissions = "u+rx,g+x,o+x";
  100. setgid = true;
  101. setuid = false;
  102. source = "${cfg.locate}/bin/locate";
  103. };
  104. };
  105. nixpkgs.config = { locate.dbfile = cfg.output; };
  106. environment.systemPackages = [ cfg.locate ];
  107. environment.variables = mkIf (!isMLocate)
  108. { LOCATE_PATH = cfg.output;
  109. };
  110. warnings = optional (isMLocate && cfg.localuser != null) "mlocate does not support searching as user other than root"
  111. ++ optional (isFindutils && cfg.pruneNames != []) "findutils locate does not support pruning by directory component"
  112. ++ optional (isFindutils && cfg.pruneBindMounts) "findutils locate does not support skipping bind mounts";
  113. systemd.services.update-locatedb =
  114. { description = "Update Locate Database";
  115. path = mkIf (!isMLocate) [ pkgs.su ];
  116. # mlocate's updatedb takes flags via a configuration file or
  117. # on the command line, but not by environment variable.
  118. script =
  119. if isMLocate
  120. then let toFlags = x: optional (cfg.${x} != [])
  121. "--${lib.toLower x} '${concatStringsSep " " cfg.${x}}'";
  122. args = concatLists (map toFlags ["pruneFS" "pruneNames" "prunePaths"]);
  123. in ''
  124. exec ${cfg.locate}/bin/updatedb \
  125. --output ${toString cfg.output} ${concatStringsSep " " args} \
  126. --prune-bind-mounts ${if cfg.pruneBindMounts then "yes" else "no"} \
  127. ${concatStringsSep " " cfg.extraFlags}
  128. ''
  129. else ''
  130. exec ${cfg.locate}/bin/updatedb \
  131. ${optionalString (cfg.localuser != null && ! isMLocate) ''--localuser=${cfg.localuser}''} \
  132. --output=${toString cfg.output} ${concatStringsSep " " cfg.extraFlags}
  133. '';
  134. environment = optionalAttrs (!isMLocate) {
  135. PRUNEFS = concatStringsSep " " cfg.pruneFS;
  136. PRUNEPATHS = concatStringsSep " " cfg.prunePaths;
  137. PRUNENAMES = concatStringsSep " " cfg.pruneNames;
  138. PRUNE_BIND_MOUNTS = if cfg.pruneBindMounts then "yes" else "no";
  139. };
  140. serviceConfig.Nice = 19;
  141. serviceConfig.IOSchedulingClass = "idle";
  142. serviceConfig.PrivateTmp = "yes";
  143. serviceConfig.PrivateNetwork = "yes";
  144. serviceConfig.NoNewPrivileges = "yes";
  145. serviceConfig.ReadOnlyPaths = "/";
  146. # Use dirOf cfg.output because mlocate creates temporary files next to
  147. # the actual database. We could specify and create them as well,
  148. # but that would make this quite brittle when they change something.
  149. # NOTE: If /var/cache does not exist, this leads to the misleading error message:
  150. # update-locatedb.service: Failed at step NAMESPACE spawning …/update-locatedb-start: No such file or directory
  151. serviceConfig.ReadWritePaths = dirOf cfg.output;
  152. };
  153. systemd.timers.update-locatedb =
  154. { description = "Update timer for locate database";
  155. partOf = [ "update-locatedb.service" ];
  156. wantedBy = [ "timers.target" ];
  157. timerConfig.OnCalendar = cfg.interval;
  158. };
  159. };
  160. }