at 17.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 = "/run/wrappers/bin/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 default = false; 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.wrappers</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 (mkIf (config.services.cron.enable) { 96 security.wrappers.crontab.source = "${cronNixosPkg}/bin/crontab"; 97 environment.systemPackages = [ cronNixosPkg ]; 98 environment.etc.crontab = 99 { source = pkgs.runCommand "crontabs" { inherit allFiles; preferLocalBuild = true; } 100 '' 101 touch $out 102 for i in $allFiles; do 103 cat "$i" >> $out 104 done 105 ''; 106 mode = "0600"; # Cron requires this. 107 }; 108 109 systemd.services.cron = 110 { description = "Cron Daemon"; 111 112 wantedBy = [ "multi-user.target" ]; 113 114 preStart = 115 '' 116 mkdir -m 710 -p /var/cron 117 118 # By default, allow all users to create a crontab. This 119 # is denoted by the existence of an empty cron.deny file. 120 if ! test -e /var/cron/cron.allow -o -e /var/cron/cron.deny; then 121 touch /var/cron/cron.deny 122 fi 123 ''; 124 125 restartTriggers = [ config.time.timeZone ]; 126 serviceConfig.ExecStart = "${cronNixosPkg}/bin/cron -n"; 127 }; 128 129 }) 130 131 ]; 132 133}