summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/systemd.nix
blob: de28895e019bacf2070ee991ca9d09c1fee13bb2 (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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{ pkgs, config, userName, ... }:
let
  xmobar = import ./xmobar pkgs.haskellPackages;
  cfg = config.home-manager.users.${userName};

  autossh-socks-script = pkgs.writeScript "autossh" ''
    #!${pkgs.zsh}/bin/zsh -xe

    host="''${1%:*}"
    port="''${1#*:}"

    typeset -a cmd
    cmd=()

    if [[ -n "''${SSHPASS_SECRET}" ]]; then
      cmd+=(${pkgs.sshpassSecret}/bin/sshpass-secret)
      cmd+=("''${(@s/:/)SSHPASS_SECRET}")
      cmd+=(--)
    fi

    cmd+=(${pkgs.openssh}/bin/ssh -vN -D localhost:''${port} "''${host}")

    ( exec -a "''${cmd[1]}" -- ''${cmd} ) &
    pid=$!
    ${pkgs.systemd}/bin/systemd-notify --pid="''${pid}"

    check_pid() {
      ${pkgs.lsof}/bin/lsof -Pi @localhost:"''${port}" -sTCP:LISTEN -t
    }

    while ! check_pid; do
      ${pkgs.coreutils}/bin/sleep 0.1
    done

    ${pkgs.systemd}/bin/systemd-notify --pid=$(check_pid)
    ${pkgs.systemd}/bin/systemd-notify --ready

    wait "''${pid}"
  '';
in {
  services = {
    sync-keepass = {
      Service = {
        Type = "oneshot";
        WorkingDirectory = "~";
        ExecStart = "${pkgs.lftp}/bin/lftp -f ${./store.kdbx.lftp}";
      };
    };
    emacs = {
      Unit = {
        After = ["graphical-session-pre.target"];
      };
    };
    taffybar = {
      Service = {
        Environment = with pkgs; "PATH=${worktime}/bin:${systemd}/bin";
      };
    };
    dunst = {
      Service = {
        Restart = "always";
      };
      Install = {
        WantedBy = ["graphical-session.target"];
      };
    };
    xiccd = {
      Service = {
        Type = "simple";
        WorkingDirectory = "~";
        ExecStart = "${pkgs.xiccd}/bin/xiccd";
        Restart = "always";
      };
    };
    "autossh-socks@proxy.mathw0h:8119" = {
      Service = {
        Type = "notify";
        NotifyAccess = "all";
        WorkingDirectory = "~";
        Restart = "always";
        ExecStart = "${autossh-socks-script} \"%I\"";
        Environment = [ "SSHPASS_SECRET=gkleen@mathw0g.math.lmu.de" ];
      };
      Unit = {
        StopWhenUnneeded = true;
      };
    };
    "proxy-to-autossh-socks@8118" = {
      Unit = {
        Requires = ["autossh-socks@proxy.mathw0h:8119.service" "proxy-to-autossh-socks@8118.socket"];
        After = ["autossh-socks@proxy.mathw0h:8119.service" "proxy-to-autossh-socks@8118.socket"];
      };
      Service = {
        ExecStart = "${pkgs.systemd}/lib/systemd/systemd-socket-proxyd --exit-idle-time=10s localhost:8119";
      };
    };
  };
  sockets = {
    "proxy-to-autossh-socks@8118" = {
      Socket = {
        ListenStream = "%I";
      };
      Install = {
        WantedBy = ["default.target"];
      };
    };
  };
  timers = {
    sync-keepass = {
      Timer = {
        OnActiveSec = "1m";
        OnUnitActiveSec = "1m";
      };

      Install = {
        WantedBy = ["default.target"];
      };
    };
  };
  targets = {
    graphical-session = {
      Unit = {
        BindsTo = ["default.target"];
        After = ["basic.target"];
      };
    };
  };
}