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