2015-07-19 14:04:28 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
2016-10-20 18:54:38 +00:00
|
|
|
with import <stockholm/lib>;
|
2015-07-19 14:04:28 +00:00
|
|
|
let
|
2015-07-24 10:03:51 +00:00
|
|
|
cfg = config.krebs.github-hosts-sync;
|
2015-07-19 14:04:28 +00:00
|
|
|
|
|
|
|
out = {
|
2015-07-24 10:03:51 +00:00
|
|
|
options.krebs.github-hosts-sync = api;
|
2016-02-14 15:43:44 +00:00
|
|
|
config = lib.mkIf cfg.enable imp;
|
2015-07-19 14:04:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
api = {
|
2015-07-24 10:03:51 +00:00
|
|
|
enable = mkEnableOption "krebs.github-hosts-sync";
|
2015-07-19 14:04:28 +00:00
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.str; # TODO path (but not just into store)
|
|
|
|
default = "/var/lib/github-hosts-sync";
|
|
|
|
};
|
2019-05-17 11:06:36 +00:00
|
|
|
srcDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "${config.krebs.tinc.retiolum.confDir}/hosts";
|
|
|
|
};
|
2015-07-19 14:04:28 +00:00
|
|
|
ssh-identity-file = mkOption {
|
2015-10-09 12:08:06 +00:00
|
|
|
type = types.suffixed-str [".ssh.id_ed25519" ".ssh.id_rsa"];
|
2019-05-17 11:06:36 +00:00
|
|
|
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";
|
2015-07-19 14:04:28 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
imp = {
|
|
|
|
systemd.services.github-hosts-sync = {
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
environment = {
|
2019-05-17 11:06:36 +00:00
|
|
|
GITHUB_HOST_SYNC_SRCDIR = cfg.srcDir;
|
|
|
|
GITHUB_HOST_SYNC_WORKTREE = cfg.workTree;
|
|
|
|
GITHUB_HOST_SYNC_URL = cfg.url;
|
2015-07-19 14:04:28 +00:00
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
PermissionsStartOnly = "true";
|
|
|
|
SyslogIdentifier = "github-hosts-sync";
|
|
|
|
User = user.name;
|
2019-05-17 11:06:36 +00:00
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
2016-06-13 00:04:22 +00:00
|
|
|
ExecStartPre = pkgs.writeDash "github-hosts-sync-init" ''
|
2015-07-19 14:04:28 +00:00
|
|
|
set -euf
|
2015-10-09 12:32:45 +00:00
|
|
|
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} \
|
2015-07-19 14:04:28 +00:00
|
|
|
${cfg.ssh-identity-file} \
|
2015-10-09 12:08:06 +00:00
|
|
|
${cfg.dataDir}/.ssh/${fileExtension cfg.ssh-identity-file}
|
2015-07-19 14:04:28 +00:00
|
|
|
'';
|
2015-08-28 22:17:25 +00:00
|
|
|
ExecStart = "${pkgs.github-hosts-sync}/bin/github-hosts-sync";
|
2015-07-19 14:04:28 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.extraUsers = singleton {
|
|
|
|
inherit (user) name uid;
|
|
|
|
home = cfg.dataDir;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-12-26 04:55:13 +00:00
|
|
|
user = rec {
|
2015-07-19 14:04:28 +00:00
|
|
|
name = "github-hosts-sync";
|
2018-12-02 19:16:16 +00:00
|
|
|
uid = genid_uint31 name;
|
2015-07-19 14:04:28 +00:00
|
|
|
};
|
2015-10-09 12:08:06 +00:00
|
|
|
|
|
|
|
# TODO move to lib?
|
|
|
|
fileExtension = s: last (splitString "." s);
|
|
|
|
|
|
|
|
in out
|