summaryrefslogtreecommitdiff
path: root/bragi.nix
blob: c85e0ace706ca6bbda6888efd62e37649d6476af (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
{ config, pkgs, ... }:

let
  trivmixService = opts: (pkgs.callPackage ./custom/trivmix-service.nix opts).out;
  thermoprint-servant = (pkgs.callPackage ./custom/thermoprint {}).thermoprint-servant;
in rec {
  imports =
    [
      ./bragi-hw.nix
      ./custom/zsh.nix
      ./users.nix
    ];

  boot.loader.grub.enable = true;
  boot.loader.grub.version = 2;

  boot.loader.grub.extraConfig = "serial; terminal_output.serial";
  boot.kernelParams = [ "console=ttyS0,115200"
                      ];

  boot.kernelModules = [ "usblp" ];

  networking.hostName = "bragi";
  networking.hostId = "2af11085";
  networking.wireless.enable = true;

  nixpkgs.config.packageOverrides = pkgs:
    rec {
      customizedHaskellPackages = pkgs.recurseIntoAttrs (
        pkgs.haskellPackages.override {
          overrides = self: super: let
            callPackage = pkgs.lib.callPackageWith ( pkgs // self );
          in {
            trivmix = callPackage ./custom/trivmix.nix {};
            # filelock = callPackage ./custom/filelock.nix {};
          };
        }
      );
      trivmix = let trivmix = customizedHaskellPackages.trivmix; in
        pkgs.stdenv.mkDerivation {
          name = "trivmix-wrapper";
          nativeBuildInputs = [ pkgs.makeWrapper ];
          buildCommand = ''
            mkdir -p $out/bin
            makeWrapper ${trivmix}/bin/trivmix $out/bin/trivmix \
              --run "umask 0" \
              --set JACK_PROMISCUOUS_SERVER 1
          '';
        };
      # jack2 = pkgs.callPackage ./customized/jack2.nix {};
      jack2Full = pkgs.symlinkJoin {
        name = "jack2";
        paths = [ (pkgs.jack2Full.override { dbus = null; }) ];
        buildInputs = [ pkgs.makeWrapper ];
        postBuild = ''
          for bin in $out/bin/${"*"}; do
            wrapProgram $bin \
              --set JACK_PROMISCUOUS_SERVER 1 \
              --run "umask 0"
          done
        '';
      };

      mpd = pkgs.symlinkJoin {
        name = "mpd";
        paths = [ (pkgs.mpd.override { gmeSupport = false; pulseaudioSupport = false; }) ];
        buildInputs = [ pkgs.makeWrapper ];
        postBuild = ''
          wrapProgram $out/bin/mpd \
            --set JACK_PROMISCUOUS_SERVER 1 \
            --run "umask 0"
        '';
      };

      mpv = pkgs.stdenv.lib.overrideDerivation pkgs.mpv (oldAttrs: {
        buildInputs = oldAttrs.buildInputs ++ [pkgs.makeWrapper];
        postInstall = ''
          wrapProgram $out/bin/mpv \
            --set JACK_PROMISCUOUS_SERVER 1 \
            --run "umask 0"
        '';
      });

      inherit (pkgs.callPackage ./custom/thermoprint {}) thermoprint-server thermoprint-webgui tprint;
    };

  environment.systemPackages = with pkgs; [
    git
    mosh
    rsync
    tmux
    nfs-utils
    jack2Full
    trivmix
    zsh
    tprint
  ];

  # List services that you want to enable:

  services.openssh = {
    enable = true;
  };

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

  services.autofs = {
    enable = true;
    autoMaster = let
      mapConf = pkgs.writeText "auto" ''
        odin -fstype=nfs,ro odin.asgard.yggdrasil:/srv/media
        dellingr -fstype=vfat,ro /dev/disk/by-uuid/6436-3432
      '';
      in ''
        /media file:${mapConf}
      '';
  };

  systemd.services.jack = {
    wantedBy = [ "sound.target" ];
    serviceConfig = {
      Type = "simple";
      ExecStart = "${pkgs.jack2Full}/bin/jackd -d alsa -d 'hw:1' -M -H -r 96000";
      User = "jack";
      Group = "audio";
    };
  };

  systemd.services."mpdmix0" = trivmixService { name = "mpdmix0"; connectOut = "system:playback_3"; group = "mpd"; };
  systemd.services."mpdmix1" = trivmixService { name = "mpdmix1"; connectOut = "system:playback_4"; group = "mpd"; };

  systemd.services."passmix0" = trivmixService { name = "passmix0"; connectOut = "system:playback_3"; connectIn = "system:capture_5"; group = "vali_out"; };
  systemd.services."passmix1" = trivmixService { name = "passmix1"; connectOut = "system:playback_4"; connectIn = "system:capture_6"; group = "vali_out"; };

  systemd.services."passmix2" = trivmixService { name = "passmix2"; connectOut = "system:playback_5"; connectIn = "system:capture_1"; group = "vali_in"; };
  systemd.services."passmix3" = trivmixService { name = "passmix3"; connectOut = "system:playback_6"; connectIn = "system:capture_1"; group = "vali_in"; };

  systemd.services."passmix4" = trivmixService { name = "passmix4"; connectOut = "system:playback_3"; connectIn = "system:capture_7"; group = "hel_out"; };
  systemd.services."passmix5" = trivmixService { name = "passmix5"; connectOut = "system:playback_4"; connectIn = "system:capture_8"; group = "hel_out"; };

  services.mpd = {
    enable = true;
    musicDirectory = "/media/odin/music";
    extraConfig = ''
      bind_to_address "bragi.bragisheimr.yggdrasil"
      bind_to_address "bragi.asgard.yggdrasil"
      bind_to_address "localhost"

      bind_to_address "/var/lib/mpd/socket"
    
      audio_output {
        name "JACK"
        type "jack"
        client_name "mpd"
        destination_ports "mpdmix0:in,mpdmix1:in"
      }
    '';
  };

  users.extraUsers.jack = {
    name = "jack";
    isSystemUser = true;
    group = "audio";
  };

  security.setuidPrograms = ["mount.nfs"];

  security.pam.loginLimits =
  [
    { domain = "@audio"; item = "memlock"; type = "-"; value = "unlimited"; }
    { domain = "@audio"; item = "rtprio"; type = "-"; value = "99"; }
    { domain = "@audio"; item = "nofile"; type = "soft"; value = "99999"; }
    { domain = "@audio"; item = "nofile"; type = "hard"; value = "99999"; }
  ];
  programs.bash.promptInit = ''
    PROMPT_COLOR="1;31m"
    return $UID && PROMPT_COLOR="1;32m"
    case "$TERM" in
      xterm*|rxvt*|kterm|aterm|gnome*) # Others can go here.
        PS1="\n\[\033[$PROMPT_COLOR\][\u@\h:\w]\\$\[\033[0m\] "
        if test "$TERM" = "xterm"; then
          PS1="\[\033]2;\h:\u:\w\007\]$PS1"
        fi
        ;;
      *)
        PS1="[\u@\h:\w]$ "
        ;;
    esac
  '';

  networking.interfaces = {
    "enp1s0" = {
      useDHCP = false;
      ipAddress = "10.141.4.1";
      prefixLength = 24;
    };
  };

  networking.nat = {
    enable = true;
    externalIP = "10.141.1.5";
    externalInterface = "wlp4s0";
    internalIPs = [ "10.141.4.0/24"
                  ];
    internalInterfaces = [ "enp1s0"
                         ];
  };

  networking.firewall = {
    enable = true;
    allowPing = true;
    allowedTCPPorts = [ 22 # SSH
                        # 8080 # thermoprint
                        6600 # MPD
                      ];
    allowedUDPPortRanges = [ { from = 60000; to = 61000; } # mosh
                           ];
    extraCommands = ''
      iptables -t nat -A POSTROUTING -o wlp4s0 -j MASQUERADE
      #iptables -A FORWARD -i wlp4s0 -o enp1s0 -m state --state RELATED,ESTABLISHED -j ACCEPT
      iptables -A FORWARD -i wlp4s0 -o enp1s0 -j ACCEPT
      iptables -A FORWARD -i enp1s0 -o wlp4s0 -j ACCEPT
    '';
  };

  services.dhcpd = {
    enable = true;
    interfaces = [ "enp1s0"
                 ];
    extraConfig = ''
      option subnet-mask 255.255.255.0;
      option domain-name-servers 10.141.1.1, 8.8.8.8, 8.8.4.4;
      option domain-name "bragisheimr.yggdrasil";
      option routers 10.141.4.1;
      subnet 10.141.4.0 netmask 255.255.255.0 {
        range 10.141.4.100 10.141.4.254;
      }

      group {
        use-host-decl-names on;

        host vali {
          hardware ethernet e0:cb:4e:f7:10:3d;
          fixed-address vali.bragisheimr.yggdrasil;
        }
      }
    '';
  };

  services.ntp = {
    enable = false;
  };

  services.chrony = {
    enable = true;
  };

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

  users.extraUsers."thermoprint" = {
    name = "thermoprint";
    group = "lp";
    isSystemUser = true;
    createHome = true;
    home = "/var/lib/thermoprint";
  };

  environment.etc."thermoprint-server/config.hs" = ''
    {-# LANGUAGE OverloadedStrings  #-}
    {-# LANGUAGE ImpredicativeTypes #-}

    module Main (main) where

    import Thermoprint.Server

    import Thermoprint.Server.Printer.Generic

    import Control.Monad.Trans.Resource
    import Control.Monad.Logger
    import Control.Monad.Reader

    import Database.Persist.Sqlite
    
   import qualified Network.Wai.Handler.Warp as Warp

    main :: IO ()
    main = thermoprintServer True (Nat runSqlite) $ (\c -> c { queueManagers = queueManagers, warpSettings = wSettings }) <$> def `withPrinters` printers
      where
        runSqlite :: ReaderT ConnectionPool (LoggingT IO) a -> IO a
        runSqlite = runStderrLoggingT . withSqlitePool "${users.extraUsers."thermoprint".home}/thermoprint.sqlite" 1 . runReaderT

        printers = [ (pure $ genericPrint "/dev/usb/lp0", def)
                   ]

        queueManagers _ = QMConfig
          { manager = union [ limitHistorySize 100
                            , limitHistoryAge 3600
                            ]
          , collapse = standardCollapse
          }

        wSettings = setHost "*" . Warp.setPort 8080 $ Warp.defaultSettings 
  '';

  systemd.services."thermoprint" = {
    environment = {
      THERMOPRINT_CONFIG = "/etc/thermoprint-server";
      THERMOPRINT_CACHE = ''${users.extraUsers."thermoprint".home}/dyre'';
    };
    serviceConfig = {
      Type = "simple";
      ExecStart = ''${pkgs.thermoprint-server}/bin/thermoprint-server'';
      User = users.extraUsers."thermoprint".name;
      Group = users.extraUsers."thermoprint".group;
    };
  };

  systemd.services."thermoprint-webgui" = {
    serviceConfig = {
      Type = "simple";
      ExecStart = ''${pkgs.thermoprint-webgui}/bin/thermoprint-webgui -P 8080 -A localhost -a "*" -p 8081'';
      User = users.extraUsers."thermoprint".name;
      Group = users.extraUsers."thermoprint".group;
    };
  };

  nix = {
    daemonIONiceLevel = 3;
    daemonNiceLevel = 10;
    gc = {
      automatic = true;
    };
  };

  system.autoUpgrade.enable = true;
  system.stateVersion = "16.09";
}