Commit 36079454 authored by Eelco Dolstra's avatar Eelco Dolstra
Browse files

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.
parent 87bea0b0
Loading
Loading
Loading
Loading
+1 −5
Original line number Diff line number Diff line
@@ -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;
      };

  };
+9 −17
Original line number Diff line number Diff line
@@ -48,22 +48,14 @@ in

  ###### implementation

  config = mkMerge [
  config = {

    { systemd.services.nix-gc =
    systemd.services.nix-gc =
      { description = "Nix Garbage Collector";
          path  = [ config.environment.nix ];
          script = "exec nix-collect-garbage ${cfg.options}";
        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;
  };
    })

  ];

}
+14 −0
Original line number Diff line number Diff line
@@ -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.
      '';
    };

  };


+8 −0
Original line number Diff line number Diff line
@@ -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";