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
|
|
|
|
|
|
|
inherit (config.services) jobsTags;
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
sendmailPath = "/var/setuid-wrappers/sendmail";
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2014-10-01 17:22:21 +02:00
|
|
|
allFiles = map (f: "\"${f}\"") (
|
|
|
|
[ "${systemCronJobsFile}" ] ++ config.services.cron.cronFiles
|
|
|
|
);
|
|
|
|
|
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;
|
2010-10-10 13:14:02 +02:00
|
|
|
default = true;
|
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
|
|
|
|
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 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
|
|
|
|
2014-10-01 17:22:21 +02:00
|
|
|
config = mkIf (config.services.cron.enable && allFiles != []) {
|
2008-02-01 14:56:36 +01:00
|
|
|
|
2010-10-10 13:35:15 +02:00
|
|
|
security.setuidPrograms = [ "crontab" ];
|
|
|
|
|
2010-10-10 13:08:10 +02:00
|
|
|
environment.systemPackages = [ cronNixosPkg ];
|
2008-11-09 17:44:43 +01:00
|
|
|
|
2014-12-15 14:50:12 +01:00
|
|
|
systemd.services.cron =
|
2013-01-10 13:59:41 +01:00
|
|
|
{ description = "Cron Daemon";
|
2008-11-09 17:44:43 +01:00
|
|
|
|
2014-12-15 14:50:12 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2011-11-25 17:32:54 +01:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
preStart =
|
|
|
|
''
|
2014-10-01 17:22:21 +02:00
|
|
|
rm -f /etc/crontab
|
|
|
|
cat ${toString allFiles} > /etc/crontab
|
|
|
|
chmod 0600 /etc/crontab
|
|
|
|
|
2009-04-03 17:14:00 +02: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 18:36:19 +02:00
|
|
|
'';
|
|
|
|
|
2014-12-15 14:50:12 +01:00
|
|
|
restartTriggers = [ config.environment.etc.localtime.source ];
|
|
|
|
serviceConfig.ExecStart = "${cronNixosPkg}/bin/cron -n";
|
2009-10-12 18:36:19 +02:00
|
|
|
};
|
2009-04-03 17:14:00 +02:00
|
|
|
|
2008-11-09 17:44:43 +01:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2007-01-10 18:09:00 +01:00
|
|
|
}
|