1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.services.fanout;
9 mknodCmds =
10 n:
11 lib.lists.imap0 (i: s: "mknod /dev/fanout${builtins.toString i} c $MAJOR ${builtins.toString i}") (
12 lib.lists.replicate n ""
13 );
14in
15{
16 options.services.fanout = {
17 enable = lib.mkEnableOption "fanout";
18 fanoutDevices = lib.mkOption {
19 type = lib.types.int;
20 default = 1;
21 description = "Number of /dev/fanout devices";
22 };
23 bufferSize = lib.mkOption {
24 type = lib.types.int;
25 default = 16384;
26 description = "Size of /dev/fanout buffer in bytes";
27 };
28 };
29
30 config = lib.mkIf cfg.enable {
31 boot.extraModulePackages = [ config.boot.kernelPackages.fanout.out ];
32
33 boot.kernelModules = [ "fanout" ];
34
35 boot.extraModprobeConfig = ''
36 options fanout buffersize=${builtins.toString cfg.bufferSize}
37 '';
38
39 systemd.services.fanout = {
40 description = "Bring up /dev/fanout devices";
41 script = ''
42 MAJOR=$(${pkgs.gnugrep}/bin/grep fanout /proc/devices | ${pkgs.gawk}/bin/awk '{print $1}')
43 ${lib.strings.concatLines (mknodCmds cfg.fanoutDevices)}
44 '';
45
46 wantedBy = [ "multi-user.target" ];
47
48 serviceConfig = {
49 Type = "oneshot";
50 User = "root";
51 RemainAfterExit = "yes";
52 Restart = "no";
53 };
54 };
55 };
56}