summaryrefslogtreecommitdiff
path: root/modules/web-services.nix
blob: 6c3bc76a3e096299f55a40740be8b6c41da6c96a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
{ config, lib, inputs, pkgs, ... }: with lib; let
  cfg = config.santi-modules.services;
  git-repo-path = "/server/git-repos";
  cgit-config = mkIf cfg.cgit.enable {
    environment.systemPackages = [
      pkgs.git
    ];
    users.users = {
      git = {
        description = "git user";
        isNormalUser = true;
        home = git-repo-path;
        openssh.authorizedKeys.keys = [ (builtins.readFile ../secrets/user-ssh-key.pub)] ++ builtins.attrValues (import ../secrets/host-pub-keys.nix);
      };
    };
    systemd.tmpfiles.rules = [
      "d ${git-repo-path} 0755 git users -"
    ];
    services.cgit.santi = let
      org2html = pkgs.writeShellScript "org2md" ''
        ${pkgs.pandoc}/bin/pandoc \
          --from org \
          --to html5 \
          --sandbox=true \
          --html-q-tags \
          --ascii \
          --standalone \
          --wrap=auto \
          --embed-resources \
          -M document-css=false
      '';
    in {
      enable = true;
      scanPath = git-repo-path;
      nginx.virtualHost = "git.santi.net.br";
      settings = {
        readme = ":README.org";
        root-title = "index";
        root-desc = "public repositories for santi.net.br";
        about-filter = toString org2html;
        source-filter = "${pkgs.cgit}/lib/cgit/filters/syntax-highlighting.py";
        enable-git-config = true;
        enable-html-cache = false;
        enable-blame = true;
        enable-log-linecount = true;
        enable-index-links = true;
        enable-index-owner = false;
        enable-commit-graph = true;
        remove-suffix = true;
      };
    };
  };
  blog-config = let
    blog-public-path = "/server/blog";
    env = pkgs.buildEnv {
      name = "post-receive-env";
      paths = [
        pkgs.git
        pkgs.coreutils
        pkgs.gnutar
        pkgs.xz
      ];
    };
    post-receive = pkgs.writeShellScript "post-receive" ''
      export PATH=${env}/bin
      set -ex
      
      GIT_DIR=$(${pkgs.git}/bin/git rev-parse --git-dir 2>/dev/null)
      if [ -z "$GIT_DIR" ]; then
              echo >&2 "fatal: post-receive: GIT_DIR not set"
              exit 1
      fi
      
      TMPDIR=$(mktemp -d)
      function cleanup() {
        rm -rf "$TMPDIR"
      }
      trap cleanup EXIT
      
      ${pkgs.git}/bin/git clone "$GIT_DIR" "$TMPDIR"
      unset GIT_DIR
      cd "$TMPDIR"
      ${pkgs.hugo}/bin/hugo --destination ${blog-public-path}
    '';
  in mkIf cfg.blog.enable {
    networking.firewall.allowedTCPPorts = [ 80 443 ];
    # TODO: enable SSL
    services.nginx = {
      enable = true;
      virtualHosts.${cfg.blog.url} = {
        addSSL = true;
        enableACME = true;
        root = blog-public-path;
      };
    };
    security.acme = {
      acceptTerms = true;
      certs.${cfg.blog.url}.email = "[email protected]";
    };
    systemd.tmpfiles.rules = [
      "d ${blog-public-path} 0755 git users -"
    ];
    systemd.services."blog-prepare-git-repo" = {
      wantedBy = [ "multi-user.target" ];
      path = [
        pkgs.git
      ];
      script = ''
        set -ex
        cd ${git-repo-path}
        chmod +rX ${blog-public-path}
        test -e blog || git init --bare blog
        ln -nsf ${post-receive} blog/hooks/post-receive
      '';
      serviceConfig = {
        Kind = "one-shot";
        User = "git";
      };
    };
  };
  
  ddns-config = mkIf cfg.ddns.enable {
    networking.enableIPv6 = true;
    services.cloudflared = {
      enable = true;
      tunnels.iori = {
        default = "http_status:404";
        credentialsFile = "/var/lib/cloudflared/iori.json";
        ingress = {
          "santi.net.br" = "http://localhost:80";
          "git.santi.net.br" = "http://localhost:80";
        };
      };
    };
    services.inadyn = {
      enable = true;
      user = "leonardo";
      group = "users";
      settings.provider."cloudflare.com" = {
        hostname="santi.net.br";
        username="santi.net.br";
        proxied = false;
        include = config.age.secrets.cloudflare.path;
      };
    };
  };
in {
  options.santi-modules.services = {
    blog = {
      enable = mkEnableOption "Enable blog hosting";
      url = mkOption {
        type = types.str;
        default = "santi.net.br";
        description = "Url to serve blog on";
      };
    };
    cgit.enable = mkEnableOption "Enable cgit instance";
    ddns.enable = mkEnableOption "Enable ddns service";
  };
  config = mkMerge [
    cgit-config
    blog-config
    ddns-config
  ];
}