1 { config, lib, pkgs, ... }:
7 # Put all the system cronjobs together.
8 systemCronJobsFile = pkgs.writeText "system-crontab"
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}"
16 ${lib.concatStrings (map (job: job + "\n") config.services.cron.systemCronJobs)}
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";
27 optional (config.services.cron.systemCronJobs != []) systemCronJobsFile
28 ++ config.services.cron.cronFiles;
43 description = "Whether to enable the Vixie cron daemon.";
47 type = types.nullOr types.str;
49 description = "Email address to which job output will be mailed.";
52 systemCronJobs = mkOption {
53 type = types.listOf types.str;
55 example = literalExpression ''
56 [ "* * * * * test ls -l / > /tmp/cronout 2>&1"
57 "* * * * * eelco echo Hello World > /home/eelco/cronout"
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`
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.
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.
76 cronFiles = mkOption {
77 type = types.listOf types.path;
80 A list of extra crontab files that will be read and appended to the main
81 crontab file when the cron service starts.
94 { services.cron.enable = mkDefault (allFiles != []); }
95 (mkIf (config.services.cron.enable) {
96 security.wrappers.crontab =
100 source = "${cronNixosPkg}/bin/crontab";
102 environment.systemPackages = [ cronNixosPkg ];
103 environment.etc.crontab =
104 { source = pkgs.runCommand "crontabs" { inherit allFiles; preferLocalBuild = true; }
107 for i in $allFiles; do
111 mode = "0600"; # Cron requires this.
114 systemd.services.cron =
115 { description = "Cron Daemon";
117 wantedBy = [ "multi-user.target" ];
121 mkdir -m 710 -p /var/cron
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
130 restartTriggers = [ config.time.timeZone ];
131 serviceConfig.ExecStart = "${cronNixosPkg}/bin/cron -n";