2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-10-12 16:36:19 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2007-01-10 17:09:00 +00:00
|
|
|
|
2008-02-01 12:01:27 +00:00
|
|
|
let
|
2009-10-12 16:36:19 +00:00
|
|
|
|
|
|
|
inherit (config.services) jobsTags;
|
|
|
|
|
|
|
|
# Put all the system cronjobs together.
|
|
|
|
systemCronJobsFile = pkgs.writeText "system-crontab"
|
|
|
|
''
|
2009-11-24 14:20:33 +00:00
|
|
|
SHELL=${pkgs.bash}/bin/bash
|
2009-11-24 15:05:08 +00:00
|
|
|
PATH=${config.system.path}/bin:${config.system.path}/sbin
|
2013-10-30 16:37:45 +00:00
|
|
|
${optionalString (config.services.cron.mailto != null) ''
|
|
|
|
MAILTO="${config.services.cron.mailto}"
|
|
|
|
''}
|
2011-11-03 19:04:54 +00:00
|
|
|
NIX_CONF_DIR=/etc/nix
|
2014-05-05 18:58:51 +00:00
|
|
|
${lib.concatStrings (map (job: job + "\n") config.services.cron.systemCronJobs)}
|
2009-10-12 16:36:19 +00:00
|
|
|
'';
|
2010-10-10 11:08:10 +00: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.
|
|
|
|
sendmailPath = "/var/setuid-wrappers/sendmail";
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2014-10-01 15:22:21 +00:00
|
|
|
allFiles = map (f: "\"${f}\"") (
|
|
|
|
[ "${systemCronJobsFile}" ] ++ config.services.cron.cronFiles
|
|
|
|
);
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2008-02-01 12:01:27 +00:00
|
|
|
|
2008-11-09 16:44:43 +00:00
|
|
|
options = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
services.cron = {
|
2008-11-09 16:44:43 +00:00
|
|
|
|
2010-10-10 11:14:02 +00:00
|
|
|
enable = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.bool;
|
2010-10-10 11:14:02 +00:00
|
|
|
default = true;
|
2013-10-30 16:37:45 +00:00
|
|
|
description = "Whether to enable the Vixie cron daemon.";
|
2010-10-10 11:14:02 +00:00
|
|
|
};
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
mailto = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Email address to which job output will be mailed.";
|
2008-11-09 16:44:43 +00:00
|
|
|
};
|
2009-10-12 16:36:19 +00:00
|
|
|
|
|
|
|
systemCronJobs = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.listOf types.str;
|
2009-10-12 16:36:19 +00:00
|
|
|
default = [];
|
2013-10-30 16:37:45 +00:00
|
|
|
example = literalExample ''
|
|
|
|
[ "* * * * * test ls -l / > /tmp/cronout 2>&1"
|
|
|
|
"* * * * * eelco echo Hello World > /home/eelco/cronout"
|
|
|
|
]
|
|
|
|
'';
|
2009-10-12 16:36:19 +00: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
|
|
|
|
sendmail. See <option>security.setuidOwners</option>
|
|
|
|
|
|
|
|
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 11:35:15 +00: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 16:36:19 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-10-01 15:22:21 +00: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 16:44:43 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2008-11-09 16:44:43 +00:00
|
|
|
};
|
|
|
|
|
2008-12-07 12:27:46 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
###### implementation
|
2008-11-09 16:44:43 +00:00
|
|
|
|
2014-10-01 15:22:21 +00:00
|
|
|
config = mkIf (config.services.cron.enable && allFiles != []) {
|
2008-02-01 13:56:36 +00:00
|
|
|
|
2010-10-10 11:35:15 +00:00
|
|
|
security.setuidPrograms = [ "crontab" ];
|
|
|
|
|
2010-10-10 11:08:10 +00:00
|
|
|
environment.systemPackages = [ cronNixosPkg ];
|
2008-11-09 16:44:43 +00:00
|
|
|
|
2014-12-15 13:50:12 +00:00
|
|
|
systemd.services.cron =
|
2013-01-10 12:59:41 +00:00
|
|
|
{ description = "Cron Daemon";
|
2008-11-09 16:44:43 +00:00
|
|
|
|
2014-12-15 13:50:12 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2011-11-25 16:32:54 +00:00
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
preStart =
|
|
|
|
''
|
2014-10-01 15:22:21 +00:00
|
|
|
rm -f /etc/crontab
|
|
|
|
cat ${toString allFiles} > /etc/crontab
|
|
|
|
chmod 0600 /etc/crontab
|
|
|
|
|
2009-04-03 15:14:00 +00:00
|
|
|
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
|
2009-10-12 16:36:19 +00:00
|
|
|
'';
|
|
|
|
|
2014-12-15 13:50:12 +00:00
|
|
|
restartTriggers = [ config.environment.etc.localtime.source ];
|
|
|
|
serviceConfig.ExecStart = "${cronNixosPkg}/bin/cron -n";
|
2009-10-12 16:36:19 +00:00
|
|
|
};
|
2009-04-03 15:14:00 +00:00
|
|
|
|
2008-11-09 16:44:43 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2007-01-10 17:09:00 +00:00
|
|
|
}
|