1{
2 lib,
3 pkgs,
4 config,
5 utils,
6 ...
7}:
8let
9 cfg = config.services.scx;
10in
11{
12 options.services.scx = {
13 enable = lib.mkEnableOption null // {
14 description = ''
15 Whether to enable SCX service, a daemon to run schedulers from userspace.
16
17 ::: {.note}
18 This service requires a kernel with the Sched-ext feature.
19 Generally, kernel version 6.12 and later are supported.
20 :::
21 '';
22 };
23
24 package = lib.mkOption {
25 type = lib.types.package;
26 default = pkgs.scx.full;
27 defaultText = lib.literalExpression "pkgs.scx.full";
28 example = lib.literalExpression "pkgs.scx.rustscheds";
29 description = ''
30 `scx` package to use. `scx.full`, which includes all schedulers, is the default.
31 You may choose a minimal package, such as `pkgs.scx.rustscheds`.
32
33 ::: {.note}
34 Overriding this does not change the default scheduler; you should set `services.scx.scheduler` for it.
35 :::
36 '';
37 };
38
39 scheduler = lib.mkOption {
40 type = lib.types.enum [
41 "scx_bpfland"
42 "scx_chaos"
43 "scx_cosmos"
44 "scx_central"
45 "scx_flash"
46 "scx_flatcg"
47 "scx_lavd"
48 "scx_layered"
49 "scx_mitosis"
50 "scx_nest"
51 "scx_p2dq"
52 "scx_pair"
53 "scx_prev"
54 "scx_qmap"
55 "scx_rlfifo"
56 "scx_rustland"
57 "scx_rusty"
58 "scx_sdt"
59 "scx_simple"
60 "scx_tickless"
61 "scx_userland"
62 "scx_wd40"
63 ];
64 default = "scx_rustland";
65 example = "scx_bpfland";
66 description = ''
67 Which scheduler to use. See [SCX documentation](https://github.com/sched-ext/scx/tree/main/scheds)
68 for details on each scheduler and guidance on selecting the most suitable one.
69 '';
70 };
71
72 extraArgs = lib.mkOption {
73 type = lib.types.listOf lib.types.singleLineStr;
74 default = [ ];
75 example = [
76 "--slice-us 5000"
77 "--verbose"
78 ];
79 description = ''
80 Parameters passed to the chosen scheduler at runtime.
81
82 ::: {.note}
83 Run `chosen-scx-scheduler --help` to see the available options. Generally,
84 each scheduler has its own set of options, and they are incompatible with each other.
85 :::
86 '';
87 };
88 };
89
90 config = lib.mkIf cfg.enable {
91 environment.systemPackages = [ cfg.package ];
92
93 systemd.services.scx = {
94 description = "SCX scheduler daemon";
95
96 # SCX service should be started only if the kernel supports sched-ext
97 unitConfig.ConditionPathIsDirectory = "/sys/kernel/sched_ext";
98
99 startLimitIntervalSec = 30;
100 startLimitBurst = 2;
101
102 serviceConfig = {
103 Type = "simple";
104 ExecStart = utils.escapeSystemdExecArgs (
105 [
106 (lib.getExe' cfg.package cfg.scheduler)
107 ]
108 ++ cfg.extraArgs
109 );
110 Restart = "on-failure";
111 };
112
113 wantedBy = [ "multi-user.target" ];
114 };
115
116 assertions = [
117 {
118 assertion = lib.versionAtLeast config.boot.kernelPackages.kernel.version "6.12";
119 message = "SCX is only supported on kernel version >= 6.12.";
120 }
121 ];
122 };
123
124 meta = {
125 inherit (pkgs.scx.full.meta) maintainers;
126 };
127}