2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2007-01-10 18:09:00 +01:00
|
|
|
|
2008-02-01 13:01:27 +01:00
|
|
|
let
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
# Put all the system cronjobs together.
|
|
|
|
systemCronJobsFile = pkgs.writeText "system-crontab"
|
|
|
|
''
|
2009-11-24 15:20:33 +01:00
|
|
|
SHELL=${pkgs.bash}/bin/bash
|
2009-11-24 16:05:08 +01:00
|
|
|
PATH=${config.system.path}/bin:${config.system.path}/sbin
|
2013-10-30 17:37:45 +01:00
|
|
|
${optionalString (config.services.cron.mailto != null) ''
|
|
|
|
MAILTO="${config.services.cron.mailto}"
|
|
|
|
''}
|
2011-11-03 20:04:54 +01:00
|
|
|
NIX_CONF_DIR=/etc/nix
|
2014-05-05 20:58:51 +02:00
|
|
|
${lib.concatStrings (map (job: job + "\n") config.services.cron.systemCronJobs)}
|
2009-10-12 18:36:19 +02:00
|
|
|
'';
|
2010-10-10 13:08:10 +02:00
|
|
|
|
|
|
|
# Vixie cron requires build-time configuration for the sendmail path.
|
|
|
|
cronNixosPkg = pkgs.cron.override {
|
|
|
|
# The mail.nix nixos module, if there is any local mail system enabled,
|
|
|
|
# should have sendmail in this path.
|
2017-01-29 11:11:01 +01:00
|
|
|
sendmailPath = "/run/wrappers/bin/sendmail";
|
2010-10-10 13:08:10 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2015-07-22 15:02:07 +02:00
|
|
|
allFiles =
|
|
|
|
optional (config.services.cron.systemCronJobs != []) systemCronJobsFile
|
|
|
|
++ config.services.cron.cronFiles;
|
2014-10-01 17:22:21 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2008-02-01 13:01:27 +01:00
|
|
|
|
2008-11-09 17:44:43 +01:00
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
services.cron = {
|
2008-11-09 17:44:43 +01:00
|
|
|
|
2010-10-10 13:14:02 +02:00
|
|
|
enable = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.bool;
|
2017-03-17 23:31:52 +01:00
|
|
|
default = false;
|
2013-10-30 17:37:45 +01:00
|
|
|
description = "Whether to enable the Vixie cron daemon.";
|
2010-10-10 13:14:02 +02:00
|
|
|
};
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
mailto = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Email address to which job output will be mailed.";
|
2008-11-09 17:44:43 +01:00
|
|
|
};
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
systemCronJobs = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.listOf types.str;
|
2009-10-12 18:36:19 +02:00
|
|
|
default = [];
|
2013-10-30 17:37:45 +01:00
|
|
|
example = literalExample ''
|
|
|
|
[ "* * * * * test ls -l / > /tmp/cronout 2>&1"
|
|
|
|
"* * * * * eelco echo Hello World > /home/eelco/cronout"
|
|
|
|
]
|
|
|
|
'';
|
2009-10-12 18:36:19 +02:00
|
|
|
description = ''
|
|
|
|
A list of Cron jobs to be appended to the system-wide
|
|
|
|
crontab. See the manual page for crontab for the expected
|
|
|
|
format. If you want to get the results mailed you must setuid
|
2017-01-29 08:58:12 +01:00
|
|
|
sendmail. See <option>security.wrappers</option>
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
If neither /var/cron/cron.deny nor /var/cron/cron.allow exist only root
|
|
|
|
will is allowed to have its own crontab file. The /var/cron/cron.deny file
|
|
|
|
is created automatically for you. So every user can use a crontab.
|
2010-10-10 13:35:15 +02:00
|
|
|
|
|
|
|
Many nixos modules set systemCronJobs, so if you decide to disable vixie cron
|
|
|
|
and enable another cron daemon, you may want it to get its system crontab
|
|
|
|
based on systemCronJobs.
|
2009-10-12 18:36:19 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-10-01 17:22:21 +02:00
|
|
|
cronFiles = mkOption {
|
|
|
|
type = types.listOf types.path;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
A list of extra crontab files that will be read and appended to the main
|
|
|
|
crontab file when the cron service starts.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2008-11-09 17:44:43 +01:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2008-11-09 17:44:43 +01:00
|
|
|
};
|
|
|
|
|
2008-12-07 13:27:46 +01:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
###### implementation
|
2008-11-09 17:44:43 +01:00
|
|
|
|
2015-07-22 15:02:07 +02:00
|
|
|
config = mkMerge [
|
2008-02-01 14:56:36 +01:00
|
|
|
|
2015-07-22 15:12:47 +02:00
|
|
|
{ services.cron.enable = mkDefault (allFiles != []); }
|
2015-07-24 18:15:37 +02:00
|
|
|
(mkIf (config.services.cron.enable) {
|
2017-02-15 08:30:58 +01:00
|
|
|
security.wrappers.crontab.source = "${cronNixosPkg}/bin/crontab";
|
2015-07-22 15:02:07 +02:00
|
|
|
environment.systemPackages = [ cronNixosPkg ];
|
2015-07-22 15:12:47 +02:00
|
|
|
environment.etc.crontab =
|
2015-10-03 08:33:13 +02:00
|
|
|
{ source = pkgs.runCommand "crontabs" { inherit allFiles; preferLocalBuild = true; }
|
2015-07-22 15:12:47 +02:00
|
|
|
''
|
|
|
|
touch $out
|
|
|
|
for i in $allFiles; do
|
|
|
|
cat "$i" >> $out
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
mode = "0600"; # Cron requires this.
|
|
|
|
};
|
|
|
|
|
2015-07-22 15:02:07 +02:00
|
|
|
systemd.services.cron =
|
|
|
|
{ description = "Cron Daemon";
|
2009-04-03 17:14:00 +02:00
|
|
|
|
2015-07-22 15:02:07 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2015-07-22 15:02:07 +02:00
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
mkdir -m 710 -p /var/cron
|
|
|
|
|
|
|
|
# By default, allow all users to create a crontab. This
|
|
|
|
# is denoted by the existence of an empty cron.deny file.
|
|
|
|
if ! test -e /var/cron/cron.allow -o -e /var/cron/cron.deny; then
|
|
|
|
touch /var/cron/cron.deny
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2017-08-12 09:47:30 +02:00
|
|
|
restartTriggers = [ config.time.timeZone ];
|
2015-07-22 15:02:07 +02:00
|
|
|
serviceConfig.ExecStart = "${cronNixosPkg}/bin/cron -n";
|
|
|
|
};
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
];
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2007-01-10 18:09:00 +01:00
|
|
|
}
|