summaryrefslogtreecommitdiff
path: root/hel.nix
blob: 3fec134fb36fdb24c00c2afb7f4cc4633ab48f09 (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
# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

{ config, lib, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hel/hw.nix
      ./hel/boot.nix
      ./users.nix
      ./custom/zsh.nix
      ./custom/tinc/def.nix
      ./custom/tinc/yggdrasil.nix
      ./custom/uucp.nix
      ./custom/btrfs-snapshots.nix
      ./custom/borgbackup.nix
    ];

  system.stateVersion = "16.09";

  networking = {
    hostName = "hel";
    domain = "midgard.yggdrasil";

    hosts = {
      "127.0.0.1" = [ "hel.midgard.yggdrasil" "hel" ];
      "::1" = [ "hel.midgard.yggdrasil" "hel" ];
    };
    
    firewall = {
      enable = true;
      allowedTCPPorts = [ 22 # ssh
                        ];
    };

    networkmanager = {
      enable = true;
      unmanaged = [ "docker0" ];
    };
  };

  systemd.services.ModemManager = {
    enable = true;
    wantedBy = [ "network.target" ];
  };

  powerManagement.enable = true;

  i18n = {
    consoleFont = "lat9w-16";
    consoleKeyMap = "dvp";
    defaultLocale = "en_US.UTF-8";
  };

  boot.kernelPackages = pkgs.linuxPackages_latest;
  # boot.kernelPatches = [
  #     { patch = ./patches/udl.patch;
  #       name = "udl-vblank";
  #     }
  #   ];

  environment.systemPackages = with pkgs; [
    git
    slock
    shadow
    (callPackage ./utils/nix/rebuild-system.nix {})
    rewacom
    autorandr
    ntfs3g
    exfat
  ];

  nixpkgs.config.packageOverrides = pkgs: rec {
    rewacom = pkgs.writeScriptBin "rewacom" ''
      #!${pkgs.stdenv.shell}
      modprobe -r wacom
      modprobe wacom
    '';

    samsung-unified-linux-driver = pkgs.stdenv.lib.overrideDerivation pkgs.samsung-unified-linux-driver (oldAttrs: {
      buildInputs = oldAttrs.buildInputs ++ [ pkgs.makeWrapper ];
      builder = pkgs.writeScript "builder.sh" ''
        #!${pkgs.stdenv.shell}

        source ${pkgs.stdenv}/setup

        ${oldAttrs.builder} ${pkgs.lib.concatStringsSep " " oldAttrs.args}

        export PATH=${pkgs.makeWrapper}/bin:$PATH
      
        echo "Wrapping samsung filters"
        wrapProgram $out/lib/cups/filter/rastertosamsungspl \
          --prefix PATH : ${pkgs.ghostscript}/bin
        wrapProgram $out/lib/cups/filter/rastertosamsungsplc \
          --prefix PATH : ${pkgs.ghostscript}/bin
      '';
    });
  };

  nixpkgs.config.allowUnfree = true;

  virtualisation.docker = {
    enable = true;
    autoPrune.enable = true;
    enableOnBoot = false;
  };

  services = {
    vnstat = {
      enable = true;
    };

    journald = {
      rateLimitBurst = 0;
    };
   
    gpsd = {
      enable = true;
      debugLevel = 3;
      device = "/dev/gps0";
      readonly = false;
    };

    logind.extraConfig = ''
      HandleLidSwitch=sleep
      HandleSuspendKey=sleep
      LidSwitchIgnoreInhibited=no
    '';

    openssh = {
      enable = true;
      extraConfig = ''
        Match User media
          ForceCommand internal-sftp
          PermitTTY no
          AllowTcpForwarding no
          AllowStreamLocalForwarding no
          X11Forwarding no
          AllowAgentForwarding no
          ChrootDirectory /run/%u
          AuthorizedKeysFile /etc/ssh/authorized_keys.d/%u
      '';
    };

    atd = {
      enable = true;
      allowEveryone = true;
    };

    xserver = {
      enable = true;
      
      layout = "us";
      xkbVariant = "dvp";
      xkbOptions = "compose:caps";
      
      displayManager.slim = {
        enable = true;
        defaultUser = "gkleen";
      };

      desktopManager = {
        default = "none";
        xterm.enable = false;
      };

      windowManager = {
        default = "xmonad";
        xmonad = {
          enable = true;
          extraPackages = haskellPackages: (with haskellPackages; [ xmonad-contrib hostname libnotify aeson temporary parsec network taffybar ]);
        };
      };

      wacom.enable = true;
      multitouch.enable = true;

      dpi = 210;
    };

    ntp.enable = false;
    timesyncd.enable = false;

    chrony = {
      enable = true;
      extraConfig = ''
        refclock SOCK /var/run/chrony.gps0.sock refid GPS
      '';
    };

    yggdrasilTinc = {
      enable = true;
      connect = true;
      name = "hel";
      interfaceConfig = {
        ip4 = [ { address = "10.141.2.3"; prefixLength = 16; } ];
      };
    };

    uucp = {
      enable = true;
      nodeName = "hel";
      remoteNodes = ["isaac" "ymir"]; # legacy name for odin
      sshUser = {
        openssh.authorizedKeys.keys = [ ''restrict,command="${config.security.wrapperDir}/uucico" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFH1QWdgoC03nzW5GBuCl2pqASHeIXIYtE9IInHdaKcO uucp@ymir''
	                                ''restrict,command="${pkgs.writeScript "odin.sh" "#!${pkgs.stdenv.shell}\necho .\nexec -a uucico ${config.security.wrapperDir}/uucico\n"}" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJhACtnt9+3j2ev4QVA2QBlPtblPnu7yol2njgfMlHtC uucp@odin''
                                      ];
      };
      sshConfig = ''
        Host isaac
          Hostname odin.asgard.yggdrasil
          IdentityFile ~/.ssh/odin
	Host ymir
	  Hostname ymir.niflheim.yggdrasil
	  IdentityFile ~/.ssh/ymir
      '';
      commandPath = [ "${pkgs.callPackage ./hel/recv-media.nix {}}/bin" config.security.wrapperDir ];
      defaultCommands = [];
      commands = {
        "isaac" = ["recv-media" "notify-gkleen"];
      };
      protocols = {
        "isaac" = "t";
      };
    };
    
    postfix = {
      enable = true;
      enableSmtp = true;
      enableSubmission = false;
      setSendmail = true;
      networksStyle = "host";
      hostname = "hel.midgard.yggdrasil";
      destination = [];
      relayHost = "uucp:ymir";
      recipientDelimiter = "+";
      extraMasterConf = ''
        uucp        unix - n n - - pipe flags=Fqhu user=uucp argv=${config.security.wrapperDir}/uux -z -a $sender - $nexthop!rmail ($recipient)
        sshsendmail unix - n n - - pipe flags=Fq user=postfix_ssh argv=${pkgs.openssh}/bin/ssh -F /var/db/postfix_ssh/ssh.config $nexthop sendmail -f $sender -G $recipient
      '';
      transport = ''
        gkleen+sent@yggdrasil.li uucp:isaac
      '';
      extraConfig = ''
        always_bcc = gkleen+sent@yggdrasil.li
      
        default_transport = uucp:ymir

        inet_interfaces = loopback-only
      
        authorized_submit_users = !uucp, static:anyone
        message_size_limit = 0

        sender_dependent_default_transport_maps = regexp:${pkgs.writeText "sender_relay" ''
          /@math(ematik)?\.(lmu|uni-muenchen)\.de$/ sshsendmail:math60.mathinst.loc
          /@(cip|stud)\.ifi\.(lmu|uni-muenchen)\.de$/ smtp:smtp.ifi.lmu.de
          /@campus\.lmu\.de$/ smtp:postout.lrz.de
        ''}

        smtp_sasl_auth_enable = yes
        smtp_sender_dependent_authentication = yes
        smtp_sasl_tls_security_options = noanonymous
        smtp_tls_security_level = dane
        smtp_sasl_password_maps = texthash:/var/db/postfix/sasl_passwd
        smtp_cname_overrides_servername = no
        smtp_always_send_ehlo = yes

        smtp_tls_loglevel = 1
        smtp_dns_support_level = dnssec
      '';
    };

    printing = {
      enable = true;
      drivers = with pkgs; [ samsung-unified-linux-driver ];
    };

    upower = {
      enable = true;
    };

    locate = {
      enable = true;
      interval = "hourly";
      locate = pkgs.mlocate;
      localuser = null;
      prunePaths = ["/tmp" "/var/tmp" "/var/cache" "/var/lock" "/var/run" "/var/spool"];
    };

    dbus = {
      enable = true;
      packages = [ (pkgs.writeTextFile {
                      name = "eavesdrop.conf";
                      text = ''
                        <!DOCTYPE busconfig PUBLIC
                        "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
                        "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
                        <busconfig>
                          <policy user="root">
                            <allow eavesdrop="true"/>
                            <allow eavesdrop="true" send_destination="*"/>
                          </policy>
                        </busconfig>
                      '';
                      destination = "/etc/dbus-1/system.d/eavesdrop.conf";
                    })
                 ];
    };
  };

  systemd.services."gpsd".wants = [ "chronyd.service" ];
  systemd.services."gpsd".after = [ "chronyd.service" ];

  users = {
    mutableUsers = false;
  
    extraUsers.root = { inherit (import ./users/gkleen.nix) shell hashedPassword; };

    extraUsers.gkleen.extraGroups = [ "media" "networkmanager" "docker" ];

    extraUsers.media = {
      group = "media";
      home = "/var/media";
      isSystemUser = true;
      openssh.authorizedKeys.keyFiles = [
        ./users/keys/gkleen-media-hel.pub
      ];
      useDefaultShell = true;
    };

    extraUsers.postfix_ssh = {
      isSystemUser = true;
      home = "/var/db/postfix_ssh";
    };

    extraGroups = {
      network = {};
      media = {
        members = [ "uucp" "media" ];
      };
    };
  };

  security = {
    sudo.extraConfig = ''
      Cmnd_Alias  SYSCTRL = /run/current-system/sw/sbin/shutdown, /run/current-system/sw/sbin/reboot, /run/current-system/sw/sbin/halt, /run/current-system/sw/bin/systemctl, ${pkgs.rewacom}/bin/rewacom
      %wheel ALL=(ALL) NOPASSWD: SYSCTRL
    '';

    wrappers = { "slock".source = "${pkgs.slock}/bin/slock";
                 "mount".source = "${pkgs.utillinux.bin}/bin/mount";
                 "umount".source = "${pkgs.utillinux.bin}/bin/umount";
                 "newgrp".source = "${pkgs.shadow}/bin/newgrp";
                 "sg".source = "${pkgs.shadow}/bin/sg";
                 "thinklight".source =
                   "${(pkgs.callPackage ./custom/thinklight.nix { thinklight = "kbd_backlight"; })}/bin/thinklight";
                 "notify-gkleen" = {
                   group = "users";
                   owner = "gkleen";
                   setgid = true;
                   setuid = true;
                   permissions = "u+rx,g+x,o+x";
                   source = ''${pkgs.callPackage ./custom/notify-user.nix { inherit (pkgs.haskellPackages) ghcWithPackages; }}/bin/notify-gkleen'';
                 };
               };

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

  time.timeZone = "Europe/Berlin";

  hardware = {
    pulseaudio = {
      enable = true;
      package = with pkgs; pulseaudioFull;
      support32Bit = true;
    };

    opengl = {
      enable = true;
      extraPackages = with pkgs; [ vaapiIntel libvdpau-va-gl vaapiVdpau ];
      driSupport32Bit = true;
    };

    bluetooth = {
      enable = true;   
      extraConfig = ''
        [General]
        Enable=Source,Sink,Media,Socket
      '';
    };

    # sane = {
    #   enable = true;
    #   extraBackends = with pkgs; [ samsung-unified-linux-driver ];
    #   configDir = "/etc/sane.d";
    # };

    sensor.iio.enable = true;
  };

  sound.enable = true;


  # nix.gc = {
  #   automatic = true;
  #   dates = "daily";
  #   options = "--delete-older-than 30d";
  # };

  nix = {
    useSandbox = true;
    autoOptimiseStore = true;
    daemonNiceLevel = 10;
    daemonIONiceLevel = 3;
  };

  environment.variables = {
    SSL_CERT_FILE = "/etc/ssl/certs/ca-certificates.crt";
    SANE_CONFIG_DIR = "/etc/sane.d";
    TPRINT_BASEURL = "http://bragi.asgard.yggdrasil/thermoprint/api";
    MPD_HOST = "bragi.asgard.yggdrasil";
    MPD_PORT = "6600";
  };

  environment.etc."X11/xorg.conf.d/50-wacom.conf".source = lib.mkForce ./hel/wacom.conf;

  environment.etc."sane.d/dll.conf".text = "xerox_mfp";
  environment.etc."sane.d/xerox_mfp.conf".text = "tcp printer.asgard.yggdrasil";

  systemd.services."kill-user@" = {
    serviceConfig = {
      Type = "oneshot";
      ExecStart = "${pkgs.systemd}/bin/loginctl kill-user %I";
    };
  };
  systemd.targets."sleep" = {
    after = [ "kill-user@uucp.service" ];
    wants = [ "kill-user@uucp.service" ];
  };

  systemd.automounts = [
    { enable = true;
      where = "/run/media/var/media";
      automountConfig = {
        DirectoryMode = "700";
      };
      wantedBy = [ "local-fs.target" ];
    }
  ];

  systemd.mounts = [
    { enable = true;
      where = "/run/media/var/media";
      what = "/var/media";
      type = "none";
      options = "bind";
      mountConfig = {
        DirectoryMode = "700";
      };
    }
  ];

  systemd.user.services."pulseaudio".enable = lib.mkForce false;
  systemd.user.services."ssh-agent".enable = lib.mkForce false;
  systemd.user.sockets."pulseaudio".enable = lib.mkForce false;

  systemd.services."ac-plugged" = {
    description = "Inhibit handling of lid-switch and sleep";

    path = with pkgs;
             [ systemd coreutils ];

    script = ''
      exec systemd-inhibit --what=handle-lid-switch:sleep --why="AC is connected" --mode=block sleep infinity
    '';
  
    serviceConfig = {
      Type = "simple";
    };
  };

  services.udev.extraRules = with pkgs; ''
    SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="${systemd}/bin/systemctl --no-block stop ac-plugged.service"
    SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="${systemd}/bin/systemctl --no-block start ac-plugged.service"

    KERNELS=="1-2:1.2", ENV{ID_MM_DEVICE_IGNORE}="1", SYMLINK+="gps0"
    KERNELS=="1-2:1.3", ENV{ID_MM_DEVICE_IGNORE}="1", SYMLINK+="gpsctl0", TAG+="systemd"
  '';

  systemd.services."gpscfg" = {
    description = "Configure GPS";

    script = ''
      printf 'AT!GPSTRACK=1,255,30,1000,1\r' > /dev/gpsctl0
    '';
  };

  systemd.timers."gpscfg" = {
    enable = true;

    bindsTo = [ "dev-gpscfg0.device" ];
    after = [ "dev-gpscfg0.device" ];

    wantedBy = [ "dev-gpscfg0.device" ];

    timerConfig = {
      OnActiveSec = "0s";
      OnUnitInactiveSec = "5m";
    };
  };

  systemd.services."NetworkManager-wait-online".enable = true;

  services.borgbackup = {
    snapshots = "btrfs";
    prefix = "automatic.yggdrasil.midgard.hel.";
    targets = {
      "odin" = {
        repo = "borg.odin:/srv/backup/borg";
        paths = [ "home-gkleen" ];
        prune = {
          "home-gkleen" =
            [ "--keep-within 24H"
              "--keep-daily 31"
              "--keep-monthly 12"
              "--keep-yearly -1"
            ];
        };
      };
      "munin" = {
        repo = "borg.munin:borg";
        paths = [ "home-gkleen" ];
        prune = {
          "home-gkleen" =
            [ "--keep-within 24H"
              "--keep-daily 31"
              "--keep-monthly 12"
              "--keep-yearly -1"
            ];
        };
      };
    };
  };

  services.btrfs.autoScrub = {
    enable = true;
    fileSystems = [ "/" ];
    interval = "weekly";
  };

  systemd.services."nix-daemon".serviceConfig = {
    MemoryAccounting = true;
    MemoryHigh = "50%";
    MemoryMax = "75%";
  };
}