4 sysctlOption = lib.mkOptionType {
5 name = "sysctl option value";
8 checkType = x: lib.isBool x || lib.isString x || lib.isInt x || x == null;
10 checkType val || (val._type or "" == "override" && checkType val.content);
11 merge = loc: defs: lib.mergeOneOption loc (lib.filterOverrides defs);
20 boot.kernel.sysctl = lib.mkOption {
22 highestValueType = lib.types.ints.unsigned // {
25 (a: b: if b.value == null then null else lib.max a b.value)
27 (lib.filterOverrides defs);
29 in lib.types.submodule {
30 freeformType = lib.types.attrsOf sysctlOption;
32 "net.core.rmem_max" = lib.mkOption {
33 type = lib.types.nullOr highestValueType;
35 description = "The maximum receive socket buffer size in bytes. In case of conflicting values, the highest will be used.";
38 "net.core.wmem_max" = lib.mkOption {
39 type = lib.types.nullOr highestValueType;
41 description = "The maximum send socket buffer size in bytes. In case of conflicting values, the highest will be used.";
46 example = lib.literalExpression ''
47 { "net.ipv4.tcp_syncookies" = false; "vm.swappiness" = 60; }
50 Runtime parameters of the Linux kernel, as set by
51 {manpage}`sysctl(8)`. Note that sysctl
52 parameters names must be enclosed in quotes
53 (e.g. `"vm.swappiness"` instead of
54 `vm.swappiness`). The value of each
55 parameter may be a string, integer, boolean, or null
56 (signifying the option will not appear at all).
65 environment.etc."sysctl.d/60-nixos.conf".text =
66 lib.concatStrings (lib.mapAttrsToList (n: v:
67 lib.optionalString (v != null) "${n}=${if v == false then "0" else toString v}\n"
68 ) config.boot.kernel.sysctl);
70 systemd.services.systemd-sysctl =
71 { wantedBy = [ "multi-user.target" ];
72 restartTriggers = [ config.environment.etc."sysctl.d/60-nixos.conf".source ];
75 # Hide kernel pointers (e.g. in /proc/modules) for unprivileged
76 # users as these make it easier to exploit kernel vulnerabilities.
77 boot.kernel.sysctl."kernel.kptr_restrict" = lib.mkDefault 1;
79 # Improve compatibility with applications that allocate
80 # a lot of memory, like modern games
81 boot.kernel.sysctl."vm.max_map_count" = lib.mkDefault 1048576;