at 16.09-beta 3.9 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 7 # Put all the system cronjobs together. 8 systemCronJobsFile = pkgs.writeText "system-crontab" 9 '' 10 SHELL=${pkgs.bash}/bin/bash 11 PATH=${config.system.path}/bin:${config.system.path}/sbin 12 ${optionalString (config.services.cron.mailto != null) '' 13 MAILTO="${config.services.cron.mailto}" 14 ''} 15 NIX_CONF_DIR=/etc/nix 16 ${lib.concatStrings (map (job: job + "\n") config.services.cron.systemCronJobs)} 17 ''; 18 19 # Vixie cron requires build-time configuration for the sendmail path. 20 cronNixosPkg = pkgs.cron.override { 21 # The mail.nix nixos module, if there is any local mail system enabled, 22 # should have sendmail in this path. 23 sendmailPath = "/var/setuid-wrappers/sendmail"; 24 }; 25 26 allFiles = 27 optional (config.services.cron.systemCronJobs != []) systemCronJobsFile 28 ++ config.services.cron.cronFiles; 29 30in 31 32{ 33 34 ###### interface 35 36 options = { 37 38 services.cron = { 39 40 enable = mkOption { 41 type = types.bool; 42 example = true; 43 description = "Whether to enable the Vixie cron daemon."; 44 }; 45 46 mailto = mkOption { 47 type = types.nullOr types.str; 48 default = null; 49 description = "Email address to which job output will be mailed."; 50 }; 51 52 systemCronJobs = mkOption { 53 type = types.listOf types.str; 54 default = []; 55 example = literalExample '' 56 [ "* * * * * test ls -l / > /tmp/cronout 2>&1" 57 "* * * * * eelco echo Hello World > /home/eelco/cronout" 58 ] 59 ''; 60 description = '' 61 A list of Cron jobs to be appended to the system-wide 62 crontab. See the manual page for crontab for the expected 63 format. If you want to get the results mailed you must setuid 64 sendmail. See <option>security.setuidOwners</option> 65 66 If neither /var/cron/cron.deny nor /var/cron/cron.allow exist only root 67 will is allowed to have its own crontab file. The /var/cron/cron.deny file 68 is created automatically for you. So every user can use a crontab. 69 70 Many nixos modules set systemCronJobs, so if you decide to disable vixie cron 71 and enable another cron daemon, you may want it to get its system crontab 72 based on systemCronJobs. 73 ''; 74 }; 75 76 cronFiles = mkOption { 77 type = types.listOf types.path; 78 default = []; 79 description = '' 80 A list of extra crontab files that will be read and appended to the main 81 crontab file when the cron service starts. 82 ''; 83 }; 84 85 }; 86 87 }; 88 89 90 ###### implementation 91 92 config = mkMerge [ 93 94 { services.cron.enable = mkDefault (allFiles != []); } 95 96 (mkIf (config.services.cron.enable) { 97 98 security.setuidPrograms = [ "crontab" ]; 99 100 environment.systemPackages = [ cronNixosPkg ]; 101 102 environment.etc.crontab = 103 { source = pkgs.runCommand "crontabs" { inherit allFiles; preferLocalBuild = true; } 104 '' 105 touch $out 106 for i in $allFiles; do 107 cat "$i" >> $out 108 done 109 ''; 110 mode = "0600"; # Cron requires this. 111 }; 112 113 systemd.services.cron = 114 { description = "Cron Daemon"; 115 116 wantedBy = [ "multi-user.target" ]; 117 118 preStart = 119 '' 120 mkdir -m 710 -p /var/cron 121 122 # By default, allow all users to create a crontab. This 123 # is denoted by the existence of an empty cron.deny file. 124 if ! test -e /var/cron/cron.allow -o -e /var/cron/cron.deny; then 125 touch /var/cron/cron.deny 126 fi 127 ''; 128 129 restartTriggers = [ config.environment.etc.localtime.source ]; 130 serviceConfig.ExecStart = "${cronNixosPkg}/bin/cron -n"; 131 }; 132 133 }) 134 135 ]; 136 137}