2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2007-01-10 17:09:00 +00:00
|
|
|
|
2008-02-01 12:01:27 +00:00
|
|
|
let
|
2009-10-12 17:36:19 +01:00
|
|
|
|
|
|
|
# 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 19:58:51 +01:00
|
|
|
${lib.concatStrings (map (job: job + "\n") config.services.cron.systemCronJobs)}
|
2009-10-12 17:36:19 +01:00
|
|
|
'';
|
2010-10-10 12:08:10 +01: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 10:11:01 +00:00
|
|
|
sendmailPath = "/run/wrappers/bin/sendmail";
|
2010-10-10 12:08:10 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2015-07-22 14:02:07 +01:00
|
|
|
allFiles =
|
|
|
|
optional (config.services.cron.systemCronJobs != []) systemCronJobsFile
|
|
|
|
++ config.services.cron.cronFiles;
|
2014-10-01 16:22:21 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
2008-02-01 12:01:27 +00:00
|
|
|
|
2008-11-09 16:44:43 +00:00
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
services.cron = {
|
2008-11-09 16:44:43 +00:00
|
|
|
|
2010-10-10 12:14:02 +01:00
|
|
|
enable = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.bool;
|
2017-03-17 22:31:52 +00:00
|
|
|
default = false;
|
2013-10-30 16:37:45 +00:00
|
|
|
description = "Whether to enable the Vixie cron daemon.";
|
2010-10-10 12:14:02 +01:00
|
|
|
};
|
|
|
|
|
2009-10-12 17:36:19 +01: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 17:36:19 +01:00
|
|
|
|
|
|
|
systemCronJobs = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.listOf types.str;
|
2009-10-12 17:36:19 +01: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 17:36:19 +01: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 07:58:12 +00:00
|
|
|
sendmail. See <option>security.wrappers</option>
|
2009-10-12 17:36:19 +01: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 12:35:15 +01: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 17:36:19 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-10-01 16:22:21 +01: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 19:20:50 +01:00
|
|
|
|
2008-11-09 16:44:43 +00:00
|
|
|
};
|
|
|
|
|
2008-12-07 12:27:46 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
###### implementation
|
2008-11-09 16:44:43 +00:00
|
|
|
|
2015-07-22 14:02:07 +01:00
|
|
|
config = mkMerge [
|
2008-02-01 13:56:36 +00:00
|
|
|
|
2015-07-22 14:12:47 +01:00
|
|
|
{ services.cron.enable = mkDefault (allFiles != []); }
|
2015-07-24 17:15:37 +01:00
|
|
|
(mkIf (config.services.cron.enable) {
|
2017-02-15 07:30:58 +00:00
|
|
|
security.wrappers.crontab.source = "${cronNixosPkg}/bin/crontab";
|
2015-07-22 14:02:07 +01:00
|
|
|
environment.systemPackages = [ cronNixosPkg ];
|
2015-07-22 14:12:47 +01:00
|
|
|
environment.etc.crontab =
|
2015-10-03 07:33:13 +01:00
|
|
|
{ source = pkgs.runCommand "crontabs" { inherit allFiles; preferLocalBuild = true; }
|
2015-07-22 14:12:47 +01:00
|
|
|
''
|
|
|
|
touch $out
|
|
|
|
for i in $allFiles; do
|
|
|
|
cat "$i" >> $out
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
mode = "0600"; # Cron requires this.
|
|
|
|
};
|
|
|
|
|
2015-07-22 14:02:07 +01:00
|
|
|
systemd.services.cron =
|
|
|
|
{ description = "Cron Daemon";
|
2009-04-03 16:14:00 +01:00
|
|
|
|
2015-07-22 14:02:07 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2015-07-22 14:02:07 +01: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 08:47:30 +01:00
|
|
|
restartTriggers = [ config.time.timeZone ];
|
2015-07-22 14:02:07 +01:00
|
|
|
serviceConfig.ExecStart = "${cronNixosPkg}/bin/cron -n";
|
|
|
|
};
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
];
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2007-01-10 17:09:00 +00:00
|
|
|
}
|