summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/github-hosts-sync.nix
blob: 233cea68df7df1610ac2dfffc8cc0a85b65dbf84 (plain)
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
{ config, lib, pkgs, ... }:

with import <stockholm/lib>;
let
  cfg = config.krebs.github-hosts-sync;

  out = {
    options.krebs.github-hosts-sync = api;
    config = lib.mkIf cfg.enable imp;
  };

  api = {
    enable = mkEnableOption "krebs.github-hosts-sync";
    dataDir = mkOption {
      type = types.str; # TODO path (but not just into store)
      default = "/var/lib/github-hosts-sync";
    };
    srcDir = mkOption {
      type = types.str;
      default = "${config.krebs.tinc.retiolum.confDir}/hosts";
    };
    ssh-identity-file = mkOption {
      type = types.suffixed-str [".ssh.id_ed25519" ".ssh.id_rsa"];
      default = toString <secrets/github-hosts-sync.ssh.id_ed25519>;
    };
    url = mkOption {
      type = types.str;
      default = "git@github.com:krebscode/hosts.git";
    };
    workTree = mkOption {
      type = types.absolute-pathname;
      default = "${cfg.dataDir}/cache";
    };
  };

  imp = {
    systemd.services.github-hosts-sync = {
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      environment = {
        GITHUB_HOST_SYNC_SRCDIR = cfg.srcDir;
        GITHUB_HOST_SYNC_WORKTREE = cfg.workTree;
        GITHUB_HOST_SYNC_URL = cfg.url;
      };
      serviceConfig = {
        PermissionsStartOnly = "true";
        SyslogIdentifier = "github-hosts-sync";
        User = user.name;
        Type = "oneshot";
        RemainAfterExit = true;
        ExecStartPre = pkgs.writeDash "github-hosts-sync-init" ''
          set -euf
          install -m 0711 -o ${user.name} -d ${cfg.dataDir}
          install -m 0700 -o ${user.name} -d ${cfg.dataDir}/.ssh
          install -m 0400 -o ${user.name} \
            ${cfg.ssh-identity-file} \
            ${cfg.dataDir}/.ssh/${fileExtension cfg.ssh-identity-file}
        '';
        ExecStart = "${pkgs.github-hosts-sync}/bin/github-hosts-sync";
      };
    };

    users.extraUsers = singleton {
      inherit (user) name uid;
      home = cfg.dataDir;
    };
  };

  user = rec {
    name = "github-hosts-sync";
    uid = genid_uint31 name;
  };

  # TODO move to lib?
  fileExtension = s: last (splitString "." s);

in out