about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/config/fanout.nix
blob: 60ee145f19af48234d229f397b1c6e58071d7034 (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
{ config, lib, pkgs, ... }:
let
  cfg = config.services.fanout;
  mknodCmds = n: lib.lists.imap0 (i: s:
    "mknod /dev/fanout${builtins.toString i} c $MAJOR ${builtins.toString i}"
  ) (lib.lists.replicate n "");
in
{
  options.services.fanout = {
    enable = lib.mkEnableOption (lib.mdDoc "fanout");
    fanoutDevices = lib.mkOption {
      type = lib.types.int;
      default = 1;
      description = "Number of /dev/fanout devices";
    };
    bufferSize = lib.mkOption {
      type = lib.types.int;
      default = 16384;
      description = "Size of /dev/fanout buffer in bytes";
    };
  };

  config = lib.mkIf cfg.enable {
    boot.extraModulePackages = [ config.boot.kernelPackages.fanout.out ];

    boot.kernelModules = [ "fanout" ];

    boot.extraModprobeConfig = ''
      options fanout buffersize=${builtins.toString cfg.bufferSize}
    '';

    systemd.services.fanout = {
      description = "Bring up /dev/fanout devices";
      script = ''
        MAJOR=$(${pkgs.gnugrep}/bin/grep fanout /proc/devices | ${pkgs.gawk}/bin/awk '{print $1}')
        ${lib.strings.concatLines (mknodCmds cfg.fanoutDevices)}
      '';

      wantedBy = [ "multi-user.target" ];

      serviceConfig = {
        Type = "oneshot";
        User = "root";
        RemainAfterExit = "yes";
        Restart = "no";
      };
    };
  };
}