about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/system/automatic-timezoned.nix
blob: 7d3cd004a7ba129601cc1b92c5622c56597d236c (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.automatic-timezoned;
in
{
  options = {
    services.automatic-timezoned = {
      enable = mkOption {
        type = types.bool;
        default = false;
        description = mdDoc ''
          Enable `automatic-timezoned`, simple daemon for keeping the system
          timezone up-to-date based on the current location. It uses geoclue2 to
          determine the current location and systemd-timedated to actually set
          the timezone.
        '';
      };
      package = mkPackageOption pkgs "automatic-timezoned" { };
    };
  };

  config = mkIf cfg.enable {
    security.polkit.extraConfig = ''
      polkit.addRule(function(action, subject) {
        if (action.id == "org.freedesktop.timedate1.set-timezone"
            && subject.user == "automatic-timezoned") {
          return polkit.Result.YES;
        }
      });
    '';

    services.geoclue2 = {
      enable = true;
      appConfig.automatic-timezoned = {
        isAllowed = true;
        isSystem = true;
        users = [ (toString config.ids.uids.automatic-timezoned) ];
      };
    };

    systemd.services = {

      automatic-timezoned = {
        description = "Automatically update system timezone based on location";
        requires = [ "automatic-timezoned-geoclue-agent.service" ];
        after = [ "automatic-timezoned-geoclue-agent.service" ];
        serviceConfig = {
          Type = "exec";
          User = "automatic-timezoned";
          ExecStart = "${cfg.package}/bin/automatic-timezoned";
        };
        wantedBy = [ "default.target" ];
      };

      automatic-timezoned-geoclue-agent = {
        description = "Geoclue agent for automatic-timezoned";
        requires = [ "geoclue.service" ];
        after = [ "geoclue.service" ];
        serviceConfig = {
          Type = "exec";
          User = "automatic-timezoned";
          ExecStart = "${pkgs.geoclue2-with-demo-agent}/libexec/geoclue-2.0/demos/agent";
          Restart = "on-failure";
          PrivateTmp = true;
        };
        wantedBy = [ "default.target" ];
      };

    };

    users = {
      users.automatic-timezoned = {
        description = "automatic-timezoned";
        uid = config.ids.uids.automatic-timezoned;
        group = "automatic-timezoned";
      };
      groups.automatic-timezoned = {
        gid = config.ids.gids.automatic-timezoned;
      };
    };
  };
}