summaryrefslogtreecommitdiff
path: root/ymir.nix
blob: daea5f2d80c445e7d978415fa2ea75689ce2b8f2 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
{ config, pkgs, lib, ... }:

with lib;

let
  luaPam = pkgs.callPackage ./custom/luaPam.nix {};
  luaPosix = pkgs.callPackage ./custom/luaPosix.nix {};
  luaSha2 = pkgs.callPackage ./custom/luaSha2.nix {};
  prosodyAuth = pkgs.callPackage ./custom/prosody-auth.nix {};
  prosodyVirtHost = name: {
    enabled = true;
    domain = name;
    ssl = {
      key = "/var/lib/acme/yggdrasil.li/key.pem";
      cert = "/var/lib/acme/yggdrasil.li/fullchain.pem";
      extraOptions = {
        dhparam = config.security.dhparams.params.prosody.path;
      };
    };
  };
  myDomains = [ "dirty-haskell.org" "www.dirty-haskell.org" "lists.dirty-haskell.org" "l.dirty-haskell.org"
                "online.141.li" "o.141.li" "ftp.141.li" "files.141.li" "f.141.li" "ymir.141.li" "141.li" "www.141.li" "lists.141.li" "l.141.li" "rpg.141.li" "odin.141.li"
                "ymir.xmpp.li" "xmpp.li" "www.xmpp.li" "lists.xmpp.li" "l.xmpp.li" "muc.xmpp.li" "proxy.xmpp.li"
                "online.yggdrasil.li" "o.yggdrasil.li" "ftp.yggdrasil.li" "files.yggdrasil.li" "f.yggdrasil.li" "ymir.yggdrasil.li" "git.yggdrasil.li" "www.yggdrasil.li" "yggdrasil.li" "lists.yggdrasil.li" "l.yggdrasil.li" "rpg.yggdrasil.li" "odin.yggdrasil.li"
                "online.praseodym.org" "o.praseodym.org" "ftp.praseodym.org" "files.praseodym.org" "f.praseodym.org" "ymir.praseodym.org" "praseodym.org" "www.praseodym.org" "lists.praseodym.org" "l.praseodym.org" "rpg.praseodym.org"
                "git.rheperire.org" "api.rheperire.org" "www.rheperire.org" "rheperire.org"
                "ymir.kleen.li" "kleen.li" "www.kleen.li"
                "ymir.nights.email" "nights.email" "www.nights.email"
              ];

   dnsZoneDir = ./ymir/zones;
   dnsZones = listToAttrs (flatten (mapAttrsToList dnsZone (builtins.readDir dnsZoneDir)));
   dnsZone = fName: type: optional (type == "regular" || type == "symlink") (nameValuePair (dnsZoneName fName) {
     data = readFile (dnsZoneDir + ("/" + fName));
   });
   dnsZoneName = fName: concatStringsSep "." (reverseList (splitString "." (removeSuffix ".soa" fName)));
in rec {
  imports =
    [
      ./nixpkgs.nix
      ./ymir/hw.nix
      ./ymir/mlmmj-expose.nix
      ./custom/zsh.nix
      ./users.nix
      ./custom/tinc/def.nix
      ./custom/tinc/yggdrasil.nix
      ./custom/ymir-nginx.nix
      ./custom/uucp.nix
      ./custom/unit-status-mail.nix
      ./utils/nix/module.nix
    ];

  boot.loader.grub = {
    enable = true;
    version = 2;
    device = "/dev/vda";
  };

  boot.tmpOnTmpfs = true;

  boot.kernel.sysctl = {
    "net.ipv4.tcp_keepalive_time" = 60;
    "net.ipv4.tcp_keepalive_intvl" = 10;
    "net.ipv4.tcp_keepalive_probes" = 6;
  };

  nixpkgs.config.allowUnfree = true;

  nixpkgs.overlays = [
    (self: super: {
      prosody = self.callPackage ./customized/prosody.nix ({
          inherit (self.lua51Packages) luasocket luaexpat luafilesystem luabitop luaevent luasec luadbi;
          lua5 = pkgs.lua5_1;
          withCommunityModules = ["carbons" "reload_modules" "csi" "cloud_notify" "csi_pump" "smacks" "track_muc_joins" "watchuntrusted"];
          extraModules = [prosodyAuth];
          extraLibs = [luaPam luaPosix luaSha2] ++ (with self.lua51Packages; [lua-zlib]);
        });
      # uwsgi = pkgs.callPackage ./customized/uwsgi.nix {
      #   extraPlugins = {
      #     cgi = {
      #       name = "cgi";
      #       interpreter = pkgs.python3.interpreter;
      #       path = "plugins/cgi";
      #       inputs = [ pkgs.python3 ];
      #       install = ''
      #         ${pkgs.python3.executable} -m compileall $out/${pkgs.python3.sitePackages}/
      #         ${pkgs.python3.executable} -O -m compileall $out/${pkgs.python3.sitePackages}/
      #       '';
      #     };
      #   };
      #   plugins = [];
      # };
      cgit = super.stdenv.lib.overrideDerivation super.cgit (oldAttrs : {
        buildInputs = oldAttrs.buildInputs ++ [
          self.perl
          self.python3
          self.makeWrapper
        ];
        postInstall = let
          pythonEnv = self.python3.buildEnv.override { extraLibs = with self.python3Packages; [ pygments markdown ]; };
        in ''
          wrapProgram $out/lib/cgit/filters/syntax-highlighting.py --prefix PYTHONPATH ':' ${pythonEnv}/lib/*/site-packages
          wrapProgram $out/lib/cgit/filters/about-formatting.sh --prefix PATH ':' ${self.coreutils}/bin
          tmpFile=$(mktemp)
          chmod +x $tmpFile
          echo "#!${pythonEnv}/bin/python3" >$tmpFile
          tail -n +2 $out/lib/cgit/filters/html-converters/md2html >>$tmpFile
          mv -v $tmpFile $out/lib/cgit/filters/html-converters/md2html
          wrapProgram $out/lib/cgit/filters/html-converters/md2html --prefix PYTHONPATH ':' ${pythonEnv}/lib/*/site-packages
          wrapProgram $out/lib/cgit/filters/html-converters/man2html --prefix PATH ':' ${self.groff}/bin
        '';
      });
      push2bin = super.writeScriptBin "push2bin" ''
        #!${self.zsh}/bin/zsh

        PATH=${self.coreutils}/bin:${self.gawk}/bin

        baseDir=/srv/www/files
        baseUrl="https://f.141.li"

        tmpFile=$(mktemp "''${baseDir}/.upload.XXXXXXXXXX")

        function zshexit() { [[ -n "''${tmpFile}" && -e "''${tmpFile}" ]] && rm -f "''${tmpFile}" }

        prefix=$(tee "''${tmpFile}" | sha512sum | awk '{ print $1; }' | head -c 10)
        prefix=''${prefix:l}
        filename="$1"
 
        [[ -z "''${prefix}" || -z "''${filename}" ]] && exit 2
        [[ ! -f "''${tmpFile}" || $(stat -c '%s' "''${tmpFile}") == "0" ]] && exit 3
 
        mkdir -p "''${baseDir}/''${prefix}"
        mv "''${tmpFile}" "''${baseDir}/''${prefix}/''${filename}"
 
        chmod 755 "''${baseDir}/''${prefix}"
        chmod 644 "''${baseDir}/''${prefix}/''${filename}"
 
        printf "%s/%s/%s" "''${baseUrl}" "''${prefix}" "''${filename}"
      '';
      pam_pwdfile = super.stdenv.mkDerivation rec {
        name = "pam-pwdfile-${version}";
        version = "1.0";
        src = super.fetchFromGitHub {
          owner = "tiwe-de";
          repo = "libpam-pwdfile";
          rev = "v${version}";
          sha256 = "0sjzwsnlf1g0xbingmjvb9gh8lnwzkkfzw10194ibnppdn4gy0zy";
        };

        buildInputs = with self; [ pam ];

        installFlags = [ "DESTDIR=$(out)" ];
      };
    })
  ];

  environment.systemPackages = with pkgs; [
    git
    mosh
    rsync
    tmux
    zsh
    mlmmj
    rebuild-system
  ];

  networking = {
    hostName = "ymir";
    hostId = "1c5c994e";
    firewall = {
      enable = true;
      allowPing = true;
      allowedTCPPorts = [ 21 # ftp
                          22 # ssh
                          25 # smtp
                          143 # imap
                          993 # imaps
                          5000 # xmpp proxy
                          5222 # xmpp.s2c
                          5269 # xmpp.s2s
                          655 # tinc.yggdrasil
                          656 # tinc.laeradhr
                          80 # http
                          443 # https
                          9418 # git
                          64738 # murmur
                          53 # DNS
                          6523 # Obby
                          4190 # Managesieve
                        ];
      allowedUDPPorts = [ 64738 # murmur
                          53 # DNS
                        ];
      allowedTCPPortRanges = [ { from = 20000; to = 21000; } # ftp
                             ];
      allowedUDPPortRanges = [ { from = 60000; to = 61000; } # mosh
                             ];

      interfaces.yggdrasil.allowedTCPPorts =
        [ 11332 # rspamd
        ];
    };
    enableIPv6 = true;
    interfaces."ens3" = {
      useDHCP = true;
      ipv6.addresses = [
          { address = "2a03:4000:6:d004::";
            prefixLength = 64;
          }
        ];
    };
    resolvconf.dnsExtensionMechanism = true;
    nameservers = [ "::1" "127.0.0.1" "10.141.1.1" "8.8.8.8" "8.8.4.4" ];
    domain = "niflheim.yggdrasil";
    search = [ "niflheim.yggdrasil" "yggdrasil" "asgard.yggdrasil" ];
  };

  users.extraUsers.root = let
    template = (import users/gkleen.nix);
    in {
        inherit (template) shell;
        openssh.authorizedKeys.keyFiles = template.openssh.authorizedKeys.keyFiles;
      };

  # List services that you want to enable:

  services.openssh = {
    enable = true;
    passwordAuthentication = false;
    challengeResponseAuthentication = false;
    extraConfig = ''
      AllowGroups ssh
    '';
    knownHosts = import ./knownHosts.nix;
  };
  users.groups."ssh" = {
    members = ["gitolite" "uucp" "root"];
  };

  services.fcron = {
    enable = true;
    systab = ''
      %weekly,erroronlymail  * *   nix-collect-garbage --delete-older-than '7d'
    '';
  };

  users.groups."ssl" = {
    members = [ "prosody"
                "nginx"
                "postfix"
                "murmur"
                "infinoted"
              ];
  };

  services.journald = {
    rateLimitBurst = 0;
    extraConfig = ''
      SystemMaxUse=100M 
    '';
  };
    
  services.prosody = {
    enable = true;
    admins = [
               "gkleen@xmpp.li"
               "gkleen@praseodym.org"
               "gkleen@141.li"
               "gkleen@yggdrasil.li"
             ];
    allowRegistration = false;
    extraModules = [ "posix"
                     "private"
                     "auth_custom"
                     "carbons"
                     "reload_modules"
                     "smacks"
                     "csi"
                     "csi_pump"
                     "cloud_notify"
                     "pep"
                     "disco"
                     "admin_adhoc"
                     "watchuntrusted"
                   ];
    extraConfig = ''
      reload_modules = { "group", "tls" }
      authentication="custom"
      custom_alias_file="/etc/prosody/aliases"
      custom_alias_secret_file="/etc/prosody/alias_secret"

      Component "alias.xmpp.li"
        Include "/etc/prosody/alias.xmpp.li.cfg.lua"

      Component "muc.xmpp.li" "muc"
        restrict_room_creation = true
        max_history_messages = 100
        name = "Multi-user chats"

      Component "proxy.xmpp.li" "proxy65"
        proxy65_acl = {"xmpp.li", "yggdrasil.li", "praseodym.org", "141.li", "nights.email"};
    '';

    virtualHosts = builtins.listToAttrs (map (name: { inherit name; value = prosodyVirtHost name; })
      ["xmpp.li" "yggdrasil.li" "praseodym.org" "141.li" "nights.email"]);
  };
  security.pam.services."xmpp".text = ''
    auth requisite  pam_succeed_if.so user ingroup xmpp
    auth required   pam_unix.so audit
  '';
  users.groups."shadow" = {
    members = [ "prosody"
              ];
  };
  users.groups."xmpp" = {};
  system.activationScripts."shadow-perms" = ''
    chown root:shadow /etc/shadow
    chmod 0640 /etc/shadow
  '';

  services.yggdrasilTinc = {
    enable = true;
    connect = false;
    useDNS = false;
    interfaceConfig = {
      ipv4 = {
        addresses = [ { address = "10.141.5.1"; prefixLength = 16; } ];
        routes = [ { address = "10.141.1.0"; prefixLength = 24; via = "10.141.1.1"; } ];
      };
      macAddress = "2e:1b:73:b2:49:6d";
    };
  };

  users.extraUsers."nginx".extraGroups = ["uwsgi"];

  services.uwsgi = {
    enable = true;
    plugins = ["python3" "cgi"];
    instance = {
      type = "emperor";
      uid = "root"; gid = "root";
      vassals = {
        "git.yggdrasil.li" =  {
          type = "normal";
          processes = 1;
          threads = 8;
          chdir = "${pkgs.cgit}/cgit";
          cgi = "${pkgs.cgit}/cgit/cgit.cgi";
          env = [
            "CGIT_CONFIG=/etc/cgit/git.yggdrasil.li"
          ];
          socket = "/tmp/git.yggdrasil.li.sock";
          chmod-socket = "660";
          chown-socket = "uwsgi:nginx";
          uid = "uwsgi"; gid = "uwsgi";
        };
        "git.rheperire.org" =  {
          type = "normal";
          processes = 1;
          threads = 8;
          chdir = "${pkgs.cgit}/cgit";
          cgi = "${pkgs.cgit}/cgit/cgit.cgi";
          env = [
            "CGIT_CONFIG=/etc/cgit/git.rheperire.org"
          ];
          socket = "/tmp/git.rheperire.org.sock";
          chmod-socket = "660";
          chown-socket = "uwsgi:nginx";
          uid = "uwsgi"; gid = "uwsgi";
        };
      };
    };
  };

  systemd.services."uwsgi" = {
    wantedBy = [ "nginx.service" ];
    before = [ "nginx.service" ];
  };
  
  users.extraUsers."uwsgi".extraGroups = ["gitolite"];

  environment.etc."cgit/git.yggdrasil.li" = {
    enable = true;
    text = ''
      robots=noindex, nofollow
      virtual-root=/
      enable-git-config=1
      remove-suffix=1

      root-title=git.yggdrasil.li
      root-desc=

      enable-http-clone=1

      enable-commit-graph=1
      snapshots=tar tar.gz tar.bz2 tar.xz zip
      side-by-side-diffs=1

      source-filter=${pkgs.cgit}/lib/cgit/filters/syntax-highlighting.py
      about-filter=${pkgs.cgit}/lib/cgit/filters/about-formatting.sh

      readme=:README.md
      readme=:README.txt
      readme=:README
      readme=:readme.md
      readme=:readme.txt
      readme=:readme

      clone-prefix=git://git.yggdrasil.li https://git.yggdrasil.li

      strict-export=git-daemon-export-ok
      section-from-path=2
      scan-path=${config.services.gitolite.dataDir}/repositories
    '';
  };
  environment.etc."cgit/git.rheperire.org" = {
    enable = true;
    text = ''
      robots=noindex, nofollow
      virtual-root=/
      enable-git-config=1
      remove-suffix=1

      root-title=git.rheperire.org
      root-desc=

      enable-http-clone=1

      enable-commit-graph=1
      snapshots=tar tar.gz tar.bz2 tar.xz zip
      side-by-side-diffs=1

      source-filter=${pkgs.cgit}/lib/cgit/filters/syntax-highlighting.py
      about-filter=${pkgs.cgit}/lib/cgit/filters/about-formatting.sh

      readme=:README.md
      readme=:README.txt
      readme=:README
      readme=:readme.md
      readme=:readme.txt
      readme=:readme

      clone-prefix=git://git.rheperire.org https://git.rheperire.org

      strict-export=git-daemon-export-ok
      project-list=${pkgs.writeText "project-list" ''
        rheperire.git
        cryptoids.git
      ''}
      section-from-path=2
      scan-path=${config.services.gitolite.dataDir}/repositories
    '';
  };

  services.gitolite = {
    enable = true;
    adminPubkey = builtins.readFile (builtins.head (import ./users/gkleen.nix).openssh.authorizedKeys.keyFiles);
    dataDir = "/srv/git";
    user = "gitolite";
    extraGitoliteRc = ''
      $RC{UMASK} = 0027;
      $RC{GIT_CONFIG_KEYS} = 'gitweb\.(owner|description|category)';
      $RC{LOG_DEST} = 'syslog';
      $RC{ROLES}{AUTHORS} = 1;
      $RC{GROUPLIST_PGM} = 'printf "@self-key-managers"';
      $RC{HOSTNAME} = '${networking.hostName}';
      $RC{LOCAL_CODE} = "$rc{GL_ADMIN_BASE}/local";
      push(@{$RC{ENABLE}}, qw(create fork D cgit repo-specific-hooks macros));
    '';
  };

  services.gitDaemon = {
    enable = true;
    basePath = services.gitolite.dataDir + "/repositories";
    user = "gitolite";
    group = "gitolite";
  };

  services.postfix = {
    enable = true;
    hostname = "ymir.yggdrasil.li";
    recipientDelimiter = "+";
    setSendmail = true;
    postmasterAlias = ""; rootAlias = ""; extraAliases = "";
    virtual = ''
      blog@dirty-haskell.org dirty-haskell@lists.yggdrasil.li
      @nights.email some@nights.email
    '';
    #destination = ["yggdrasil.li" "ymir.yggdrasil.li" "praseodym.org" "ymir.praseodym.org" "141.li" "ymir.141.li" "xmpp.li" "ymir.xmpp.li" "dirty-haskell.org" "explainuxul.de" "www.explainuxul.de" "lmu.li" "www.lmu.li" "localhost.yggdrasil.li" "localhost"];
    destination = [''regexp:${pkgs.writeText "destination" ''
        /\.?yggdrasil\.li$/ ACCEPT
        /\.?praseodym\.org$/ ACCEPT
        /\.?141\.li$/ ACCEPT
        /\.?xmpp\.li$/ ACCEPT
        /\.?kleen\.li$/ ACCEPT
        /\.?dirty-haskell\.org$/ ACCEPT
        /\.?nights\.email$/ ACCEPT
        /\.?yggdrasil$/ ACCEPT
        /\.?localdomain$/ ACCEPT
        /^localhost$/ ACCEPT
        /\.?ymir$/ ACCEPT
      ''}''];
    sslCert = "/var/lib/acme/yggdrasil.li/fullchain.pem";
    sslKey = "/var/lib/acme/yggdrasil.li/key.pem";
    config = {
      #the dh params
      smtpd_tls_dh1024_param_file = config.security.dhparams.params."postfix-1024".path;
      smtpd_tls_dh512_param_file = config.security.dhparams.params."postfix-512".path;
      #enable ECDH
      smtpd_tls_eecdh_grade = "strong";
      #enabled SSL protocols, don't allow SSLv2 and SSLv3
      smtpd_tls_protocols = [ "!SSLv2" "!SSLv3"];
      smtpd_tls_mandatory_protocols = ["!SSLv2" "!SSLv3"];
      #allowed ciphers for smtpd_tls_security_level=encrypt
      smtpd_tls_mandatory_ciphers = "high";
      #allowed ciphers for smtpd_tls_security_level=may
      #smtpd_tls_ciphers = high
      #enforce the server cipher preference
      tls_preempt_cipherlist = true;
      #disable following ciphers for smtpd_tls_security_level=encrypt
      smtpd_tls_mandatory_exclude_ciphers = ["aNULL" "MD5" "DES" "ADH" "RC4" "PSD" "SRP" "3DES" "eNULL"];
      #disable following ciphers for smtpd_tls_security_level=may
      smtpd_tls_exclude_ciphers = ["aNULL" "MD5" "DES" "ADH" "RC4" "PSD" "SRP" "3DES" "eNULL"];
      #enable TLS logging to see the ciphers for inbound connections
      smtpd_tls_loglevel = "1";
      #enable TLS logging to see the ciphers for outbound connections
      smtp_tls_loglevel = "1";

      smtp_dns_support_level = "dnssec";
      smtp_tls_security_level = "dane";

      transport_maps = ''regexp:${pkgs.writeText "transport" ''
        /@(rpgs?|lists?|l)\.(.*\.)?(yggdrasil\.li|praseodym\.org|141\.li|xmpp\.li|kleen\.li|dirty-haskell\.org|nights\.email|yggdrasil|localdomain|localhost|ymir)$/ mlmmj:
        /@subs?\.(rpgs?|lists?|l)\.(.*\.)?(yggdrasil\.li|praseodym\.org|141\.li|xmpp\.li|kleen\.li|dirty-haskell\.org|nights\.email|yggdrasil|localdomain|localhost|ymir)$/ mlmmj-subs:
        /@odin(\.asgard\.yggdrasil)?$/ uucp:odin
      ''} regexp:/srv/mail/transport pipemap:{texthash:/srv/mail/discard,static:{discard:}}'';

      local_recipient_maps = "";

      luser_relay = ''gkleen+''${local}'';
      
      # 10 GiB
      message_size_limit = "10737418240";
      # 10 GiB
      mailbox_size_limit = "10737418240";

      mailbox_transport_maps = "pipemap:{unix:passwd.byname, static:{lmtp:unix:private/dovecot-lmtp}}";
      #mailbox_command = ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -f "$SENDER" -a "$RECIPIENT"

      smtpd_sasl_type = "dovecot";
      smtpd_sasl_path = "private/dovecot-auth";

      smtpd_sasl_auth_enable = true;
      smtpd_sasl_security_options = ["noanonymous" "noplaintext"];
      smtpd_sasl_tls_security_options = "noanonymous";
      smtpd_tls_auth_only = true;

      smtpd_delay_reject = true;
      smtpd_helo_required = true;
      smtpd_helo_restrictions = "permit";

      smtpd_recipient_restrictions = [
        "reject_unauth_pipelining"
        "reject_non_fqdn_recipient"
        "reject_unknown_recipient_domain"
        "permit_mynetworks"
        "permit_sasl_authenticated"
        "reject_non_fqdn_helo_hostname"
        "reject_invalid_helo_hostname"
        "reject_unauth_destination"
      ];

      smtpd_relay_restrictions = [
        "permit_mynetworks"
        "permit_sasl_authenticated"
        "reject_unauth_destination"
      ];

      mlmmj_destination_recipient_limit = "1";
      mlmmj-subs_destination_recipient_limit = "1";
      propagate_unmatched_extensions = ["canonical" "virtual" "alias"];
      smtpd_authorized_verp_clients = "$authorized_verp_clients";
      authorized_verp_clients = "$mynetworks";

      milter_default_action = "accept";
      smtpd_milters = ["local:private/dkim" "local:private/rspamd"];
      non_smtpd_milters = ["local:private/dkim" "local:private/rspamd"];

      alias_maps = ''texthash:${pkgs.writeText "aliases" ''
        postmaster gkleen
        webmaster gkleen
        abuse gkleen
        noc gkleen
        security gkleen
        hostmaster gkleen
        usenet gkleen
        news gkleen
        www gkleen
        uucp gkleen
        ftp gkleen
        root gkleen
      ''} texthash:/srv/mail/spm
      '';

      queue_run_delay = "10s";
      minimal_backoff_time = "1m";
      maximal_backoff_time = "10m";
      maximal_queue_lifetime = "100m";
      bounce_queue_lifetime = "20m";

      sender_canonical_maps = "tcp:localhost:10001";
      sender_canonical_classes = "envelope_sender";
      recipient_canonical_maps = "tcp:localhost:10002";
      recipient_canonical_classes = ["envelope_recipient" "header_recipient"];

      smtpd_discard_ehlo_keyword_address_maps = "cidr:${pkgs.writeText "esmtp_access" ''
        # Allow DSN requests from local subnet only
        192.168.0.0/16      silent-discard
        172.16.0.0/12       silent-discard
        10.0.0.0/8          silent-discard
        0.0.0.0/0           silent-discard, dsn
        fd00::/8            silent-discard
        ::/0                silent-discard, dsn
      ''}";
    };
    masterConfig = {
      uucp = {
        type = "unix";
        private = true;
        privileged = true;
        chroot = false;
        command = "pipe";
        args = [ "flags=Fqhu" "user=uucp" ''argv=${config.security.wrapperDir}/uux -z -a $sender - $nexthop!rmail ($recipient)'' ];
      };
      mlmmj = {
        type = "unix";
        private = true;
        privileged = true;
        chroot = false;
        command = "pipe";
        args = [ "flags=XORhu" "user=mlmmj" ''argv=${pkgs.mlmmj}/bin/mlmmj-receive -F -L /srv/mail/lists/''${user} -s ''${sender} -e ''${extension}'' ];
      };
      mlmmj-subs = {
        type = "unix";
        private = true;
        privileged = true;
        chroot = false;
        command = "pipe";
        args = [ "flags=Fqhu" "user=mlmmj" ''argv=${pkgs.mlmmj-exposed}/bin/mlmmj-exposed /srv/mail/lists/''${user} ''${extension}'' ];
      };
    };
    networks = ["127.0.0.0/8" "[::ffff:127.0.0.0]/104" "[::1]/128" "10.141.0.0/16"];
  };

  services.postsrsd = {
    enable = true;
    domain = "srs.141.li";
    separator = "+";
    excludeDomains = [ ".yggdrasil.li" "yggdrasil.li"
                       ".praseodym.org" "praseodym.org"
                       ".141.li" "141.li"
                       ".xmpp.li" "xmpp.li"
                       ".kleen.li" "kleen.li"
                       ".nights.email" "nights.email"
                       ".lmu.li" "lmu.li"
                       ".dirty-haskell.org" "dirty-haskell.org"
                     ];
  };

  systemd.timers."mlmmj-maintd" = {
    description = "run mlmmj maintenance daemon";
    wantedBy = [ "multi-user.target" ];

    timerConfig = {
      OnActiveSec = "10m";
      OnUnitActiveSec = "10m";
    };
  };

  systemd.services."mlmmj-maintd" = {
    description = "mlmmj maintenance daemon";

    serviceConfig = {
      User = "mlmmj";
      Group = "mlmmj";
      ExecStart = "${pkgs.mlmmj}/bin/mlmmj-maintd -F -d /srv/mail/lists";
    };
  };

  services.opendkim = {
    enable = true;
    user = "postfix"; group = "postfix";
    socket = "local:/var/lib/postfix/queue/private/dkim";
    domains = ''csl:${concatStringsSep "," myDomains}'';
    keyPath = "/var/lib/dkim/";
    selector = "ymir";
    configFile = builtins.toFile "opendkim.conf" ''
      Syslog true
      MTACommand ${config.security.wrapperDir}/sendmail
      LogResults true
    '';
  };

  services.dovecot2 = {
    enable = true;
    enableImap = true;
    enableLmtp = true;
    enablePop3 = false;
    enablePAM = false; # do that manualy
    sslServerCert = "/var/lib/acme/yggdrasil.li/fullchain.pem";
    sslServerKey = "/var/lib/acme/yggdrasil.li/key.pem";
    mailLocation = "maildir:~/mail:LAYOUT=index:UTF-8";
    modules = with pkgs; [ dovecot_pigeonhole ];
    protocols = [ "sieve" ];
    extraConfig = ''
      userdb {
        driver = passwd
      }

      passdb {
        driver = pam
        args = dovecot2

        result_success = continue-ok
      }

      passdb {
        driver = passwd-file
        args = /srv/mail/dovecot.passwd
      
        result_success = continue-ok
      }
    
      mail_plugins = $mail_plugins quota
      mailbox_list_index = yes
      postmaster_address = postmaster@yggdrasil.li
      recipient_delimiter = +
      auth_username_format = %n

      service auth {
        unix_listener /var/lib/postfix/queue/private/dovecot-auth {
          mode = 0600
          user = postfix
          group = postfix
        }
      }

      service lmtp {
        unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
          mode = 0600
          user = postfix
          group = postfix
        }
      }

      protocol lmtp {
        mail_plugins = $mail_plugins sieve
      }

      protocol lda {
        mail_plugins = $mail_plugins sieve
      }

      namespace inbox {
        separator = /
        inbox = yes
        prefix = 
      }

      plugin {
        quota = maildir:User quota
        quota_rule = *:storage=1GB
        quota_rule2 = Trash:storage=+10%%
        quota_status_overquota = "552 5.2.2 Mailbox is full"
        quota_status_success = DUNNO
        quota_status_nouser = DUNNO
        quota_grace = 10%%
      }

      protocol imap {
        mail_max_userip_connections = 50
        mail_plugins = $mail_plugins imap_quota
      }

      service managesieve-login {
        inet_listener sieve {
          port = 4190
        }
      }

      plugin {
        sieve = file:~/sieve;active=~/.dovecot.sieve
        sieve_redirect_envelope_from = orig_recipient
      }
    '';
  };
  security.pam.services.dovecot2.text = ''
    auth requisite  pam_succeed_if.so user ingroup mail
    auth required   pam_unix.so audit
    account sufficient pam_unix.so
  '';
  users.groups."mail" = {};

  users.extraUsers."mlmmj" = {
    isSystemUser = true;
    group = "mlmmj";
    extraGroups = [ "mail" ];
  };

  users.extraGroups."mlmmj" = {};

  users.extraGroups."mladmin" = {
    members = [ "gkleen" ];
  };
  
  users.extraGroups."infinoted" = {
    members = [ "infinoted" "gitolite" ];
  };

  security.sudo.extraConfig = ''
    %mladmin ALL=(mlmmj) NOPASSWD: ALL
    %infinoted ALL=(infinoted) NOPASSWD: ALL
  '';

  security.polkit = {
      enable = true;
      extraConfig = ''
        polkit.addRule(function(action, subject) {
          if (    action.id == "org.freedesktop.systemd1.manage-units"
               && action.lookup("unit") == "infinoted.service"
               && subject.isInGroup("infinoted")
             ) {
              return polkit.Result.YES;
          }
        });
      '';
    };

  security.wrappers = { "newgrp".source = "${pkgs.shadow}/bin/newgrp"; };

  security.acme = {
    acceptTerms = true;
    certs = {
      "yggdrasil.li" = {
        allowKeysForGroup = true;
        group = "ssl";
        webroot = "/srv/www/acme";
        email = "phikeebaogobaegh@141.li";
        extraDomains = builtins.listToAttrs (builtins.map (name: { inherit name; value = null; }) myDomains);
        postRun = ''
          systemctl reload nginx.service dovecot2.service postfix.service prosody.service vsftpd.service infinoted.service
        '';
      };
    };
  };

  systemd.services."acme-yggdrasil.li".requires = [ "nginx.service" ]; 
  systemd.tmpfiles.rules
    = let mkDir = domain: "d /srv/www/acme 0775 root ssl 10d -";
       in map mkDir myDomains;

  services.uucp = {
    enable = true;
    nodeName = "ymir";
    remoteNodes = {
      "odin" = {
        publicKeys = ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKT/BsAMLJs9NYhKIso4J3EF+VzRBm3c+qCQ5ONKc/1s uucp@odin"];
        hostnames = ["odin.asgard.yggdrasil"];
      };
      "hel" = {
        publicKeys = ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOWBybBQKbPucqBgULQ1phv7IKFWl1Xc4drkCx3D5mIz uucp@hel"];
        hostnames = ["hel.midgard.yggdrasil"];
      };
      "sif" = {
        publicKeys = ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINDLcU8Qi+Ogu+jBSd4hJ5XO6HhRYs6/Y4mVAtTwdime root@sif"];
        hostnames = ["sif.midgard.yggdrasil"];
      };
    };

    commandPath = ["${pkgs.rmail}/bin" "${pkgs.push2bin}/bin" "${pkgs.rspamd}/bin"];
    defaultCommands = ["rmail" "push2bin" "rspamc"];
  };

  services.atd = {
    enable = true;
  };

  users.groups."filebin" = {
    members = ["gkleen" "uucp"];
  };

  services.nsd = {
    enable = true;
    verbosity = 3;
    interfaces = [ "10.142.0.3" "188.68.51.254" "2a03:4000:6:d004::" ];
    ipTransparent = true;
    remoteControl = {
      enable = true;
    };
    zones = {
      "inwx" = {
        notify = [ "217.70.142.96 NOKEY"
                   "185.181.104.96 NOKEY"
                 ];
        provideXFR = [ "217.70.142.96 NOKEY"
                       "185.181.104.96 NOKEY"
                     ];
        outgoingInterface = "188.68.51.254";
        children = dnsZones;
        dnssec = true;
        dnssecPolicy = {
          coverage = "2mo";
        };
      };
    };
  };

  services.unbound = {
    enable = true;
    allowedAccess = ["127.0.0.0/8" "::ffff:127.0.0.0/104" "::1/128" "10.141.0.0/16"];
    interfaces = ["127.0.0.1" "::1" "10.141.5.1"];
    extraConfig = ''
      verbosity: 1
    
      private-domain: "yggdrasil"
    
      domain-insecure: "10.in-addr.arpa"
      domain-insecure: "yggdrasil"

      local-zone: "10.in-addr.arpa" nodefault

      forward-zone:
        name: "10.in-addr.arpa"
        forward-addr: 10.141.1.1
      forward-zone:
        name: "yggdrasil"
        forward-addr: 10.141.1.1
    '';
  };

  services.dhcpd4 = {
    enable = true;
    interfaces = [ "yggdrasil" ];
    machines = [
      { hostName = "hel"; ethernetAddress = "ee:10:15:9a:cc:1f"; ipAddress = "hel.midgard.yggdrasil"; }
      { hostName = "sif"; ethernetAddress = "5c:93:21:c3:61:39"; ipAddress = "sif.midgard.yggdrasil"; }
    ];
    extraConfig = ''
      option rfc3442-classless-static-routes code 121 = array of integer 8;
      option ms-classless-static-routes code 249 = array of integer 8;

      subnet 10.141.0.0 netmask 255.255.0.0 {
        range 10.141.255.0 10.141.255.254;

        option rfc3442-classless-static-routes 24, 10, 141, 4, 10, 141, 1, 5, 24, 10, 141, 1, 10, 141, 1, 1, 24, 192, 168, 178, 10, 141, 1, 1;
        option ms-classless-static-routes 24, 10, 141, 4, 10, 141, 1, 5, 24, 10, 141, 1, 10, 141, 1, 1, 24, 192, 168, 178, 10, 141, 1, 1;

        option domain-name "yggdrasil";
        option domain-name-servers 10.141.1.1, 8.8.8.8, 8.8.4.4, 192.168.178.1;
      }
    '';
  };

  services.infinoted = {
    enable = true;
    keyFile = "/var/lib/acme/yggdrasil.li/key.pem";
    certificateFile = "/var/lib/acme/yggdrasil.li/fullchain.pem";
    plugins = [ "note-text" "note-chat" "logging" "autosave" "certificate-auth" "directory-sync" ];
    extraConfig = ''
      [certificate-auth]
      ca-list=/var/lib/infinoted/ca.cert.pem
      ca-key=/var/lib/infinoted/ca.key.pem
      accept-unauthenticated-clients=true

      [autosave]
      interval=5

      [directory-sync]
      directory=/var/lib/infinoted/dirsync
      interval=5
      hook=${pkgs.writeScript "git-sync.sh" ''
        #!${pkgs.zsh}/bin/zsh

        git -C ''${2:h} rev-parse --is-inside-work-tree &>/dev/null || exit 0

        repository=$(git -C ''${2:h} rev-parse --show-toplevel)
        [[ $? -ne 0 ]] && exit $?

        git() {
            $(whence -cp git) -C ''${repository} ''${@}
        }

        typeset -a changeSet
        changeSet=()
        git diff -z --name-only | \
            while IFS= read -r -d $'\0' change; do changeSet=(''${changeSet} ''${change}); done

        [[ ''${changeSet[(i)$(realpath ''${2} --relative-to=''${repository})]} -le ''${#changeSet} ]] || exit 0

        commitMessage=$(printf "%s modified via infinoted" $(realpath ''${2} --relative-to=''${repository}))
        git add ''${2}
        git commit -m ''${commitMessage} --no-edit ''${2}
        git push
      ''}
    '';
  };

  systemd.services."infinoted".serviceConfig.ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";

  users.extraUsers."infinoted" = {
    isSystemUser = true;
  };

  services.haveged = {
    enable = true;
  };

  system.autoUpgrade.enable = true;
  system.stateVersion = "17.09";

  systemd.services."nixos-upgrade".path = with pkgs; [ git ];
  systemd.services."nixos-upgrade".preStart = ''
    git -C /etc/nixos pull
    git -C /etc/nixos submodule update
  '';

  systemd.status-mail = {
    onFailure = [ "nixos-upgrade" "postfix" "dovecot2" "prosody" "opendkim" "nsd" "unbound" "tinc@yggdrasil" "postsrsd" ];
  };

  services.vsftpd = {
    enable = true;
    forceLocalLoginsSSL = true;
    forceLocalDataSSL = true;
    localUsers = true;
    writeEnable = true;
    chrootlocalUser = true;
    rsaKeyFile = "/var/lib/acme/yggdrasil.li/key.pem";
    rsaCertFile = "/var/lib/acme/yggdrasil.li/fullchain.pem";
    extraConfig = ''
      local_umask=022
    
      log_ftp_protocol=NO
      xferlog_enable=YES
    
      pam_service_name=vsftpd

      port_enable=NO

      pasv_enable=YES
      pasv_max_port=21000
      pasv_min_port=20000

      allow_writeable_chroot=YES

      guest_enable=YES
      guest_username=vsftpd
      virtual_use_local_privs=YES
      user_sub_token=$USER
      local_root=/srv/ftp/$USER
      hide_ids=YES
    '';
  };

  systemd.services."vsftpd".serviceConfig.ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";

  security.pam.services."vsftpd".text = ''
    auth required ${pkgs.pam_pwdfile}/lib/security/pam_pwdfile.so pwdfile=/srv/ftp.htpasswd
    account required pam_permit.so
  '';

  users.extraUsers."vsftpd" = {
    home = mkForce "/srv/ftp";
  };

  security.dhparams = {
    enable = true;
    params = {
      nginx.bits = 3072;
      "postfix-512".bits = 512;
      "postfix-1024".bits = 1024;
      "dovecot2".bits = 2048;
      prosody.bits = 3072;
    };
  };

  services.rspamd = {
    enable = true;
    workers = {
      controller = {};
      external = {
        type = "rspamd_proxy";
        bindSockets = [
          { mode = "0660";
            socket = "/var/lib/postfix/queue/private/rspamd";
            owner = config.services.rspamd.user;
            group = config.services.postfix.group;
          }
        ];
        extraConfig = ''
          milter = yes;

          upstream "local" {
            default = yes;
            self_scan = yes;
          }
        '';
      };
      internal = {
        type = "rspamd_proxy";
        bindSockets = [
          "ymir.niflheim.yggdrasil:11332"
        ];
        extraConfig = ''
          milter = yes;

          upstream "local" {
            default = yes;
            self_scan = yes;
            settings_id = "internal";
          }
        '';
      };
    };
    locals = {
      "milter_headers.conf".text = ''
        use = ["authentication-results", "x-spamd-result", "x-rspamd-queue-id", "x-rspamd-server"];
        extended_headers_rcpt = ["@odin.asgard.yggdrasil"];
      '';
      "actions.conf".text = ''
        reject = 15;
        add_header = 10;
        greylist = 5;
      '';
      "groups.conf".text = ''
        symbols {
          "BAYES_SPAM" {
            weight = 2.0;
          }
        }
      '';
      "dmarc.conf".text = ''
        reporting = true;
        send_reports = true;
        report_settings {
          org_name = "Yggdrasil.li";
          domain = "yggdrasil.li";
          email = "postmaster@yggdrasil.li";
        }
      '';
      "redis.conf".text = ''
        servers = "localhost";
      '';
      "dkim_signing.conf".text = "enabled = false;";
      "neural.conf".text = "enabled = false;";
      "classifier-bayes.conf".text = ''
        enable = true;
        expire = 8640000;
        new_schema = true;
        backend = "redis";
        per_user = true;
        min_learns = 0;

        autolearn = [0, 10];
        
        statfile {
            symbol = "BAYES_HAM";
            spam = false;
        }
        statfile {
            symbol = "BAYES_SPAM";
            spam = true;
        }
      '';
      "settings.conf".text = ''
        internal {
          priority = high;

          apply {
            milter_headers {
              skip_local = false;
              skip_authenticated = false;
              authenticated_headers = ["authentication-results", "x-spamd-result", "x-rspamd-queue-id", "x-rspamd-server"];
            }

            actions {
              reject = null;
              greylist = null;
              add_header = 10;
            }

            rules_disabled = ["FORGED_RECIPIENTS", "RCVD_NO_TLS_LAST"];
          }
        }
      '';
    };
  };

  systemd.services.rspamd = {
    requires = [ "redis.service" ];
    bindsTo = [ "redis.service" ];
  };

  services.redis = {
    enable = true;
    vmOverCommit = true;
    bind = "127.0.0.1 ::1";
  };
}