at 23.11-pre 4.0 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 = lib.mdDoc "Whether to enable the Vixie cron daemon."; 44 }; 45 46 mailto = mkOption { 47 type = types.nullOr types.str; 48 default = null; 49 description = lib.mdDoc "Email address to which job output will be mailed."; 50 }; 51 52 systemCronJobs = mkOption { 53 type = types.listOf types.str; 54 default = []; 55 example = literalExpression '' 56 [ "* * * * * test ls -l / > /tmp/cronout 2>&1" 57 "* * * * * eelco echo Hello World > /home/eelco/cronout" 58 ] 59 ''; 60 description = lib.mdDoc '' 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` 65 66 If neither /var/cron/cron.deny nor /var/cron/cron.allow exist only root 67 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 = lib.mdDoc '' 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 = 97 { setuid = true; 98 owner = "root"; 99 group = "root"; 100 source = "${cronNixosPkg}/bin/crontab"; 101 }; 102 environment.systemPackages = [ cronNixosPkg ]; 103 environment.etc.crontab = 104 { source = pkgs.runCommand "crontabs" { inherit allFiles; preferLocalBuild = true; } 105 '' 106 touch $out 107 for i in $allFiles; do 108 cat "$i" >> $out 109 done 110 ''; 111 mode = "0600"; # Cron requires this. 112 }; 113 114 systemd.services.cron = 115 { description = "Cron Daemon"; 116 117 wantedBy = [ "multi-user.target" ]; 118 119 preStart = 120 '' 121 mkdir -m 710 -p /var/cron 122 123 # By default, allow all users to create a crontab. This 124 # is denoted by the existence of an empty cron.deny file. 125 if ! test -e /var/cron/cron.allow -o -e /var/cron/cron.deny; then 126 touch /var/cron/cron.deny 127 fi 128 ''; 129 130 restartTriggers = [ config.time.timeZone ]; 131 serviceConfig.ExecStart = "${cronNixosPkg}/bin/cron -n"; 132 }; 133 134 }) 135 136 ]; 137 138}