about summary refs log tree commit diff
path: root/nixos/modules/services/scheduling/atd.nix
blob: c6f128ec40260d27875fae96aaf2095ce14a5b4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.atd;

  inherit (pkgs) at;

in

{

  ###### interface

  options = {

    services.atd.enable = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to enable the <command>at</command> daemon, a command scheduler.
      '';
    };

    services.atd.allowEveryone = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to make <filename>/var/spool/at{jobs,spool}</filename>
        writeable by everyone (and sticky).  This is normally not
        needed since the <command>at</command> commands are
        setuid/setgid <literal>atd</literal>.
     '';
    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    security.setuidOwners = map (program: {
      inherit program;
      owner = "atd";
      group = "atd";
      setuid = true;
      setgid = true;
    }) [ "at" "atq" "atrm" "batch" ];

    environment.systemPackages = [ at ];

    security.pam.services.atd = {};

    users.extraUsers = singleton
      { name = "atd";
        uid = config.ids.uids.atd;
        description = "atd user";
        home = "/var/empty";
      };

    users.extraGroups = singleton
      { name = "atd";
        gid = config.ids.gids.atd;
      };

    jobs.atd =
      { description = "Job Execution Daemon (atd)";

        startOn = "stopped udevtrigger";

        path = [ at ];

        preStart =
          ''
            # Snippets taken and adapted from the original `install' rule of
            # the makefile.

            # We assume these values are those actually used in Nixpkgs for
            # `at'.
            spooldir=/var/spool/atspool
            jobdir=/var/spool/atjobs
            etcdir=/etc/at

            for dir in "$spooldir" "$jobdir" "$etcdir"; do
              if [ ! -d "$dir" ]; then
                  mkdir -p "$dir"
                  chown atd:atd "$dir"
              fi
            done
            chmod 1770 "$spooldir" "$jobdir"
            ${if cfg.allowEveryone then ''chmod a+rwxt "$spooldir" "$jobdir" '' else ""}
            if [ ! -f "$etcdir"/at.deny ]; then
                touch "$etcdir"/at.deny
                chown root:atd "$etcdir"/at.deny
                chmod 640 "$etcdir"/at.deny
            fi
            if [ ! -f "$jobdir"/.SEQ ]; then
                touch "$jobdir"/.SEQ
                chown atd:atd "$jobdir"/.SEQ
                chmod 600 "$jobdir"/.SEQ
            fi
          '';

        exec = "atd";

        daemonType = "fork";
      };

  };

}