Make it easier to define timer units for services

Systemd services now have a startAt attribute.  If set, NixOS will
automatically emit a timer unit that causes the service to start at
the specified time.  For example:

  systemd.services.foo =
    { script = "... bla bla ...";
      startAt = "02:15";
    };

causes the given script to be started at 02:15 every day.
This commit is contained in:
Eelco Dolstra 2013-10-09 14:28:35 +02:00
parent 87bea0b09e
commit 36079454e5
4 changed files with 30 additions and 20 deletions

View file

@ -167,11 +167,7 @@ in
serviceConfig.User = "${cfg.user}";
serviceConfig.Group = "${cfg.group}";
environment.OPENSSL_X509_CERT_FILE = "/etc/ssl/certs/ca-bundle.crt";
};
systemd.timers.venus =
{ wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = cfg.dates;
startOn = cfg.dates;
};
};

View file

@ -48,22 +48,14 @@ in
###### implementation
config = mkMerge [
config = {
{ systemd.services.nix-gc =
{ description = "Nix Garbage Collector";
path = [ config.environment.nix ];
script = "exec nix-collect-garbage ${cfg.options}";
};
}
systemd.services.nix-gc =
{ description = "Nix Garbage Collector";
serviceConfig.ExecStart = "${config.environment.nix}/bin/nix-collect-garbage ${cfg.options}";
startAt = optionalString cfg.automatic cfg.dates;
};
(mkIf cfg.automatic {
systemd.timers.nix-gc =
{ wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = cfg.dates;
};
})
];
};
}

View file

@ -229,6 +229,20 @@ rec {
'';
};
startAt = mkOption {
type = types.uniq types.string;
default = "";
example = "Sun 14:00:00";
description = ''
Automatically start this unit at the given date/time, which
must be in the format described in
<citerefentry><refentrytitle>systemd.time</refentrytitle>
<manvolnum>5</manvolnum></citerefentry>. This is equivalent
to adding a corresponding timer unit with
<option>OnCalendar</option> set to the value given here.
'';
};
};

View file

@ -628,6 +628,14 @@ in
users.extraGroups.systemd-journal.gid = config.ids.gids.systemd-journal;
# Generate timer units for all services that have a startAt value.
systemd.timers =
mapAttrs (name: service:
{ wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = service.startAt;
})
(filterAttrs (name: service: service.startAt != "") cfg.services);
# FIXME: These are borrowed from upstream systemd.
systemd.services."systemd-update-utmp" =
{ description = "Update UTMP about System Reboot/Shutdown";