about summary refs log tree commit diff
path: root/machines/srv1/configuration.nix
blob: 924e4085d1187a809cb360ce18c43ecc34a0ff7a (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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
{ config, pkgs, ... }:
let
  ModSecurity-nginx = pkgs.callPackage ./modsecurity.nix { };
  crs = pkgs.callPackage ./coreruleset.nix { };
  nvim = (import (pkgs.fetchzip {
    url = "https://github.com/nixos/nixpkgs/archive/517c29935b6e4dec12571e7d101e2b0da220263d.zip";
    sha256 = "1s85sz62iykvca90d3cgd981670rnkd5c171wda7wpwdj0d52sf3";
  }) { }).neovim.override {
    vimAlias = true;
  };

  mirror = pkgs.writeScriptBin "mirror" ''
  #!/bin/sh

  name=`echo "$1" | rev | cut -d'/' -f1 | rev`

  cd /srv/git
  sudo -u git ${pkgs.git}/bin/git clone --mirror $1 $name
  '';
in
  {
    imports =
      [
      #./hardware-configuration.nix
      ./cgit.nix
      ./yggdrasil.nix
    ];

    boot.loader.grub.enable = true;
    boot.loader.grub.version = 2;
    boot.loader.grub.device = "/dev/sda"; # or "nodev" for efi only

    networking.hostName = "srv1";
    networking.extraHosts = ''
      192.168.1.136 srv1.niedzwiedzinski.cyou git.niedzwiedzinski.cyou tmp.niedzwiedzinski.cyou zhr.niedzwiedzinski.cyou help.niedzwiedzinski.cyou niedzwiedzinski.cyou
    '' + pkgs.stdenv.lib.readFile ( pkgs.fetchurl {
      url = "https://raw.githubusercontent.com/StevenBlack/hosts/d2be343994aacdec74865ff8d159cf6e46359adf/alternates/fakenews-gambling-porn/hosts";
      sha256 = "1la5rd0znc25q8yd1iwbx22zzqi6941vyzmgar32jx568j856s8j";
    } );

    services.dnsmasq = {
      enable = true;
      servers = [ "1.1.1.1" "8.8.8.8" ];
      extraConfig = ''
        address=/.srv1.niedzwiedzinski.cyou/192.168.1.136
      '';
    };

    time.timeZone = "Europe/Warsaw";
    i18n.defaultLocale = "en_US.UTF-8"; # Less confusing locale than polish one
    console.keyMap = "pl";

    nix.gc = {
      automatic = true;
      options = "--delete-older-than 30d";
    };
    nix.optimise.automatic = true;
    nix.trustedUsers = [ "pn" ];
    system.autoUpgrade = {
      enable = true;
      allowReboot = true;
    };

    nixpkgs.config = {
      packageOverrides = super: {
        rss-bridge = super.rss-bridge.overrideDerivation (attrs: {
    src = pkgs.fetchFromGitHub {
            owner = "RSS-Bridge";
            repo = "rss-bridge";
            rev = "ee5d190391afffd037e09c04418a240f7ac67ecd";
            sha256 = "0sxdl6ycqmhd76hc5r8i1yv8vgl18ssmv1p9dzx8ikp5imvfgakc";
          };
        });
      };
    };

  # The global useDHCP flag is deprecated, therefore explicitly set to false here.
  # Per-interface useDHCP will be mandatory in the future, so this generated config
  # replicates the default behaviour.
  networking.useDHCP = false;
  networking.interfaces.enp4s0.useDHCP = true;

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

  # Select internationalisation properties.
  # i18n.defaultLocale = "en_US.UTF-8";
  # console = {
  #   font = "Lat2-Terminus16";
  #   keyMap = "us";
  # };

  environment.systemPackages = with pkgs; [
    curl wget htop git
    nvim lm_sensors
    mirror
  ];

  services.openssh.enable = true;
  services.openssh.passwordAuthentication = false;
  services.sshguard = {
    enable = true;
    whitelist = [
      "192.168.0.0/18"
      "201:da2c:2873:5ee3:cc87:79ce:5a12:fff9"
    ];
  };

  services.nginx.enable = true;
  services.nginx.package = (pkgs.nginx.override { modules = [ ModSecurity-nginx ]; });
  services.nginx.appendHttpConfig = ''
    modsecurity on;
    # modsecurity_rules '
    #   SecRuleEngine On
    #   Include ${crs}/crs-setup.conf;
    #   Include ${crs}/rules/*.conf;
    # ';
    charset utf-8;
    source_charset utf-8;
  '';
  services.nginx.virtualHosts = {
    "srv1.niedzwiedzinski.cyou" = let
      modsec_config = builtins.toFile "modsecurity_rules.conf" ''
        SecRuleEngine On
        SecRule ARGS:testparam "@contains test" "id:1234,deny,status:403"
      '';
    in {
      enableACME = true;
      forceSSL = true;
      extraConfig = ''
        location ~ /*.md {
	  types { } default_type "text/markdown; charset=utf-8";
        }
        modsecurity_rules_file ${modsec_config};
      '';
      root = "/var/www/srv1.niedzwiedzinski.cyou";
    };
    "pics.srv1.niedzwiedzinski.cyou" = {
      enableACME = true;
      forceSSL = true;
      root = "/var/www/pics.srv1.niedzwiedzinski.cyou";
    };
    "rss.srv1.niedzwiedzinski.cyou" = {
      enableACME = true;
      forceSSL = true;
      extraConfig = ''
        modsecurity_rules '
          SecRuleEngine On
          SecRule ARGS:u "@rx life[-_]*hack(s)?" "id:1234,deny,status:403"
        ';
      '';
    };
    "git.niedzwiedzinski.cyou" = {
      locations."/".proxyPass = "http://0.0.0.0:8080/cgit/";
      locations."/cgit/".extraConfig = ''
        rewrite ^/cgit/(.*) https://git.niedzwiedzinski.cyou/$1;
      '';
      enableACME = true;
      forceSSL = true;
    };
    "tmp.niedzwiedzinski.cyou" = {
      enableACME = true;
      addSSL = true;
      root = "/var/www/tmp.niedzwiedzinski.cyou";
      extraConfig = ''
        modsecurity_rules '
          SecRuleEngine On
          SecRule ARGS:testparam "@contains test" "id:1234,deny,status:403"
          Include ${crs}/crs-setup.conf
          Include ${crs}/all-rules.conf
        ';
      '';
    };
    "niedzwiedzinski.cyou" = {
      enableACME = true;
      forceSSL = true;
      root = "/var/www/niedzwiedzinski.cyou";
    };
    "y.niedzwiedzinski.cyou" = {
      enableACME = true;
      forceSSL = true;
      root = "/var/www/niedzwiedzinski.cyou";
      locations."/omick.net".proxyPass = "http://omick.net/";
      locations."/suckless.org".proxyPass = "http://suckless.org/";
      locations."/based.cooking".proxyPass = "http://based.cooking/";

    };
    "zhr.niedzwiedzinski.cyou" = {
      enableACME = true;
      forceSSL = true;
      root = "/var/www/zhr.niedzwiedzinski.cyou";
    };
    "help.niedzwiedzinski.cyou" = {
      enableACME = true;
      forceSSL = true;
      root = "/var/www/niedzwiedzinski.cyou/help";
    };
  };
  security.acme.email = "pniedzwiedzinski19@gmail.com";
  security.acme.acceptTerms = true;

  networking.firewall.allowedTCPPorts = [ 53 80 443 ];
  networking.firewall.allowedUDPPorts = [ 53 ];

  services.molly-brown = {
    #hostName = "srv1.niedzwiedzinski.cyou";
    #enable = true;
  };

  systemd = {
    services.git-fetch = {
      script = ''
        #!/bin/sh
        cd /srv/git
        for f in `find . -name HEAD`; do
          cd ''${f%HEAD}
          ${pkgs.git}/bin/git fetch
          cd /srv/git
        done
      '';
      serviceConfig = {
        Type = "oneshot";
        User = "git";
      };
    };
    timers.git-fetch = {
      partOf = [ "git-fetch.service" ];
      wantedBy = ["timers.target" ];
      timerConfig = {
        OnCalendar = "hourly";
        Unit = "git-fetch.service";
      };
    };

  };

  services.lighttpd = {
    enable = true;
    port = 8080;
    pn-cgit = {
      logo = "${./baby-yoda.png.comp}";
      enable = true;
      configText = let
        aboutFilter = pkgs.writeScriptBin "about-format.sh" ''
          #!/bin/sh
          ${pkgs.coreutils}/bin/cat << EOF
          <style>
          .md blockquote {
            background: #eee;
            font-style: italic;
            padding: 0 1em;
          }
          </style>
          <div class="md">
          EOF
          ${pkgs.coreutils}/bin/cat /dev/stdin | ${pkgs.lowdown}/bin/lowdown
          echo '</div>'
  '';
      in ''
        # source-filter=${pkgs.cgit}/lib/cgit/filters/syntax-highlighting.sh
        about-filter=${aboutFilter}/bin/about-format.sh
        #about-filter=${pkgs.discount}/bin/markdown
        cache-size=1000
        root-title=git.niedzwiedzinski.cyou
        root-desc=Personal git server, because I can
        readme=:README.md
        snapshots=tar.gz
        clone-prefix=https://git.niedzwiedzinski.cyou
        section-from-path=1
        scan-path=/srv/git/
      '';
    };
  };

  services.rss-bridge = {
    enable = true;
    virtualHost = "rss.srv1.niedzwiedzinski.cyou";
    whitelist = [
      "Instagram"
      "Soundcloud"
      "Facebook"
    ];
  };

  users = {
    groups = {
      git = {};
    };
    users = {
      pn = {
        isNormalUser = true;
        extraGroups = [ "wheel" "git" ]; # Enable ‘sudo’ for the user.
        openssh.authorizedKeys.keys = [
          "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIqlCe4ovKa/Gwl5xmgu9nvVPmFXMgwdeLRYW7Gg7RWx pniedzwiedzinski19@gmail.com"
        ];
      };

      git = {
        isSystemUser = true;
        group = "git";
        description = "git user";
        home = "/srv/git";
        shell = "${pkgs.git}/bin/git-shell";
        openssh.authorizedKeys.keys = [
          "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIqlCe4ovKa/Gwl5xmgu9nvVPmFXMgwdeLRYW7Gg7RWx pniedzwiedzinski19@gmail.com"
        ];
      };
    };
  };

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "20.09"; # Did you read the comment?

}